EVM Integration Guide
How to drive Sai using an EVM signer through the EVM Interface contract. You pass a JSON‑encoded WASM message (wasmMsgBytes) along with token amounts/addresses.
0. Getting Started
Prerequisites
Node.js ≥ 18
pnpm, npm, or yarn
New Project
mkdir sai-evm && cd sai-evm
pnpm init -y
pnpm add ethers bignumber.js
pnpm add -D typescript ts-node @types/node dotenv
npx tsc --init --target ES2022 --module NodeNext --moduleResolution NodeNextIf you also want to call the Cosmos side in the same app, add:
@nibiruchain/nibijs @cosmjs/cosmwasm-stargate @cosmjs/stargate cosmjs-types.
Environment Setup
Create a .env file with your configuration:
Bootstrap Provider + Signer
Interface ABI
Import the ABI that exposes these methods: openTrade, executeSimpleFunctions, deposit, makeWithdrawRequest, redeem, and executeVaultSimpleFunctions.
Gas Estimation Helpers
1. Collateral / Payment Tokens
Decimal Handling
BANK units use 6 decimals (Cosmos side)
ERC‑20 units use the token's decimals (typically 6)
Testnet-1
USDC
0xAb68f1D1d91854383fd4Df9016E3040D03e8191a
stNIBI
0xCae3d404AFB50016154a4B18091351065154E9bD
Mainnet
USDC
0x0829F361A05D993d5CEb035cA6DF3446b060970b
stNIBI
0xcA0a9Fb5FBF692fa12fD13c0A900EC56Bb3f0a7b
Converting Between Decimals
When combining BANK and ERC-20 amounts, ensure both use BANK units (6 decimals) before summing:
2. Perps – Open Trade
Method Signature
Example
3. Perps – Close Trade
Close an open position:
4. Referral – Create & Redeem Codes
Create a Referral Code
Redeem a Referral Code
5. Vault – Deposit
Deposit collateral into a vault:
6. Vault – Make Withdraw Request
Request to withdraw funds from a vault:
7. Vault – Redeem
Redeem shares from a vault:
8. Vault – Cancel Withdraw Request
Cancel a pending withdraw request:
Error Handling & Troubleshooting
Common Issues
"No EVM signer"
Ensure makeSigner() is called and PRIVATE_KEY is set in .env
Gas estimation fails
The function automatically falls back to 5,000,000 gas limit and 1 gwei price
Transaction reverts on-chain
Use debug_traceTransaction on your archive RPC to inspect the deepest call error
Decimal conversion errors
Always convert ERC-20 and BANK amounts to the same decimal base before summing
Invalid INTERFACE_ADDR
Verify the address matches your network (Testnet-1 vs Mainnet)
Best Practices
Always use an EVM signer (provider alone cannot send transactions)
Apply gas buffering to avoid "out of gas" errors
Validate that
is_evm_origin: trueis set in all trade messagesConvert token decimals correctly before combining BANK and ERC-20 amounts
Store
PRIVATE_KEYsecurely and never commit it to version control
Last updated
