Example Usage
The Sei precompiles can be used like any standard smart contract on the EVM. For example, using ethers.js , you can query and execute actions on a CosmWasm contract as follows:
Setup
To install ethers
, run the following command in your project directory
terminal:
npm install ethers
npm install @sei-js/evm
Next, you’ll need to use one of the precompiles in EVM Precompiles
section. In
this example, we’re going to be using the wasmd precompile:
// Import Wasm precompile address and ABI
// View the entire ABI here: https://github.com/sei-protocol/sei-chain/tree/evm/precompiles/wasmd
import { WASM_PRECOMPILE_ABI, WASM_PRECOMPILE_ADDRESS } from '@sei-js/evm';
Using the contract
Next, we’ll set up a provider and contract to interact with the blockchain:
import { ethers, toUtf8Bytes, toUtf8String } from 'ethers';
// Using MetaMask as the signer and provider
const provider = new ethers.BrowserProvider(window.ethereum);
const signer = await provider.getSigner();
// Create a contract with the signer
const contract = new ethers.Contract(WASM_PRECOMPILE_ADDRESS, WASM_PRECOMPILE_ABI, signer);
Querying & Executing a CosmWasm Contract
Once you have the contract, you can query and execute messages to any CosmWasm
smart contract. In the below example we assume there is a counter contract with a get_count
function
// Replace with your contract
const COUNTER_CONTRACT_ADDRESS = 'sei1eyf...xff';
// Query to get the count on the counter contract (replace with function from your ABI)
const queryMsg = { get_count: {} };
const queryResponse = await contract.query(COUNTER_CONTRACT_ADDRESS, toUtf8Bytes(JSON.stringify(queryMsg)));
console.log(toUtf8String(queryResponse));
// Execute message to increment the count on the contract. (Replace with any function from your ABI)
// No funds are attached since the increment method does not require sei. (Your ABI may require funds)
const executeMsg = { increment: {} };
const executeResponse = await contract.execute(
COUNTER_CONTRACT_ADDRESS,
toUtf8Bytes(JSON.stringify(executeMsg)),
toUtf8Bytes(JSON.stringify([])) // Used for sending funds if needed
);
// Wait for the transaction to be confirmed
await executeResponse.wait();
console.log(executeResponse);
Executing a payable function
In this example, we execute the ‘donate’ method on our contract. This is similar
to the increment
method, but also receives funds from the user and stores it
in the contract.
// Execute a message to donate to the contract.
const executeMsg = { donate: {} };
// Funds are attached via overrides. This example is specific to ethers.js
const overrides = {
value: ethers.parseEther('3.2') // Sending 3.2 sei
};
const executeResponse = await contract.execute(
COUNTER_CONTRACT_ADDRESS,
toUtf8Bytes(JSON.stringify(executeJSON)),
toUtf8Bytes(JSON.stringify({ denom: 'usdc', amount: '100' })), // Also send 100 usdc
overrides
);
await executeResponse.wait();
const receipt = await provider.getTransactionReceipt(executionResponse.hash);
For payable contracts, Sei amounts have to be sent directly to the contract
while other denoms should use the coins
field.