Skip to content

Commit d85a0eb

Browse files
committed
transfer only if enough gas left
1 parent 2b49afe commit d85a0eb

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

contracts/protocol/facets/ExchangeHandlerFacet.sol

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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

test/protocol/ExchangeHandlerTest.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2361,7 +2361,7 @@ describe("IBosonExchangeHandler", function () {
23612361
exchange.id = Number(exchange.id) + 1;
23622362

23632363
// Redeem the voucher
2364-
tx = await exchangeHandler.connect(buyer).redeemVoucher(exchange.id);
2364+
tx = await exchangeHandler.connect(buyer).redeemVoucher(exchange.id, { gasLimit: 1000000 }); // limit gas to speed up test
23652365

23662366
// Voucher should be revoked and both transfers should fail
23672367
await expect(tx).to.emit(exchangeHandler, "VoucherRevoked").withArgs(offerId, exchange.id, buyer.address);
@@ -2664,7 +2664,7 @@ describe("IBosonExchangeHandler", function () {
26642664
exchange.id = Number(exchange.id) + 1;
26652665

26662666
// Redeem the voucher
2667-
tx = await exchangeHandler.connect(buyer).redeemVoucher(exchange.id);
2667+
tx = await exchangeHandler.connect(buyer).redeemVoucher(exchange.id, { gasLimit: 1000000 }); // limit gas to speed up test
26682668

26692669
// Voucher should be revoked and both transfers should fail
26702670
await expect(tx).to.emit(exchangeHandler, "VoucherRevoked").withArgs(offerId, exchange.id, buyer.address);
@@ -2907,7 +2907,7 @@ describe("IBosonExchangeHandler", function () {
29072907
exchange.id = Number(exchange.id) + 1;
29082908

29092909
// Redeem the voucher
2910-
tx = await exchangeHandler.connect(buyer).redeemVoucher(exchange.id);
2910+
tx = await exchangeHandler.connect(buyer).redeemVoucher(exchange.id, { gasLimit: 1000000 }); // limit gas to speed up test
29112911

29122912
// Voucher should be revoked and both transfers should fail
29132913
await expect(tx).to.emit(exchangeHandler, "VoucherRevoked").withArgs(offerId, exchange.id, buyer.address);
@@ -3346,7 +3346,7 @@ describe("IBosonExchangeHandler", function () {
33463346
exchange.id = Number(exchange.id) + 1;
33473347

33483348
// Redeem the voucher
3349-
tx = await exchangeHandler.connect(buyer).redeemVoucher(exchange.id);
3349+
tx = await exchangeHandler.connect(buyer).redeemVoucher(exchange.id, { gasLimit: 1000000 }); // limit gas to speed up test
33503350

33513351
// Voucher should be revoked and both transfers should fail
33523352
await expect(tx).to.emit(exchangeHandler, "VoucherRevoked").withArgs(offerId, exchange.id, buyer.address);

0 commit comments

Comments
 (0)