简体   繁体   English

发送 ERC20 代币

[英]Sending ERC20 Tokens

I have a solidity contract that has minted a fixed number of ERC20 tokens (using the ropsten test network).我有一个 Solidity 合约,它铸造了固定数量的 ERC20 代币(使用 ropsten 测试网络)。 I am in need of a way to send the tokens from a wallet to another wallet (preferrably the using the web3js library, but JSON-RPC would work, I have the private key to the account).我需要一种将令牌从钱包发送到另一个钱包的方法(最好使用 web3js 库,但 JSON-RPC 可以工作,我有帐户的私钥)。

Here is my code thus far到目前为止,这是我的代码

var Web3 = require('web3')
var web3 = new Web3(new 
Web3.providers.HttpProvider('https://ropsten.infura.io/xxxxxxx'));
const abi = [ {} ];
const contract = new web3.eth.Contract(abi).at("0x...")
contract.transferFrom('0x....', '0x.....', 100);

When I execute this snippet, I get issues saying "TypeError: (intermediate value).at is not a function".当我执行此代码段时,出现“TypeError: (intermediate value).at is not a function”的问题。 I am relatively new to web3, so any thoughts/suggestions would be most appreciated.我对 web3 比较陌生,所以任何想法/建议都将不胜感激。

You can try this code你可以试试这个代码

transferTokensTo: function(contract, address_from, address, tokens) {
    return new Promise(function(resolve, reject) {
        contract.methods.decimals().call().then(function (result) {
            var decimals = result;
            console.log("Token decimals: " + decimals);
            var amount = tokens * Math.pow(10, decimals);

            console.log('Transfer to:', address);
            console.log('Tokens: ' + tokens + " (" + amount + ")");
            contract.methods.transfer(address, amount).send({
                from: address_from,
                gas: 150000
            }).on('transactionHash', function (hash) {
                console.log('\n[TRANSACTION_HASH]\n\n' + hash);
            }).on('confirmation', function (confirmationNumber, receipt) {
                console.log('\n[CONFIRMATION] ', confirmationNumber);

                resolve(receipt);
            }).on('receipt', function (receipt) {
                console.log('\n[RECEIPT]\n\n', receipt);

                // TODO: process receipt if needed
            }).on('error', function (error) {
                console.log('\n[ERROR]\n\n' + error);

                reject(error);
            }).then(function (done) {
                console.log('\n[DONE]\n\n', done);
            });
        });
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM