Skip to content

Commit

Permalink
refactor: rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscotobar committed Sep 9, 2024
1 parent 5806237 commit a7fa29e
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 71 deletions.
48 changes: 41 additions & 7 deletions contracts/test/TestSwap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -43,7 +81,7 @@ contract TestSwap is NativeSwap {
timelock
);

checkSwapIsLocked(hash);
_checkSwapIsLocked(hash);

delete swaps[hash];

Expand All @@ -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 {}

}
5 changes: 0 additions & 5 deletions scripts/GasEstimation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 0 additions & 8 deletions test/RelayHub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -997,10 +997,6 @@ describe('RelayHub', function () {
'Boltz'
);
swap = await deployContract<TestSwap>('TestSwap');
await fundedAccount.sendTransaction({
to: swap.address,
value: ethers.utils.parseEther('1'),
});

smartWalletAddress = await boltzFactory.getSmartWalletAddress(
owner.address,
Expand Down Expand Up @@ -1217,10 +1213,6 @@ describe('RelayHub', function () {
'MinimalBoltz'
);
swap = await deployContract<TestSwap>('TestSwap');
await fundedAccount.sendTransaction({
to: swap.address,
value: ethers.utils.parseEther('1'),
});

smartWalletAddress = await minimalBoltzFactory.getSmartWalletAddress(
owner.address,
Expand Down
5 changes: 0 additions & 5 deletions test/relayclient/RelayClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
32 changes: 6 additions & 26 deletions test/relayserver/RelayServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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');
});
});

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 0 additions & 4 deletions test/relayserver/RelayServerNoClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,6 @@ describe('RelayServerNoClient', function () {

before(async function () {
swap = await deployContract<TestSwap>('TestSwap');
await fundedAccount.sendTransaction({
to: swap.address,
value: ethers.utils.parseEther('1'),
});
});

async function prepareRequest(options: EnvelopingRequestOptional) {
Expand Down
12 changes: 4 additions & 8 deletions test/utils/TestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,14 +600,10 @@ const addSwapHash = async ({
external = false,
}: AddSwapParams): Promise<string> => {
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(
Expand Down
15 changes: 7 additions & 8 deletions test/verifier/verifiers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MinimalBoltzSmartWallet>(
Expand Down Expand Up @@ -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(
{
Expand Down

0 comments on commit a7fa29e

Please sign in to comment.