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

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions scripts/GasEstimation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,18 +385,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 +409,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
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
Loading

0 comments on commit 226da32

Please sign in to comment.