Skip to content

Commit

Permalink
translate tests code from truffle to hh but most still dont work as i…
Browse files Browse the repository at this point in the history
…t probably needs complete rewrite of structure
  • Loading branch information
0xCardinalError committed Mar 21, 2024
1 parent 1642843 commit 290a1e0
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 194 deletions.
19 changes: 16 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,23 @@
"lint": "eslint \"**/*.{js,ts}\"",
"lint:fix": "eslint --fix \"**/*.{js,ts}\"",
"format": "prettier --write \"**/*.{ts,js,sol}\"",
"hardhat": "hardhat",
"coverage": "hardhat coverage",
"format:check": "prettier --check \"**/*.{ts,js,sol}\"",
"test": "cross-env HARDHAT_DEPLOY_FIXTURE=true HARDHAT_COMPILE=true mocha --bail --recursive test --timeout 300000",
"test:coverage": "hardhat coverage",
"dev": "hardhat node --reset --watch --export contractsInfo.json",
"compile": "hardhat compile",
"test": "hardhat test"
"local:deploy": "hardhat --network localhost deploy",
"local:run": "cross-env HARDHAT_NETWORK=localhost ts-node --files",
"local:export": "hardhat --network localhost export",
"void:deploy": "hardhat deploy",
"staging:deploy": "hardhat --network staging deploy",
"staging:export": "hardhat --network staging export",
"staging:seed": "cross-env HARDHAT_NETWORK=staging ts-node --files scripts/seed.ts",
"hardhat": "hardhat",
"deploy:hardhat": "hardhat run scripts/deployer.ts --network hardhat",
"deploy:testnet": "hardhat run scripts/deployer.ts --network testnet",
"deploy:mainnet": "hardhat run scripts/deployer.ts --network mainnet",
"deploy": "hardhat run scripts/deployer.ts --network"
},
"keywords": [],
"author": "",
Expand Down
113 changes: 59 additions & 54 deletions test/ERC20SimpleSwap.should.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
import { BN, time } from '@openzeppelin/test-helpers';
import { BN, balance, time, expectEvent, expectRevert } from '@openzeppelin/test-helpers';
import { expect } from 'chai';
import { ethers, getNamedAccounts, getUnnamedAccounts } from 'hardhat';
import { ethers, deployments, getNamedAccounts, getUnnamedAccounts } from 'hardhat';
import { BigNumber, Contract, ContractTransaction } from 'ethers';

import { signCheque, signCashOut, signCustomDecreaseTimeout } from './swutils';

// Assuming ERC20SimpleSwap, SimpleSwapFactory, and TestToken are in the same directory
import ERC20SimpleSwapArtifact from '../artifacts/contracts/ERC20SimpleSwap.sol/ERC20SimpleSwap.json';
import SimpleSwapFactoryArtifact from '../artifacts/contracts/SimpleSwapFactory.sol/SimpleSwapFactory.json';
import TestTokenArtifact from '../artifacts/contracts/TestToken.sol/TestToken.json';

