The Sodium Wallet is compatible with various methods for sending transactions
Simple Transfer
// send 1eth to 0x812D3C67d283F3b9d1F1E347DF37F0C7fbD6a0B0consttransaction= { to:"0x812D3C67d283F3b9d1F1E347DF37F0C7fbD6a0B0", value:"1000000000000000000"}constsigner=wallet.getSigner()consttxnResponse=awaitsigner.sendTransaction(transaction)console.log(txnResponse)// wait for the transaction to be minedawaittxnResponse.wait()
It creates a transaction object with the destination address and amount in wei, gets a Signer from the wallet, and calls sendTransaction() to send the signed transaction.
Transfer ERC20
// ERC20 contract address to send fromconsterc20ContractAddress="";// Destination address for the transferconstrecipientAddress="";// Transfer amountconstamount="";consterc20Interface=newethers.utils.Interface(['function transfer(address _to, uint256 _value)'])// Encode an ERC-20 token transfer to recipient of the specified amountconstdata=erc20Interface.encodeFunctionData('transfer', [recipientAddress, amount])consttransaction= { to: erc20ContractAddress, data}constsigner=wallet.getSigner()consttxnResponse=awaitsigner.sendTransaction(transaction)console.log(txnResponse)// wait for the transaction to be minedawaittxnResponse.wait()
It encodes an ERC20 transfer call, creates a transaction with the data, and sends the transaction.
Batch Transactions
Sodium Wallet supports batch transactions.
This is useful for scenarios like token approval.
// ERC20 contract address to send fromconsterc20ContractAddress="";// Destination address for the first transferconstrecipientAddress1="";// Transfer amount for the first transferconstamount1="";// Destination address for the second transferconstrecipientAddress2="";// Transfer amount for the second transferconstamount2="";consterc20Interface=newethers.utils.Interface(['function transfer(address _to, uint256 _value)'])// Encode two different ERC-20 token transfersconstdata1=erc20Interface.encodeFunctionData('transfer', [recipient1Address, amount1])constdata2=erc20Interface.encodeFunctionData('transfer', [recipient2Address, amount2])consttransaction1= { to: erc20ContractAddress, data: data1}consttransaction2= { to: erc20ContractAddress, data: data2}// Send a multiple transactions as a single bundle which is executed as one transaction on chain.constsigner=wallet.getSigner()consttxnResponse=awaitsigner.sendTransactionBatch([transaction1, transaction2])console.log(txnResponse)// Wait for the batched transaction to be minedawaittxnResponse.wait()javasc
It shows how to send multiple transactions in a single call by passing an array of transactions to sendTransactionBatch(). This allows executing multiple transactions as one on-chain.
In summary:
sendTransaction() sends a single ether or ERC20 transaction
sendTransactionBatch() sends multiple transactions in a single call
encodeFunctionData() encodes an ERC20 transfer call
getSigner() obtains a Signer to sign transactions from the wallet