Skip to content

Commit

Permalink
transfer only if enough gas left
Browse files Browse the repository at this point in the history
  • Loading branch information
zajck committed Jun 21, 2023
1 parent 2b49afe commit d85a0eb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
60 changes: 30 additions & 30 deletions contracts/protocol/facets/ExchangeHandlerFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -758,46 +758,46 @@ contract ExchangeHandlerFacet is IBosonExchangeHandler, BuyerBase, DisputeBase {
: twin.supplyAvailable - twin.amount;
}

if (tokenType == TokenType.FungibleToken) {
// ERC-20 style transfer
(success, result) = twin.tokenAddress.call{ gas: gasleft() - reservedGas }(
abi.encodeWithSignature(
"transferFrom(address,address,uint256)",
seller.assistant,
sender,
twin.amount
)
);
} else if (tokenType == TokenType.NonFungibleToken) {
// Token transfer order is ascending to avoid overflow when twin supply is unlimited
if (twin.supplyAvailable == type(uint256).max) {
twin.tokenId++;
} else {
// Token transfer order is descending
tokenId = twin.tokenId + twin.supplyAvailable;
}
// ERC-721 style transfer
(success, result) = twin.tokenAddress.call{ gas: gasleft() - reservedGas }(
abi.encodeWithSignature(
{
// Calldata to transfer the twin
bytes memory data;

if (tokenType == TokenType.FungibleToken) {
// ERC-20 style transfer
data = abi.encodeCall(IERC20.transferFrom, (seller.assistant, sender, twin.amount));
} else if (tokenType == TokenType.NonFungibleToken) {
// Token transfer order is ascending to avoid overflow when twin supply is unlimited
if (twin.supplyAvailable == type(uint256).max) {
twin.tokenId++;
} else {
// Token transfer order is descending
tokenId = twin.tokenId + twin.supplyAvailable;
}

// ERC-721 style transfer
data = abi.encodeWithSignature(
"safeTransferFrom(address,address,uint256,bytes)",
seller.assistant,
sender,
tokenId,
""
)
);
} else if (twin.tokenType == TokenType.MultiToken) {
// ERC-1155 style transfer
(success, result) = twin.tokenAddress.call{ gas: gasleft() - reservedGas }(
abi.encodeWithSignature(
);
} else if (twin.tokenType == TokenType.MultiToken) {
// ERC-1155 style transfer
data = abi.encodeWithSignature(
"safeTransferFrom(address,address,uint256,uint256,bytes)",
seller.assistant,
sender,
tokenId,
twin.amount,
""
)
);
);
}

// Make call only if there is enough gas. If not, skip the call and mark the transfer as failed
if (gasleft() > reservedGas) {
(success, result) = twin.tokenAddress.call{ gas: gasleft() - reservedGas }(data);
}
}

// Reduce minimum gas required for succesful execution
Expand All @@ -816,7 +816,7 @@ contract ExchangeHandlerFacet is IBosonExchangeHandler, BuyerBase, DisputeBase {
twinReceipt.amount = twin.amount;
twinReceipt.tokenType = twin.tokenType;

emit TwinTransferred(twin.id, twin.tokenAddress, exchangeId, tokenId, twin.amount, msgSender());
emit TwinTransferred(twin.id, twin.tokenAddress, exchangeId, tokenId, twin.amount, sender);
}
}

Expand Down
8 changes: 4 additions & 4 deletions test/protocol/ExchangeHandlerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2361,7 +2361,7 @@ describe("IBosonExchangeHandler", function () {
exchange.id = Number(exchange.id) + 1;

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

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

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

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

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

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

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

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

0 comments on commit d85a0eb

Please sign in to comment.