function shouldDeploy(issuer, defaultHardDepositTimeout, from, value) {
const salt = '0x000000000000000000000000000000000000000000000000000000000000abcd';

beforeEach(async function () {
this.TestToken = await TestToken.new({ from: issuer });
await this.TestToken.mint(issuer, 1000000000, { from: issuer });
this.simpleSwapFactory = await SimpleSwapFactory.new(this.TestToken.address);
let { logs } = await this.simpleSwapFactory.deploySimpleSwap(issuer, defaultHardDepositTimeout, salt);
this.ERC20SimpleSwapAddress = logs[0].args.contractAddress;
this.ERC20SimpleSwap = await ERC20SimpleSwap.at(this.ERC20SimpleSwapAddress);
const TestToken = await ethers.getContract('TestToken');
await TestToken.mint(issuer, 1000000000, { from: issuer });

const SimpleSwapFactory = await ethers.getContract('SimpleSwapFactory');
const deployTx = await SimpleSwapFactory.deploySimpleSwap(issuer, defaultHardDepositTimeout, salt);

// Wait for the transaction to be mined to access the events and their arguments
const receipt = await deployTx.wait();
const ERC20SimpleSwapAddress = receipt.events?.filter((x) => x.event === 'SimpleSwapDeployed')[0].args
.contractAddress;

// Assuming 'YourEventName' is the event name that logs the contract address, replace it accordingly

const ERC20SimpleSwap = await ethers.getContractAt('ERC20SimpleSwap', ERC20SimpleSwapAddress);

// If value is not equal to 0, transfer tokens from issuer to the ERC20SimpleSwap contract
if (value != 0) {
await this.TestToken.transfer(this.ERC20SimpleSwap.address, value, { from: issuer });
await this.TestToken.connect(issuer).transfer(ERC20SimpleSwap.address, value);
}
this.postconditions = {
issuer: await this.ERC20SimpleSwap.issuer(),
defaultHardDepositTimeout: await this.ERC20SimpleSwap.defaultHardDepositTimeout(),

// Assuming 'issuer' is a Signer or you have access to the Signer to perform the transfer

// Read postconditions from the deployed ERC20SimpleSwap contract
const postconditions = {
issuer: await ERC20SimpleSwap.issuer(),
defaultHardDepositTimeout: await ERC20SimpleSwap.defaultHardDepositTimeout(),
};
});

Expand All @@ -37,12 +46,12 @@ function shouldDeploy(issuer, defaultHardDepositTimeout, from, value) {
expect(this.postconditions.issuer).to.be.equal(issuer);
});
it('should set the defaultHardDepositTimeout', function () {
expect(this.postconditions.defaultHardDepositTimeout).bignumber.to.be.equal(defaultHardDepositTimeout);
expect(this.postconditions.defaultHardDepositTimeout).to.equal(defaultHardDepositTimeout);
});
}
function shouldReturnDefaultHardDepositTimeout(expected) {
it('should return the expected defaultHardDepositTimeout', async function () {
expect(await this.ERC20SimpleSwap.defaultHardDepositTimeout()).bignumber.to.be.equal(expected);
expect(await this.ERC20SimpleSwap.defaultHardDepositTimeout()).to.equal(expected);
});
}

Expand All @@ -51,7 +60,7 @@ function shouldReturnPaidOut(beneficiary, expectedAmount) {
this.paidOut = await this.ERC20SimpleSwap.paidOut(beneficiary);
});
it('should return the expected amount', function () {
expect(expectedAmount).bignumber.to.be.equal(this.paidOut);
expect(expectedAmount).to.equal(this.paidOut);
});
}

Expand All @@ -60,7 +69,7 @@ function shouldReturnTotalPaidOut(expectedAmount) {
this.totalPaidOut = await this.ERC20SimpleSwap.totalPaidOut();
});
it('should return the expected amount', function () {
expect(expectedAmount).bignumber.to.be.equal(this.totalPaidOut);
expect(expectedAmount).to.equal(this.totalPaidOut);
});
}

Expand All @@ -82,13 +91,13 @@ function shouldReturnHardDeposits(
this.hardDeposits = await this.ERC20SimpleSwap.hardDeposits(beneficiary);
});
it('should return the expected amount', function () {
expect(expectedAmount).bignumber.to.be.equal(this.hardDeposits.amount);
expect(expectedAmount).to.equal(this.hardDeposits.amount);
});
it('should return the expected decreaseAmount', function () {
expect(expectedDecreaseAmount).bignumber.to.be.equal(this.hardDeposits.decreaseAmount);
expect(expectedDecreaseAmount).to.equal(this.hardDeposits.decreaseAmount);
});
it('should return the expected timeout', function () {
expect(expectedDecreaseTimeout).bignumber.to.be.equal(this.hardDeposits.timeout);
expect(expectedDecreaseTimeout).to.equal(this.hardDeposits.timeout);
});
it('should return the exptected canBeDecreasedAt', function () {
expect(this.expectedCanBeDecreasedAt.toNumber()).to.be.closeTo(this.hardDeposits.canBeDecreasedAt.toNumber(), 5);
Expand All @@ -101,7 +110,7 @@ function shouldReturnTotalHardDeposit(expectedTotalHardDeposit) {
});

it('should return the expectedTotalHardDeposit', function () {
expect(expectedTotalHardDeposit).bignumber.to.be.equal(this.totalHardDeposit);
expect(expectedTotalHardDeposit).to.equal(this.totalHardDeposit);
});
}

