Create ABI parser module
See original GitHub issueIssue Description
I want to add more type safety to the Contract
call by adding type parameters to call
and invoke
(related: https://github.com/apibara/starknet-react/issues/98).
The first issue I noticed is that the encoder/decoder for cairo types is coupled with the Contract class, which for example forces tests to make a contract call to test the encoding/decoding. I propose to create a new encoding
module with the following functions:
// from typescript types to felt arrays
function encodeInputs<A>(args: A, method: string, abi: Abi): string[];
function encodeFelt(arg: string | number): string[];
function encodeStruct<A extends object>(arg: A, abi: Abi): string[];
function encodeTuple<A extends unknown[]>(arg: A, abi: Abi): string[];
function encodeArary<A>(arg: A[], abi: Abi): string[];
// from felt array to typescript
function decodeOutputs<R>(data: string[], method: 'string', abi: Abi): R;
function decodeFelt(data: string[]): string; // seems useless
function decodeStruct<R extends object>(data: string[], abi: Abi): R;
function decodeTuple<R extends unknown[]>(data: string[], abi: Abi): R;
function decodeArray<R>(data: string[], abi: Abi): R[];
A couple of notes:
encodeInputs
must accept either an array of arguments (technically a tuple) or an object to be backward compatible with howcall
parses inputs at the momentdecodeOutputs
is more tricky because the current implementation returns an object that’s both an array and an object.
Related: can add a { strict: true }
option to throw an error if any struct/tuple has more arguments than required (#140).
After this issue is fixed adding type checking to call
and invoke
should be relatively easy.
Issue Analytics
- State:
- Created 10 months ago
- Reactions:2
- Comments:11 (3 by maintainers)
Top Results From Across the Web
eth_abi.codec module - the eth-abi documentation!
Encodes the python values in args as a sequence of binary values of the ABI types in types via the head-tail mechanism. Parameters:...
Read more >shamatar/EthereumABI: Ethereum ABI parser in vanilla Swift ...
Ethereum ABI parser in vanilla Swift. Supports ABIv2 spec for Solidity 0.5 - GitHub - shamatar/EthereumABI: Ethereum ABI parser in vanilla Swift.
Read more >What is an ABI? explained - step-by-step beginners guides
The ABI indicates the caller of the function to encode the needed information like function signatures and variable declarations in a format that...
Read more >How do you get a json file (ABI) from a known contract address?
... Exports contract ABI in JSON ABI_ENDPOINT = 'https://api.etherscan.io/api?module=contract&action=getabi&address=' parser = argparse.
Read more >Application Binary Interface - Web3j
The ABI module in web3j provides full support for the ABI specification, and includes: Java implementations of all ABI types, including conversion from...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For now it is a simple POC, it works for an ERC20 contract… But a lot is missing, you are free to suggest!
@cwkang1998 feel free to contribute to https://github.com/ivpavici/abi-wan-kanabi 😃