JavaScript
1
Вклад в тег
const
tokenAddress = '0x58....e4',
fromAddress = '0xaA....46c',
privateKey = '0x09........79',
toAddress = '0xE3....f7',
decimals = web3.utils.toBN(0),
amount = web3.utils.toBN(5),
value = amount.mul(web3.utils.toBN(10).pow(decimals)), // value = 5 = 5*(10^0)
sender = web3.eth.accounts.privateKeyToAccount(privateKey),
token = abi => new web3.eth.Contract(abi, tokenAddress).methods;
axios.get('https://api.etherscan.io/api?module=contract&action=getabi&address=' + tokenAddress)
.then(answer => {
const
abi = JSON.parse(answer.data.result),
data = token(abi)["transfer"](toAddress, value).encodeABI(),
tx = {
gas: '2000000',
from: fromAddress,
to: tokenAddress,
data,
};
token(abi).name().call().then(console.log);
token(abi).symbol().call().then(console.log);
token(abi).balanceOf(fromAddress).call().then(console.log);
sender.signTransaction(tx)
.then(signedTx => {
web3.eth.sendSignedTransaction(signedTx.rawTransaction)
.then(console.log);
});
});
Для того, чтобы перевести токены - нужно вызвать соответствующий метод контракта токена, предварительно его подписав. Примерный код:const sender = web3.eth.accounts.privateKeyToAccount(privateKeyFrom); const tokenContract = new web3.eth.Contract(abi, contractAddress); const data = tokenContract.methods["transfer"](toAddress, amount).encodeABI(); const tx = { gas: '2000000', from: fromAddress, to: contractAddress, data, }; const signedTx = await sender.signTransaction(tx); const result = await web3.eth .sendSignedTransaction(signedTx.rawTransaction);