Skip to content

Commit

Permalink
refactor(Test): verifiers refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscotobar committed Apr 16, 2024
1 parent e440f3c commit 51d1da2
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 355 deletions.
198 changes: 76 additions & 122 deletions package-lock.json

Large diffs are not rendered by default.

73 changes: 17 additions & 56 deletions test/RelayHub.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';
import { ethers } from 'hardhat';
import { BigNumber, Wallet, constants, providers, utils } from 'ethers';
import { BigNumber, Wallet, constants, providers } from 'ethers';
import {
UtilToken,
SmartWalletFactory,
Expand Down Expand Up @@ -39,6 +39,7 @@ import {
evmMineMany,
deployRelayHub,
deployContract,
addSwapHash,
} from './utils/TestUtils';
import { RelayWorkersAddedEvent } from 'typechain-types/@rsksmart/rif-relay-contracts/contracts/RelayHub';

Expand Down Expand Up @@ -1006,32 +1007,13 @@ describe('RelayHub', function () {
constants.AddressZero,
nextWalletIndex
);

claimedValue = ethers.utils.parseEther('0.5');
const refundAddress = Wallet.createRandom().address;
const timelock = 500;
const preimageHash = utils.soliditySha256(
['bytes32'],
[constants.HashZero]
);
data = swap.interface.encodeFunctionData(
'claim(bytes32,uint256,address,address,uint256)',
[
constants.HashZero,
claimedValue,
smartWalletAddress,
refundAddress,
timelock,
]
);
const hash = await swap.hashValues(
preimageHash,
claimedValue,
smartWalletAddress,
refundAddress,
timelock
);
await swap.addSwap(hash);
data = await addSwapHash({
swap,
amount: claimedValue,
claimAddress: smartWalletAddress,
refundAddress: Wallet.createRandom().address,
});
});

