Web3J BSC – How to Point Web3J to Other Networks Like BSC

bscweb3j

I would like to use web3j and to execute the functions of contracts that are found on Binance Smart Chain. How is this done ?

Would it be just

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

Then, how do I set the chain ID to 0x38 ?

Best Answer

If you are using the smart contact wrapper then you usually need to give one of the available transaction managers as a parameter and then give the chain Id when you initialize it

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(........);

if you are not using the web3j wrapper and build your transaction manually then:

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();
Related Topic