Skip to content

Sai – 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.


  • Node.js ≥ 18
  • pnpm, npm, or yarn
Terminal window
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 NodeNext

If you also want to call the Cosmos side in the same app, add: @nibiruchain/nibijs @cosmjs/cosmwasm-stargate @cosmjs/stargate cosmjs-types.

Create a .env file with your configuration:

Terminal window
# Testnet-2
EVM_RPC=https://evm-rpc.testnet-2.nibiru.fi/
# Or Mainnet
# EVM_RPC=https://evm-rpc.nibiru.fi/
PRIVATE_KEY=0x... # Your private key (keep this secret!)
INTERFACE_ADDR=0x... # Contract address from saiEvmInterfaceFromChainType(chainType)
src/evm.ts
import "dotenv/config"
import { ethers } from "ethers"
export function makeSigner() {
const rpc = process.env.EVM_RPC!
const pk = process.env.PRIVATE_KEY!
const provider = new ethers.JsonRpcProvider(rpc)
const signer = new ethers.Wallet(pk, provider)
return { provider, signer }
}

Import the ABI that exposes these methods: openTrade, executeSimpleFunctions, deposit, makeWithdrawRequest, redeem, and executeVaultSimpleFunctions.

import PerpVaultEvmInterfaceAbi from "./PerpVaultEvmInterface.json" // Place your ABI file here

eth_estimateGas runs against an infinite Cosmos SDK gas meter, so it does not model the 63/64 throttling applied to ERC-20 sub-calls at execution time and can return less gas than the same call needs when it runs. Pad generously and apply a floor: these calls execute at a zero fee price, so an oversized limit costs nothing while an undersized one loses the transaction.

const FALLBACK_GAS_LIMIT = 5_000_000n
const MIN_GAS_LIMIT = 2_500_000n
const FALLBACK_GAS_PRICE = ethers.parseUnits("1", "gwei")
const withGasBuffer = (g: bigint) => {
const padded = g <= 0n ? 0n : g * 2n
return padded > MIN_GAS_LIMIT ? padded : MIN_GAS_LIMIT
}
async function estimateGasWithFallback(
fn: () => Promise<bigint>,
info?: unknown
) {
try {
const est = await fn()
return { gasLimit: withGasBuffer(est) }
} catch (error) {
console.warn("Gas estimation failed, using fallback", info)
return { gasLimit: FALLBACK_GAS_LIMIT, gasPrice: FALLBACK_GAS_PRICE }
}
}
const toTxOverrides = ({
gasLimit,
gasPrice,
}: {
gasLimit: bigint
gasPrice?: bigint
}) => (gasPrice === undefined ? { gasLimit } : { gasLimit, gasPrice })

  • BANK units use 6 decimals (Cosmos side)
  • ERC‑20 units use the token’s decimals (typically 6)
TokenERC-20 Address
USDC0xAb68f1D1d91854383fd4Df9016E3040D03e8191a
stNIBI0xCae3d404AFB50016154a4B18091351065154E9bD
TokenERC-20 Address
USDC0x0829F361A05D993d5CEb035cA6DF3446b060970b
stNIBI0xcA0a9Fb5FBF692fa12fD13c0A900EC56Bb3f0a7b

When combining BANK and ERC-20 amounts, ensure both use BANK units (6 decimals) before summing:

const toBankUnits = (
amt: bigint,
sourceDecimals: number,
bankDecimals: number = 6
): bigint => {
if (sourceDecimals === bankDecimals) return amt
const diff = Math.abs(sourceDecimals - bankDecimals)
const factor = 10n ** BigInt(diff)
return sourceDecimals > bankDecimals ? amt / factor : amt * factor
}

