Encode easily the data value of Ethereum (Also works with Binance Smartchain, and any ethereum fork) for token transactions.
Install using NPM with the following command:
npm i eth-token-encode
Here are a few examples that can be used:
const genData = require('eth-token-encode');
let data = new genData(18); // creates object
data.generate('0x82B176BBdd8892df7d6B4b46Ec446a07A60F347E', 0.1) // returns 0xa9059cbb00000000000000000000000082B176BBdd8892df7d6B4b46Ec446a07A60F347E000000000000000000000000000000000000000000000000016345785d8a0000
Keep in mind that this is only for token transfers! You should set the value
to 0x0
and data
to this encoded value.
For now it only works with the contract function transfer(address,uint256)
(0xa9059cbb
)
decimals
is the number of decimals of said token/contract, (Ex. 18 for most tokens)
toAddress
is the receiving address in a valid hex format.
Amount
is the number of tokens desired to send.
Run npm test
for more tests.
- This is still a WIP, functional but basic.
- Main reason of this was to interact with Binance SmartChain as most existing packages won't work on the signing part.
- If you had the same trouble as me, I'd recommend to use @warren-bank/ethereumjs-tx-sign instead of ethereumjs-tx (ChainID for BSC doesn't work with ethereumjs-tx, plus it's lightweight).
- Direct use with @warren-bank/ethereumjs-tx-sign for easier signing.
- File with common tokens to support easier search.