Erc-20

使用 ERC-20 java 庫時出錯:“僅允許通過 RPC 進行重放保護 (EIP-155) 事務”

  • February 7, 2022

我正在嘗試使用 ERC-20 java 庫進行簡單的交易。我將孟買測試網路用於多邊形。程式碼從一個錢包從另一個錢包轉移穩定幣。在批准方法呼叫期間,我收到以下錯誤:

“處理事務請求時出錯:僅允許通過 RPC 進行重放保護 (EIP-155) 事務”

當我將 rinkeby 測試網路用於乙太坊並進行相同的穩定幣交易時,它按預期工作。

是否可以使用 ERC-20 java 庫集成重放保護(EIP-155)事務,這兩種情況(乙太坊和多邊形)都適用?

       String network = "https://rpc-mumbai.maticvigil.com/";
       String contractAddress = "0xE10a59f3dAAe01e0544E3d45F5579C3eBb7e9c9D";
       String custumerPk = "private key";
       String bridgePk = "private key";
       String bridgeWallet = "0x9d7Be2E6d684Aa8A5995Fe322fF425E9Bdbd6BE7";
       String customerWallet = "0x515e34F4760c7fdcd61d9cAa278f80C15AC7ECdB";

       Web3j web3j = Web3j.build(new HttpService(networkPoly));
       Credentials bridgeCreds = Credentials.create(bridgePk);
       ERC20 bridgeToken = ERC20.load(contractAddress ,web3j,bridgeCreds,new DefaultGasProvider());
       Credentials cutromerCreds = Credentials.create(custoerPk);

       ERC20 cutomerToken = ERC20.load(contractAddress ,web3j,cutromerCreds, new DefaultGasProvider());

       BigInteger amount = new BigInteger("1000000000000000000");

       TransactionReceipt send = cutomerToken.approve(bridgeWallet, amount).send();

       TransactionReceipt send1 = bridgeToken.transferFrom(customerWallet, bridgeWallet, amount).send();

您需要提供chainId。這是倫敦分叉之後的要求,否則將不接受tx。

一種方法是定義一個事務管理器,Web3j 中有許多類型的事務管理器,這“只是”一個例子:

long chainIdOfPolygon = 80001;
Web3j web3j = Web3j.build(new HttpService(networkPoly));

Credentials bridgeCreds = Credentials.create(bridgePk);
TransactionManager bridgeTokenTxManager = new RawTransactionManager(
   web3j, bridgeCreds, chainIdOfPolygon);
ERC20 bridgeToken = ERC20.load(contractAddress, web3j, bridgeTokenTxManager, new DefaultGasProvider());

Credentials cutromerCreds = Credentials.create(custoerPk);
TransactionManager cutomerTokenTxManager = new RawTransactionManager(
   web3j, cutromerCreds, chainIdOfPolygon);
ERC20 cutomerToken = ERC20.load(contractAddress, web3j , cutomerTokenTxManager, new DefaultGasProvider());

引用自:https://ethereum.stackexchange.com/questions/121003