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

erc20 转账问题

  •  
  •   brust 2019-09-18 18:23:46 +08:00 2598 次点击
    这是一个创建于 2214 天前的主题,其中的信息可能已经有所发展或是发生改变。
    第 1 条附言  &bsp; 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     Solana     6094 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 02:30 PVG 10:30 LAX 19:30 JFK 22:30
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86