How can I move ERC1155 tokens between contracts with Ethers.js?

Hey everyone! I’m trying to figure out how to transfer ERC1155 tokens from one smart contract to another using Ethers.js. I’ve been messing around with it for a while but can’t seem to get it right. Has anyone done this before? What’s the best way to go about it? I’d really appreciate some pointers or examples if you’ve got them. Thanks in advance for any help!

// Example of what I'm trying to do
const sourceContract = new ethers.Contract(sourceAddress, abi, signer);
const targetContract = new ethers.Contract(targetAddress, abi, signer);

// How do I transfer the token here?
const tx = await sourceContract.someTransferFunction(targetContract.address, tokenId, amount);

I’m not sure if this is the right approach, so any guidance would be awesome!

hey pixstar, i’ve done this before. you’ll wanna use the safeTransferFrom function from the ERC1155 standard. it goes like this:

await sourceContract.safeTransferFrom(sourceContract.address, targetContract.address, tokenId, amount, '0x')

make sure you’ve got approval set up first tho. hope this helps!

To transfer ERC1155 tokens between contracts using Ethers.js, you’ll need to use the safeTransferFrom function from the ERC1155 standard. Here’s how you can implement it:

const tx = await sourceContract.safeTransferFrom(
  sourceContract.address,
  targetContract.address,
  tokenId,
  amount,
  '0x'
);
await tx.wait();

Ensure you have the necessary approvals set up beforehand. Also, remember to handle potential errors and gas costs. If you’re transferring multiple token types simultaneously, consider using safeBatchTransferFrom for efficiency.

Always test thoroughly on a testnet before deploying to mainnet to avoid costly mistakes.

hey there pixstar54! i’m curious about your project. what kind of tokens are you trying to move between contracts? :thinking:

i’ve played around with erc1155 transfers before, and dancingcloud’s suggestion is a good starting point. but have you considered using the safeBatchTransferFrom function instead? it could be super handy if you need to move multiple token ids at once.

here’s a rough idea of how it might look:

const tokenIds = [1, 2, 3]; // example token ids
const amounts = [10, 20, 30]; // corresponding amounts

await sourceContract.safeBatchTransferFrom(
  sourceContract.address,
  targetContract.address,
  tokenIds,
  amounts,
  '0x'
);

just make sure you’ve got the right permissions set up. oh, and don’t forget to handle any potential errors!

what’s the coolest thing about the tokens you’re working with? i’d love to hear more about your project if you’re up for sharing!