Skip to main content

Send NFT transfers

ChainGate supports transferring NFTs on EVM networks. Use transferNft() for ERC-721 tokens (unique NFTs) and transferErc1155() for ERC-1155 tokens (semi-fungible).

Transfer ERC-721 NFT

import { importWallet, ChainGate } from 'chaingate'

const wallet = importWallet({ phrase: 'your phrase here...' })
const cg = new ChainGate({ apiKey: 'your-api-key' })

const ethereum = cg.connect(cg.networks.ethereum, wallet)

const tx = await ethereum.transferNft(
'0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D', // BAYC contract
'42', // Token ID
'0xRecipient...' // Recipient address
)

const fees = tx.recommendedFees()
tx.setFee(fees.high)
const broadcasted = await tx.signAndBroadcast()
console.log('TX ID:', broadcasted.transactionId)

Transfer ERC-1155 token

const tx = await ethereum.transferErc1155(
'0xContract...', // ERC-1155 contract address
'42', // Token ID
10n, // Amount of tokens
'0xRecipient...' // Recipient address
)

const broadcasted = await tx.signAndBroadcast()

Custom smart contract calls

For advanced interactions, use callContract() to send arbitrary calldata to any smart contract:

const tx = await ethereum.callContract(
'0xContractAddress...', // Contract address
'0xa9059cbb000000000000000000000000...', // ABI-encoded calldata
cg.networks.ethereum.amount(0) // ETH value (0 for pure calls)
)

const broadcasted = await tx.signAndBroadcast()

Custom EVM RPC networks

All methods (transferNft(), transferErc1155(), callContract()) also work on custom EVM RPC networks:

const polygon = cg.networks.evmRpc({
rpcUrl: cg.rpcUrls.polygon,
chainId: 137,
name: 'Polygon',
symbol: 'POL',
})

const connector = cg.connect(polygon, wallet)
const tx = await connector.transferNft('0xContract...', '42', '0xRecipient...')

Key points

  • ERC-721: transferNft() uses safeTransferFrom under the hood.
  • ERC-1155: transferErc1155() uses safeTransferFrom with an amount parameter.
  • Gas fees: NFT transfers require native coin (ETH, POL, etc.) for gas fees.
  • All EVM networks: All methods work on both native EVM connectors and custom EVM RPC connectors.
  • Custom calls: Use callContract() for any smart contract interaction not covered by the built-in methods.