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 e6a7d5c
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 396 deletions.
198 changes: 76 additions & 122 deletions package-lock.json

Large diffs are not rendered by default.

70 changes: 29 additions & 41 deletions scripts/GasEstimation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
ContractReceipt,
Wallet,
constants,
utils,
} from 'ethers';
import {
createSmartWalletFactory,
Expand All @@ -28,6 +27,7 @@ import {
SupportedSmartWallet,
SupportedSmartWalletName,
getSmartWalletAddress,
addSwapHash,
} from '../test/utils/TestUtils';
import { TestSwap, TestVerifierEverythingAccepted } from 'typechain-types';
import { Penalizer, RelayHub } from '@rsksmart/rif-relay-contracts';
Expand Down Expand Up @@ -170,23 +170,20 @@ async function executeSwapWithoutRelay(swap: TestSwap) {

const claimedValue = ethers.utils.parseEther('0.5');
const timelock = 500;
const preimageHash = utils.soliditySha256(['bytes32'], [constants.HashZero]);

const hash = await swap.hashValues(
preimageHash,
claimedValue,
senderAccount.address,
constants.AddressZero,
timelock
);

await swap.addSwap(hash);
const refundAddress = Wallet.createRandom().address;
await addSwapHash({
swap,
amount: CLAIMED_AMOUNT,
claimAddress: senderAccount.address,
refundAddress,
external: true,
});

const claimTx = await swap['claim(bytes32,uint256,address,address,uint256)'](
constants.HashZero,
claimedValue,
senderAccount.address,
constants.AddressZero,
refundAddress,
timelock
);

Expand Down Expand Up @@ -385,18 +382,6 @@ async function estimateRelayCost(fees = NO_FEES, payment: Payment = 'erc20') {
},
};

const {
smartWalletTokenBalance: smartWalletInitialBalance,
relayWorkerTokenBalance: workerInitialBalance,
relayWorkerRBTCBalance: workerInitialBalanceRBTC,
ownerRBTCBalance: ownerInitialBalanceRBTC,
} = await getBalances({
owner,
smartWalletAddress: smartWallet.address,
token,
relayWorker,
});

const transferReceiver = ethers.Wallet.createRandom();

const { data, to, gas } = await getDestinationContractCallParams(
Expand All @@ -421,6 +406,18 @@ async function estimateRelayCost(fees = NO_FEES, payment: Payment = 'erc20') {
owner
);

const {
smartWalletTokenBalance: smartWalletInitialBalance,
relayWorkerTokenBalance: workerInitialBalance,
relayWorkerRBTCBalance: workerInitialBalanceRBTC,
ownerRBTCBalance: ownerInitialBalanceRBTC,
} = await getBalances({
owner,
smartWalletAddress: smartWallet.address,
token,
relayWorker,
});

const relayCallResult = await relayHub
.connect(relayWorker)
.relayCall(completeReq, signature, {
Expand Down Expand Up @@ -694,22 +691,13 @@ async function getExecutionParameters(
swap: TestSwap,
swAddress: string
): Promise<DestinationContractCallParams> {
const timelock = 500;
const preimageHash = utils.soliditySha256(['bytes32'], [constants.HashZero]);

const encodedFunction = swap.interface.encodeFunctionData(
'claim(bytes32,uint256,address,uint256)',
[constants.HashZero, CLAIMED_AMOUNT, constants.AddressZero, timelock]
);

const hash = await swap.hashValues(
preimageHash,
CLAIMED_AMOUNT,
swAddress,
constants.AddressZero,
timelock
);
await swap.addSwap(hash);
const encodedFunction = await addSwapHash({
swap,
amount: CLAIMED_AMOUNT,
claimAddress: swAddress,
refundAddress: Wallet.createRandom().address,
external: true,
});

const estimatedDestinationCallGasCorrected =
await getEstimatedGasWithCorrection(
Expand Down
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
Loading

0 comments on commit e6a7d5c

Please sign in to comment.