Web3j

如何將 web3j 指向其他網路,例如 BSC?

  • April 18, 2021

我想使用 web3j 並執行幣安智能鏈上的合約功能。這是怎麼做的?

會不會只是

Web3j web3 = Web3j.build(new HttpService("https://bsc-dataseed.binance.org/"))

那麼,如何將鏈 ID 設置為 0x38 ?

如果您使用的是智能聯繫人包裝器,那麼您通常需要提供一個可用的事務管理器作為參數,然後在初始化時提供鏈 ID

Web3j web3 = Web3j.build(new HttpService("https://bsc-dataseed.binance.org/"));
long chainId = 56;
FastRawTransactionManager txMananger = new FastRawTransactionManager(web3, <your-credentials-instance>, chainId);
MyContract contract = new MyContract(<address of contract>, web3, txManager, new DefaultGasProvider());
contract.transfer(........);

如果您不使用 web3j 包裝器並手動建構事務,則:

Web3j web3 = Web3j.build(new HttpService("https://bsc-dataseed.binance.org/"));
long chainId = 56;
RawTransaction rawTransaction = RawTransaction.createTransaction(<address-nonce>, <gas price as big integer>, <gas limit as big integer>, <contract address>, <endcoded function of contract>);
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId , <you Credentials>);
String hexValue = Numeric.toHexString(signedMessage);
EthSendTransaction transactionResponse = web3 .ethSendRawTransaction(hexValue).sendAsync().get();

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