question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create ABI parser module

See original GitHub issue

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 how call parses inputs at the moment
  • decodeOutputs 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:open
  • Created a year ago
  • Reactions:2
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ivpavicicommented, Nov 29, 2022

For now it is a simple POC, it works for an ERC20 contract… But a lot is missing, you are free to suggest!

1reaction
ivpavicicommented, Nov 29, 2022
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found