본문으로 건너뛰기

Sending transactions

To send transactions, you must request the transact scope when initialising your Passport client.

📋Prerequisites

Sending transactions with Passport is simple and familiar. The following example shows a basic smart contract interaction (an ERC721 transfer):

import { passport } from '@imtbl/sdk';
import { ethers, providers } from 'ethers';

async function sendTransaction(passportInstance: passport.Passport) {
const provider = passportInstance.connectEvm();
const web3Provider = new providers.Web3Provider(provider);
const signer = web3Provider.getSigner();
const [ userAddress ] = await provider.request({ method: "eth_requestAccounts"});
const toAddress = '<address to transfer the token to>';
const erc721Address = '<address of the ERC-721 contract>';
const tokenId = 1234;

// The Application Binary Interface (ABI) of a contract provides instructions for
// encoding and decoding typed transaction data.
// Read more about [ABI Formats](https://docs.ethers.org/v5/api/utils/abi/formats/).
const abi = ['function safeTransferFrom(address from, address to, uint256 tokenId)'];

// Ethers provides an helper class called `Contract` that allows us to interact with smart contracts
// by abstracting away data-encoding using the contract ABI (definition of the contract's interface).
const contract = new ethers.Contract(erc721Address, abi, signer);

let tx;
// Send the transaction
try {
tx = await contract.safeTransferFrom(userAddress, toAddress, tokenId);
} catch (error: any) {
// Handle user denying signature
if (error.code === 4001){

}
}

// Wait for the transaction to complete
// On Immutable zkEVM, this takes 1-8 seconds in 99.9% of cases
const receipt = await tx.wait();

switch (receipt.status) {
// Failure
case 0:
break;
// Success
case 1:
break;
}

}

Under the hood, ethers will build a eth_sendTransaction RPC call to the Passport provider.

{
"data": "0x42842e0e...the rest of encoded the data",
"to": "<address of the ERC-721 contract>",
"from": "<your wallet address>"
}

Working with Typescript

If you want typing for your contracts, consider libraries like Typechain to generate Typescript interfaces from the contract ABI.