it('should fail if revert from destination contract', async function () {
Expand Down Expand Up @@ -1223,9 +1205,6 @@ describe('RelayHub', function () {
let minimalBoltzFactory: MinimalBoltzSmartWalletFactory;
let claimedValue: BigNumber;
let smartWalletAddress: string;
const timelock = 500;
let refundAddress: string;
let preimageHash: string;

beforeEach(async function () {
const smartWalletTemplate =
Expand All @@ -1248,42 +1227,24 @@ describe('RelayHub', function () {
constants.AddressZero,
nextWalletIndex
);

refundAddress = Wallet.createRandom().address;
claimedValue = ethers.utils.parseEther('0.5');
preimageHash = utils.soliditySha256(
['bytes32'],
[constants.HashZero]
);
data = swap.interface.encodeFunctionData(
'claim(bytes32,uint256,address,address,uint256)',
[
constants.HashZero,
claimedValue,
smartWalletAddress,
refundAddress,
timelock,
]
);
const hash = await swap.hashValues(
preimageHash,
claimedValue,
smartWalletAddress,
refundAddress,
timelock
);
await swap.addSwap(hash);
data = await addSwapHash({
swap,
amount: claimedValue,
claimAddress: smartWalletAddress,
refundAddress: Wallet.createRandom().address,
});
});

it('should fail if revert from destination contract', async function () {
data = swap.interface.encodeFunctionData(
'claim(bytes32,uint256,address,address,uint256)',
[
preimageHash,
constants.HashZero,
ethers.utils.parseEther('2'),
smartWalletAddress,
refundAddress,
timelock,
constants.AddressZero,
500,
]
);

Expand Down
33 changes: 8 additions & 25 deletions test/relayclient/RelayClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import {
addSwapHash,
assertLog,
createSmartWalletFactory,
createSupportedSmartWallet,
Expand Down Expand Up @@ -29,7 +30,7 @@ import {
HttpClient,
HttpWrapper,
} from '@rsksmart/rif-relay-client';
import { constants, utils, Wallet } from 'ethers';
import { constants, Wallet } from 'ethers';
import { ethers } from 'hardhat';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import { loadConfiguration } from '../relayserver/ServerTestUtils';
Expand Down Expand Up @@ -560,31 +561,13 @@ describe('RelayClient', function () {
constants.AddressZero,
nextWalletIndex
);
const refundAddress = Wallet.createRandom().address;
const claimedValue = ethers.utils.parseEther('0.5');
const preimageHash = utils.soliditySha256(
['bytes32'],
[constants.HashZero]
);
const timelock = 500;
data = swap.interface.encodeFunctionData(
'claim(bytes32,uint256,address,address,uint256)',
[
constants.HashZero,
claimedValue,
smartWalletAddress,
refundAddress,
timelock,
]
);
const hash = await swap.hashValues(
preimageHash,
claimedValue,
smartWalletAddress,
refundAddress,
timelock
);
await swap.addSwap(hash);
data = await addSwapHash({
swap,
amount: claimedValue,
claimAddress: smartWalletAddress,
refundAddress: Wallet.createRandom().address,
});
});

it('without tokenGas', async function () {
Expand Down
70 changes: 17 additions & 53 deletions test/relayserver/RelayServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
createRelayUserDefinedRequest,
createDeployUserDefinedRequest,
getSmartWalletTemplate,
addSwapHash,
} from '../utils/TestUtils';
import config from 'config';
import {
Expand All @@ -56,7 +57,7 @@ import {
RelayRequestBody,
setEnvelopingConfig,
} from '@rsksmart/rif-relay-client';
import { BigNumber, constants, utils, Wallet } from 'ethers';
import { BigNumber, constants, Wallet } from 'ethers';
import { spy, match } from 'sinon';
import {
TestDeployVerifierConfigurableMisbehavior,
Expand Down Expand Up @@ -728,39 +729,20 @@ describe('RelayServer', function () {
let index = 1;

beforeEach(async function () {
index++;
swap = await deployContract<TestSwap>('TestSwap');
const smartWalletAddress = await boltzFactory.getSmartWalletAddress(
owner.address,
constants.AddressZero,
index
);
const claimedValue = ethers.utils.parseEther('0.5');
const refundAddress = Wallet.createRandom().address;
const timelock = 500;
const preimageHash = utils.soliditySha256(
['bytes32'],
[constants.HashZero]
);
encodedData = swap.interface.encodeFunctionData(
'claim(bytes32,uint256,address,address,uint256)',
[
constants.HashZero,
claimedValue,
smartWalletAddress,
refundAddress,
timelock,
]
);
const hash = await swap.hashValues(
preimageHash,
claimedValue,
smartWalletAddress,
refundAddress,
timelock
);
await swap.addSwap(hash);
encodedData = await addSwapHash({
swap,
amount: ethers.utils.parseEther('0.5'),
claimAddress: smartWalletAddress,
refundAddress: Wallet.createRandom().address,
});
await boltzVerifier.acceptContract(swap.address);
index++;
});

it('should relay deploy transaction with contract execution', async function () {
Expand Down Expand Up @@ -823,7 +805,7 @@ describe('RelayServer', function () {
relayServer.createRelayTransaction(
stringifyEnvelopingTx(envelopingTxRequest)
)
).to.be.rejectedWith('Claiming value lower than fees');
).to.be.rejectedWith('Native balance too lo');
});

// FIXME - Should bubble up error but its failing with a different error
Expand Down Expand Up @@ -868,30 +850,12 @@ describe('RelayServer', function () {
index
);
const claimedValue = ethers.utils.parseEther('0.5');
const refundAddress = Wallet.createRandom().address;
const timelock = 500;
const preimageHash = utils.soliditySha256(
['bytes32'],
[constants.HashZero]
);
encodedData = swap.interface.encodeFunctionData(
'claim(bytes32,uint256,address,address,uint256)',
[
constants.HashZero,
claimedValue,
smartWalletAddress,
refundAddress,
timelock,
]
);
const hash = await swap.hashValues(
preimageHash,
claimedValue,
smartWalletAddress,
refundAddress,
timelock
);
await swap.addSwap(hash);
encodedData = await addSwapHash({
swap,
amount: claimedValue,
claimAddress: smartWalletAddress,
refundAddress: Wallet.createRandom().address,
});
await minimalBoltzVerifier.acceptContract(swap.address);
index++;
});
Expand Down Expand Up @@ -956,7 +920,7 @@ describe('RelayServer', function () {
relayServer.createRelayTransaction(
stringifyEnvelopingTx(envelopingTxRequest)
)
).to.be.rejectedWith('Claiming value lower than fees');
).to.be.rejectedWith('Native balance too low');
});
});
});
Expand Down
63 changes: 14 additions & 49 deletions test/relayserver/RelayServerNoClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import axios, { AxiosResponse } from 'axios';
import { expect } from 'chai';
import config from 'config';
import { Wallet, constants, utils } from 'ethers';
import { Wallet, constants } from 'ethers';
import { ethers } from 'hardhat';
import {
EnvelopingRequestOptional,
Expand All @@ -31,6 +31,7 @@ import {
} from 'typechain-types';
import {
RSK_URL,
addSwapHash,
assertLog,
createSmartWalletFactory,
deployContract,
Expand Down Expand Up @@ -194,30 +195,12 @@ describe('RelayServerNoClient', function () {
0
);
const claimedValue = ethers.utils.parseEther('0.5');
const refundAddress = Wallet.createRandom().address;
const timelock = 500;
const preimageHash = utils.soliditySha256(
['bytes32'],
[constants.HashZero]
);
data = swap.interface.encodeFunctionData(
'claim(bytes32,uint256,address,address,uint256)',
[
constants.HashZero,
claimedValue,
smartWalletAddress,
refundAddress,
timelock,
]
);
const hash = await swap.hashValues(
preimageHash,
claimedValue,
smartWalletAddress,
refundAddress,
timelock
);
await swap.addSwap(hash);
data = await addSwapHash({
swap,
amount: claimedValue,
claimAddress: smartWalletAddress,
refundAddress: Wallet.createRandom().address,
});

baseRequestFields = {
request: {
Expand Down Expand Up @@ -292,30 +275,12 @@ describe('RelayServerNoClient', function () {
0
);
const claimedValue = ethers.utils.parseEther('0.5');
const refundAddress = Wallet.createRandom().address;
const timelock = 500;
const preimageHash = utils.soliditySha256(
['bytes32'],
[constants.HashZero]
);
data = swap.interface.encodeFunctionData(
'claim(bytes32,uint256,address,address,uint256)',
[
constants.HashZero,
claimedValue,
smartWalletAddress,
refundAddress,
timelock,
]
);
const hash = await swap.hashValues(
preimageHash,
claimedValue,
smartWalletAddress,
refundAddress,
timelock
);
await swap.addSwap(hash);
data = await addSwapHash({
swap,
amount: claimedValue,
claimAddress: smartWalletAddress,
refundAddress: Wallet.createRandom().address,
});

baseRequestFields = {
request: {
Expand Down
Loading

0 comments on commit 51d1da2

Please sign in to comment.