@@ -758,46 +758,46 @@ contract ExchangeHandlerFacet is IBosonExchangeHandler, BuyerBase, DisputeBase {
758758 : twin.supplyAvailable - twin.amount;
759759 }
760760
761- if (tokenType == TokenType.FungibleToken) {
762- // ERC-20 style transfer
763- (success, result) = twin.tokenAddress.call { gas: gasleft () - reservedGas }(
764- abi.encodeWithSignature (
765- "transferFrom(address,address,uint256) " ,
766- seller.assistant,
767- sender,
768- twin.amount
769- )
770- );
771- } else if (tokenType == TokenType.NonFungibleToken) {
772- // Token transfer order is ascending to avoid overflow when twin supply is unlimited
773- if (twin.supplyAvailable == type (uint256 ).max) {
774- twin.tokenId++ ;
775- } else {
776- // Token transfer order is descending
777- tokenId = twin.tokenId + twin.supplyAvailable;
778- }
779- // ERC-721 style transfer
780- (success, result) = twin.tokenAddress.call { gas: gasleft () - reservedGas }(
781- abi.encodeWithSignature (
761+ {
762+ // Calldata to transfer the twin
763+ bytes memory data;
764+
765+ if (tokenType == TokenType.FungibleToken) {
766+ // ERC-20 style transfer
767+ data = abi.encodeCall (IERC20 .transferFrom, (seller.assistant, sender, twin.amount));
768+ } else if (tokenType == TokenType.NonFungibleToken) {
769+ // Token transfer order is ascending to avoid overflow when twin supply is unlimited
770+ if (twin.supplyAvailable == type (uint256 ).max) {
771+ twin.tokenId++ ;
772+ } else {
773+ // Token transfer order is descending
774+ tokenId = twin.tokenId + twin.supplyAvailable;
775+ }
776+
777+ // ERC-721 style transfer
778+ data = abi.encodeWithSignature (
782779 "safeTransferFrom(address,address,uint256,bytes) " ,
783780 seller.assistant,
784781 sender,
785782 tokenId,
786783 ""
787- )
788- );
789- } else if (twin.tokenType == TokenType.MultiToken) {
790- // ERC-1155 style transfer
791- (success, result) = twin.tokenAddress.call { gas: gasleft () - reservedGas }(
792- abi.encodeWithSignature (
784+ );
785+ } else if (twin.tokenType == TokenType.MultiToken) {
786+ // ERC-1155 style transfer
787+ data = abi.encodeWithSignature (
793788 "safeTransferFrom(address,address,uint256,uint256,bytes) " ,
794789 seller.assistant,
795790 sender,
796791 tokenId,
797792 twin.amount,
798793 ""
799- )
800- );
794+ );
795+ }
796+
797+ // Make call only if there is enough gas. If not, skip the call and mark the transfer as failed
798+ if (gasleft () > reservedGas) {
799+ (success, result) = twin.tokenAddress.call { gas: gasleft () - reservedGas }(data);
800+ }
801801 }
802802
803803 // Reduce minimum gas required for succesful execution
@@ -816,7 +816,7 @@ contract ExchangeHandlerFacet is IBosonExchangeHandler, BuyerBase, DisputeBase {
816816 twinReceipt.amount = twin.amount;
817817 twinReceipt.tokenType = twin.tokenType;
818818
819- emit TwinTransferred (twin.id, twin.tokenAddress, exchangeId, tokenId, twin.amount, msgSender () );
819+ emit TwinTransferred (twin.id, twin.tokenAddress, exchangeId, tokenId, twin.amount, sender );
820820 }
821821 }
822822
0 commit comments