I’m working on a smart contract that can handle multiple operations in a single transaction using delegateCall. What I want to do is create an NFT, give permission to a marketplace contract, and then list it for sale all at once.
function createAndListNFT(address payable receiver, string memory tokenUri, address nftContract, uint256 price, address marketContract)
external
payable
returns (uint256)
{
address nftCollection = nftContract;
address marketplaceAddr = marketContract;
uint256 newTokenId = executeNFTCreation(nftCollection, receiver, tokenUri);
executeMarketplaceApproval(nftCollection, marketContract);
executeListingForSale(marketplaceAddr, nftContract, newTokenId, price);
return newTokenId;
}
function executeNFTCreation(address nftAddr, address owner, string memory metadataUri) internal returns(uint256 resultTokenId){
INFTContract nftInterface = INFTContract(nftAddr);
resultTokenId = nftInterface.createToken(payable(owner), metadataUri);
return resultTokenId;
}
function executeMarketplaceApproval(address nftAddr, address approvedOperator) internal{
bool approvalStatus = true;
bytes memory callData = abi.encodeWithSignature("setApprovalForAll(address,bool)", approvedOperator, approvalStatus);
bool callSuccess;
assembly {
let memPtr := mload(0x40)
callSuccess := delegatecall(
gas(),
nftAddr,
add(callData, 0x20),
mload(callData),
0,
0
)
}
}
function executeListingForSale(address marketAddr, address tokenContract, uint256 tokenNumber, uint256 salePrice) internal{
bytes memory callData = abi.encodeWithSignature("setSalePrice(address,uint256,uint256)", tokenContract, tokenNumber, salePrice);
bool callSuccess;
assembly {
let memPtr := mload(0x40)
callSuccess := delegatecall(
gas(),
marketAddr,
add(callData, 0x20),
mload(callData),
0,
0
)
}
}
The NFT creation part works fine but I’m getting errors with the delegateCall operations for approval and listing. The transaction fails during these steps. Has anyone dealt with similar issues when trying to batch these operations together?