From a7fa29eeac5655751e468b378b3752d30b3f1ee0 Mon Sep 17 00:00:00 2001 From: Francisco Tobar Date: Mon, 9 Sep 2024 12:23:06 -0600 Subject: [PATCH] refactor: rebase --- contracts/test/TestSwap.sol | 48 +++++++++++++++++--- scripts/GasEstimation.ts | 5 -- test/RelayHub.test.ts | 8 ---- test/relayclient/RelayClient.test.ts | 5 -- test/relayserver/RelayServer.test.ts | 32 +++---------- test/relayserver/RelayServerNoClient.test.ts | 4 -- test/utils/TestUtils.ts | 12 ++--- test/verifier/verifiers.test.ts | 15 +++--- 8 files changed, 58 insertions(+), 71 deletions(-) diff --git a/contracts/test/TestSwap.sol b/contracts/test/TestSwap.sol index a943558f..15afa8d9 100644 --- a/contracts/test/TestSwap.sol +++ b/contracts/test/TestSwap.sol @@ -10,10 +10,48 @@ contract TestSwap is NativeSwap { bytes32 public DOMAIN_SEPARATOR; bytes32 public TYPEHASH_REFUND; + event Lockup( + bytes32 indexed preimageHash, + uint amount, + address claimAddress, + address indexed refundAddress, + uint timelock + ); + + mapping (bytes32 => bool) public override swaps; - function addSwap(bytes32 hash) public { + function lock( + bytes32 preimageHash, + address claimAddress, + address refundAddress, + uint timelock + ) external payable { + _lockEther(preimageHash, msg.value, claimAddress, refundAddress, timelock); + } + + + function _lockEther(bytes32 preimageHash, uint amount, address claimAddress, address refundAddress, uint timelock) private { + // Locking zero WEI in the contract is pointless + require(amount > 0, "EtherSwap: locked amount must not be zero"); + + // Hash the values of the swap + bytes32 hash = hashValues( + preimageHash, + amount, + claimAddress, + refundAddress, + timelock + ); + + // Make sure no swap with this value hash exists yet + require(swaps[hash] == false, "EtherSwap: swap exists already"); + + // Save to the state that funds were locked for this swap swaps[hash] = true; + + // Emit the "Lockup" event + emit Lockup(preimageHash, amount, claimAddress, refundAddress, timelock); } function claim( @@ -43,7 +81,7 @@ contract TestSwap is NativeSwap { timelock ); - checkSwapIsLocked(hash); + _checkSwapIsLocked(hash); delete swaps[hash]; @@ -67,11 +105,7 @@ contract TestSwap is NativeSwap { )); } - function checkSwapIsLocked(bytes32 hash) private view { + function _checkSwapIsLocked(bytes32 hash) private view { require(swaps[hash] == true, "NativeSwap: swap has no RBTC"); } - - // solhint-disable-next-line no-empty-blocks - receive() external payable {} - } \ No newline at end of file diff --git a/scripts/GasEstimation.ts b/scripts/GasEstimation.ts index 9269cc21..9715f5e2 100644 --- a/scripts/GasEstimation.ts +++ b/scripts/GasEstimation.ts @@ -103,11 +103,6 @@ async function deployAndSetup(payment: Payment = 'erc20') { const oneEther = ethers.utils.parseEther('1'); - await fundedAccount.sendTransaction({ - to: swap.address, - value: oneEther, - }); - await fundedAccount.sendTransaction({ to: owner.address, value: oneEther, diff --git a/test/RelayHub.test.ts b/test/RelayHub.test.ts index 68f6a3de..05984166 100644 --- a/test/RelayHub.test.ts +++ b/test/RelayHub.test.ts @@ -997,10 +997,6 @@ describe('RelayHub', function () { 'Boltz' ); swap = await deployContract('TestSwap'); - await fundedAccount.sendTransaction({ - to: swap.address, - value: ethers.utils.parseEther('1'), - }); smartWalletAddress = await boltzFactory.getSmartWalletAddress( owner.address, @@ -1217,10 +1213,6 @@ describe('RelayHub', function () { 'MinimalBoltz' ); swap = await deployContract('TestSwap'); - await fundedAccount.sendTransaction({ - to: swap.address, - value: ethers.utils.parseEther('1'), - }); smartWalletAddress = await minimalBoltzFactory.getSmartWalletAddress( owner.address, diff --git a/test/relayclient/RelayClient.test.ts b/test/relayclient/RelayClient.test.ts index 346d6a12..fdf2510a 100644 --- a/test/relayclient/RelayClient.test.ts +++ b/test/relayclient/RelayClient.test.ts @@ -571,11 +571,6 @@ describe('RelayClient', function () { }); it('without tokenGas', async function () { - await fundedAccount.sendTransaction({ - to: swap.address, - value: ethers.utils.parseEther('1'), - }); - const updatedDeployRequest = { ...envelopingDeployRequest, request: { diff --git a/test/relayserver/RelayServer.test.ts b/test/relayserver/RelayServer.test.ts index 69ebd803..39c0b088 100644 --- a/test/relayserver/RelayServer.test.ts +++ b/test/relayserver/RelayServer.test.ts @@ -733,13 +733,6 @@ describe('RelayServer', function () { }); it('should relay deploy transaction with contract execution', async function () { - const [fundedAccount] = (await ethers.getSigners()) as [ - SignerWithAddress - ]; - await fundedAccount.sendTransaction({ - to: swap.address, - value: ethers.utils.parseEther('1'), - }); const userDefinedRelayRequest = createDeployUserDefinedRequest( { from: owner.address, @@ -790,11 +783,14 @@ describe('RelayServer', function () { await expect( relayServer.createRelayTransaction(httpEnvelopingTxRequest) - ).to.be.rejectedWith('Native balance too lo'); + ).to.be.rejectedWith('Native balance too low'); }); - // FIXME - Should bubble up error but its failing with a different error it('should fail if destination contract throws error', async function () { + encodedData = swap.interface.encodeFunctionData( + 'claim(bytes32,uint256,address,uint256)', + [constants.HashZero, 0, constants.AddressZero, 0] + ); const userDefinedRelayRequest = createDeployUserDefinedRequest( { from: owner.address, @@ -817,7 +813,7 @@ describe('RelayServer', function () { await expect( relayServer.createRelayTransaction(httpEnvelopingTxRequest) - ).to.be.rejectedWith('transaction reverted'); + ).to.be.rejectedWith('NativeSwap: swap has no RBTC'); }); }); @@ -845,13 +841,6 @@ describe('RelayServer', function () { }); it('should relay deploy transaction with contract execution', async function () { - const [fundedAccount] = (await ethers.getSigners()) as [ - SignerWithAddress - ]; - await fundedAccount.sendTransaction({ - to: swap.address, - value: ethers.utils.parseEther('1'), - }); const userDefinedRelayRequest = createDeployUserDefinedRequest( { from: owner.address, @@ -1331,15 +1320,6 @@ describe('RelayServer', function () { }); it('should estimate paying with native', async function () { - const [fundedAccount] = (await ethers.getSigners()) as [ - SignerWithAddress - ]; - - await fundedAccount.sendTransaction({ - to: recipient.address, - value: utils.parseEther('10'), - }); - const userDefinedRelayRequest = createDeployUserDefinedRequest( { from: owner.address, diff --git a/test/relayserver/RelayServerNoClient.test.ts b/test/relayserver/RelayServerNoClient.test.ts index a87d57b0..22271836 100644 --- a/test/relayserver/RelayServerNoClient.test.ts +++ b/test/relayserver/RelayServerNoClient.test.ts @@ -138,10 +138,6 @@ describe('RelayServerNoClient', function () { before(async function () { swap = await deployContract('TestSwap'); - await fundedAccount.sendTransaction({ - to: swap.address, - value: ethers.utils.parseEther('1'), - }); }); async function prepareRequest(options: EnvelopingRequestOptional) { diff --git a/test/utils/TestUtils.ts b/test/utils/TestUtils.ts index cdc840e9..639d41ef 100644 --- a/test/utils/TestUtils.ts +++ b/test/utils/TestUtils.ts @@ -600,14 +600,10 @@ const addSwapHash = async ({ external = false, }: AddSwapParams): Promise => { const preimageHash = utils.soliditySha256(['bytes32'], [preimage]); - const hash = await swap.hashValues( - preimageHash, - amount, - claimAddress, - refundAddress, - timelock - ); - await swap.addSwap(hash); + + await swap.lock(preimageHash, claimAddress, refundAddress, timelock, { + value: amount, + }); if (external) { return swap.interface.encodeFunctionData( diff --git a/test/verifier/verifiers.test.ts b/test/verifier/verifiers.test.ts index f77ee778..3d2fda28 100644 --- a/test/verifier/verifiers.test.ts +++ b/test/verifier/verifiers.test.ts @@ -965,16 +965,10 @@ describe('Verifiers tests', function () { const timelock = 500; beforeEach(async function () { - const [, localRelayHub, funder] = await hardhat.getSigners(); + const [, localRelayHub] = await hardhat.getSigners(); relayHub = localRelayHub as SignerWithAddress; swap = await deployContract('TestSwap'); - - await funder?.sendTransaction({ - to: swap.address, - value: TOKEN_AMOUNT_TO_TRANSFER, - }); - owner = Wallet.createRandom().connect(rskProvider); const smartWalletTemplate = await deployContract( @@ -1165,7 +1159,12 @@ describe('Verifiers tests', function () { }); it('Should fail if the method is not allowed', async function () { - data = swap.interface.encodeFunctionData('addSwap', [constants.HashZero]); + data = swap.interface.encodeFunctionData('lock', [ + constants.HashZero, + constants.AddressZero, + constants.AddressZero, + constants.One, + ]); const deployRequest = createDeployEnvelopingRequest( {