|
| 1 | +const { ethers } = require('hardhat'); |
| 2 | +const { expect } = require('chai'); |
| 3 | +const { loadFixture, setBalance } = require('@nomicfoundation/hardhat-network-helpers'); |
| 4 | +const { |
| 5 | + EXEC_TYPE_DEFAULT, |
| 6 | + EXEC_TYPE_TRY, |
| 7 | + encodeSingle, |
| 8 | + encodeBatch, |
| 9 | + encodeDelegate, |
| 10 | + CALL_TYPE_CALL, |
| 11 | + CALL_TYPE_BATCH, |
| 12 | + encodeMode, |
| 13 | +} = require('../../helpers/erc7579'); |
| 14 | +const { selector } = require('../../helpers/methods'); |
| 15 | + |
| 16 | +const coder = ethers.AbiCoder.defaultAbiCoder(); |
| 17 | + |
| 18 | +const fixture = async () => { |
| 19 | + const [sender] = await ethers.getSigners(); |
| 20 | + const utils = await ethers.deployContract('$ERC7579Utils'); |
| 21 | + const utilsGlobal = await ethers.deployContract('$ERC7579UtilsGlobalMock'); |
| 22 | + const target = await ethers.deployContract('CallReceiverMock'); |
| 23 | + const anotherTarget = await ethers.deployContract('CallReceiverMock'); |
| 24 | + await setBalance(utils.target, ethers.parseEther('1')); |
| 25 | + return { utils, utilsGlobal, target, anotherTarget, sender }; |
| 26 | +}; |
| 27 | + |
| 28 | +describe('ERC7579Utils', function () { |
| 29 | + beforeEach(async function () { |
| 30 | + Object.assign(this, await loadFixture(fixture)); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('execSingle', function () { |
| 34 | + it('calls the target with value', async function () { |
| 35 | + const value = 0x012; |
| 36 | + const data = encodeSingle(this.target, value, this.target.interface.encodeFunctionData('mockFunction')); |
| 37 | + |
| 38 | + await expect(this.utils.$execSingle(EXEC_TYPE_DEFAULT, data)).to.emit(this.target, 'MockFunctionCalled'); |
| 39 | + |
| 40 | + expect(ethers.provider.getBalance(this.target)).to.eventually.equal(value); |
| 41 | + }); |
| 42 | + |
| 43 | + it('calls the target with value and args', async function () { |
| 44 | + const value = 0x432; |
| 45 | + const data = encodeSingle( |
| 46 | + this.target, |
| 47 | + value, |
| 48 | + this.target.interface.encodeFunctionData('mockFunctionWithArgs', [42, '0x1234']), |
| 49 | + ); |
| 50 | + |
| 51 | + await expect(this.utils.$execSingle(EXEC_TYPE_DEFAULT, data)) |
| 52 | + .to.emit(this.target, 'MockFunctionCalledWithArgs') |
| 53 | + .withArgs(42, '0x1234'); |
| 54 | + |
| 55 | + expect(ethers.provider.getBalance(this.target)).to.eventually.equal(value); |
| 56 | + }); |
| 57 | + |
| 58 | + it('reverts when target reverts in default ExecType', async function () { |
| 59 | + const value = 0x012; |
| 60 | + const data = encodeSingle( |
| 61 | + this.target, |
| 62 | + value, |
| 63 | + this.target.interface.encodeFunctionData('mockFunctionRevertsReason'), |
| 64 | + ); |
| 65 | + |
| 66 | + await expect(this.utils.$execSingle(EXEC_TYPE_DEFAULT, data)).to.be.revertedWith('CallReceiverMock: reverting'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('emits ERC7579TryExecuteFail event when target reverts in try ExecType', async function () { |
| 70 | + const value = 0x012; |
| 71 | + const data = encodeSingle( |
| 72 | + this.target, |
| 73 | + value, |
| 74 | + this.target.interface.encodeFunctionData('mockFunctionRevertsReason'), |
| 75 | + ); |
| 76 | + |
| 77 | + await expect(this.utils.$execSingle(EXEC_TYPE_TRY, data)) |
| 78 | + .to.emit(this.utils, 'ERC7579TryExecuteFail') |
| 79 | + .withArgs( |
| 80 | + CALL_TYPE_CALL, |
| 81 | + ethers.solidityPacked( |
| 82 | + ['bytes4', 'bytes'], |
| 83 | + [selector('Error(string)'), coder.encode(['string'], ['CallReceiverMock: reverting'])], |
| 84 | + ), |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + it('reverts with an invalid exec type', async function () { |
| 89 | + const value = 0x012; |
| 90 | + const data = encodeSingle(this.target, value, this.target.interface.encodeFunctionData('mockFunction')); |
| 91 | + |
| 92 | + await expect(this.utils.$execSingle('0x03', data)) |
| 93 | + .to.be.revertedWithCustomError(this.utils, 'ERC7579UnsupportedExecType') |
| 94 | + .withArgs('0x03'); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('execBatch', function () { |
| 99 | + it('calls the targets with value', async function () { |
| 100 | + const value1 = 0x012; |
| 101 | + const value2 = 0x234; |
| 102 | + const data = encodeBatch( |
| 103 | + [this.target, value1, this.target.interface.encodeFunctionData('mockFunction')], |
| 104 | + [this.anotherTarget, value2, this.anotherTarget.interface.encodeFunctionData('mockFunction')], |
| 105 | + ); |
| 106 | + |
| 107 | + await expect(this.utils.$execBatch(EXEC_TYPE_DEFAULT, data)) |
| 108 | + .to.emit(this.target, 'MockFunctionCalled') |
| 109 | + .to.emit(this.anotherTarget, 'MockFunctionCalled'); |
| 110 | + |
| 111 | + expect(ethers.provider.getBalance(this.target)).to.eventually.equal(value1); |
| 112 | + expect(ethers.provider.getBalance(this.anotherTarget)).to.eventually.equal(value2); |
| 113 | + }); |
| 114 | + |
| 115 | + it('calls the targets with value and args', async function () { |
| 116 | + const value1 = 0x012; |
| 117 | + const value2 = 0x234; |
| 118 | + const data = encodeBatch( |
| 119 | + [this.target, value1, this.target.interface.encodeFunctionData('mockFunctionWithArgs', [42, '0x1234'])], |
| 120 | + [ |
| 121 | + this.anotherTarget, |
| 122 | + value2, |
| 123 | + this.anotherTarget.interface.encodeFunctionData('mockFunctionWithArgs', [42, '0x1234']), |
| 124 | + ], |
| 125 | + ); |
| 126 | + |
| 127 | + await expect(this.utils.$execBatch(EXEC_TYPE_DEFAULT, data)) |
| 128 | + .to.emit(this.target, 'MockFunctionCalledWithArgs') |
| 129 | + .to.emit(this.anotherTarget, 'MockFunctionCalledWithArgs'); |
| 130 | + |
| 131 | + expect(ethers.provider.getBalance(this.target)).to.eventually.equal(value1); |
| 132 | + expect(ethers.provider.getBalance(this.anotherTarget)).to.eventually.equal(value2); |
| 133 | + }); |
| 134 | + |
| 135 | + it('reverts when any target reverts in default ExecType', async function () { |
| 136 | + const value1 = 0x012; |
| 137 | + const value2 = 0x234; |
| 138 | + const data = encodeBatch( |
| 139 | + [this.target, value1, this.target.interface.encodeFunctionData('mockFunction')], |
| 140 | + [this.anotherTarget, value2, this.anotherTarget.interface.encodeFunctionData('mockFunctionRevertsReason')], |
| 141 | + ); |
| 142 | + |
| 143 | + await expect(this.utils.$execBatch(EXEC_TYPE_DEFAULT, data)).to.be.revertedWith('CallReceiverMock: reverting'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('emits ERC7579TryExecuteFail event when any target reverts in try ExecType', async function () { |
| 147 | + const value1 = 0x012; |
| 148 | + const value2 = 0x234; |
| 149 | + const data = encodeBatch( |
| 150 | + [this.target, value1, this.target.interface.encodeFunctionData('mockFunction')], |
| 151 | + [this.anotherTarget, value2, this.anotherTarget.interface.encodeFunctionData('mockFunctionRevertsReason')], |
| 152 | + ); |
| 153 | + |
| 154 | + await expect(this.utils.$execBatch(EXEC_TYPE_TRY, data)) |
| 155 | + .to.emit(this.utils, 'ERC7579TryExecuteFail') |
| 156 | + .withArgs( |
| 157 | + CALL_TYPE_BATCH, |
| 158 | + ethers.solidityPacked( |
| 159 | + ['bytes4', 'bytes'], |
| 160 | + [selector('Error(string)'), coder.encode(['string'], ['CallReceiverMock: reverting'])], |
| 161 | + ), |
| 162 | + ); |
| 163 | + |
| 164 | + // Check balances |
| 165 | + expect(ethers.provider.getBalance(this.target)).to.eventually.equal(value1); |
| 166 | + expect(ethers.provider.getBalance(this.anotherTarget)).to.eventually.equal(0); |
| 167 | + }); |
| 168 | + |
| 169 | + it('reverts with an invalid exec type', async function () { |
| 170 | + const value1 = 0x012; |
| 171 | + const value2 = 0x234; |
| 172 | + const data = encodeBatch( |
| 173 | + [this.target, value1, this.target.interface.encodeFunctionData('mockFunction')], |
| 174 | + [this.anotherTarget, value2, this.anotherTarget.interface.encodeFunctionData('mockFunction')], |
| 175 | + ); |
| 176 | + |
| 177 | + await expect(this.utils.$execBatch('0x03', data)) |
| 178 | + .to.be.revertedWithCustomError(this.utils, 'ERC7579UnsupportedExecType') |
| 179 | + .withArgs('0x03'); |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
| 183 | + describe('execDelegateCall', function () { |
| 184 | + it('delegate calls the target', async function () { |
| 185 | + const slot = ethers.hexlify(ethers.randomBytes(32)); |
| 186 | + const value = ethers.hexlify(ethers.randomBytes(32)); |
| 187 | + const data = encodeDelegate( |
| 188 | + this.target, |
| 189 | + this.target.interface.encodeFunctionData('mockFunctionWritesStorage', [slot, value]), |
| 190 | + ); |
| 191 | + |
| 192 | + expect(ethers.provider.getStorage(this.utils.target, slot)).to.eventually.equal(ethers.ZeroHash); |
| 193 | + await this.utils.$execDelegateCall(EXEC_TYPE_DEFAULT, data); |
| 194 | + expect(ethers.provider.getStorage(this.utils.target, slot)).to.eventually.equal(value); |
| 195 | + }); |
| 196 | + |
| 197 | + it('reverts when target reverts in default ExecType', async function () { |
| 198 | + const data = encodeDelegate(this.target, this.target.interface.encodeFunctionData('mockFunctionRevertsReason')); |
| 199 | + await expect(this.utils.$execDelegateCall(EXEC_TYPE_DEFAULT, data)).to.be.revertedWith( |
| 200 | + 'CallReceiverMock: reverting', |
| 201 | + ); |
| 202 | + }); |
| 203 | + |
| 204 | + it('emits ERC7579TryExecuteFail event when target reverts in try ExecType', async function () { |
| 205 | + const data = encodeDelegate(this.target, this.target.interface.encodeFunctionData('mockFunctionRevertsReason')); |
| 206 | + await expect(this.utils.$execDelegateCall(EXEC_TYPE_TRY, data)) |
| 207 | + .to.emit(this.utils, 'ERC7579TryExecuteFail') |
| 208 | + .withArgs( |
| 209 | + CALL_TYPE_CALL, |
| 210 | + ethers.solidityPacked( |
| 211 | + ['bytes4', 'bytes'], |
| 212 | + [selector('Error(string)'), coder.encode(['string'], ['CallReceiverMock: reverting'])], |
| 213 | + ), |
| 214 | + ); |
| 215 | + }); |
| 216 | + |
| 217 | + it('reverts with an invalid exec type', async function () { |
| 218 | + const data = encodeDelegate(this.target, this.target.interface.encodeFunctionData('mockFunction')); |
| 219 | + await expect(this.utils.$execDelegateCall('0x03', data)) |
| 220 | + .to.be.revertedWithCustomError(this.utils, 'ERC7579UnsupportedExecType') |
| 221 | + .withArgs('0x03'); |
| 222 | + }); |
| 223 | + }); |
| 224 | + |
| 225 | + it('encodes Mode', async function () { |
| 226 | + const callType = CALL_TYPE_BATCH; |
| 227 | + const execType = EXEC_TYPE_TRY; |
| 228 | + const selector = '0x12345678'; |
| 229 | + const payload = ethers.toBeHex(0, 22); |
| 230 | + |
| 231 | + expect(this.utils.$encodeMode(callType, execType, selector, payload)).to.eventually.equal( |
| 232 | + encodeMode({ |
| 233 | + callType, |
| 234 | + execType, |
| 235 | + selector, |
| 236 | + payload, |
| 237 | + }), |
| 238 | + ); |
| 239 | + }); |
| 240 | + |
| 241 | + it('decodes Mode', async function () { |
| 242 | + const callType = CALL_TYPE_BATCH; |
| 243 | + const execType = EXEC_TYPE_TRY; |
| 244 | + const selector = '0x12345678'; |
| 245 | + const payload = ethers.toBeHex(0, 22); |
| 246 | + |
| 247 | + expect( |
| 248 | + this.utils.$decodeMode( |
| 249 | + encodeMode({ |
| 250 | + callType, |
| 251 | + execType, |
| 252 | + selector, |
| 253 | + payload, |
| 254 | + }), |
| 255 | + ), |
| 256 | + ).to.eventually.deep.equal([callType, execType, selector, payload]); |
| 257 | + }); |
| 258 | + |
| 259 | + it('encodes single', async function () { |
| 260 | + const target = this.target; |
| 261 | + const value = 0x123; |
| 262 | + const data = '0x12345678'; |
| 263 | + |
| 264 | + expect(this.utils.$encodeSingle(target, value, data)).to.eventually.equal(encodeSingle(target, value, data)); |
| 265 | + }); |
| 266 | + |
| 267 | + it('decodes single', async function () { |
| 268 | + const target = this.target; |
| 269 | + const value = 0x123; |
| 270 | + const data = '0x12345678'; |
| 271 | + |
| 272 | + expect(this.utils.$decodeSingle(encodeSingle(target, value, data))).to.eventually.deep.equal([ |
| 273 | + target.target, |
| 274 | + value, |
| 275 | + data, |
| 276 | + ]); |
| 277 | + }); |
| 278 | + |
| 279 | + it('encodes batch', async function () { |
| 280 | + const entries = [ |
| 281 | + [this.target, 0x123, '0x12345678'], |
| 282 | + [this.anotherTarget, 0x456, '0x12345678'], |
| 283 | + ]; |
| 284 | + |
| 285 | + expect(this.utils.$encodeBatch(entries)).to.eventually.equal(encodeBatch(...entries)); |
| 286 | + }); |
| 287 | + |
| 288 | + it('decodes batch', async function () { |
| 289 | + const entries = [ |
| 290 | + [this.target.target, 0x123, '0x12345678'], |
| 291 | + [this.anotherTarget.target, 0x456, '0x12345678'], |
| 292 | + ]; |
| 293 | + |
| 294 | + expect(this.utils.$decodeBatch(encodeBatch(...entries))).to.eventually.deep.equal(entries); |
| 295 | + }); |
| 296 | + |
| 297 | + it('encodes delegate', async function () { |
| 298 | + const target = this.target; |
| 299 | + const data = '0x12345678'; |
| 300 | + |
| 301 | + expect(this.utils.$encodeDelegate(target, data)).to.eventually.equal(encodeDelegate(target, data)); |
| 302 | + }); |
| 303 | + |
| 304 | + it('decodes delegate', async function () { |
| 305 | + const target = this.target; |
| 306 | + const data = '0x12345678'; |
| 307 | + |
| 308 | + expect(this.utils.$decodeDelegate(encodeDelegate(target, data))).to.eventually.deep.equal([target.target, data]); |
| 309 | + }); |
| 310 | + |
| 311 | + describe('global', function () { |
| 312 | + describe('eqCallTypeGlobal', function () { |
| 313 | + it('returns true if both call types are equal', async function () { |
| 314 | + expect(this.utilsGlobal.$eqCallTypeGlobal(CALL_TYPE_BATCH, CALL_TYPE_BATCH)).to.eventually.be.true; |
| 315 | + }); |
| 316 | + |
| 317 | + it('returns false if both call types are different', async function () { |
| 318 | + expect(this.utilsGlobal.$eqCallTypeGlobal(CALL_TYPE_CALL, CALL_TYPE_BATCH)).to.eventually.be.false; |
| 319 | + }); |
| 320 | + }); |
| 321 | + |
| 322 | + describe('eqExecTypeGlobal', function () { |
| 323 | + it('returns true if both exec types are equal', async function () { |
| 324 | + expect(this.utilsGlobal.$eqExecTypeGlobal(EXEC_TYPE_TRY, EXEC_TYPE_TRY)).to.eventually.be.true; |
| 325 | + }); |
| 326 | + |
| 327 | + it('returns false if both exec types are different', async function () { |
| 328 | + expect(this.utilsGlobal.$eqExecTypeGlobal(EXEC_TYPE_DEFAULT, EXEC_TYPE_TRY)).to.eventually.be.false; |
| 329 | + }); |
| 330 | + }); |
| 331 | + |
| 332 | + describe('eqModeSelectorGlobal', function () { |
| 333 | + it('returns true if both selectors are equal', async function () { |
| 334 | + expect(this.utilsGlobal.$eqModeSelectorGlobal('0x12345678', '0x12345678')).to.eventually.be.true; |
| 335 | + }); |
| 336 | + |
| 337 | + it('returns false if both selectors are different', async function () { |
| 338 | + expect(this.utilsGlobal.$eqModeSelectorGlobal('0x12345678', '0x87654321')).to.eventually.be.false; |
| 339 | + }); |
| 340 | + }); |
| 341 | + |
| 342 | + describe('eqModePayloadGlobal', function () { |
| 343 | + it('returns true if both payloads are equal', async function () { |
| 344 | + expect(this.utilsGlobal.$eqModePayloadGlobal(ethers.toBeHex(0, 22), ethers.toBeHex(0, 22))).to.eventually.be |
| 345 | + .true; |
| 346 | + }); |
| 347 | + |
| 348 | + it('returns false if both payloads are different', async function () { |
| 349 | + expect(this.utilsGlobal.$eqModePayloadGlobal(ethers.toBeHex(0, 22), ethers.toBeHex(1, 22))).to.eventually.be |
| 350 | + .false; |
| 351 | + }); |
| 352 | + }); |
| 353 | + }); |
| 354 | +}); |
0 commit comments