contract.openTrade(
wasmMsgBytes, // JSON-encoded WASM message with is_evm_origin: true
collateralIndex, // Token index (e.g., 0 for first collateral)
totalAmountBankUnits, // Total amount in BANK units
useErc20Amount, // ERC-20 amount in token decimals
overrides // Gas overrides
)
import { ethers } from "ethers"
import BigNumber from "bignumber.js"
import PerpVaultEvmInterfaceAbi from "./PerpVaultEvmInterface.json"
import { makeSigner } from "./evm"
async function openTrade() {
const { signer } = makeSigner()
const iface = new ethers.Contract(
process.env.INTERFACE_ADDR!,
PerpVaultEvmInterfaceAbi.abi,
signer
)
const msg = {
open_trade: {
market_index: "MarketIndex(0)",
leverage: "5",
long: true,
collateral_index: "TokenIndex(0)",
trade_type: "trade",
open_price: "70000",
tp: undefined,
sl: undefined,
slippage_p: "0.01", // CosmWasm Decimal fraction: 0.01 = 1%
is_evm_origin: true, // IMPORTANT: Set to true for EVM origin
},
}
const bankDecimals = 6
const erc20Decimals = 6
const displayAmount = new BigNumber("100")
const bankAmount = ethers.parseUnits(displayAmount.toString(), bankDecimals)
const erc20Amount = ethers.parseUnits(displayAmount.toString(), erc20Decimals)
const toBankUnits = (
amt: bigint,
ercDec: number,
bankDec: number
): bigint => {
if (ercDec === bankDec) return amt
const diff = Math.abs(ercDec - bankDec)
const factor = 10n ** BigInt(diff)
return ercDec > bankDec ? amt / factor : amt * factor
}
const totalBank = bankAmount + toBankUnits(erc20Amount, erc20Decimals, bankDecimals)
const wasmMsgBytes = ethers.toUtf8Bytes(JSON.stringify(msg))
const gas = await estimateGasWithFallback(() =>
iface.openTrade.estimateGas(wasmMsgBytes, 0, totalBank, erc20Amount)
)
const tx = await iface.openTrade(
wasmMsgBytes,
0,
totalBank,
erc20Amount,
toTxOverrides(gas)
)
console.log("Transaction submitted:", tx.hash)
const receipt = await tx.wait()
console.log("Mined in block:", receipt?.blockNumber)
}

Close an open position:

async function closeTrade() {
const { signer } = makeSigner()
const iface = new ethers.Contract(
process.env.INTERFACE_ADDR!,
PerpVaultEvmInterfaceAbi.abi,
signer
)
const msg = { close_trade: { trade_index: "UserTradeIndex(0)" } }
const wasmMsgBytes = ethers.toUtf8Bytes(JSON.stringify(msg))
const gas = await estimateGasWithFallback(() =>
iface.executeSimpleFunctions.estimateGas(wasmMsgBytes)
)
const tx = await iface.executeSimpleFunctions(wasmMsgBytes, toTxOverrides(gas))
const receipt = await tx.wait()
console.log("Trade closed in block:", receipt?.blockNumber)
}

async function createReferralCode() {
const { signer } = makeSigner()
const iface = new ethers.Contract(
process.env.INTERFACE_ADDR!,
PerpVaultEvmInterfaceAbi.abi,
signer
)
const msg = { create_referrer_code: { code: "MYCODE" } }
const bytes = ethers.toUtf8Bytes(JSON.stringify(msg))
const gas = await estimateGasWithFallback(() =>
iface.executeSimpleFunctions.estimateGas(bytes)
)
const tx = await iface.executeSimpleFunctions(bytes, toTxOverrides(gas))
await tx.wait()
console.log("Referral code created")
}
async function redeemReferralCode() {
const { signer } = makeSigner()
const iface = new ethers.Contract(
process.env.INTERFACE_ADDR!,
PerpVaultEvmInterfaceAbi.abi,
signer
)
const msg = { redeem_referrer_code: { code: "PARTNER" } }
const bytes = ethers.toUtf8Bytes(JSON.stringify(msg))
const gas = await estimateGasWithFallback(() =>
iface.executeSimpleFunctions.estimateGas(bytes)
)
const tx = await iface.executeSimpleFunctions(bytes, toTxOverrides(gas))
await tx.wait()
console.log("Referral code redeemed")
}

Deposit collateral into a vault:

