V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
brust
V2EX  ›  区块链

erc20 转账问题

  •  
  •   brust · 2019-09-18 18:23:46 +08:00 · 2261 次点击
    这是一个创建于 1654 天前的主题,其中的信息可能已经有所发展或是发生改变。
    第 1 条附言  ·  2019-09-18 19:18:18 +08:00
       //转账
        public void sendTx(String fromPrivateKey,String toAddress) throws IOException, ExecutionException, InterruptedException {
            String contractAddress = "0xdac17f958d2ee523a2206206994597c13d831ec7";
            ECKeyPair ecKeyPair = ECKeyPair.create(new BigInteger(fromPrivateKey, 16));
            String from = "0x"+Keys.getAddress(ecKeyPair.getPublicKey());
            log.info("from:\t\t{}",from);
            log.info("  to:\t\t{}",toAddress);
            BigDecimal amount = new BigDecimal("1");
            BigInteger amountBigInteger = Convert.toWei(amount, Convert.Unit.MWEI).toBigInteger();
            EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
                    from, DefaultBlockParameterName.LATEST).sendAsync().get();
            BigInteger nonce = ethGetTransactionCount.getTransactionCount();
            //离线签名 代币
            Credentials credentials = Credentials.create(ecKeyPair);
            byte[] signedMessage;
            Function function = new Function(
                    "transfer",
                    Arrays.asList(new Address(toAddress), new Uint256(amountBigInteger)),
                    Arrays.asList(new TypeReference<Type>(){})
            );
            String data = FunctionEncoder.encode(function);
    		
            RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, Convert.toWei("38", Convert.Unit.GWEI).toBigInteger(),
                    Convert.toWei("30000", Convert.Unit.WEI).toBigInteger(), contractAddress, data);
            signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
            String hexValue = Numeric.toHexString(signedMessage);
            log.info("signedData : " + hexValue);
            //广播
            EthSendTransaction send = web3j.ethSendRawTransaction(hexValue).send();
            ...
        }
    
    13 条回复    2020-01-02 09:39:31 +08:00
    brust
        1
    brust  
    OP
       2019-09-18 18:25:18 +08:00
    brust
        2
    brust  
    OP
       2019-09-18 18:33:10 +08:00
    Hconk
        3
    Hconk  
       2019-09-18 18:46:03 +08:00 via iPhone
    明显 gas limit 给少了,erc20 一般的转账给 5w 差不多够了。
    brust
        4
    brust  
    OP
       2019-09-18 18:52:42 +08:00
    @Hconk #3
    我给 5w 还是失败了
    Hconk
        5
    Hconk  
       2019-09-18 18:53:20 +08:00 via iPhone
    @brust 交易 hash 发出来看下
    brust
        6
    brust  
    OP
       2019-09-18 18:54:02 +08:00
    @Hconk #5 0xd973acf0f426b15e07075bb05c6d4e33f4d3156d6a3e961c18ed221bf7ebbd7f
    Hconk
        7
    Hconk  
       2019-09-18 19:04:48 +08:00 via iPhone
    @brust 你的 usd 不够了,你给的链接里面 decimal 是 18 的 token 举例的,你转账的这个 erc20 的 decimal 是 6,改一下金额转换时候的单位,把 18 改成 6。
    brust
        8
    brust  
    OP
       2019-09-18 19:16:01 +08:00
    @Hconk #7
    谢谢
    但是我把金额改成 6 了后
    0x5b97b6cdd65996c942bc32e1ff818078dee5530e29e73a98453dd71aa3cf55b3
    还是失败了
    Hconk
        9
    Hconk  
       2019-09-18 19:29:19 +08:00 via iPhone
    @brust 你追加的代码里面 decimal 给了 38 ?
    brust
        10
    brust  
    OP
       2019-09-18 20:53:37 +08:00
    @Hconk #9
    Convert.toWei("38", Convert.Unit.GWEI )这是 gasPrice

    这个不对吗?
    amount 和 gasprice 和 gaslimit 要怎么设置好
    为什么我的 Gas Used by Transaction:都是 100%
    brust
        11
    brust  
    OP
       2019-09-19 10:32:06 +08:00

    转账成功了,精度弄错了

    还有一个疑问就是 手续费似乎有点高?

    https://etherscan.io/tx/0x43a0c50f4b0f46b71ade7ba3221b78f7de33c6b562127031fd1b7e383d8039d7
    mutongs
        12
    mutongs  
       2019-12-20 10:36:24 +08:00
    erc20 代币,如果用户没有 eth,怎么转账?我们自己再往用户地址转 eth 进去吗?
    brust
        13
    brust  
    OP
       2020-01-02 09:39:31 +08:00
    @mutongs #12 是这样的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2757 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 12:16 · PVG 20:16 · LAX 05:16 · JFK 08:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.