Expand All @@ -113,13 +122,13 @@ function shouldReturnIssuer(expectedIssuer) {

function shouldReturnLiquidBalance(expectedLiquidBalance) {
it('should return the expected liquidBalance', async function () {
expect(await this.ERC20SimpleSwap.liquidBalance()).bignumber.to.equal(expectedLiquidBalance);
expect(await this.ERC20SimpleSwap.liquidBalance()).to.equal(expectedLiquidBalance);
});
}

function shouldReturnLiquidBalanceFor(beneficiary, expectedLiquidBalanceFor) {
it('should return the expected liquidBalance', async function () {
expect(await this.ERC20SimpleSwap.liquidBalanceFor(beneficiary)).bignumber.to.equal(expectedLiquidBalanceFor);
expect(await this.ERC20SimpleSwap.liquidBalanceFor(beneficiary)).to.equal(expectedLiquidBalanceFor);
});
}

Expand Down Expand Up @@ -148,25 +157,25 @@ function cashChequeInternal(beneficiary, recipient, cumulativePayout, callerPayo
expectedDecreaseHardDeposit = this.preconditions.hardDepositFor.amount;
}
// totalHarddeposit
expect(this.postconditions.totalHardDeposit).bignumber.to.be.equal(
expect(this.postconditions.totalHardDeposit).to.equal(
this.preconditions.totalHardDeposit.sub(expectedDecreaseHardDeposit)
);
// hardDepositFor
expect(this.postconditions.hardDepositFor.amount).bignumber.to.be.equal(
expect(this.postconditions.hardDepositFor.amount).to.equal(
this.preconditions.hardDepositFor.amount.sub(expectedDecreaseHardDeposit)
);
});

it('should update paidOut', async function () {
expect(this.postconditions.paidOut).bignumber.to.be.equal(this.preconditions.paidOut.add(this.totalPayout));
expect(this.postconditions.paidOut).to.equal(this.preconditions.paidOut.add(this.totalPayout));
});

it('should update totalPaidOut', async function () {
expect(this.postconditions.totalPaidOut).bignumber.to.be.equal(this.preconditions.paidOut.add(this.totalPayout));
expect(this.postconditions.totalPaidOut).to.equal(this.preconditions.paidOut.add(this.totalPayout));
});

it('should transfer the correct amount to the recipient', async function () {
expect(this.postconditions.recipientBalance).bignumber.to.be.equal(
expect(this.postconditions.recipientBalance).to.equal(
this.preconditions.recipientBalance.add(this.totalPayout.sub(callerPayout))
);
});
Expand All @@ -177,9 +186,7 @@ function cashChequeInternal(beneficiary, recipient, cumulativePayout, callerPayo
} else {
expectedAmountCaller = callerPayout;
}
expect(this.postconditions.callerBalance).bignumber.to.be.equal(
this.preconditions.callerBalance.add(expectedAmountCaller)
);
expect(this.postconditions.callerBalance).to.equal(this.preconditions.callerBalance.add(expectedAmountCaller));
});