async function depositToVault() {
const { signer } = makeSigner()
const iface = new ethers.Contract(
process.env.INTERFACE_ADDR!,
PerpVaultEvmInterfaceAbi.abi,
signer
)
const msg = { deposit: {} }
const bytes = ethers.toUtf8Bytes(JSON.stringify(msg))
const bankDecimals = 6
const erc20Decimals = 6
const bankAmount = ethers.parseUnits("250", bankDecimals)
const erc20Amount = ethers.parseUnits("250", erc20Decimals)
const toBankUnits = (
amt: bigint,
ercDec: number,
bankDec: number
): bigint => {
if (ercDec === bankDec) return amt
const diff = Math.abs(ercDec - bankDec)
const factor = 10n ** BigInt(diff)
return ercDec > bankDec ? amt / factor : amt * factor
}
const totalBank = bankAmount + toBankUnits(erc20Amount, erc20Decimals, bankDecimals)
const vaultAddr = "<bech32 vault address>"
const collateralErc20 = "<erc20 token address>"
const gas = await estimateGasWithFallback(() =>
iface.deposit.estimateGas(
bytes,
totalBank,
erc20Amount,
vaultAddr,
collateralErc20,
true
)
)
const tx = await iface.deposit(
bytes,
totalBank,
erc20Amount,
vaultAddr,
collateralErc20,
true,
toTxOverrides(gas)
)
await tx.wait()
console.log("Deposited to vault")
}

Request to withdraw funds from a vault:

async function makeWithdrawRequest() {
const { signer } = makeSigner()
const iface = new ethers.Contract(
process.env.INTERFACE_ADDR!,
PerpVaultEvmInterfaceAbi.abi,
signer
)
const msg = { make_withdraw_request: {} }
const bytes = ethers.toUtf8Bytes(JSON.stringify(msg))
const sharesBank = 1_000_000n // 1.0 shares in BANK units
const sharesFromErc20 = 0n
const totalShares = sharesBank + sharesFromErc20
const vaultAddr = "<vault address>"
const gas = await estimateGasWithFallback(() =>
iface.makeWithdrawRequest.estimateGas(vaultAddr, bytes, totalShares, sharesFromErc20)
)
const tx = await iface.makeWithdrawRequest(
vaultAddr,
bytes,
totalShares,
sharesFromErc20,
toTxOverrides(gas)
)
await tx.wait()
console.log("Withdraw request created")
}

Redeem shares from a vault:

async function redeemVaultShares() {
const { signer } = makeSigner()
const iface = new ethers.Contract(
process.env.INTERFACE_ADDR!,
PerpVaultEvmInterfaceAbi.abi,
signer
)
const msg = { redeem: { shares: "500000" } } // 0.5 shares
const bytes = ethers.toUtf8Bytes(JSON.stringify(msg))
const vaultAddr = "<vault address>"
const sendToEvm = true
const gas = await estimateGasWithFallback(() =>
iface.redeem.estimateGas(bytes, vaultAddr, "500000", sendToEvm)
)
const tx = await iface.redeem(
bytes,
vaultAddr,
"500000",
sendToEvm,
toTxOverrides(gas)
)
await tx.wait()
console.log("Shares redeemed")
}

Cancel a pending withdraw request:

async function cancelWithdrawRequest() {
const { signer } = makeSigner()
const iface = new ethers.Contract(
process.env.INTERFACE_ADDR!,
PerpVaultEvmInterfaceAbi.abi,
signer
)
const msg = { cancel_withdraw_request: { unlock_epoch: 123 } }
const bytes = ethers.toUtf8Bytes(JSON.stringify(msg))
const vaultAddr = "<vault address>"
const gas = await estimateGasWithFallback(() =>
iface.executeVaultSimpleFunctions.estimateGas(bytes, vaultAddr)
)
const tx = await iface.executeVaultSimpleFunctions(
bytes,
vaultAddr,
toTxOverrides(gas)
)
await tx.wait()
console.log("Withdraw request cancelled")
}

IssueSolution
”No EVM signer”Ensure makeSigner() is called and PRIVATE_KEY is set in .env
Gas estimation failsThe function automatically falls back to 5,000,000 gas limit and 1 gwei price
Transaction reverts on-chainUse debug_traceTransaction on your archive RPC to inspect the deepest call error
Decimal conversion errorsAlways convert ERC-20 and BANK amounts to the same decimal base before summing
Invalid INTERFACE_ADDRVerify the address matches your network (Testnet-2 vs Mainnet)
  • Always use an EVM signer (provider alone cannot send transactions)
  • Apply gas buffering to avoid “out of gas” errors
  • Validate that is_evm_origin: true is set in all trade messages
  • Convert token decimals correctly before combining BANK and ERC-20 amounts
  • Store PRIVATE_KEY securely and never commit it to version control