EVM
Interacting with EVM Transactions using MoonSDK
This tutorial will guide you through the process of sending an EVM transaction using MoonSDK. We will cover sending transactions with and without encoded ABI data, as well as creating and listing accounts.
Prerequisites
Node.js installed on your machine
An EVM-compatible account with some balance
Moon API key (optional, but required for some features)
Installation
First, install the required dependencies:
Initialization
Create a new file, evm_transaction.ts
, and import the necessary modules:
Initialize the MoonSDK with your API key (if you have one):
Sending a Transaction without Encoded ABI Data
To send a transaction without encoded ABI data, you need to prepare the transaction data, sign the transaction, and broadcast it to the blockchain.
Step 1: Prepare the Transaction Data
Store data pertaining to the transaction in an InputBody object:
Step 2: Sign the Transaction
Use the signTransaction function to sign the transaction and get the raw transaction string to broadcast the transaction:
Step 3: Broadcast the Signed Message to the Blockchain
Store the raw signed transaction string into a BroadcastInput object and use the input object to broadcast the transaction using broadcastTx:
Sending a Transaction with Encoded ABI Data
To send a transaction with encoded ABI data, you need to encode the data, prepare the transaction data, sign the transaction, and broadcast it to the blockchain.
Step 1: Encode the ABI Data
Encode the ABI data using ethers.js:
Step 2: Prepare the Transaction Data
Store data pertaining to the transaction in an InputBody object, including the encoded data:
Step 2: Sign the Transaction
Use the signTransaction function to sign the transaction and get the raw transaction string to broadcast the transaction:
Step 3: Broadcast the Signed Message to the Blockchain
Store the raw signed transaction string into a BroadcastInput object and use the input object to broadcast the transaction using broadcastTx:
Creating and Listing Accounts
You can create a new account using the createAccount function and list all accounts using the listAccounts function.
Creating an Account
To create a new account, use the createAccount function:
Listing Accounts
To list all accounts, use the listAccounts function:
Conclusion
You have successfully sent EVM transactions using MoonSDK, both with and without encoded ABI data, and created and listed accounts. You can now use MoonSDK to interact with various blockchain networks and services.
Last updated