Ethers.js: What is the equivalent to the Web3 getData() function?

Created on 10 Dec 2018  路  2Comments  路  Source: ethers-io/ethers.js

Web3 allows me to get the data of a contract function and then manually include it in a transaction. I need that functionality in ethers.js. Having problems finding it in the documentation.

discussion

Most helpful comment

I think what you are looking for is something like:

// function transfer(address to, uint256 amount)
let data = contract.interface.functions.transfer.encode([ toAddress, amount ]);

If you are only interested in the data, you can skip creating a Contract instance and just create an Interface object:

let abi = [
    "function someFunc(address about, string what) returns (uint256)"
];
let iface = new ethers..utils.Interface(abi);

// This Description object has lots of useful things on it...
let someFunc = iface.functions.someFunc;

// Encode data (what you are looking for)
let encodedData = someFunc.encode([about, what]);

let resultData = await provider.call({ to: address, data: data });

// Parsing the result of calls
let result = someFunc.decode(resultData);

All 2 comments

I think what you are looking for is something like:

// function transfer(address to, uint256 amount)
let data = contract.interface.functions.transfer.encode([ toAddress, amount ]);

If you are only interested in the data, you can skip creating a Contract instance and just create an Interface object:

let abi = [
    "function someFunc(address about, string what) returns (uint256)"
];
let iface = new ethers..utils.Interface(abi);

// This Description object has lots of useful things on it...
let someFunc = iface.functions.someFunc;

// Encode data (what you are looking for)
let encodedData = someFunc.encode([about, what]);

let resultData = await provider.call({ to: address, data: data });

// Parsing the result of calls
let result = someFunc.decode(resultData);

Hazzah! It works, thank you, you the man

Was this page helpful?
0 / 5 - 0 ratings