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.

ABI Encoding for offline method calls

See original GitHub issue

Hi ! I’m using ethers in combination with web3 in order to achieve the following:

  • have the user import his json wallet/restore from mnemonic
  • connect to the smart contract
  • create a transaction that calls a non-constant method of the SC (ethers)
  • sign the transaction offline or client side
  • send the transaction to any node I need this so that the user can be the only one with access to the wallet

It seems fine with simple methods, like those that pass integers. But I got issues with more complex data. The code I use is this:

                    w1 =  new ethers.Wallet(priv, provider);
                    iface = new ethers.utils.Interface(contract_abi);
                    txdata = iface.functions[method].encode(args);
                    signonce = await w1.getTransactionCount();
                    tx = {
                        "from" : addr,
                        "to" : contract_address,
                        "gasLimit" : 1500000,
                        "gasPrice" : 2,
                        "nonce" : signonce,
                        "data" : txdata,
                        "value" : 0,
                    };
                    web3 = new Web3( new Web3.providers.HttpProvider(rpcEndpoint));
                    account = await  web3.eth.accounts.privateKeyToAccount(priv);
                    stx1 = await web3.eth.accounts.signTransaction(tx, priv);
                    web3.eth.sendSignedTransaction(stx1.rawTransaction)

This part works well. The problem is creating agrs to be encoded. If a prameter is an int or a string, no problem.

var args = {"i":32};

But how do I encode bytes? arrays? addresses? I get errors about the format/type/length of the parameter

Any help/advice/feedback would be greatly appreciated.

TIA

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
ricmoocommented, May 19, 2020

Heya!

I also recommend you try out v5, which has a. simpler API for dealing with Interface and Contracts for offline signing.

w1 =  new ethers.Wallet(priv, provider);
contract = new ethers.Contract(contract_address, contract_abi, w1);
overrides = {
  nonce: w1.getTransactionCount(),
  gasLimit: 1500000,
  gasPrice: utils.parseUnits("2.0", "gwei"),
  value: 0
};
unsignedTx = await contract.populateTransaction[method](...args, overrides);

signedTx = w1.signTransaction(unsignedTx);

provider.sendTransaction(signedTx);

This is all doable in v4 too, but v5 specifically added some API to make offline-signing easier. 😃

2reactions
zemsecommented, Jul 19, 2020

@xzbnm

// This line returns the transaction object, which is already signed and sent
let unsignedTx = await contract.functions.populateTransaction(toAddress,numberOfTokens,transaction)

// Just replace that line with bellow one, it should probably work in your code
let unsignedTx = await contract.populateTransaction.populateTransaction(toAddress,numberOfTokens,transaction)

Edit: Hey, I got a feeling that you probably don’t have a populateTransaction in a smart contract and you actually meant to create a populated tx for transfer method? If that’s the case:

let unsignedTx = await contract.populateTransaction.transfer(toAddress,numberOfTokens,transaction)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Ethereum Contract ABI - Encoding for functions with no ...
I am using eth.sendRawTransaction to call a function in my deployed smart contract. Everything is working fine when the functions have arguments ...
Read more >
ABI details - Algorand Developer Portal
The ABI (Application Binary Interface) is a specification that defines the encoding/decoding of data types and a standard for exposing and invoking methods...
Read more >
Contract ABI Specification — Solidity 0.8.17 documentation
This descriptive data is the encoding of an error and its arguments in the same way as data for a function call. As...
Read more >
web3.eth.Contract — web3.js 1.0.0 documentation
Creates a transaction object for that method, which then can be called, send, estimated, or ABI encoded. The methods of this smart contract...
Read more >
headlong: A Contract ABI and RLP Library for the JVM
headlong can parse any function signature or JSON description at runtime and do the type-checking of inputs before encoding and of outputs during...
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