From fab0ba62dcb2b3c99f682bafc535aa3a58862ba8 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 25 Feb 2024 13:06:11 +0100 Subject: [PATCH] feat: do a better revert check for failures --- evm/script/config/config_contracts.json | 10 +++++----- evm/src/CatalystVaultCommon.sol | 8 +++++++- evm/src/interfaces/ICatalystV1VaultErrors.sol | 3 ++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/evm/script/config/config_contracts.json b/evm/script/config/config_contracts.json index 2fc17580..2de6679c 100644 --- a/evm/script/config/config_contracts.json +++ b/evm/script/config/config_contracts.json @@ -1,10 +1,10 @@ { "contracts": { - "amplified_mathlib": "0xC2523cF07bF0bb130a132e8F748437aD75D9F4B6", - "amplified_template": "0x668BBC28675849Ef0C922109051ECd6790EeB75b", - "factory": "0x79B5768E5B9c2DCa173FdE9029A20fC90fb3e321", - "volatile_mathlib": "0x131C296a98197C4Da4eD376cCd4B9bAe5851C66E", - "volatile_template": "0xCc8a20A14616fF9d891Ce457C60Ede6cbA8aa4Dd" + "amplified_mathlib": "0x35D899B80D95516b6184CE42C2378Eb1b0a466f0", + "amplified_template": "0xef7f6BB9028dA01Aa96f16Fa731Ad808885aa0E8", + "factory": "0x5DFd12098a98cCae1fDcF55994a42a6ae1d3C981", + "volatile_mathlib": "0x81AC53c7583995d92E9006e5b4EEFc1D1bF20f7d", + "volatile_template": "0xB13B927C4889aD206c282b1666685Bc1a4d3737a" }, "registry": { "describer": "0xA9C609b79d62b9E27CDefA685CB795982f852d3c", diff --git a/evm/src/CatalystVaultCommon.sol b/evm/src/CatalystVaultCommon.sol index 97d4d81e..71dc4cb0 100644 --- a/evm/src/CatalystVaultCommon.sol +++ b/evm/src/CatalystVaultCommon.sol @@ -527,15 +527,21 @@ abstract contract CatalystVaultCommon is // This call provides re-entry protection against re-entering this call. Otherwise, this call can always be called. address fallbackAddress = _releaseAssetEscrow(sendAssetHash, escrowAmount, escrowToken); // Only reverts for missing escrow, + // We are going to make a low-level call. It may revert (see comment below) but it should not revert if it runs out of gas (that should be raised). As such, get the current gas in the contract. + uint256 gasLeftBeforeCall = gasleft(); + // Make a low level call such that the transfer never fails. This is important for tokens // that use block lists. // This also implies that if you get blacklisted between when you initiated the swap and the swap failed, you // would lose the tokens. bytes memory payload = abi.encodeWithSignature("transfer(address,uint256)", fallbackAddress, escrowAmount); assembly ("memory-safe") { - let success := call(gas(), escrowToken, 0, add(payload, 0x20), mload(payload), 0, 0) + // We send gas as the largest bit, such that we don't have to estimate anything. This is equiv to sending all. + let success := call(0x8000000000000000000000000000000000000000000000000000000000000000, escrowToken, 0, add(payload, 0x20), mload(payload), 0, 0) // ERC20(escrowToken).safeTransfer(fallbackAddress, escrowAmount); } + // Check that the call didn't use all of its gas. + if(gasleft() < gasLeftBeforeCall * 1 / 63) revert NotEnoughGas(); emit SendAssetFailure( // Never reverts. channelId, diff --git a/evm/src/interfaces/ICatalystV1VaultErrors.sol b/evm/src/interfaces/ICatalystV1VaultErrors.sol index d56308bc..745405f6 100644 --- a/evm/src/interfaces/ICatalystV1VaultErrors.sol +++ b/evm/src/interfaces/ICatalystV1VaultErrors.sol @@ -6,4 +6,5 @@ error ReturnInsufficient(uint256 result, uint256 minimum); // 24557f0 error VaultNotConnected(); // 2c64c1b error WithdrawRatioNotZero(); // b8003bf error UnusedUnitsAfterWithdrawal(uint256 Units); // 0289311 -error EscrowAlreadyExists(); // ed77877 \ No newline at end of file +error EscrowAlreadyExists(); // ed77877 +error NotEnoughGas(); // dd629f86 \ No newline at end of file