Contract Encoding
Encode ERC-20, ERC-721, ERC-1155, and arbitrary contract calls for use in UserOperations.
The contract encoding module provides async helper functions that produce ABI-encoded calldata (0x${string}) ready to be passed as the data field of a UserOperation. Internally it uses viem for encoding.
Generic Contract Calls
Encode any contract function using its ABI:
import { encodeContractCall } from '@embarkai/core'
const abi = [
{
name: 'swap',
type: 'function',
inputs: [
{ name: 'tokenIn', type: 'address' },
{ name: 'tokenOut', type: 'address' },
{ name: 'amountIn', type: 'uint256' },
],
outputs: [{ name: 'amountOut', type: 'uint256' }],
},
] as const
const calldata = await encodeContractCall(abi, 'swap', [
'0xTokenInAddress',
'0xTokenOutAddress',
1000000000000000000n,
])ERC-20 Token Operations
Transfer
import { encodeERC20Transfer } from '@embarkai/core'
const data = await encodeERC20Transfer(
'0xRecipientAddress', // to
1000000000000000000n, // amount (1 token with 18 decimals)
)Approve
import { encodeERC20Approve } from '@embarkai/core'
const data = await encodeERC20Approve(
'0xSpenderAddress', // spender
5000000000000000000n, // amount
)Transfer From
import { encodeERC20TransferFrom } from '@embarkai/core'
const data = await encodeERC20TransferFrom(
'0xFromAddress', // from
'0xToAddress', // to
1000000000000000000n, // amount
)ERC-721 (NFT) Operations
Transfer
import { encodeERC721TransferFrom } from '@embarkai/core'
const data = await encodeERC721TransferFrom(
'0xFromAddress',
'0xToAddress',
42n, // tokenId
)Safe Transfer
Includes a receiver check to prevent NFTs being sent to contracts that cannot handle them:
import { encodeERC721SafeTransferFrom } from '@embarkai/core'
const data = await encodeERC721SafeTransferFrom(
'0xFromAddress',
'0xToAddress',
42n, // tokenId
'0xoptional', // additional data (optional)
)Approve & Set Approval for All
import {
encodeERC721Approve,
encodeERC721SetApprovalForAll,
} from '@embarkai/core'
const approveData = await encodeERC721Approve('0xOperator', 42n)
const approvalForAllData = await encodeERC721SetApprovalForAll(
'0xOperator',
true, // approved
)ERC-1155 (Multi-Token) Operations
Single Transfer
import { encodeERC1155SafeTransferFrom } from '@embarkai/core'
const data = await encodeERC1155SafeTransferFrom(
'0xFromAddress',
'0xToAddress',
1n, // token id
100n, // amount
'0x', // additional data
)Batch Transfer
Transfer multiple token types in a single call:
import { encodeERC1155SafeBatchTransferFrom } from '@embarkai/core'
const data = await encodeERC1155SafeBatchTransferFrom(
'0xFromAddress',
'0xToAddress',
[1n, 2n, 3n], // token ids
[100n, 200n, 50n], // amounts
'0x', // additional data
)Set Approval for All
import { encodeERC1155SetApprovalForAll } from '@embarkai/core'
const data = await encodeERC1155SetApprovalForAll('0xOperator', true)API Reference
All encoding functions are async and return a hex-encoded calldata string.
| Function | Token Standard | Description |
|---|---|---|
encodeContractCall(abi, functionName, args?) | Any | Encode arbitrary contract call |
encodeERC20Transfer(to, amount) | ERC-20 | Transfer tokens |
encodeERC20Approve(spender, amount) | ERC-20 | Approve spending |
encodeERC20TransferFrom(from, to, amount) | ERC-20 | Transfer from approved address |
encodeERC721TransferFrom(from, to, tokenId) | ERC-721 | Transfer NFT |
encodeERC721SafeTransferFrom(from, to, tokenId, data?) | ERC-721 | Safe transfer NFT |
encodeERC721Approve(to, tokenId) | ERC-721 | Approve single NFT |
encodeERC721SetApprovalForAll(operator, approved) | ERC-721 | Approve all NFTs |
encodeERC1155SafeTransferFrom(from, to, id, amount, data?) | ERC-1155 | Transfer multi-token |
encodeERC1155SafeBatchTransferFrom(from, to, ids, amounts, data?) | ERC-1155 | Batch transfer |
encodeERC1155SetApprovalForAll(operator, approved) | ERC-1155 | Approve all tokens |
Usage with ServerWallet
Combine encoded calldata with sendUserOperation to execute token operations through your smart account:
import { createServerWalletManager, MemoryKeyshareStorage } from '@embarkai/core'
import { encodeERC20Transfer } from '@embarkai/core'
const manager = createServerWalletManager({
apiKey: process.env.EMBARK_API_KEY!,
chainId: 994873017,
storage: new MemoryKeyshareStorage(),
})
const wallet = await manager.getWallet('treasury')
const TOKEN_CONTRACT = '0xYourERC20TokenAddress'
const calldata = await encodeERC20Transfer('0xRecipient', 1000000n)
const hash = await wallet.sendUserOperation(TOKEN_CONTRACT, '0', calldata)
const receipt = await wallet.waitForUserOperationReceipt(hash)
console.log('Transfer confirmed:', receipt.transactionHash)Next Steps
- Bundler — submit encoded calldata as UserOperations
- Server Wallets — high-level wallet API with
sendUserOperation - Authentication — set up auth before making transactions
Last updated on