it('should emit a ChequeCashed event', function () {
Expand Down Expand Up @@ -398,7 +405,7 @@ function shouldPrepareDecreaseHardDeposit(beneficiary, decreaseAmount, from) {
});

it('should update the decreaseAmount', function () {
expect(this.postconditions.hardDepositFor.decreaseAmount).bignumber.to.be.equal(decreaseAmount);
expect(this.postconditions.hardDepositFor.decreaseAmount).to.equal(decreaseAmount);
});

it('should emit a HardDepositDecreasePrepared event', function () {
Expand Down Expand Up @@ -433,19 +440,19 @@ function shouldDecreaseHardDeposit(beneficiary, from) {
});

it('decreases the hardDeposit amount for the beneficiary', function () {
expect(this.postconditions.hardDepositFor.amount).bignumber.to.be.equal(
expect(this.postconditions.hardDepositFor.amount).to.equal(
this.preconditions.hardDepositFor.amount.sub(this.preconditions.hardDepositFor.decreaseAmount)
);
});

it('decreases the total hardDeposits', function () {
expect(this.postconditions.hardDeposit).bignumber.to.be.equal(
expect(this.postconditions.hardDeposit).to.equal(
this.preconditions.hardDeposit.sub(this.preconditions.hardDepositFor.decreaseAmount)
);
});

it('resets the canBeDecreased at', function () {
expect(this.postconditions.hardDepositFor.canBeDecreasedAt).bignumber.to.be.equal(new BN(0));
expect(this.postconditions.hardDepositFor.canBeDecreasedAt).to.equal(new BN(0));
});

it('emits a hardDepositAmountChanged event', function () {
Expand Down Expand Up @@ -484,33 +491,31 @@ function shouldIncreaseHardDeposit(beneficiary, amount, from) {
});

it('should decrease the liquidBalance', function () {
expect(this.postconditions.liquidBalance).bignumber.to.be.equal(this.preconditions.liquidBalance.sub(amount));
expect(this.postconditions.liquidBalance).to.equal(this.preconditions.liquidBalance.sub(amount));
});

it('should not affect the liquidBalanceFor', function () {
expect(this.postconditions.liquidBalanceFor).bignumber.to.be.equal(this.preconditions.liquidBalanceFor);
expect(this.postconditions.liquidBalanceFor).to.equal(this.preconditions.liquidBalanceFor);
});

it('should not affect the balance', function () {
expect(this.postconditions.balance).bignumber.to.be.equal(this.preconditions.balance);
expect(this.postconditions.balance).to.equal(this.preconditions.balance);
});

it('should increase the totalHardDeposit', function () {
expect(this.postconditions.totalHardDeposit).bignumber.to.be.equal(this.preconditions.totalHardDeposit.add(amount));
expect(this.postconditions.totalHardDeposit).to.equal(this.preconditions.totalHardDeposit.add(amount));
});

it('should increase the hardDepositFor', function () {
expect(this.postconditions.hardDepositFor.amount).bignumber.to.be.equal(
this.preconditions.hardDepositFor.amount.add(amount)
);
expect(this.postconditions.hardDepositFor.amount).to.equal(this.preconditions.hardDepositFor.amount.add(amount));
});

it('should not influence the timeout', function () {
expect(this.postconditions.hardDepositFor.timeout).bignumber.to.be.equal(this.preconditions.hardDepositFor.timeout);
expect(this.postconditions.hardDepositFor.timeout).to.equal(this.preconditions.hardDepositFor.timeout);
});

it('should set canBeDecreasedAt to zero', function () {
expect(this.postconditions.hardDepositFor.canBeDecreasedAt).bignumber.to.be.equal(new BN(0));
expect(this.postconditions.hardDepositFor.canBeDecreasedAt).to.equal(new BN(0));
});

it('emits a hardDepositAmountChanged event', function () {
Expand Down Expand Up @@ -543,7 +548,7 @@ function shouldSetCustomHardDepositTimeout(beneficiary, timeout, from) {
});

it('should have set the timeout', async function () {
expect(this.postconditions.hardDepositFor.timeout).bignumber.to.be.equal(timeout);
expect(this.postconditions.hardDepositFor.timeout).to.equal(timeout);
});

it('emits a HardDepositTimeoutChanged event', function () {
Expand Down Expand Up @@ -589,11 +594,11 @@ function shouldWithdraw(amount, from) {
});

it('should have updated the liquidBalance', function () {
expect(this.postconditions.liquidBalance).bignumber.to.be.equal(this.preconditions.liquidBalance.sub(amount));
expect(this.postconditions.liquidBalance).to.equal(this.preconditions.liquidBalance.sub(amount));
});

it('should have updated the callerBalance', function () {
expect(this.postconditions.callerBalance).bignumber.to.be.equal(this.preconditions.callerBalance.add(amount));
expect(this.postconditions.callerBalance).to.equal(this.preconditions.callerBalance.add(amount));
});
}
function shouldNotWithdraw(amount, from, value, revertMessage) {
Expand All @@ -613,13 +618,13 @@ function shouldDeposit(amount, from) {
this.logs = logs;
});
it('should update the liquidBalance of the checkbook', async function () {
expect(await this.ERC20SimpleSwap.liquidBalance()).bignumber.to.equal(this.preconditions.liquidBalance.add(amount));
expect(await this.ERC20SimpleSwap.liquidBalance()).to.equal(this.preconditions.liquidBalance.add(amount));
});
it('should update the balance of the checkbook', async function () {
expect(await this.ERC20SimpleSwap.balance()).bignumber.to.equal(this.preconditions.balance.add(amount));
expect(await this.ERC20SimpleSwap.balance()).to.equal(this.preconditions.balance.add(amount));
});
it('should not afect the totalHardDeposit', async function () {
expect(await this.ERC20SimpleSwap.totalHardDeposit()).bignumber.to.equal(this.preconditions.totalHardDeposit);
expect(await this.ERC20SimpleSwap.totalHardDeposit()).to.equal(this.preconditions.totalHardDeposit);
});
it('should emit a transfer event', async function () {
expectEvent.inLogs(this.logs, 'Transfer', {
Expand Down
41 changes: 22 additions & 19 deletions test/ERC20SimpleSwap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { shouldBehaveLikeERC20SimpleSwap } from './ERC20SimpleSwap.behavior';
import { shouldDeploy } from './ERC20SimpleSwap.should';

import { expect } from 'chai';
import { ethers, getNamedAccounts, getUnnamedAccounts } from 'hardhat';
import { ethers, getNamedAccounts, getUnnamedAccounts, deployments } from 'hardhat';
import { BigNumber, Contract, ContractTransaction } from 'ethers';

describe('ERC20SimpleSwap', async function () {
Expand All @@ -20,24 +20,27 @@ describe('ERC20SimpleSwap', async function () {
defaultHardDepositTimeout = new BN(86400);
});

describe("when we don't deposit while deploying", async function () {
const defaultHardDepositTimeout = new BN(86400);
const value = new BN(0);
// describe("when we don't deposit while deploying", async function () {
// const defaultHardDepositTimeout = new BN(86400);
// const value = new BN(0);

it('should check', async function () {
const sender = issuer;
console.log(alice);
// shouldDeploy(issuer, defaultHardDepositTimeout, sender, value);
shouldBehaveLikeERC20SimpleSwap([issuer, alice, bob, agent], new BN(86400));
});
});
describe('when we deposit while deploying', async function () {
const sender = issuer;
const defaultHardDepositTimeout = new BN(86400);
const value = new BN(50);
// beforeEach(async function () {
// await deployments.fixture();
// });

it('should check', async function () {
//shouldDeploy(issuer, defaultHardDepositTimeout, sender, value);
});
});
// it('should check 1', async function () {
// const sender = issuer;
// shouldDeploy(issuer, defaultHardDepositTimeout, sender, value);
// shouldBehaveLikeERC20SimpleSwap([issuer, alice, bob, agent], new BN(86400));
// });
// });
// describe('when we deposit while deploying', async function () {
// const sender = issuer;
// const defaultHardDepositTimeout = new BN(86400);
// const value = new BN(50);

// it('should check 2', async function () {
// // shouldDeploy(issuer, defaultHardDepositTimeout, sender, value);
// });
// });
});
Loading

0 comments on commit 290a1e0

Please # to comment.