How to transfer ERC-20 tokens and NFTs in a single transaction

I’m working on a project where I need to send both ERC-20 tokens and NFTs (either ERC-721 or ERC-1155) to someone in just one transaction. I want to make it more efficient instead of doing separate transfers.

Is this actually possible to do? I’ve been looking around but can’t find clear information about combining these transfers into one transaction.

If anyone has done this before, could you share how you did it? Maybe with some code examples showing the smart contract functions or web3 calls needed? I’m particularly interested in understanding if there are any special considerations or gotchas I should watch out for when doing batch transfers like this.

Any help would be really appreciated since I’m still learning about these token standards and how they work together.

yeah this is definitly doable but you’ll need a wrapper contract. basically create a function that calls both token contracts in sequence - first the erc20 transfer then the nft transfer. just make sure you handle approvals correctly beforehand or the whole thing will revert. gas costs can get prety high tho so test on testnet first.

To achieve combined transfers of ERC-20 tokens and NFTs within a single transaction, consider implementing a batch transfer contract that accommodates both standards. In my experience, employing a multicall structure is effective; this necessitates passing arrays for token addresses, recipient addresses, and the respective amounts or token IDs. One challenge to watch for is the differing transfer methods: ERC-20 utilizes ‘transfer()’, while ERC-721 requires ‘safeTransferFrom()’, and ERC-1155 has its own functions. I recommend utilizing ERC-165 interface checks to identify the appropriate method to invoke. Additionally, be cautious of gas limits, as multiple external calls can complicate estimation, and ensure that your contract reverts the entire transaction if any individual transfer encounters an error, preventing silent failures.

hmm thats an interesting use case! i’m curious tho - what kind of project are you building that needs both erc20s and nfts transfered together? sounds like it could be some kind of gaming or marketplace thing?

anyway, i’ve seen people do this with proxy contracts but im wondering if you’ve considered just using existing solutions like gnosis safe’s batch transactions? might be easier than rolling your own contract, especially if you’re still learning the ropes.

also got to ask - are you planning to let users approve your contract to spend thier tokens, or are you thinking more of a direct transfer approach? because the approval flow gets a bit tricky when you’re dealing with multiple token types at once. users might get confused having to approve multiple contracts.

one thing that caught my attention is the gas optimization aspect you mentioned. have you done any rough calculations on whether the gas savings are actually worth the added complexity? sometimes keeping things simple is better than trying to optimize prematurely.

what blockchain are you targeting btw? some l2s have better tooling for this kind of batch operations.