|
| 1 | +/// @notice Safe ETH and ERC20 transfer library that gracefully handles missing return values. |
| 2 | +/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/SafeTransferLib.sol) |
| 3 | +/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/SafeTransferLib.sol) |
| 4 | +/// @author clabby <https://github.com/clabby> |
| 5 | +/// @dev Caution! This library won't check that a token has code, responsibility is delegated to the caller. |
| 6 | + |
| 7 | +#define macro SAFE_TRANSFER_ETH() = takes (2) { |
| 8 | + // Input stack: [to, amount] |
| 9 | + 0x00 dup1 dup1 dup1 // [0x00, 0x00, 0x00, 0x00, to, amount] |
| 10 | + swap5 swap1 swap4 // [to, amount, 0x00, 0x00, 0x00, 0x00] |
| 11 | + gas call // [call_success] |
| 12 | + success jumpi // [] |
| 13 | + |
| 14 | + // Revert with `ETHTransferFailed()` error |
| 15 | + 0xb12d13eb 0x00 mstore |
| 16 | + 0x04 0x1c revert |
| 17 | + |
| 18 | + success: |
| 19 | + // Return stack: [] |
| 20 | +} |
| 21 | + |
| 22 | +#define macro SAFE_TRANSFER_FROM(mem_ptr) = takes (4) { |
| 23 | + // Input stack: [from, to, amount, token] |
| 24 | + __RIGHTPAD(0x23b872dd) // [transferFrom_selector, from, to, amount, token] |
| 25 | + <mem_ptr> mstore // [from, to, amount, token] |
| 26 | + |
| 27 | + // TODO: If we designate a 128 byte scratch space for this macro, |
| 28 | + // no need to do these additions at runtime. |
| 29 | + <mem_ptr> 0x04 add // [mem_ptr + 0x04, from, to, amount, token] |
| 30 | + mstore // [to, amount, token] |
| 31 | + <mem_ptr> 0x24 add // [mem_ptr + 0x24, to, amount, token] |
| 32 | + mstore // [amount, token] |
| 33 | + <mem_ptr> 0x44 add // [mem_ptr + 0x44, amount, token] |
| 34 | + mstore // [token] |
| 35 | + |
| 36 | + <mem_ptr> // [mem_ptr, token] |
| 37 | + 0x64 dup2 0x00 // [0x00, mem_ptr, 0x64, mem_ptr, token] |
| 38 | + 0x20 swap5 // [token, 0x00, mem_ptr, 0x64, mem_ptr, 0x20] |
| 39 | + gas call // [success] |
| 40 | + |
| 41 | + returndatasize // [returndatasize, success] |
| 42 | + iszero // [returndatasize == 0, success] |
| 43 | + <mem_ptr> mload // [data, returndatasize == 0, success] |
| 44 | + 0x01 eq // [data == 0x01, returndatasize == 0, success] |
| 45 | + or // [data == 0x01 | returndatasize == 0, success] |
| 46 | + |
| 47 | + and // [success & (data == 0x01 | returndatasize == 0)] |
| 48 | + success jumpi // [] |
| 49 | + |
| 50 | + 0x7939f424 0x00 mstore |
| 51 | + 0x04 0x1c revert |
| 52 | + |
| 53 | + success: |
| 54 | + // Return stack: [] |
| 55 | +} |
| 56 | + |
| 57 | +#define macro SAFE_TRANSFER(mem_ptr) = takes (3) { |
| 58 | + // Input stack: [to, amount, token] |
| 59 | + |
| 60 | + __RIGHTPAD(0xa9059cbb) // [transfer_selector, to, amount, token] |
| 61 | + <mem_ptr> mstore // [to, amount, token] |
| 62 | + |
| 63 | + // TODO: If we designate a 96 byte scratch space for this macro, |
| 64 | + // no need to do these additions at runtime. |
| 65 | + <mem_ptr> 0x04 add // [mem_ptr + 0x04, to, amount, token] |
| 66 | + mstore // [amount, token] |
| 67 | + <mem_ptr> 0x24 add // [mem_ptr + 0x24, amount, token] |
| 68 | + mstore |
| 69 | + |
| 70 | + <mem_ptr> // [mem_ptr, token] |
| 71 | + 0x44 dup2 0x00 // [0x00, mem_ptr, 0x44, mem_ptr, token] |
| 72 | + 0x20 swap5 // [token, 0x00, mem_ptr, 0x44, mem_ptr, 0x20] |
| 73 | + gas call // [success] |
| 74 | + |
| 75 | + returndatasize // [returndatasize, success] |
| 76 | + iszero // [returndatasize == 0, success] |
| 77 | + <mem_ptr> mload // [data, returndatasize == 0, success] |
| 78 | + 0x01 eq // [data == 0x01, returndatasize == 0, success] |
| 79 | + or // [data == 0x01 | returndatasize == 0, success] |
| 80 | + |
| 81 | + and // [success & (data == 0x01 | returndatasize == 0)] |
| 82 | + success jumpi // [] |
| 83 | + |
| 84 | + 0x90b8ec18 0x00 mstore |
| 85 | + 0x04 0x1c revert |
| 86 | + |
| 87 | + success: |
| 88 | + // Return stack: [] |
| 89 | +} |
| 90 | + |
| 91 | +#define macro SAFE_APPROVE(mem_ptr) = takes (3) { |
| 92 | + // Input stack: [to, amount, token] |
| 93 | + |
| 94 | + __RIGHTPAD(0x095ea7b3) // [transfer_selector, to, amount, token] |
| 95 | + <mem_ptr> mstore // [to, amount, token] |
| 96 | + |
| 97 | + // TODO: If we designate a 96 byte scratch space for this macro, |
| 98 | + // no need to do these additions at runtime. |
| 99 | + <mem_ptr> 0x04 add // [mem_ptr + 0x04, to, amount, token] |
| 100 | + mstore // [amount, token] |
| 101 | + <mem_ptr> 0x24 add // [mem_ptr + 0x24, amount, token] |
| 102 | + mstore |
| 103 | + |
| 104 | + <mem_ptr> // [mem_ptr, token] |
| 105 | + 0x44 dup2 0x00 // [0x00, mem_ptr, 0x44, mem_ptr, token] |
| 106 | + 0x20 swap5 // [token, 0x00, mem_ptr, 0x44, mem_ptr, 0x20] |
| 107 | + gas call // [success] |
| 108 | + |
| 109 | + returndatasize // [returndatasize, success] |
| 110 | + iszero // [returndatasize == 0, success] |
| 111 | + <mem_ptr> mload // [data, returndatasize == 0, success] |
| 112 | + 0x01 eq // [data == 0x01, returndatasize == 0, success] |
| 113 | + or // [data == 0x01 | returndatasize == 0, success] |
| 114 | + |
| 115 | + and // [success & (data == 0x01 | returndatasize == 0)] |
| 116 | + success jumpi // [] |
| 117 | + |
| 118 | + 0x3e3f8f73 0x00 mstore |
| 119 | + 0x04 0x1c revert |
| 120 | + |
| 121 | + success: |
| 122 | + // Return stack: [] |
| 123 | +} |
0 commit comments