首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Web3js传输令牌

Web3js传输令牌
EN

Stack Overflow用户
提问于 2018-07-19 07:08:48
回答 1查看 1.7K关注 0票数 0

我想把代币转到其他地址。在调用传递函数之前,我调用estimateGas()来估计所需的气体。

守则如下:

代码语言:javascript
复制
var count = await web3Config.web3.eth.getTransactionCount(process.env.OWNER_ADDRESS);
        var data = web3Config.contractInstance.methods.transfer(req.body.address, req.body.amount*Math.pow(10,18)).encodeABI();

        var estimatedGasPrice = await web3Config.web3.eth.estimateGas({
          from : process.env.OWNER_ADDRESS,
          to:   process.env.CONTRACT_ADDRESS,
          data : data
        });

          logs.error(estimatedGasPrice)
          var rawTx = {
              from : process.env.OWNER_ADDRESS,
              to:   process.env.CONTRACT_ADDRESS,
              data : data,
              nonce: "0x" + count.toString(16),
              gasPrice: web3Config.gasPrice,
              value: '0x0',
              gasLimit: estimatedGasPrice,
              chainId: 3
          }

          var tx = new Tx(rawTx);
          tx.sign(web3Config.ownerPrivKey);
          var serializedTx = tx.serialize();

          var receipt = await web3Config.web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'));
          res.send(receipt)
          logs.log(receipt)

我合同中的小数是18

如果我想传输1 token,那么我需要将它转换为1000000000000000000,因为它是小数点。

问题在这一行req.body.amount*Math.pow(10,18)中。当我尝试传输超过100000的令牌时,它给出了下面提到的异常:

代码语言:javascript
复制
Error: [number-to-bn] while converting number 1e+22 to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported. Given value: "1e+22"
warning.js:18
    at toBN (/home/akshay/WS/ethereum/node_modules/web3-utils/src/utils.js:64:15)
    at Object.toTwosComplement (/home/akshay/WS/ethereum/node_modules/web3-utils/src/utils.js:77:18)
    at SolidityTypeUInt.formatInputInt [as _inputFormatter] (/home/akshay/WS/ethereum/node_modules/web3-eth-abi/src/formatters.js:44:36)
    at SolidityTypeUInt.SolidityType.encode (/home/akshay/WS/ethereum/node_modules/web3-eth-abi/src/type.js:188:17)
    at /home/akshay/WS/ethereum/node_modules/web3-eth-abi/src/index.js:255:29
    at Array.map (<anonymous>)
    at ABICoder.encodeParameters (/home/akshay/WS/ethereum/node_modules/web3-eth-abi/src/index.js:254:34)
    at /home/akshay/WS/ethereum/node_modules/web3-eth-contract/src/index.js:420:24
    at Array.map (<anonymous>)
    at Object._encodeMethodABI (/home/akshay/WS/ethereum/node_modules/web3-eth-contract/src/index.js:419:12)
    at main (/home/akshay/WS/ethereum/routes/blockchain.js:55:116)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

它超过了数字的范围。

如有任何帮助/建议,将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-20 03:12:36

如果在控制台中键入value,您将看到该值太大,以致javascript转换该值。

代码语言:javascript
复制
> 1*Math.pow(10,18)

1000000000000000000

> 100000000000000*Math.pow(10,18)

1e+32

我认为您可以使用web3十进制对象来挖掘或计算令牌。

或者您可以使用web3 toWei来做同样的事情。

如果您的令牌小数点为18 (就像ethereum到wei一样),那么您可以简单地使用toWei(1, 'ether')来获取令牌数量。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51416417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档