diff --git a/.eslintrc.js b/.eslintrc.js index 392a214..9494a7c 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,16 +1,15 @@ module.exports = { - parser: '@typescript-eslint/parser', // Specifies the ESLint parser + parser: '@typescript-eslint/parser', parserOptions: { - ecmaVersion: 2020, // Allows for the parsing of modern ECMAScript features - sourceType: 'module', // Allows for the use of imports + ecmaVersion: 2020, + sourceType: 'module', }, extends: [ - 'plugin:@typescript-eslint/recommended', // Uses the recommended rules from the @typescript-eslint/eslint-plugin - 'plugin:prettier/recommended', // Enable + 'plugin:@typescript-eslint/recommended', + 'plugin:prettier/recommended', // Puts Prettier last to override other settings ], rules: { - // Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs - // e.g. "@typescript-eslint/explicit-function-return-type": "off", '@typescript-eslint/no-non-null-assertion': 'off', + // other rules... }, }; diff --git a/abigen/code.go.js b/abigen/code.go.js index 0150952..3bf8227 100755 --- a/abigen/code.go.js +++ b/abigen/code.go.js @@ -1,12 +1,12 @@ -const fs = require('fs') +const fs = require('fs'); -const [,,package,compiled,contract] = process.argv +const [, , package, compiled, contract] = process.argv; -const output = JSON.parse(fs.readFileSync(compiled)) +const output = JSON.parse(fs.readFileSync(compiled)); -function makeCodeFile(contract, path=`contracts/${contract}.sol`) { - const Contract = output.contracts[`${path}:${contract}`] - const binRuntime = Contract['bin-runtime'] +function makeCodeFile(contract, path = `contracts/${contract}.sol`) { + const Contract = output.contracts[`${path}:${contract}`]; + const binRuntime = Contract['bin-runtime']; return ` // Copyright 2019 The Swarm Authors // This file is part of the Swarm library. @@ -29,7 +29,7 @@ function makeCodeFile(contract, path=`contracts/${contract}.sol`) { package ${package} // ${contract}DeployedCode is the bytecode ${contract} will have after deployment - const ${contract}DeployedCode = "0x${binRuntime}"` + const ${contract}DeployedCode = "0x${binRuntime}"`; } -console.log(makeCodeFile(contract)) \ No newline at end of file +console.log(makeCodeFile(contract)); diff --git a/deploy/local/000_deploy_factory.ts b/deploy/local/000_deploy_factory.ts index e103b1f..46910cd 100755 --- a/deploy/local/000_deploy_factory.ts +++ b/deploy/local/000_deploy_factory.ts @@ -1,14 +1,13 @@ import { DeployFunction } from 'hardhat-deploy/types'; const func: DeployFunction = async function ({ deployments, getNamedAccounts, network }) { - const { deploy, log } = deployments; const { deployer } = await getNamedAccounts(); - const waitBlockConfirmations = network.name != "testnet" ? 1 : 6; + const waitBlockConfirmations = network.name != 'testnet' ? 1 : 6; - log("----------------------------------------------------"); + log('----------------------------------------------------'); - const token = await deploy("TestToken", { + const token = await deploy('TestToken', { from: deployer, log: true, }); @@ -16,7 +15,7 @@ const func: DeployFunction = async function ({ deployments, getNamedAccounts, ne log(`Token deployed at address ${token.address}`); const deployArgs: string[] = [token.address]; - const factory = await deploy("SimpleSwapFactory", { + const factory = await deploy('SimpleSwapFactory', { from: deployer, args: deployArgs, log: true, @@ -27,4 +26,4 @@ const func: DeployFunction = async function ({ deployments, getNamedAccounts, ne }; export default func; -func.tags = ["factory"]; +func.tags = ['factory']; diff --git a/deploy/local/001_deploy_oracle.ts b/deploy/local/001_deploy_oracle.ts index 702286b..252239d 100755 --- a/deploy/local/001_deploy_oracle.ts +++ b/deploy/local/001_deploy_oracle.ts @@ -1,12 +1,11 @@ - import { DeployFunction } from 'hardhat-deploy/types'; -const func: DeployFunction = async function ({ deployments, getNamedAccounts }) { +const func: DeployFunction = async function ({ deployments, getNamedAccounts }) { const { deploy, log } = deployments; const { deployer } = await getNamedAccounts(); const waitBlockConfirmations = 1; - log("----------------------------------------------------"); + log('----------------------------------------------------'); const deployArgs: [number, number] = [100, 200]; // Deploy the PriceOracle contract @@ -22,4 +21,4 @@ const func: DeployFunction = async function ({ deployments, getNamedAccounts }) }; export default func; -func.tags = ["factory"]; +func.tags = ['factory']; diff --git a/deploy/local/002_deploy_env_variables.ts b/deploy/local/002_deploy_env_variables.ts index 5d7cc2d..063af1c 100644 --- a/deploy/local/002_deploy_env_variables.ts +++ b/deploy/local/002_deploy_env_variables.ts @@ -1,16 +1,15 @@ -import fs from "fs"; -import path from "path"; +import fs from 'fs'; +import path from 'path'; import { DeployFunction } from 'hardhat-deploy/types'; - const func: DeployFunction = async function ({ deployments, config }) { const { get, log } = deployments; - const SimpleSwapFactory = await get("SimpleSwapFactory"); - const PriceOracle = await get("PriceOracle"); + const SimpleSwapFactory = await get('SimpleSwapFactory'); + const PriceOracle = await get('PriceOracle'); // Generate content for the environment file - let content = ""; + let content = ''; content += `echo "----- USE THE COMMANDS BELOW TO SETUP YOUR TERMINALS -----" >&2\n\n`; content += `export BEE_SWAP_FACTORY_ADDRESS=${SimpleSwapFactory.address}\n`; @@ -18,14 +17,14 @@ const func: DeployFunction = async function ({ deployments, config }) { content += `export BEE_SWAP_PRICE_ORACLE_ADDRESS=${PriceOracle.address}\n`; content += `export BEE_SWAP_ENDPOINT=${config.networks.localhost.url}\n`; - const envFilePath = path.join(__dirname, "../../deployedContracts.sh"); + const envFilePath = path.join(__dirname, '../../deployedContracts.sh'); // Write the content to the file - fs.writeFileSync(envFilePath, content, { flag: "a" }); + fs.writeFileSync(envFilePath, content, { flag: 'a' }); log(`Exported contract addresses to ${envFilePath}`); - log("----------------------------------------------------"); + log('----------------------------------------------------'); }; export default func; -func.tags = ["variables"]; +func.tags = ['variables']; diff --git a/deploy/main/000_deploy.ts b/deploy/main/000_deploy.ts index 1e1e2d7..aa84d1c 100755 --- a/deploy/main/000_deploy.ts +++ b/deploy/main/000_deploy.ts @@ -1,17 +1,16 @@ import { DeployFunction } from 'hardhat-deploy/types'; const func: DeployFunction = async function ({ deployments, getNamedAccounts, network }) { - const { deploy, log } = deployments; const { deployer } = await getNamedAccounts(); // This code is just used for Sepolia testnet deployment - const waitBlockConfirmations = network.name !== "mainnet" ? 1 : 6; + const waitBlockConfirmations = network.name !== 'mainnet' ? 1 : 6; - log("----------------------------------------------------"); + log('----------------------------------------------------'); // sBZZ token address // TODO this still needs to be done for the first time }; -func.tags = ["factory"]; +func.tags = ['factory']; export default func; diff --git a/deploy/test/000_deploy_factory.ts b/deploy/test/000_deploy_factory.ts index 79dbb7c..51229d0 100755 --- a/deploy/test/000_deploy_factory.ts +++ b/deploy/test/000_deploy_factory.ts @@ -1,5 +1,4 @@ - -import { verify } from "../../utils/verify"; +import { verify } from '../../utils/verify'; import { DeployFunction } from 'hardhat-deploy/types'; const func: DeployFunction = async function ({ deployments, getNamedAccounts, network }) { @@ -7,11 +6,11 @@ const func: DeployFunction = async function ({ deployments, getNamedAccounts, ne const { deployer } = await getNamedAccounts(); // This code is just used for Sepolia testnet deployment - const waitBlockConfirmations = network.name !== "testnet" ? 1 : 6; + const waitBlockConfirmations = network.name !== 'testnet' ? 1 : 6; - log("----------------------------------------------------"); - const deployArgs: string[] = ["0x543ddb01ba47acb11de34891cd86b675f04840db"]; - const factory = await deploy("SimpleSwapFactory", { + log('----------------------------------------------------'); + const deployArgs: string[] = ['0x543ddb01ba47acb11de34891cd86b675f04840db']; + const factory = await deploy('SimpleSwapFactory', { from: deployer, args: deployArgs, log: true, @@ -21,11 +20,11 @@ const func: DeployFunction = async function ({ deployments, getNamedAccounts, ne log(`Factory deployed at address ${factory.address}`); // Verify the deployment - if (network.name === "testnet" && process.env.TESTNET_ETHERSCAN_KEY) { - log("Verifying..."); + if (network.name === 'testnet' && process.env.TESTNET_ETHERSCAN_KEY) { + log('Verifying...'); await verify(factory.address, arguments); } }; -func.tags = ["factory"]; +func.tags = ['factory']; export default func; diff --git a/deploy/test/001_deploy_oracle.ts b/deploy/test/001_deploy_oracle.ts index 8f6327a..0f708f7 100755 --- a/deploy/test/001_deploy_oracle.ts +++ b/deploy/test/001_deploy_oracle.ts @@ -2,7 +2,6 @@ import verify from '../../utils/verify'; import { DeployFunction } from 'hardhat-deploy/types'; const func: DeployFunction = async function ({ deployments, getNamedAccounts, network }) { - const { deploy, log } = deployments; const { deployer } = await getNamedAccounts(); const args = [100000, 100]; @@ -16,14 +15,14 @@ const func: DeployFunction = async function ({ deployments, getNamedAccounts, ne }); // Log the address at which the Oracle is deployed - console.log('Oracle deployed at address ' + oracle.address) + console.log('Oracle deployed at address ' + oracle.address); // Verify the deployment - if (network.name == "testnet" && process.env.TESTNET_ETHERSCAN_KEY) { - log("Verifying..."); + if (network.name == 'testnet' && process.env.TESTNET_ETHERSCAN_KEY) { + log('Verifying...'); await verify(oracle.address, arguments); } }; -func.tags = ["factory"]; -export default func; \ No newline at end of file +func.tags = ['factory']; +export default func; diff --git a/deployedContracts.sh b/deployedContracts.sh index e997c23..187ea69 100644 --- a/deployedContracts.sh +++ b/deployedContracts.sh @@ -28,3 +28,9 @@ export BEE_SWAP_FACTORY_ADDRESS=0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512 export BEE_SWAP_LEGACY_FACTORY_ADDRESSES=0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512 export BEE_SWAP_PRICE_ORACLE_ADDRESS=0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0 export BEE_SWAP_ENDPOINT=http://localhost:8545 +echo "----- USE THE COMMANDS BELOW TO SETUP YOUR TERMINALS -----" >&2 + +export BEE_SWAP_FACTORY_ADDRESS=0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512 +export BEE_SWAP_LEGACY_FACTORY_ADDRESSES=0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512 +export BEE_SWAP_PRICE_ORACLE_ADDRESS=0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0 +export BEE_SWAP_ENDPOINT=http://localhost:8545 diff --git a/hardhat.config.ts b/hardhat.config.ts index e4a432a..141b672 100755 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -9,23 +9,21 @@ import { HardhatUserConfig } from 'hardhat/types'; const PRIVATE_RPC_MAINNET: string | undefined = process.env.PRIVATE_RPC_MAINNET; const PRIVATE_RPC_TESTNET: string | undefined = process.env.PRIVATE_RPC_TESTNET; -const walletSecret: string = - process.env.WALLET_SECRET === undefined - ? "undefined" - : process.env.WALLET_SECRET; +const walletSecret: string = process.env.WALLET_SECRET === undefined ? 'undefined' : process.env.WALLET_SECRET; -if (walletSecret === "undefined") { - console.log("Please set your WALLET_SECRET in a .env file"); +if (walletSecret === 'undefined') { + console.log('Please set your WALLET_SECRET in a .env file'); } else if (walletSecret.length !== 64) { - console.log("WALLET_SECRET must be 64 characters long."); + console.log('WALLET_SECRET must be 64 characters long.'); } const mainnetEtherscanKey: string | undefined = process.env.MAINNET_ETHERSCAN_KEY; const testnetEtherscanKey: string | undefined = process.env.TESTNET_ETHERSCAN_KEY; -const accounts: string[] | { mnemonic: string } = walletSecret.length === 64 ? [walletSecret] : { mnemonic: walletSecret }; +const accounts: string[] | { mnemonic: string } = + walletSecret.length === 64 ? [walletSecret] : { mnemonic: walletSecret }; const config: HardhatUserConfig = { - defaultNetwork: "hardhat", + defaultNetwork: 'hardhat', solidity: { compilers: [ { @@ -38,59 +36,59 @@ const config: HardhatUserConfig = { }, }, { - version: "0.7.6", + version: '0.7.6', settings: { optimizer: { enabled: true, runs: 200, }, }, - } - ] + }, + ], }, networks: { hardhat: { - deploy: ["deploy/local/"], + deploy: ['deploy/local/'], }, localhost: { - url: "http://localhost:8545", + url: 'http://localhost:8545', accounts, chainId: 31337, - deploy: ["deploy/local/"], + deploy: ['deploy/local/'], }, testnet: { - url: PRIVATE_RPC_TESTNET || "https://1rpc.io/sepolia", + url: PRIVATE_RPC_TESTNET || 'https://1rpc.io/sepolia', accounts, chainId: 11155111, - deploy: ["deploy/test/"], + deploy: ['deploy/test/'], }, mainnet: { - url: PRIVATE_RPC_MAINNET || "https://rpc.gnosischain.com", + url: PRIVATE_RPC_MAINNET || 'https://rpc.gnosischain.com', accounts, chainId: 100, - deploy: ["deploy/main/"], + deploy: ['deploy/main/'], }, }, etherscan: { apiKey: { - mainnet: mainnetEtherscanKey || "", - testnet: testnetEtherscanKey || "", + mainnet: mainnetEtherscanKey || '', + testnet: testnetEtherscanKey || '', }, customChains: [ { - network: "testnet", + network: 'testnet', chainId: 11155111, urls: { - apiURL: "https://api-sepolia.etherscan.io/api", - browserURL: "https://sepolia.etherscan.io/address/", + apiURL: 'https://api-sepolia.etherscan.io/api', + browserURL: 'https://sepolia.etherscan.io/address/', }, }, { - network: "mainnet", + network: 'mainnet', chainId: 100, urls: { - apiURL: "https://api.gnosisscan.io/api", - browserURL: "https://gnosisscan.io/address/", + apiURL: 'https://api.gnosisscan.io/api', + browserURL: 'https://gnosisscan.io/address/', }, }, ], @@ -103,7 +101,7 @@ const config: HardhatUserConfig = { other: 4, }, paths: { - sources: "contracts", + sources: 'contracts', }, }; diff --git a/package.json b/package.json index 5fe56f8..0ef53c6 100755 --- a/package.json +++ b/package.json @@ -35,8 +35,8 @@ "cross-env": "^7.0.2", "dotenv": "^16.0.3", "eslint": "^8.35.0", - "eslint-config-prettier": "^8.7.0", - "eslint-plugin-prettier": "^4.2.1", + "eslint-config-prettier": "^9.1.0", + "eslint-plugin-prettier": "^5.1.3", "ethereum-waffle": "^4.0.10", "ethereumjs-abi": "^0.6.8", "ethers": "^5.7.0", diff --git a/test/ERC20SimpleSwap.behavior.js b/test/ERC20SimpleSwap.behavior.js index c998e21..c670896 100755 --- a/test/ERC20SimpleSwap.behavior.js +++ b/test/ERC20SimpleSwap.behavior.js @@ -1,7 +1,4 @@ -const { - BN, - time -} = require("@openzeppelin/test-helpers"); +const { BN, time } = require('@openzeppelin/test-helpers'); const { shouldReturnDefaultHardDepositTimeout, @@ -27,7 +24,7 @@ const { shouldWithdraw, shouldNotWithdraw, shouldDeposit, -} = require('./ERC20SimpleSwap.should.js') +} = require('./ERC20SimpleSwap.should.js'); // switch to false if you don't want to test the particular function enabledTests = { @@ -45,18 +42,18 @@ enabledTests = { increaseHardDeposit: true, setCustomHardDepositTimeout: true, withdraw: true, - deposit: true -} + deposit: true, +}; // constants to make the test-log more readable -const describeFunction = 'FUNCTION: ' -const describePreCondition = 'PRE-CONDITION: ' -const describeTest = 'TEST: ' +const describeFunction = 'FUNCTION: '; +const describePreCondition = 'PRE-CONDITION: '; +const describeTest = 'TEST: '; // @param balance total ether deposited in checkbook // @param liquidBalance totalDeposit - hardDeposits // @param issuer the issuer of the checkbook -// @param alice a counterparty of the checkbook +// @param alice a counterparty of the checkbook // @param bob a counterparty of the checkbook function shouldBehaveLikeERC20SimpleSwap([issuer, alice, bob, carol], defaultHardDepositTimeout) { // defaults used throught the tests @@ -66,823 +63,979 @@ function shouldBehaveLikeERC20SimpleSwap([issuer, alice, bob, carol], defaultHar firstCumulativePayout: new BN(500), secondCumulativePayout: new BN(1000), deposit: new BN(10000), - } + }; context('as a simple swap', function () { describe(describeFunction + 'defaultHardDepositTimeout', function () { if (enabledTests.defaultHardDepositTimeout) { - shouldReturnDefaultHardDepositTimeout(defaultHardDepositTimeout) + shouldReturnDefaultHardDepositTimeout(defaultHardDepositTimeout); } - }) + }); describe(describeFunction + 'paidOutCheques', function () { if (enabledTests.cheques) { - const beneficiary = defaults.beneficiary + const beneficiary = defaults.beneficiary; context('when no cheque was ever cashed', function () { describe(describeTest + 'shouldReturnPaidOut', function () { - const expectedAmount = new BN(0) - shouldReturnPaidOut(beneficiary, expectedAmount) - shouldReturnTotalPaidOut(expectedAmount) - }) - }) + const expectedAmount = new BN(0); + shouldReturnPaidOut(beneficiary, expectedAmount); + shouldReturnTotalPaidOut(expectedAmount); + }); + }); context('when a cheque was cashed', function () { describe(describePreCondition + 'shouldDeposit', function () { - shouldDeposit(defaults.deposit, issuer) + shouldDeposit(defaults.deposit, issuer); describe(describePreCondition + 'shouldCashChequeBeneficiary', function () { - shouldCashChequeBeneficiary(defaults.recipient, defaults.firstCumulativePayout, issuer, defaults.beneficiary) + shouldCashChequeBeneficiary( + defaults.recipient, + defaults.firstCumulativePayout, + issuer, + defaults.beneficiary + ); describe(describeTest + 'shouldReturnPaidOut', function () { - const expectedAmount = defaults.firstCumulativePayout - shouldReturnPaidOut(beneficiary, expectedAmount) - shouldReturnTotalPaidOut(expectedAmount) - }) - }) - }) - }) + const expectedAmount = defaults.firstCumulativePayout; + shouldReturnPaidOut(beneficiary, expectedAmount); + shouldReturnTotalPaidOut(expectedAmount); + }); + }); + }); + }); } - }) + }); describe(describeFunction + 'hardDeposits', function () { if (enabledTests.hardDeposits) { - const beneficiary = defaults.beneficiary + const beneficiary = defaults.beneficiary; context('when no hardDeposit was allocated', function () { - const expectedAmount = new BN(0) - const exptectedDecreaseAmount = new BN(0) - const exptectedCanBeDecreasedAt = new BN(0) + const expectedAmount = new BN(0); + const exptectedDecreaseAmount = new BN(0); + const exptectedCanBeDecreasedAt = new BN(0); context('when no custom timeout was set', function () { - const expectedDecreaseTimeout = new BN(0) + const expectedDecreaseTimeout = new BN(0); describe(describeTest + 'shouldReturnHardDeposits', function () { - shouldReturnHardDeposits(beneficiary, expectedAmount, exptectedDecreaseAmount, expectedDecreaseTimeout, exptectedCanBeDecreasedAt) - }) - }) + shouldReturnHardDeposits( + beneficiary, + expectedAmount, + exptectedDecreaseAmount, + expectedDecreaseTimeout, + exptectedCanBeDecreasedAt + ); + }); + }); context('when a custom timeout was set', function () { - const expectedDecreaseTimeout = new BN(60) + const expectedDecreaseTimeout = new BN(60); describe(describePreCondition + 'shouldSetCustomDecreaseTimeout', function () { - shouldSetCustomHardDepositTimeout(beneficiary, expectedDecreaseTimeout, issuer) + shouldSetCustomHardDepositTimeout(beneficiary, expectedDecreaseTimeout, issuer); describe(describeTest + 'shouldReturnHardDeposits', function () { - shouldReturnHardDeposits(beneficiary, expectedAmount, exptectedDecreaseAmount, expectedDecreaseTimeout, exptectedCanBeDecreasedAt) - }) - }) - }) - }) + shouldReturnHardDeposits( + beneficiary, + expectedAmount, + exptectedDecreaseAmount, + expectedDecreaseTimeout, + exptectedCanBeDecreasedAt + ); + }); + }); + }); + }); context('when a hardDeposit was allocated', function () { describe(describePreCondition + 'shouldDeposit', function () { - const depositAmount = new BN(50) - shouldDeposit(depositAmount, issuer) + const depositAmount = new BN(50); + shouldDeposit(depositAmount, issuer); describe(describePreCondition + 'shouldIncreaseHardDeposit', function () { - shouldIncreaseHardDeposit(beneficiary, depositAmount, issuer) + shouldIncreaseHardDeposit(beneficiary, depositAmount, issuer); context('when the hardDeposit was not requested to decrease', function () { - const expectedDecreaseAmount = new BN(0) - const expectedCanBeDecreasedAt = new BN(0) - const expectedDecreaseTimeout = new BN(0) + const expectedDecreaseAmount = new BN(0); + const expectedCanBeDecreasedAt = new BN(0); + const expectedDecreaseTimeout = new BN(0); describe(describeTest + 'shouldReturnHardDeposits', function () { - shouldReturnHardDeposits(beneficiary, depositAmount, expectedDecreaseAmount, expectedDecreaseTimeout, expectedCanBeDecreasedAt) - }) - }) + shouldReturnHardDeposits( + beneficiary, + depositAmount, + expectedDecreaseAmount, + expectedDecreaseTimeout, + expectedCanBeDecreasedAt + ); + }); + }); context('when the hardDeposit was requested to decrease', function () { describe(describePreCondition + 'shouldPrepareDecreaseHardDeposit', function () { - const toDecrease = depositAmount.div(new BN(2)) - shouldPrepareDecreaseHardDeposit(beneficiary, toDecrease, issuer) + const toDecrease = depositAmount.div(new BN(2)); + shouldPrepareDecreaseHardDeposit(beneficiary, toDecrease, issuer); describe(describeTest + 'shouldReturnHardDeposits', function () { - const expectedDecreaseTimeout = new BN(0) + const expectedDecreaseTimeout = new BN(0); - shouldReturnHardDeposits(beneficiary, depositAmount, toDecrease, expectedDecreaseTimeout, new BN(42)) // 42 (not BN(0)) signifies that we have to define it later - }) - }) - }) - }) - }) - }) + shouldReturnHardDeposits( + beneficiary, + depositAmount, + toDecrease, + expectedDecreaseTimeout, + new BN(42) + ); // 42 (not BN(0)) signifies that we have to define it later + }); + }); + }); + }); + }); + }); } - }) + }); describe(describeFunction + 'totalHardDeposits', function () { if (enabledTests.totalHardDeposit) { context('when there are no hardDeposits', function () { describe(describeTest + 'shouldReturnTotalHardDeposit', function () { - shouldReturnTotalHardDeposit(new BN(0)) - }) - }) + shouldReturnTotalHardDeposit(new BN(0)); + }); + }); context('when there are hardDeposits', function () { - const depositAmount = defaults.deposit + const depositAmount = defaults.deposit; describe(describePreCondition + 'shouldDeposit', function () { - shouldDeposit(depositAmount, issuer) + shouldDeposit(depositAmount, issuer); describe(describePreCondition + 'shouldIncreaseHardDeposit', function () { - const hardDeposit = defaults.deposit - shouldIncreaseHardDeposit(defaults.beneficiary, hardDeposit, issuer) + const hardDeposit = defaults.deposit; + shouldIncreaseHardDeposit(defaults.beneficiary, hardDeposit, issuer); describe(describeTest + 'shouldReturnTotalHardDeposit', function () { - shouldReturnTotalHardDeposit(hardDeposit) - }) - }) - }) - }) + shouldReturnTotalHardDeposit(hardDeposit); + }); + }); + }); + }); } - }) + }); describe(describeFunction + 'issuer', function () { if (enabledTests.issuer) { - shouldReturnIssuer(issuer) + shouldReturnIssuer(issuer); } - }) + }); describe(describeFunction + 'liquidBalance', function () { if (enabledTests.liquidBalance) { context('when there is some balance', function () { - const depositAmount = defaults.deposit + const depositAmount = defaults.deposit; describe(describePreCondition + 'shouldDeposit', function () { - shouldDeposit(depositAmount, issuer) + shouldDeposit(depositAmount, issuer); context('when there are hardDeposits', function () { describe('when the hardDeposits equal the depositAmount', function () { describe(describePreCondition + 'shouldIncreaseHardDeposit', function () { - const hardDeposit = depositAmount - shouldIncreaseHardDeposit(defaults.beneficiary, hardDeposit, issuer) + const hardDeposit = depositAmount; + shouldIncreaseHardDeposit(defaults.beneficiary, hardDeposit, issuer); describe(describeTest + 'liquidBalance', function () { - shouldReturnLiquidBalance(new BN(0)) - }) - }) + shouldReturnLiquidBalance(new BN(0)); + }); + }); describe('when the hardDeposits are lower than the depositAmount', function () { - const hardDeposit = defaults.deposit.div(new BN(2)) + const hardDeposit = defaults.deposit.div(new BN(2)); describe(describePreCondition + 'shouldIncreaseHardDeposit', function () { - shouldIncreaseHardDeposit(defaults.beneficiary, hardDeposit, issuer) + shouldIncreaseHardDeposit(defaults.beneficiary, hardDeposit, issuer); describe(describeTest + 'shouldReturnLiquidBalance', function () { - shouldReturnLiquidBalance(depositAmount.sub(hardDeposit)) - }) - }) - }) - }) + shouldReturnLiquidBalance(depositAmount.sub(hardDeposit)); + }); + }); + }); + }); context('when there are no hardDeposits', function () { describe(describeTest + 'shouldReturnLiquidBalance', function () { - shouldReturnLiquidBalance(depositAmount) - }) - }) - }) - }) + shouldReturnLiquidBalance(depositAmount); + }); + }); + }); + }); context('when there is no balance', function () { describe(describeTest + 'shouldReturnLiquidBalance', function () { - shouldReturnLiquidBalance(new BN(0)) - }) - }) - }) + shouldReturnLiquidBalance(new BN(0)); + }); + }); + }); } - }) + }); describe(describeFunction + 'shouldReturnLiquidBalanceFor', function () { if (enabledTests.liquidBalanceFor) { - const beneficiary = bob - const depositAmount = new BN(50) + const beneficiary = bob; + const depositAmount = new BN(50); context('when there is some balance', function () { describe(describePreCondition + 'shoulDeposit', function () { - shouldDeposit(depositAmount, issuer) + shouldDeposit(depositAmount, issuer); context('when there are no hard deposits', function () { describe(describeTest + 'shouldReturnLiquidBalanceFor', function () { - shouldReturnLiquidBalanceFor(beneficiary, depositAmount) - }) - }) + shouldReturnLiquidBalanceFor(beneficiary, depositAmount); + }); + }); context('when there are no hard deposits', function () { - const hardDeposit = new BN(10) + const hardDeposit = new BN(10); describe('when these hard deposits are assigned to the beneficiary', function () { describe(describePreCondition + 'shouldIncreaseHardDeposit', function () { - shouldIncreaseHardDeposit(beneficiary, hardDeposit, issuer) + shouldIncreaseHardDeposit(beneficiary, hardDeposit, issuer); describe(describeTest + 'shouldReturnLiquidBalanceFor', function () { - shouldReturnLiquidBalanceFor(beneficiary, depositAmount) - }) - }) - }) + shouldReturnLiquidBalanceFor(beneficiary, depositAmount); + }); + }); + }); describe('when these hard deposits are assigned to somebody else', function () { describe(describePreCondition + 'shouldIncreaseHardDeposit', function () { - shouldIncreaseHardDeposit(alice, hardDeposit, issuer) + shouldIncreaseHardDeposit(alice, hardDeposit, issuer); describe(describeTest + 'shouldReturnLiquidBalanceFor', function () { - shouldReturnLiquidBalanceFor(beneficiary, depositAmount.sub(hardDeposit)) - }) - }) - }) - }) - }) - }) + shouldReturnLiquidBalanceFor(beneficiary, depositAmount.sub(hardDeposit)); + }); + }); + }); + }); + }); + }); describe('when there is no balance', function () { - shouldReturnLiquidBalanceFor(beneficiary, new BN(0)) - }) + shouldReturnLiquidBalanceFor(beneficiary, new BN(0)); + }); } - }) + }); describe(describeFunction + 'cashCheque', function () { if (enabledTests.cashCheque) { - const beneficiary = defaults.beneficiary - const firstCumulativePayout = defaults.firstCumulativePayout - const recipient = defaults.recipient + const beneficiary = defaults.beneficiary; + const firstCumulativePayout = defaults.firstCumulativePayout; + const recipient = defaults.recipient; context('when the sender is not the issuer', function () { - const caller = alice + const caller = alice; context("when we don't send value along", function () { - const value = new BN(0) + const value = new BN(0); context('when the beneficiary provides the beneficiarySig', function () { - const beneficiarySignee = beneficiary + const beneficiarySignee = beneficiary; context('when the issuer provides the issuerSig', function () { - const issuerSignee = issuer + const issuerSignee = issuer; context('when the callerPayout is non-zero', function () { - const callerPayout = defaults.firstCumulativePayout.div(new BN(100)) + const callerPayout = defaults.firstCumulativePayout.div(new BN(100)); context('when there is some money deposited', function () { context('when the money fully covers the cheque', function () { - const depositAmount = firstCumulativePayout.add(defaults.secondCumulativePayout) + const depositAmount = firstCumulativePayout.add(defaults.secondCumulativePayout); describe(describePreCondition + 'shouldDeposit', function () { - shouldDeposit(depositAmount, issuer) + shouldDeposit(depositAmount, issuer); context('when there are hardDeposits assigned to the beneficiary', function () { context('when the hardDeposits fully cover the cheque', function () { describe(describePreCondition + 'shouldIncreaseHardDeposit', function () { - shouldIncreaseHardDeposit(beneficiary, firstCumulativePayout, issuer) + shouldIncreaseHardDeposit(beneficiary, firstCumulativePayout, issuer); context('when we submit one cheque', function () { describe(describeTest + 'shouldCashCheque', function () { - shouldCashCheque(beneficiary, recipient, firstCumulativePayout, callerPayout, caller, beneficiarySignee, issuerSignee) - }) - }) + shouldCashCheque( + beneficiary, + recipient, + firstCumulativePayout, + callerPayout, + caller, + beneficiarySignee, + issuerSignee + ); + }); + }); context('when we attempt to submit two cheques', function () { describe(describePreCondition + 'shouldCashCheque', function () { - shouldCashCheque(beneficiary, recipient, firstCumulativePayout, callerPayout, caller, beneficiarySignee, issuerSignee) - context('when the second cumulativePayout is higher than the first cumulativePayout', function () { - const secondCumulativePayout = defaults.secondCumulativePayout - describe(describeTest + 'shouldCashCheque', function () { - shouldCashCheque(beneficiary, recipient, secondCumulativePayout, callerPayout, caller, beneficiarySignee, issuerSignee) - }) - }) - context('when the second cumulativePayout is lower than the first cumulativePayout', function () { - const secondCumulativePayout = firstCumulativePayout.sub(new BN(1)) - const revertMessage = 'SafeMath: subtraction overflow' - const beneficiaryToSign = { - cumulativePayout: secondCumulativePayout, - recipient, - callerPayout + shouldCashCheque( + beneficiary, + recipient, + firstCumulativePayout, + callerPayout, + caller, + beneficiarySignee, + issuerSignee + ); + context( + 'when the second cumulativePayout is higher than the first cumulativePayout', + function () { + const secondCumulativePayout = defaults.secondCumulativePayout; + describe(describeTest + 'shouldCashCheque', function () { + shouldCashCheque( + beneficiary, + recipient, + secondCumulativePayout, + callerPayout, + caller, + beneficiarySignee, + issuerSignee + ); + }); } - const issuerToSign = { - beneficiary, - cumulativePayout: secondCumulativePayout, + ); + context( + 'when the second cumulativePayout is lower than the first cumulativePayout', + function () { + const secondCumulativePayout = firstCumulativePayout.sub(new BN(1)); + const revertMessage = 'SafeMath: subtraction overflow'; + const beneficiaryToSign = { + cumulativePayout: secondCumulativePayout, + recipient, + callerPayout, + }; + const issuerToSign = { + beneficiary, + cumulativePayout: secondCumulativePayout, + }; + const toSubmit = Object.assign({}, beneficiaryToSign, issuerToSign); + describe(describeTest + 'shouldNotCashCheque', function () { + shouldNotCashCheque( + beneficiaryToSign, + issuerToSign, + toSubmit, + value, + caller, + beneficiarySignee, + issuerSignee, + revertMessage + ); + }); } - const toSubmit = Object.assign({}, beneficiaryToSign, issuerToSign) - describe(describeTest + 'shouldNotCashCheque', function () { - shouldNotCashCheque(beneficiaryToSign, issuerToSign, toSubmit, value, caller, beneficiarySignee, issuerSignee, revertMessage) - }) - }) - }) - }) - }) - }) + ); + }); + }); + }); + }); context('when the hardDeposits partly cover the cheque', function () { describe(describePreCondition + 'shouldIncreaseHardDeposit', function () { - shouldIncreaseHardDeposit(beneficiary, firstCumulativePayout.div(new BN(2)), issuer) + shouldIncreaseHardDeposit(beneficiary, firstCumulativePayout.div(new BN(2)), issuer); describe(describeTest + 'shouldCashCheque', function () { - shouldCashCheque(beneficiary, recipient, firstCumulativePayout, callerPayout, caller, beneficiarySignee, issuerSignee) - }) - }) - }) - }) - }) - }) + shouldCashCheque( + beneficiary, + recipient, + firstCumulativePayout, + callerPayout, + caller, + beneficiarySignee, + issuerSignee + ); + }); + }); + }); + }); + }); + }); context('when the money partly covers the cheque', function () { - const depositAmount = firstCumulativePayout.div(new BN(2)) + const depositAmount = firstCumulativePayout.div(new BN(2)); describe(describePreCondition + 'shouldDeposit', function () { - shouldDeposit(depositAmount, issuer) + shouldDeposit(depositAmount, issuer); describe(describeTest + 'shouldCashCheque', function () { - shouldCashCheque(beneficiary, recipient, firstCumulativePayout, callerPayout, caller, beneficiarySignee, issuerSignee) - }) - }) - }) - }) + shouldCashCheque( + beneficiary, + recipient, + firstCumulativePayout, + callerPayout, + caller, + beneficiarySignee, + issuerSignee + ); + }); + }); + }); + }); context('when no money is deposited', function () { - const revertMessage = 'cannot pay caller' + const revertMessage = 'cannot pay caller'; const beneficiaryToSign = { cumulativePayout: firstCumulativePayout, recipient, - callerPayout - } + callerPayout, + }; const issuerToSign = { beneficiary, cumulativePayout: firstCumulativePayout, - } - const toSubmit = Object.assign({}, beneficiaryToSign, issuerToSign) + }; + const toSubmit = Object.assign({}, beneficiaryToSign, issuerToSign); describe(describeTest + 'shouldNotCashCheque', function () { - shouldNotCashCheque(beneficiaryToSign, issuerToSign, toSubmit, value, caller, beneficiarySignee, issuerSignee, revertMessage) - }) - }) - }) + shouldNotCashCheque( + beneficiaryToSign, + issuerToSign, + toSubmit, + value, + caller, + beneficiarySignee, + issuerSignee, + revertMessage + ); + }); + }); + }); context('when the callerPayout is zero', function () { - const callerPayout = new BN(0) + const callerPayout = new BN(0); describe(describeTest + 'shouldCashCheque', function () { - shouldCashCheque(beneficiary, recipient, firstCumulativePayout, callerPayout, caller, beneficiarySignee, issuerSignee) - }) - }) - }) + shouldCashCheque( + beneficiary, + recipient, + firstCumulativePayout, + callerPayout, + caller, + beneficiarySignee, + issuerSignee + ); + }); + }); + }); context('when the issuer does not provide the issuerSig', function () { - const issuerSignee = alice - const callerPayout = defaults.firstCumulativePayout.div(new BN(100)) - const revertMessage = 'invalid issuer signature' + const issuerSignee = alice; + const callerPayout = defaults.firstCumulativePayout.div(new BN(100)); + const revertMessage = 'invalid issuer signature'; const beneficiaryToSign = { cumulativePayout: firstCumulativePayout, recipient, - callerPayout - } + callerPayout, + }; const issuerToSign = { beneficiary, cumulativePayout: firstCumulativePayout, - } - const toSubmit = Object.assign({}, beneficiaryToSign, issuerToSign) + }; + const toSubmit = Object.assign({}, beneficiaryToSign, issuerToSign); describe(describeTest + 'shouldNotCashCheque', function () { - shouldNotCashCheque(beneficiaryToSign, issuerToSign, toSubmit, value, caller, beneficiarySignee, issuerSignee, revertMessage) - }) - }) - - }) + shouldNotCashCheque( + beneficiaryToSign, + issuerToSign, + toSubmit, + value, + caller, + beneficiarySignee, + issuerSignee, + revertMessage + ); + }); + }); + }); context('when the beneficiary does not provide the beneficiarySig', function () { - const beneficiarySignee = alice - const issuerSignee = issuer - const callerPayout = defaults.firstCumulativePayout.div(new BN(100)) - const revertMessage = 'invalid beneficiary signature' + const beneficiarySignee = alice; + const issuerSignee = issuer; + const callerPayout = defaults.firstCumulativePayout.div(new BN(100)); + const revertMessage = 'invalid beneficiary signature'; const beneficiaryToSign = { cumulativePayout: firstCumulativePayout, recipient, - callerPayout - } + callerPayout, + }; const issuerToSign = { beneficiary, cumulativePayout: firstCumulativePayout, - } - const toSubmit = Object.assign({}, beneficiaryToSign, issuerToSign) + }; + const toSubmit = Object.assign({}, beneficiaryToSign, issuerToSign); describe(describeTest + 'shouldNotCashCheque', function () { - shouldNotCashCheque(beneficiaryToSign, issuerToSign, toSubmit, value, caller, beneficiarySignee, issuerSignee, revertMessage) - }) - }) - }) + shouldNotCashCheque( + beneficiaryToSign, + issuerToSign, + toSubmit, + value, + caller, + beneficiarySignee, + issuerSignee, + revertMessage + ); + }); + }); + }); context('when we send value along', function () { - const value = new BN(50) - const beneficiarySignee = alice - const issuerSignee = issuer - const callerPayout = defaults.firstCumulativePayout.div(new BN(100)) - const revertMessage = 'revert' + const value = new BN(50); + const beneficiarySignee = alice; + const issuerSignee = issuer; + const callerPayout = defaults.firstCumulativePayout.div(new BN(100)); + const revertMessage = 'revert'; const beneficiaryToSign = { cumulativePayout: firstCumulativePayout, recipient, - callerPayout - } + callerPayout, + }; const issuerToSign = { beneficiary, cumulativePayout: firstCumulativePayout, - } - const toSubmit = Object.assign({}, beneficiaryToSign, issuerToSign) + }; + const toSubmit = Object.assign({}, beneficiaryToSign, issuerToSign); describe(describeTest + 'shouldNotCashCheque', function () { - shouldNotCashCheque(beneficiaryToSign, issuerToSign, toSubmit, value, caller, beneficiarySignee, issuerSignee, revertMessage) - }) - }) - }) + shouldNotCashCheque( + beneficiaryToSign, + issuerToSign, + toSubmit, + value, + caller, + beneficiarySignee, + issuerSignee, + revertMessage + ); + }); + }); + }); context('when the sender is the issuer', function () { - const caller = issuer - const callerPayout = new BN(0) - const beneficiarySignee = beneficiary - const issuerSignee = beneficiary // on purpose not the correct signee, as it is not needed + const caller = issuer; + const callerPayout = new BN(0); + const beneficiarySignee = beneficiary; + const issuerSignee = beneficiary; // on purpose not the correct signee, as it is not needed describe(describeTest + 'shouldCashCheque', function () { - shouldCashCheque(beneficiary, recipient, firstCumulativePayout, callerPayout, caller, beneficiarySignee, issuerSignee) - }) - }) - + shouldCashCheque( + beneficiary, + recipient, + firstCumulativePayout, + callerPayout, + caller, + beneficiarySignee, + issuerSignee + ); + }); + }); } - }) + }); describe(describeFunction + 'cashChequeBeneficiary', function () { if (enabledTests.cashChequeBeneficiary) { - const beneficiary = defaults.beneficiary + const beneficiary = defaults.beneficiary; context("when we don't send value along", function () { - const value = new BN(0) + const value = new BN(0); context('when the issuer is a signee', function () { - const sender = beneficiary - const signee = issuer + const sender = beneficiary; + const signee = issuer; context('when the signee signs the correct fields', function () { context('when the recipient is not the beneficiary', function () { - const recipient = defaults.recipient + const recipient = defaults.recipient; context('when we have not cashed a cheque before', function () { - const requestPayout = defaults.firstCumulativePayout + const requestPayout = defaults.firstCumulativePayout; context('when there is some balance', function () { context('when the balance is bigger than the requestPayout', function () { describe(describePreCondition + 'shouldDeposit', function () { - const depositAmount = requestPayout.add(new BN(50)) - shouldDeposit(depositAmount, issuer) + const depositAmount = requestPayout.add(new BN(50)); + shouldDeposit(depositAmount, issuer); context('when there are hardDeposits', function () { describe('when the hardDeposits are assigned to the sender', function () { - const hardDepositReceiver = sender + const hardDepositReceiver = sender; context('when the hardDeposit is more the requestPayout', function () { - const hardDeposit = requestPayout.add(new BN(1)) + const hardDeposit = requestPayout.add(new BN(1)); describe(describePreCondition + 'shouldIncreaseHardDeposit', function () { - shouldIncreaseHardDeposit(hardDepositReceiver, hardDeposit, issuer) + shouldIncreaseHardDeposit(hardDepositReceiver, hardDeposit, issuer); describe(describeTest + 'shouldCashChequeBeneficiary', function () { - shouldCashChequeBeneficiary(recipient, requestPayout, signee, sender) - }) - }) - }) + shouldCashChequeBeneficiary(recipient, requestPayout, signee, sender); + }); + }); + }); context('when the hardDeposit equals the requestPayout', function () { - const hardDeposit = requestPayout + const hardDeposit = requestPayout; describe(describePreCondition + 'shouldIncreaseHardDeposit', function () { - shouldIncreaseHardDeposit(hardDepositReceiver, hardDeposit, issuer) + shouldIncreaseHardDeposit(hardDepositReceiver, hardDeposit, issuer); describe(describeTest + 'shouldCashChequeBeneficiary', function () { - shouldCashChequeBeneficiary(recipient, requestPayout, signee, sender) - }) - }) - }) + shouldCashChequeBeneficiary(recipient, requestPayout, signee, sender); + }); + }); + }); context('when the hardDeposit is less than the requestPayout', function () { - const hardDeposit = requestPayout.sub(new BN(1)) + const hardDeposit = requestPayout.sub(new BN(1)); describe(describePreCondition + 'shouldIncreaseHardDeposit', function () { - shouldIncreaseHardDeposit(hardDepositReceiver, hardDeposit, issuer) + shouldIncreaseHardDeposit(hardDepositReceiver, hardDeposit, issuer); describe(describeTest + 'shouldCashChequeBeneficiary', function () { - shouldCashChequeBeneficiary(recipient, requestPayout, signee, sender) - }) - }) - }) - }) + shouldCashChequeBeneficiary(recipient, requestPayout, signee, sender); + }); + }); + }); + }); describe('when the hardDeposits are assigned to somebody else', function () { - const hardDepositReceiver = alice - const hardDeposit = requestPayout.add(new BN(1)) + const hardDepositReceiver = alice; + const hardDeposit = requestPayout.add(new BN(1)); describe(describePreCondition + 'shouldIncreaseHardDeposit', function () { - shouldIncreaseHardDeposit(hardDepositReceiver, hardDeposit, issuer) + shouldIncreaseHardDeposit(hardDepositReceiver, hardDeposit, issuer); describe(describeTest + 'shouldCashChequeBeneficiary', function () { - shouldCashChequeBeneficiary(recipient, requestPayout, signee, sender) - }) - }) - }) - }) - }) - }) + shouldCashChequeBeneficiary(recipient, requestPayout, signee, sender); + }); + }); + }); + }); + }); + }); context('when the balance equals the requestPayout', function () { describe(describePreCondition + 'shouldDeposit', function () { - const depositAmount = requestPayout - shouldDeposit(depositAmount, issuer) + const depositAmount = requestPayout; + shouldDeposit(depositAmount, issuer); describe(describeTest + 'shouldCashChequeBeneficiary', function () { - shouldCashChequeBeneficiary(recipient, requestPayout, signee, sender) - }) - }) - }) - }) + shouldCashChequeBeneficiary(recipient, requestPayout, signee, sender); + }); + }); + }); + }); context('when there is no balance', function () { describe(describeTest + 'shouldCashChequeBeneficiary', function () { - shouldCashChequeBeneficiary(recipient, requestPayout, signee, sender) - }) - }) - }) + shouldCashChequeBeneficiary(recipient, requestPayout, signee, sender); + }); + }); + }); context('when we have cashed a cheque before', function () { describe(describePreCondition + 'shouldDeposit', function () { - shouldDeposit(defaults.deposit, issuer) + shouldDeposit(defaults.deposit, issuer); describe(describePreCondition + 'shouldCashChequeBeneficiary', function () { - shouldCashChequeBeneficiary(recipient, defaults.firstCumulativePayout, signee, sender) + shouldCashChequeBeneficiary(recipient, defaults.firstCumulativePayout, signee, sender); describe(describeTest + 'shouldCashChequeBeneficiary', function () { - shouldCashChequeBeneficiary(recipient, defaults.firstCumulativePayout, signee, sender) - }) - }) - }) - }) - }) + shouldCashChequeBeneficiary(recipient, defaults.firstCumulativePayout, signee, sender); + }); + }); + }); + }); + }); context('when the recipient is the beneficiary', function () { - const recipient = defaults.beneficiary - const requestPayout = defaults.firstCumulativePayout + const recipient = defaults.beneficiary; + const requestPayout = defaults.firstCumulativePayout; describe(describeTest + 'shouldCashChequeBeneficiary', function () { - shouldCashChequeBeneficiary(recipient, requestPayout, signee, sender) - }) - }) - }) + shouldCashChequeBeneficiary(recipient, requestPayout, signee, sender); + }); + }); + }); context('when the signee does not sign the correct fields', function () { - const revertMessage = "invalid issuer signature" - const recipient = defaults.recipient - const toSubmitCumulativePayment = defaults.firstCumulativePayout - const toSignCumulativePayment = new BN(1) - const sender = beneficiary - shouldNotCashChequeBeneficiary(recipient, toSubmitCumulativePayment, toSignCumulativePayment, signee, sender, value, revertMessage) - }) - }) + const revertMessage = 'invalid issuer signature'; + const recipient = defaults.recipient; + const toSubmitCumulativePayment = defaults.firstCumulativePayout; + const toSignCumulativePayment = new BN(1); + const sender = beneficiary; + shouldNotCashChequeBeneficiary( + recipient, + toSubmitCumulativePayment, + toSignCumulativePayment, + signee, + sender, + value, + revertMessage + ); + }); + }); context('when the issuer is not a signee', function () { - const revertMessage = "invalid issuer signature" - const signee = alice - const recipient = defaults.recipient - const toSubmitCumulativePayment = defaults.firstCumulativePayout - const toSignCumulativePayment = toSubmitCumulativePayment - const sender = beneficiary - shouldNotCashChequeBeneficiary(recipient, toSubmitCumulativePayment, toSignCumulativePayment, signee, sender, value, revertMessage) - }) - }) + const revertMessage = 'invalid issuer signature'; + const signee = alice; + const recipient = defaults.recipient; + const toSubmitCumulativePayment = defaults.firstCumulativePayout; + const toSignCumulativePayment = toSubmitCumulativePayment; + const sender = beneficiary; + shouldNotCashChequeBeneficiary( + recipient, + toSubmitCumulativePayment, + toSignCumulativePayment, + signee, + sender, + value, + revertMessage + ); + }); + }); context('when we send value along', function () { - const value = new BN(1) - const revertMessage = "revert" - const signee = alice - const recipient = defaults.recipient - const toSubmitCumulativePayment = defaults.firstCumulativePayout - const toSignCumulativePayment = toSubmitCumulativePayment - const sender = beneficiary + const value = new BN(1); + const revertMessage = 'revert'; + const signee = alice; + const recipient = defaults.recipient; + const toSubmitCumulativePayment = defaults.firstCumulativePayout; + const toSignCumulativePayment = toSubmitCumulativePayment; + const sender = beneficiary; describe(describeTest + 'shouldNotCashChequeBeneficiary', function () { - shouldNotCashChequeBeneficiary(recipient, toSubmitCumulativePayment, toSignCumulativePayment, signee, sender, value, revertMessage) - }) - }) + shouldNotCashChequeBeneficiary( + recipient, + toSubmitCumulativePayment, + toSignCumulativePayment, + signee, + sender, + value, + revertMessage + ); + }); + }); } - }) - + }); describe(describeFunction + 'prepareDecreaseHardDeposit', function () { if (enabledTests.prepareDecreaseHardDeposit) { - const beneficiary = defaults.beneficiary + const beneficiary = defaults.beneficiary; context("when we don't send value along", function () { - const value = new BN(0) + const value = new BN(0); context('when there are hardDeposits', function () { - const hardDepositAmount = new BN(50) + const hardDepositAmount = new BN(50); describe(describePreCondition + 'shouldDeposit', function () { - shouldDeposit(hardDepositAmount, issuer) + shouldDeposit(hardDepositAmount, issuer); describe(describePreCondition + 'shouldIncreaseHardDeposit', function () { - shouldIncreaseHardDeposit(beneficiary, hardDepositAmount, issuer) + shouldIncreaseHardDeposit(beneficiary, hardDepositAmount, issuer); context('when the sender is the issuer', function () { - const sender = issuer + const sender = issuer; context('when the decreaseAmount is the hardDepositAmount', function () { - const decreaseAmount = hardDepositAmount + const decreaseAmount = hardDepositAmount; context('when we have set a custom timeout', function () { describe(describePreCondition + 'shouldSetCustomHardDepositTimeout', function () { - const customTimeout = new BN(10) - shouldSetCustomHardDepositTimeout(beneficiary, customTimeout, issuer) + const customTimeout = new BN(10); + shouldSetCustomHardDepositTimeout(beneficiary, customTimeout, issuer); context('when we have not set a custom timeout', function () { describe(describeTest + 'prepareDecreaseHardDeposit', function () { - shouldPrepareDecreaseHardDeposit(beneficiary, decreaseAmount, sender) - }) - }) - }) - }) + shouldPrepareDecreaseHardDeposit(beneficiary, decreaseAmount, sender); + }); + }); + }); + }); context('when we have not set a custom timeout', function () { describe(describeTest + 'prepareDecreaseHardDeposit', function () { - shouldPrepareDecreaseHardDeposit(beneficiary, decreaseAmount, sender) - }) - }) - }) + shouldPrepareDecreaseHardDeposit(beneficiary, decreaseAmount, sender); + }); + }); + }); context('when the decreaseAmount is less than the hardDepositAmount', function () { - const decreaseAmount = hardDepositAmount.div(new BN(2)) + const decreaseAmount = hardDepositAmount.div(new BN(2)); describe(describeTest + 'prepareDecreaseHardDeposit', function () { - shouldPrepareDecreaseHardDeposit(beneficiary, decreaseAmount, sender) - }) - }) + shouldPrepareDecreaseHardDeposit(beneficiary, decreaseAmount, sender); + }); + }); context('when the decreaseAmount is higher than the hardDepositAmount', function () { - const decreaseAmount = hardDepositAmount.add(new BN(1)) - const revertMessage = "hard deposit not sufficient" + const decreaseAmount = hardDepositAmount.add(new BN(1)); + const revertMessage = 'hard deposit not sufficient'; describe(describeTest + 'shouldNotPrepareDecreaseHardDeposit', function () { - shouldNotPrepareDecreaseHardDeposit(beneficiary, decreaseAmount, sender, value, revertMessage) - }) - }) - }) + shouldNotPrepareDecreaseHardDeposit(beneficiary, decreaseAmount, sender, value, revertMessage); + }); + }); + }); context('when the sender is the issuer', function () { - const sender = alice - const revertMessage = "not issuer" - const decreaseAmount = hardDepositAmount + const sender = alice; + const revertMessage = 'not issuer'; + const decreaseAmount = hardDepositAmount; describe(describeTest + 'shouldNotPrepareDecreaseHardDeposit', function () { - shouldNotPrepareDecreaseHardDeposit(beneficiary, decreaseAmount, sender, value, revertMessage) - }) - }) - }) + shouldNotPrepareDecreaseHardDeposit(beneficiary, decreaseAmount, sender, value, revertMessage); + }); + }); + }); context('when there are no hardDeposits', function () { - const sender = issuer - const revertMessage = "hard deposit not sufficient" - const decreaseAmount = new BN(50) + const sender = issuer; + const revertMessage = 'hard deposit not sufficient'; + const decreaseAmount = new BN(50); describe(describeTest + 'shouldNotPrepareDecreaseHardDeposit', function () { - shouldNotPrepareDecreaseHardDeposit(beneficiary, decreaseAmount, sender, value, revertMessage) - }) - }) - }) - }) - }) + shouldNotPrepareDecreaseHardDeposit(beneficiary, decreaseAmount, sender, value, revertMessage); + }); + }); + }); + }); + }); context('when we send value along', function () { - const value = new BN(1) - const sender = issuer - const revertMessage = "revert" - const decreaseAmount = new BN(50) + const value = new BN(1); + const sender = issuer; + const revertMessage = 'revert'; + const decreaseAmount = new BN(50); describe(describeTest + 'shouldNotPrepareDecreaseHardDeposit', function () { - shouldNotPrepareDecreaseHardDeposit(beneficiary, decreaseAmount, sender, value, revertMessage) - }) - }) + shouldNotPrepareDecreaseHardDeposit(beneficiary, decreaseAmount, sender, value, revertMessage); + }); + }); } - }) + }); describe(describeFunction + 'decreaseHardDeposit', function () { if (enabledTests.decreaseHardDeposit) { - const beneficiary = defaults.beneficiary + const beneficiary = defaults.beneficiary; context("when we don't send value along", function () { - const value = new BN(0) + const value = new BN(0); context('when the sender is the issuer', function () { - const sender = issuer - context("when we have prepared the decreaseHardDeposit", function () { - const hardDeposit = new BN(50) - describe(describePreCondition + "shouldDeposit", function () { - shouldDeposit(hardDeposit, issuer) - describe(describePreCondition + "shouldIncreaseHardDeposit", function () { - shouldIncreaseHardDeposit(beneficiary, hardDeposit, issuer) - describe(describePreCondition + "shouldPrepareDecreaseHardDeposit", function () { - shouldPrepareDecreaseHardDeposit(beneficiary, hardDeposit, issuer) + const sender = issuer; + context('when we have prepared the decreaseHardDeposit', function () { + const hardDeposit = new BN(50); + describe(describePreCondition + 'shouldDeposit', function () { + shouldDeposit(hardDeposit, issuer); + describe(describePreCondition + 'shouldIncreaseHardDeposit', function () { + shouldIncreaseHardDeposit(beneficiary, hardDeposit, issuer); + describe(describePreCondition + 'shouldPrepareDecreaseHardDeposit', function () { + shouldPrepareDecreaseHardDeposit(beneficiary, hardDeposit, issuer); context('when we have waited more than timeout time', function () { beforeEach(async function () { - await time.increase(await this.ERC20SimpleSwap.defaultHardDepositTimeout()) - }) + await time.increase(await this.ERC20SimpleSwap.defaultHardDepositTimeout()); + }); describe(describeTest + 'shouldDecreaseHardDeposit', function () { - shouldDecreaseHardDeposit(beneficiary, sender) - }) - }) + shouldDecreaseHardDeposit(beneficiary, sender); + }); + }); context('when we have not waited more than defaultHardDepositTimeout time', function () { describe(describeTest + 'shouldNotDecreaseHardDeposit', function () { - const revertMessage = "deposit not yet timed out" - shouldNotDecreaseHardDeposit(beneficiary, sender, value, revertMessage) - }) - }) - }) - }) - }) - }) + const revertMessage = 'deposit not yet timed out'; + shouldNotDecreaseHardDeposit(beneficiary, sender, value, revertMessage); + }); + }); + }); + }); + }); + }); context('when we have not prepared the decreaseHardDeposit', function () { describe(describeTest + 'shouldNotDecreaseHardDeposit', function () { - const revertMessage = "deposit not yet timed out" - shouldNotDecreaseHardDeposit(beneficiary, sender, value, revertMessage) - }) - }) - }) - }) - context("when we send value along", function () { - const value = new BN(1) - const sender = issuer + const revertMessage = 'deposit not yet timed out'; + shouldNotDecreaseHardDeposit(beneficiary, sender, value, revertMessage); + }); + }); + }); + }); + context('when we send value along', function () { + const value = new BN(1); + const sender = issuer; describe(describeTest + 'shouldNotDecreaseHardDeposit', function () { - const revertMessage = "revert" - shouldNotDecreaseHardDeposit(beneficiary, sender, value, revertMessage) - }) - - }) + const revertMessage = 'revert'; + shouldNotDecreaseHardDeposit(beneficiary, sender, value, revertMessage); + }); + }); } - }) + }); describe(describeFunction + 'increaseHardDeposit', function () { if (enabledTests.increaseHardDeposit) { - const hardDepositIncrease = new BN(50) - const beneficiary = defaults.beneficiary + const hardDepositIncrease = new BN(50); + const beneficiary = defaults.beneficiary; context("when we don't send value along", function () { - const value = new BN(0) + const value = new BN(0); context('when the sender is the issuer', function () { - const sender = issuer + const sender = issuer; context('when there is more liquidBalance than the requested hardDepositIncrease', function () { - const deposit = hardDepositIncrease.mul(new BN(2)) + const deposit = hardDepositIncrease.mul(new BN(2)); describe(describePreCondition + 'shouldDeposit', function () { - shouldDeposit(deposit, issuer) + shouldDeposit(deposit, issuer); context('when we have set a customHardDepositTimeout', function () { - const customHardDepositTimeout = new BN(60) + const customHardDepositTimeout = new BN(60); describe(describePreCondition + 'shouldSetCustomHardDepositTimeout', function () { - shouldSetCustomHardDepositTimeout(beneficiary, customHardDepositTimeout, issuer) + shouldSetCustomHardDepositTimeout(beneficiary, customHardDepositTimeout, issuer); describe(describeTest + 'shouldIncreaseHardDeposit', function () { - shouldIncreaseHardDeposit(beneficiary, hardDepositIncrease, sender) - }) - }) - }) - }) - }) + shouldIncreaseHardDeposit(beneficiary, hardDepositIncrease, sender); + }); + }); + }); + }); + }); context('when there is as much liquidBalance as the requested hardDepositIncrease', function () { - const deposit = hardDepositIncrease + const deposit = hardDepositIncrease; describe(describePreCondition + 'shouldDeposit', function () { - shouldDeposit(deposit, issuer) + shouldDeposit(deposit, issuer); describe(describeTest + 'shouldIncreaseHardDeposit', function () { - shouldIncreaseHardDeposit(beneficiary, hardDepositIncrease, sender) - }) - }) - }) + shouldIncreaseHardDeposit(beneficiary, hardDepositIncrease, sender); + }); + }); + }); context('when the liquidBalance is less than the requested hardDepositIncrease', function () { describe(describeTest + 'shouldNotIncreaseHardDeposit', function () { - const revertMessage = "hard deposit exceeds balance" - shouldNotIncreaseHardDeposit(beneficiary, hardDepositIncrease, sender, value, revertMessage) - }) - }) - }) + const revertMessage = 'hard deposit exceeds balance'; + shouldNotIncreaseHardDeposit(beneficiary, hardDepositIncrease, sender, value, revertMessage); + }); + }); + }); context('when the sender is not the issuer', function () { - const sender = alice + const sender = alice; describe(describeTest + 'shouldNotIncreaseHardDeposit', function () { - const revertMessage = "not issuer" - shouldNotIncreaseHardDeposit(beneficiary, hardDepositIncrease, sender, value, revertMessage) - }) - }) - }) + const revertMessage = 'not issuer'; + shouldNotIncreaseHardDeposit(beneficiary, hardDepositIncrease, sender, value, revertMessage); + }); + }); + }); context('when we send value along', function () { - const value = new BN(1) - const hardDepositIncrease = new BN(50) - const beneficiary = defaults.beneficiary - const sender = issuer + const value = new BN(1); + const hardDepositIncrease = new BN(50); + const beneficiary = defaults.beneficiary; + const sender = issuer; describe(describeTest + 'shouldNotIncreaseHardDeposit', function () { - const revertMessage = "revert" - shouldNotIncreaseHardDeposit(beneficiary, hardDepositIncrease, sender, value, revertMessage) - }) - }) + const revertMessage = 'revert'; + shouldNotIncreaseHardDeposit(beneficiary, hardDepositIncrease, sender, value, revertMessage); + }); + }); } - }) + }); describe(describeFunction + 'setCustomHardDepositTimeout', function () { if (enabledTests.setCustomHardDepositTimeout) { - const beneficiary = defaults.beneficiary - const timeout = new BN(60) + const beneficiary = defaults.beneficiary; + const timeout = new BN(60); context("when we don't send value along", function () { - const value = new BN(0) + const value = new BN(0); context('when the sender is the issuer', function () { - const sender = issuer + const sender = issuer; context('when the beneficiary is a signee', function () { - const signee = beneficiary + const signee = beneficiary; context('when the beneficiary signs the correct fields', function () { describe(describeTest + 'shouldSetCustomHardDepositTimeout', function () { - shouldSetCustomHardDepositTimeout(beneficiary, timeout, sender) - }) - }) + shouldSetCustomHardDepositTimeout(beneficiary, timeout, sender); + }); + }); context('when the beneficiary does not sign the correct fields', function () { describe(describeTest + 'shouldNotSetCustomHardDepositTimeout', function () { - const toSubmit = { beneficiary, timeout } - const toSign = { beneficiary, timeout: timeout.sub(new BN(1)) } - const revertMessage = "invalid beneficiary signature" - shouldNotSetCustomHardDepositTimeout(toSubmit, toSign, signee, sender, value, revertMessage) - }) - }) - }) + const toSubmit = { beneficiary, timeout }; + const toSign = { beneficiary, timeout: timeout.sub(new BN(1)) }; + const revertMessage = 'invalid beneficiary signature'; + shouldNotSetCustomHardDepositTimeout(toSubmit, toSign, signee, sender, value, revertMessage); + }); + }); + }); context('when the beneficiary is not a signee', function () { - const signee = alice + const signee = alice; describe(describeTest + 'shouldNotSetCustomHardDepositTimeout', function () { - const toSubmit = { beneficiary, timeout } - const toSign = toSubmit - const revertMessage = "invalid beneficiary signature" - shouldNotSetCustomHardDepositTimeout(toSubmit, toSign, signee, sender, value, revertMessage) - }) - }) - }) + const toSubmit = { beneficiary, timeout }; + const toSign = toSubmit; + const revertMessage = 'invalid beneficiary signature'; + shouldNotSetCustomHardDepositTimeout(toSubmit, toSign, signee, sender, value, revertMessage); + }); + }); + }); context('when the sender is not the issuer', function () { - const sender = alice + const sender = alice; describe(describeTest + 'shouldNotSetCustomHardDepositTimeout', function () { - const toSubmit = { beneficiary, timeout } - const toSign = toSubmit - const signee = beneficiary - const revertMessage = "not issuer" - shouldNotSetCustomHardDepositTimeout(toSubmit, toSign, signee, sender, value, revertMessage) - }) - }) - }) + const toSubmit = { beneficiary, timeout }; + const toSign = toSubmit; + const signee = beneficiary; + const revertMessage = 'not issuer'; + shouldNotSetCustomHardDepositTimeout(toSubmit, toSign, signee, sender, value, revertMessage); + }); + }); + }); context('when we send value along', function () { - const value = new BN(1) - const sender = issuer - const signee = beneficiary + const value = new BN(1); + const sender = issuer; + const signee = beneficiary; describe(describeTest + 'shouldNotSetCustomHardDepositTimeout', function () { - const toSubmit = { beneficiary, timeout } - const toSign = toSubmit - const revertMessage = "revert" - shouldNotSetCustomHardDepositTimeout(toSubmit, toSign, signee, sender, value, revertMessage) - }) - }) + const toSubmit = { beneficiary, timeout }; + const toSign = toSubmit; + const revertMessage = 'revert'; + shouldNotSetCustomHardDepositTimeout(toSubmit, toSign, signee, sender, value, revertMessage); + }); + }); } - }) + }); describe(describeFunction + 'withdraw', function () { if (enabledTests.withdraw) { - const withdrawAmount = new BN(50) + const withdrawAmount = new BN(50); context("when we don't send value along", function () { - const value = new BN(0) + const value = new BN(0); context('when the sender is the issuer', function () { - const sender = issuer + const sender = issuer; context('when the liquidBalance is more than the withdrawAmount', function () { - const depositAmount = withdrawAmount.mul(new BN(2)) + const depositAmount = withdrawAmount.mul(new BN(2)); describe(describePreCondition + 'shouldDeposit', function () { - shouldDeposit(depositAmount, issuer) + shouldDeposit(depositAmount, issuer); describe(describeTest + 'shouldWithdraw', function () { - shouldWithdraw(withdrawAmount, sender) - }) - }) - }) + shouldWithdraw(withdrawAmount, sender); + }); + }); + }); context('when the liquidBalance equals the withdrawAount', function () { - const depositAmount = withdrawAmount + const depositAmount = withdrawAmount; describe(describePreCondition + 'shouldDeposit', function () { - shouldDeposit(depositAmount, issuer) + shouldDeposit(depositAmount, issuer); describe(describeTest + 'shouldWithdraw', function () { - shouldWithdraw(withdrawAmount, sender) - }) - }) - }) + shouldWithdraw(withdrawAmount, sender); + }); + }); + }); context('when the liquidBalance is less than the withdrawAmount', function () { - const revertMessage = "liquidBalance not sufficient" - shouldNotWithdraw(withdrawAmount, sender, value, revertMessage) - }) - }) + const revertMessage = 'liquidBalance not sufficient'; + shouldNotWithdraw(withdrawAmount, sender, value, revertMessage); + }); + }); context('when the sender is not the issuer', function () { - const sender = alice - const revertMessage = "not issuer" - shouldNotWithdraw(withdrawAmount, sender, value, revertMessage) - }) - }) + const sender = alice; + const revertMessage = 'not issuer'; + shouldNotWithdraw(withdrawAmount, sender, value, revertMessage); + }); + }); context('when we send value along', function () { - const value = new BN(1) - const sender = alice - const revertMessage = "revert" - shouldNotWithdraw(withdrawAmount, sender, value, revertMessage) - }) + const value = new BN(1); + const sender = alice; + const revertMessage = 'revert'; + shouldNotWithdraw(withdrawAmount, sender, value, revertMessage); + }); } - }) + }); describe(describeFunction + 'deposit', function () { if (enabledTests.deposit) { context('when the depositAmount is not zero', function () { - const depositAmount = new BN(1) + const depositAmount = new BN(1); describe(describeTest + 'shouldDeposit', function () { - shouldDeposit(depositAmount, issuer) - }) - }) + shouldDeposit(depositAmount, issuer); + }); + }); } - }) - }) + }); + }); } module.exports = { - shouldBehaveLikeERC20SimpleSwap -}; \ No newline at end of file + shouldBehaveLikeERC20SimpleSwap, +}; diff --git a/test/ERC20SimpleSwap.should.js b/test/ERC20SimpleSwap.should.js index d8bb387..201ba00 100755 --- a/test/ERC20SimpleSwap.should.js +++ b/test/ERC20SimpleSwap.should.js @@ -1,30 +1,24 @@ -const { - BN, - balance, - time, - expectEvent, - expectRevert -} = require("@openzeppelin/test-helpers"); +const { BN, balance, time, expectEvent, expectRevert } = require('@openzeppelin/test-helpers'); // Import ethers from hardhat -const { ethers } = require("hardhat"); -const { use, expect } = require("chai"); -const { solidity } = require("@ethereum-waffle/chai"); +const { ethers } = require('hardhat'); +const { use, expect } = require('chai'); +const { solidity } = require('@ethereum-waffle/chai'); // Import additional utilities -const { signCheque, signCashOut, signCustomDecreaseTimeout } = require("./swutils"); +const { signCheque, signCashOut, signCustomDecreaseTimeout } = require('./swutils'); async function shouldDeploy(issuer, defaultHardDepositTimeout, from, value) { // Variables to hold contract instances let ERC20SimpleSwap, SimpleSwapFactory, TestToken; let erc20SimpleSwap, simpleSwapFactory, testToken; - const salt = "0x000000000000000000000000000000000000000000000000000000000000abcd"; + const salt = '0x000000000000000000000000000000000000000000000000000000000000abcd'; beforeEach(async function () { // Get the contract factories - TestToken = await ethers.getContractFactory("TestToken"); - SimpleSwapFactory = await ethers.getContractFactory("SimpleSwapFactory"); + TestToken = await ethers.getContractFactory('TestToken'); + SimpleSwapFactory = await ethers.getContractFactory('SimpleSwapFactory'); ERC20SimpleSwap = await ethers.getContractFactory('ERC20SimpleSwap'); // Deploy the contracts @@ -42,7 +36,7 @@ async function shouldDeploy(issuer, defaultHardDepositTimeout, from, value) { const receipt = await deployTx.wait(); // Assume your event provides the address of the deployed ERC20SimpleSwap contract - const event = receipt.events.find(e => e.event === 'SimpleSwapDeployed'); + const event = receipt.events.find((e) => e.event === 'SimpleSwapDeployed'); const erc20SimpleSwapAddress = event.args.contractAddress; erc20SimpleSwap = await ERC20SimpleSwap.attach(erc20SimpleSwapAddress); @@ -55,7 +49,7 @@ async function shouldDeploy(issuer, defaultHardDepositTimeout, from, value) { // Set postconditions for later use this.postconditions = { issuer: await erc20SimpleSwap.issuer(), - defaultHardDepositTimeout: await erc20SimpleSwap.defaultHardDepositTimeout() + defaultHardDepositTimeout: await erc20SimpleSwap.defaultHardDepositTimeout(), }; }); @@ -64,169 +58,180 @@ async function shouldDeploy(issuer, defaultHardDepositTimeout, from, value) { }); it('should set the issuer', function () { - expect(this.postconditions.issuer).to.be.equal(issuer) - }) + 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).bignumber.to.be.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()).bignumber.to.be.equal(expected); + }); } function shouldReturnPaidOut(beneficiary, expectedAmount) { beforeEach(async function () { - this.paidOut = await this.ERC20SimpleSwap.paidOut(beneficiary) - }) + this.paidOut = await this.ERC20SimpleSwap.paidOut(beneficiary); + }); it('should return the expected amount', function () { - expect(expectedAmount).bignumber.to.be.equal(this.paidOut) - }) + expect(expectedAmount).bignumber.to.be.equal(this.paidOut); + }); } function shouldReturnTotalPaidOut(expectedAmount) { beforeEach(async function () { - this.totalPaidOut = await this.ERC20SimpleSwap.totalPaidOut() - }) + this.totalPaidOut = await this.ERC20SimpleSwap.totalPaidOut(); + }); it('should return the expected amount', function () { - expect(expectedAmount).bignumber.to.be.equal(this.totalPaidOut) - }) + expect(expectedAmount).bignumber.to.be.equal(this.totalPaidOut); + }); } -function shouldReturnHardDeposits(beneficiary, expectedAmount, expectedDecreaseAmount, expectedDecreaseTimeout, expectedCanBeDecreasedAt) { +function shouldReturnHardDeposits( + beneficiary, + expectedAmount, + expectedDecreaseAmount, + expectedDecreaseTimeout, + expectedCanBeDecreasedAt +) { beforeEach(async function () { // If we expect this not to be the default value, we have to set the value here, as it depends on the most current time if (!expectedCanBeDecreasedAt.eq(new BN(0))) { - this.expectedCanBeDecreasedAt = (await time.latest()).add(await this.ERC20SimpleSwap.defaultHardDepositTimeout()) + this.expectedCanBeDecreasedAt = (await time.latest()).add(await this.ERC20SimpleSwap.defaultHardDepositTimeout()); } else { - this.expectedCanBeDecreasedAt = expectedCanBeDecreasedAt + this.expectedCanBeDecreasedAt = expectedCanBeDecreasedAt; } - this.exptectedCanBeDecreasedAt = (await time.latest()).add(await this.ERC20SimpleSwap.defaultHardDepositTimeout()) - this.hardDeposits = await this.ERC20SimpleSwap.hardDeposits(beneficiary) - }) + this.exptectedCanBeDecreasedAt = (await time.latest()).add(await this.ERC20SimpleSwap.defaultHardDepositTimeout()); + 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).bignumber.to.be.equal(this.hardDeposits.amount); + }); it('should return the expected decreaseAmount', function () { - expect(expectedDecreaseAmount).bignumber.to.be.equal(this.hardDeposits.decreaseAmount) - }) + expect(expectedDecreaseAmount).bignumber.to.be.equal(this.hardDeposits.decreaseAmount); + }); it('should return the expected timeout', function () { - expect(expectedDecreaseTimeout).bignumber.to.be.equal(this.hardDeposits.timeout) - }) + expect(expectedDecreaseTimeout).bignumber.to.be.equal(this.hardDeposits.timeout); + }); it('should return the exptected canBeDecreasedAt', function () { - expect(this.expectedCanBeDecreasedAt.toNumber()).to.be.closeTo(this.hardDeposits.canBeDecreasedAt.toNumber(), 5) - }) + expect(this.expectedCanBeDecreasedAt.toNumber()).to.be.closeTo(this.hardDeposits.canBeDecreasedAt.toNumber(), 5); + }); } function shouldReturnTotalHardDeposit(expectedTotalHardDeposit) { beforeEach(async function () { - this.totalHardDeposit = await this.ERC20SimpleSwap.totalHardDeposit() - }) + this.totalHardDeposit = await this.ERC20SimpleSwap.totalHardDeposit(); + }); it('should return the expectedTotalHardDeposit', function () { - expect(expectedTotalHardDeposit).bignumber.to.be.equal(this.totalHardDeposit) - }) + expect(expectedTotalHardDeposit).bignumber.to.be.equal(this.totalHardDeposit); + }); } function shouldReturnIssuer(expectedIssuer) { it('should return the expected issuer', async function () { - expect(await this.ERC20SimpleSwap.issuer()).to.be.equal(expectedIssuer) - }) - + expect(await this.ERC20SimpleSwap.issuer()).to.be.equal(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()).bignumber.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)).bignumber.to.equal(expectedLiquidBalanceFor); + }); } - function cashChequeInternal(beneficiary, recipient, cumulativePayout, callerPayout, from) { - beforeEach(async function () { - let requestPayout = cumulativePayout.sub(this.preconditions.paidOut) + let requestPayout = cumulativePayout.sub(this.preconditions.paidOut); //if the requested payout is less than the liquidBalance available for beneficiary if (requestPayout.lt(this.preconditions.liquidBalanceFor)) { // full amount requested can be paid out - this.totalPayout = requestPayout + this.totalPayout = requestPayout; } else { // partial amount requested can be paid out (the liquid balance available to the node) - this.totalPayout = this.preconditions.liquidBalanceFor + this.totalPayout = this.preconditions.liquidBalanceFor; } - this.totalPaidOut = this.preconditions.totalPaidOut + this.totalPayout - }) + this.totalPaidOut = this.preconditions.totalPaidOut + this.totalPayout; + }); it('should update the totalHardDeposit and hardDepositFor ', function () { - let expectedDecreaseHardDeposit + let expectedDecreaseHardDeposit; // if the hardDeposits can cover the totalPayout if (this.totalPayout.lt(this.preconditions.hardDepositFor.amount)) { // hardDeposit decreases by totalPayout - expectedDecreaseHardDeposit = this.totalPayout + expectedDecreaseHardDeposit = this.totalPayout; } else { // hardDeposit decreases by the full amount (and rest is from global liquid balance) - expectedDecreaseHardDeposit = this.preconditions.hardDepositFor.amount + expectedDecreaseHardDeposit = this.preconditions.hardDepositFor.amount; } // totalHarddeposit - expect(this.postconditions.totalHardDeposit).bignumber.to.be.equal(this.preconditions.totalHardDeposit.sub(expectedDecreaseHardDeposit)) + expect(this.postconditions.totalHardDeposit).bignumber.to.be.equal( + this.preconditions.totalHardDeposit.sub(expectedDecreaseHardDeposit) + ); // hardDepositFor - expect(this.postconditions.hardDepositFor.amount).bignumber.to.be.equal(this.preconditions.hardDepositFor.amount.sub(expectedDecreaseHardDeposit)) - }) + expect(this.postconditions.hardDepositFor.amount).bignumber.to.be.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).bignumber.to.be.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).bignumber.to.be.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(this.preconditions.recipientBalance.add(this.totalPayout.sub(callerPayout))) - }) + expect(this.postconditions.recipientBalance).bignumber.to.be.equal( + this.preconditions.recipientBalance.add(this.totalPayout.sub(callerPayout)) + ); + }); it('should transfer the correct amount to the caller', async function () { - let expectedAmountCaller + let expectedAmountCaller; if (recipient == from) { - expectedAmountCaller = this.totalPayout + expectedAmountCaller = this.totalPayout; } else { - expectedAmountCaller = callerPayout + expectedAmountCaller = callerPayout; } - expect(this.postconditions.callerBalance).bignumber.to.be.equal(this.preconditions.callerBalance.add(expectedAmountCaller)) - }) + expect(this.postconditions.callerBalance).bignumber.to.be.equal( + this.preconditions.callerBalance.add(expectedAmountCaller) + ); + }); it('should emit a ChequeCashed event', function () { - expectEvent.inLogs(this.logs, "ChequeCashed", { + expectEvent.inLogs(this.logs, 'ChequeCashed', { beneficiary, recipient: recipient, caller: from, totalPayout: this.totalPayout, cumulativePayout, callerPayout, - }) - }) + }); + }); it('should only emit a chequeBounced event when insufficient funds', function () { if (this.totalPayout.lt(cumulativePayout.sub(this.preconditions.paidOut))) { - expectEvent.inLogs(this.logs, "ChequeBounced", {}) + expectEvent.inLogs(this.logs, 'ChequeBounced', {}); } else { - const events = this.logs.filter(e => e.event === 'ChequeBounced'); - expect(events.length > 0).to.equal(false, `There is a ChequeBounced event`) + const events = this.logs.filter((e) => e.event === 'ChequeBounced'); + expect(events.length > 0).to.equal(false, `There is a ChequeBounced event`); } - }) + }); it('should only set the bounced field when insufficient funds', function () { if (this.totalPayout.lt(cumulativePayout.sub(this.preconditions.paidOut))) { - expect(this.postconditions.bounced).to.be.true + expect(this.postconditions.bounced).to.be.true; } else { - expect(this.postconditions.bounced).to.be.false + expect(this.postconditions.bounced).to.be.false; } - }) + }); } function shouldCashChequeBeneficiary(recipient, cumulativePayout, signee, from) { @@ -240,14 +245,16 @@ function shouldCashChequeBeneficiary(recipient, cumulativePayout, signee, from) liquidBalanceFor: await this.ERC20SimpleSwap.liquidBalanceFor(from), chequebookBalance: await this.ERC20SimpleSwap.balance(), paidOut: await this.ERC20SimpleSwap.paidOut(from), - totalPaidOut: await this.ERC20SimpleSwap.totalPaidOut() - } + totalPaidOut: await this.ERC20SimpleSwap.totalPaidOut(), + }; - const issuerSig = await signCheque(this.ERC20SimpleSwap, from, cumulativePayout, signee) + const issuerSig = await signCheque(this.ERC20SimpleSwap, from, cumulativePayout, signee); - const { logs, receipt } = await this.ERC20SimpleSwap.cashChequeBeneficiary(recipient, cumulativePayout, issuerSig, { from: from }) - this.logs = logs - this.receipt = receipt + const { logs, receipt } = await this.ERC20SimpleSwap.cashChequeBeneficiary(recipient, cumulativePayout, issuerSig, { + from: from, + }); + this.logs = logs; + this.receipt = receipt; this.postconditions = { callerBalance: await this.TestToken.balanceOf(from), @@ -259,29 +266,52 @@ function shouldCashChequeBeneficiary(recipient, cumulativePayout, signee, from) chequebookBalance: await this.ERC20SimpleSwap.balance(), paidOut: await this.ERC20SimpleSwap.paidOut(from), totalPaidOut: await this.ERC20SimpleSwap.totalPaidOut(), - bounced: await this.ERC20SimpleSwap.bounced() - } - }) - cashChequeInternal(from, recipient, cumulativePayout, new BN(0), from) + bounced: await this.ERC20SimpleSwap.bounced(), + }; + }); + cashChequeInternal(from, recipient, cumulativePayout, new BN(0), from); } -function shouldNotCashChequeBeneficiary(recipient, toSubmitCumulativePayout, toSignCumulativePayout, signee, from, value, revertMessage) { +function shouldNotCashChequeBeneficiary( + recipient, + toSubmitCumulativePayout, + toSignCumulativePayout, + signee, + from, + value, + revertMessage +) { beforeEach(async function () { - this.issuerSig = await signCheque(this.ERC20SimpleSwap, from, toSignCumulativePayout, signee) - }) + this.issuerSig = await signCheque(this.ERC20SimpleSwap, from, toSignCumulativePayout, signee); + }); it('reverts', async function () { - await expectRevert(this.ERC20SimpleSwap.cashChequeBeneficiary( - recipient, - toSubmitCumulativePayout, - this.issuerSig, - { from: from, value: value }), + await expectRevert( + this.ERC20SimpleSwap.cashChequeBeneficiary(recipient, toSubmitCumulativePayout, this.issuerSig, { + from: from, + value: value, + }), revertMessage - ) - }) + ); + }); } -function shouldCashCheque(beneficiary, recipient, cumulativePayout, callerPayout, from, beneficiarySignee, issuerSignee) { +function shouldCashCheque( + beneficiary, + recipient, + cumulativePayout, + callerPayout, + from, + beneficiarySignee, + issuerSignee +) { beforeEach(async function () { - const beneficiarySig = await signCashOut(this.ERC20SimpleSwap, from, cumulativePayout, recipient, callerPayout, beneficiarySignee) - const issuerSig = await signCheque(this.ERC20SimpleSwap, beneficiary, cumulativePayout, issuerSignee) + const beneficiarySig = await signCashOut( + this.ERC20SimpleSwap, + from, + cumulativePayout, + recipient, + callerPayout, + beneficiarySignee + ); + const issuerSig = await signCheque(this.ERC20SimpleSwap, beneficiary, cumulativePayout, issuerSignee); this.preconditions = { callerBalance: await this.TestToken.balanceOf(from), recipientBalance: await this.TestToken.balanceOf(recipient), @@ -291,11 +321,19 @@ function shouldCashCheque(beneficiary, recipient, cumulativePayout, callerPayout liquidBalanceFor: await this.ERC20SimpleSwap.liquidBalanceFor(beneficiary), chequebookBalance: await this.ERC20SimpleSwap.balance(), paidOut: await this.ERC20SimpleSwap.paidOut(beneficiary), - totalPaidOut: await this.ERC20SimpleSwap.totalPaidOut() - } - const { logs, receipt } = await this.ERC20SimpleSwap.cashCheque(beneficiary, recipient, cumulativePayout, beneficiarySig, callerPayout, issuerSig, { from: from }) - this.logs = logs - this.receipt = receipt + totalPaidOut: await this.ERC20SimpleSwap.totalPaidOut(), + }; + const { logs, receipt } = await this.ERC20SimpleSwap.cashCheque( + beneficiary, + recipient, + cumulativePayout, + beneficiarySig, + callerPayout, + issuerSig, + { from: from } + ); + this.logs = logs; + this.receipt = receipt; this.postconditions = { callerBalance: await this.TestToken.balanceOf(from), @@ -307,122 +345,147 @@ function shouldCashCheque(beneficiary, recipient, cumulativePayout, callerPayout chequebookBalance: await this.ERC20SimpleSwap.balance(), paidOut: await this.ERC20SimpleSwap.paidOut(beneficiary), totalPaidOut: await this.ERC20SimpleSwap.totalPaidOut(), - bounced: await this.ERC20SimpleSwap.bounced() - } - }) - cashChequeInternal(beneficiary, recipient, cumulativePayout, callerPayout, from) + bounced: await this.ERC20SimpleSwap.bounced(), + }; + }); + cashChequeInternal(beneficiary, recipient, cumulativePayout, callerPayout, from); } -function shouldNotCashCheque(beneficiaryToSign, issuerToSign, toSubmitFields, value, from, beneficiarySignee, issuerSignee, revertMessage) { +function shouldNotCashCheque( + beneficiaryToSign, + issuerToSign, + toSubmitFields, + value, + from, + beneficiarySignee, + issuerSignee, + revertMessage +) { beforeEach(async function () { - this.beneficiarySig = await signCashOut(this.ERC20SimpleSwap, from, beneficiaryToSign.cumulativePayout, beneficiaryToSign.recipient, beneficiaryToSign.callerPayout, beneficiarySignee) - this.issuerSig = await signCheque(this.ERC20SimpleSwap, issuerToSign.beneficiary, issuerToSign.cumulativePayout, issuerSignee) - }) + this.beneficiarySig = await signCashOut( + this.ERC20SimpleSwap, + from, + beneficiaryToSign.cumulativePayout, + beneficiaryToSign.recipient, + beneficiaryToSign.callerPayout, + beneficiarySignee + ); + this.issuerSig = await signCheque( + this.ERC20SimpleSwap, + issuerToSign.beneficiary, + issuerToSign.cumulativePayout, + issuerSignee + ); + }); it('reverts', async function () { - await expectRevert(this.ERC20SimpleSwap.cashCheque( - toSubmitFields.beneficiary, - toSubmitFields.recipient, - toSubmitFields.cumulativePayout, - this.beneficiarySig, - toSubmitFields.callerPayout, - this.issuerSig, - { from: from, value: value }), + await expectRevert( + this.ERC20SimpleSwap.cashCheque( + toSubmitFields.beneficiary, + toSubmitFields.recipient, + toSubmitFields.cumulativePayout, + this.beneficiarySig, + toSubmitFields.callerPayout, + this.issuerSig, + { from: from, value: value } + ), revertMessage - ) - }) + ); + }); } function shouldPrepareDecreaseHardDeposit(beneficiary, decreaseAmount, from) { beforeEach(async function () { this.preconditions = { - hardDepositFor: await this.ERC20SimpleSwap.hardDeposits(beneficiary) - } - const { logs } = await this.ERC20SimpleSwap.prepareDecreaseHardDeposit(beneficiary, decreaseAmount, { from: from }) - this.logs = logs + hardDepositFor: await this.ERC20SimpleSwap.hardDeposits(beneficiary), + }; + const { logs } = await this.ERC20SimpleSwap.prepareDecreaseHardDeposit(beneficiary, decreaseAmount, { from: from }); + this.logs = logs; this.postconditions = { - hardDepositFor: await this.ERC20SimpleSwap.hardDeposits(beneficiary) - } - }) + hardDepositFor: await this.ERC20SimpleSwap.hardDeposits(beneficiary), + }; + }); - it("should update the canBeDecreasedAt", async function () { - let expectedCanBeDecreasedAt - let personalDecreaseTimeout = (await this.ERC20SimpleSwap.hardDeposits(beneficiary)).timeout + it('should update the canBeDecreasedAt', async function () { + let expectedCanBeDecreasedAt; + let personalDecreaseTimeout = (await this.ERC20SimpleSwap.hardDeposits(beneficiary)).timeout; // if personalDecreaseTimeout is zero if (personalDecreaseTimeout.eq(new BN(0))) { // use the contract's default - expectedCanBeDecreasedAt = await this.ERC20SimpleSwap.defaultHardDepositTimeout() + expectedCanBeDecreasedAt = await this.ERC20SimpleSwap.defaultHardDepositTimeout(); } else { // use the value that was set - expectedCanBeDecreasedAt = personalDecreaseTimeout + expectedCanBeDecreasedAt = personalDecreaseTimeout; } - expect(this.postconditions.hardDepositFor.canBeDecreasedAt.toNumber()).to.be.closeTo(((await time.latest()).add(expectedCanBeDecreasedAt)).toNumber(), 5) - }) + expect(this.postconditions.hardDepositFor.canBeDecreasedAt.toNumber()).to.be.closeTo( + (await time.latest()).add(expectedCanBeDecreasedAt).toNumber(), + 5 + ); + }); it('should update the decreaseAmount', function () { - expect(this.postconditions.hardDepositFor.decreaseAmount).bignumber.to.be.equal(decreaseAmount) - }) + expect(this.postconditions.hardDepositFor.decreaseAmount).bignumber.to.be.equal(decreaseAmount); + }); it('should emit a HardDepositDecreasePrepared event', function () { expectEvent.inLogs(this.logs, 'HardDepositDecreasePrepared', { beneficiary, - decreaseAmount - }) - }) + decreaseAmount, + }); + }); } function shouldNotPrepareDecreaseHardDeposit(beneficiary, decreaseAmount, from, value, revertMessage) { it('reverts', async function () { - await expectRevert(this.ERC20SimpleSwap.prepareDecreaseHardDeposit( - beneficiary, - decreaseAmount, - { from: from, value: value }), + await expectRevert( + this.ERC20SimpleSwap.prepareDecreaseHardDeposit(beneficiary, decreaseAmount, { from: from, value: value }), revertMessage - ) - }) + ); + }); } function shouldDecreaseHardDeposit(beneficiary, from) { beforeEach(async function () { this.preconditions = { hardDeposit: await this.ERC20SimpleSwap.totalHardDeposit(), - hardDepositFor: await this.ERC20SimpleSwap.hardDeposits(beneficiary) - } + hardDepositFor: await this.ERC20SimpleSwap.hardDeposits(beneficiary), + }; - const { logs } = await this.ERC20SimpleSwap.decreaseHardDeposit(beneficiary, { from: from }) - this.logs = logs + const { logs } = await this.ERC20SimpleSwap.decreaseHardDeposit(beneficiary, { from: from }); + this.logs = logs; this.postconditions = { hardDeposit: await this.ERC20SimpleSwap.totalHardDeposit(), - hardDepositFor: await this.ERC20SimpleSwap.hardDeposits(beneficiary) - } - }) + hardDepositFor: await this.ERC20SimpleSwap.hardDeposits(beneficiary), + }; + }); it('decreases the hardDeposit amount for the beneficiary', function () { - expect(this.postconditions.hardDepositFor.amount).bignumber.to.be.equal(this.preconditions.hardDepositFor.amount.sub(this.preconditions.hardDepositFor.decreaseAmount)) - }) - + expect(this.postconditions.hardDepositFor.amount).bignumber.to.be.equal( + this.preconditions.hardDepositFor.amount.sub(this.preconditions.hardDepositFor.decreaseAmount) + ); + }); it('decreases the total hardDeposits', function () { - expect(this.postconditions.hardDeposit).bignumber.to.be.equal(this.preconditions.hardDeposit.sub(this.preconditions.hardDepositFor.decreaseAmount)) - }) + expect(this.postconditions.hardDeposit).bignumber.to.be.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).bignumber.to.be.equal(new BN(0)); + }); it('emits a hardDepositAmountChanged event', function () { expectEvent.inLogs(this.logs, 'HardDepositAmountChanged', { beneficiary, - amount: this.postconditions.hardDepositFor.amount - }) - }) + amount: this.postconditions.hardDepositFor.amount, + }); + }); } function shouldNotDecreaseHardDeposit(beneficiary, from, value, revertMessage) { it('reverts', async function () { - await expectRevert(this.ERC20SimpleSwap.decreaseHardDeposit( - beneficiary, - { from: from, value: value }), + await expectRevert( + this.ERC20SimpleSwap.decreaseHardDeposit(beneficiary, { from: from, value: value }), revertMessage - ) - }) - + ); + }); } function shouldIncreaseHardDeposit(beneficiary, amount, from) { beforeEach(async function () { @@ -432,132 +495,135 @@ function shouldIncreaseHardDeposit(beneficiary, amount, from) { liquidBalanceFor: await this.ERC20SimpleSwap.liquidBalanceFor(beneficiary), totalHardDeposit: await this.ERC20SimpleSwap.totalHardDeposit(), hardDepositFor: await this.ERC20SimpleSwap.hardDeposits(beneficiary), - } - const { logs } = await this.ERC20SimpleSwap.increaseHardDeposit(beneficiary, amount, { from: from }) - this.logs = logs + }; + const { logs } = await this.ERC20SimpleSwap.increaseHardDeposit(beneficiary, amount, { from: from }); + this.logs = logs; this.postconditions = { balance: await this.ERC20SimpleSwap.balance(), liquidBalance: await this.ERC20SimpleSwap.liquidBalance(), liquidBalanceFor: await this.ERC20SimpleSwap.liquidBalanceFor(beneficiary), totalHardDeposit: await this.ERC20SimpleSwap.totalHardDeposit(), - hardDepositFor: await this.ERC20SimpleSwap.hardDeposits(beneficiary) - } - }) + hardDepositFor: await this.ERC20SimpleSwap.hardDeposits(beneficiary), + }; + }); it('should decrease the liquidBalance', function () { - expect(this.postconditions.liquidBalance).bignumber.to.be.equal(this.preconditions.liquidBalance.sub(amount)) - }) + expect(this.postconditions.liquidBalance).bignumber.to.be.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).bignumber.to.be.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).bignumber.to.be.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).bignumber.to.be.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).bignumber.to.be.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).bignumber.to.be.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).bignumber.to.be.equal(new BN(0)); + }); it('emits a hardDepositAmountChanged event', function () { expectEvent.inLogs(this.logs, 'HardDepositAmountChanged', { beneficiary, - amount - }) - }) + amount, + }); + }); } function shouldNotIncreaseHardDeposit(beneficiary, amount, from, value, revertMessage) { it('reverts', async function () { - await expectRevert(this.ERC20SimpleSwap.increaseHardDeposit( - beneficiary, - amount, - { from: from, value: value }), + await expectRevert( + this.ERC20SimpleSwap.increaseHardDeposit(beneficiary, amount, { from: from, value: value }), revertMessage - ) - }) + ); + }); } function shouldSetCustomHardDepositTimeout(beneficiary, timeout, from) { beforeEach(async function () { - const beneficiarySig = await signCustomDecreaseTimeout(this.ERC20SimpleSwap, beneficiary, timeout, beneficiary) + const beneficiarySig = await signCustomDecreaseTimeout(this.ERC20SimpleSwap, beneficiary, timeout, beneficiary); - const { logs } = await this.ERC20SimpleSwap.setCustomHardDepositTimeout(beneficiary, timeout, beneficiarySig, { from: from }) - this.logs = logs + const { logs } = await this.ERC20SimpleSwap.setCustomHardDepositTimeout(beneficiary, timeout, beneficiarySig, { + from: from, + }); + this.logs = logs; this.postconditions = { - hardDepositFor: await this.ERC20SimpleSwap.hardDeposits(beneficiary) - } - }) + hardDepositFor: await this.ERC20SimpleSwap.hardDeposits(beneficiary), + }; + }); it('should have set the timeout', async function () { - expect(this.postconditions.hardDepositFor.timeout).bignumber.to.be.equal(timeout) - }) + expect(this.postconditions.hardDepositFor.timeout).bignumber.to.be.equal(timeout); + }); it('emits a HardDepositTimeoutChanged event', function () { expectEvent.inLogs(this.logs, 'HardDepositTimeoutChanged', { beneficiary, - timeout - }) - }) + timeout, + }); + }); } function shouldNotSetCustomHardDepositTimeout(toSubmit, toSign, signee, from, value, revertMessage) { beforeEach(async function () { - this.beneficiarySig = await signCustomDecreaseTimeout(this.ERC20SimpleSwap, toSign.beneficiary, toSign.timeout, signee) - }) + this.beneficiarySig = await signCustomDecreaseTimeout( + this.ERC20SimpleSwap, + toSign.beneficiary, + toSign.timeout, + signee + ); + }); it('reverts', async function () { - await expectRevert(this.ERC20SimpleSwap.setCustomHardDepositTimeout( - toSubmit.beneficiary, - toSubmit.timeout, - this.beneficiarySig, - { from: from, value: value }), + await expectRevert( + this.ERC20SimpleSwap.setCustomHardDepositTimeout(toSubmit.beneficiary, toSubmit.timeout, this.beneficiarySig, { + from: from, + value: value, + }), revertMessage - ) - }) + ); + }); } function shouldWithdraw(amount, from) { beforeEach(async function () { this.preconditions = { callerBalance: await this.TestToken.balanceOf(from), - liquidBalance: await this.ERC20SimpleSwap.liquidBalance() - } + liquidBalance: await this.ERC20SimpleSwap.liquidBalance(), + }; - await this.ERC20SimpleSwap.withdraw(amount, { from: from }) + await this.ERC20SimpleSwap.withdraw(amount, { from: from }); this.postconditions = { callerBalance: await this.TestToken.balanceOf(from), - liquidBalance: await this.ERC20SimpleSwap.liquidBalance() - } - }) + liquidBalance: await this.ERC20SimpleSwap.liquidBalance(), + }; + }); it('should have updated the liquidBalance', function () { - expect(this.postconditions.liquidBalance).bignumber.to.be.equal(this.preconditions.liquidBalance.sub(amount)) - }) + expect(this.postconditions.liquidBalance).bignumber.to.be.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).bignumber.to.be.equal(this.preconditions.callerBalance.add(amount)); + }); } function shouldNotWithdraw(amount, from, value, revertMessage) { it('reverts', async function () { - await expectRevert(this.ERC20SimpleSwap.withdraw( - amount, - { from: from, value: value }), - revertMessage - ) - }) + await expectRevert(this.ERC20SimpleSwap.withdraw(amount, { from: from, value: value }), revertMessage); + }); } function shouldDeposit(amount, from) { @@ -565,27 +631,27 @@ function shouldDeposit(amount, from) { this.preconditions = { balance: await this.ERC20SimpleSwap.balance(), totalHardDeposit: await this.ERC20SimpleSwap.totalHardDeposit(), - liquidBalance: await this.ERC20SimpleSwap.liquidBalance() - } - const { logs } = await this.TestToken.transfer(this.ERC20SimpleSwap.address, amount, { from: from }) - this.logs = logs - }) + liquidBalance: await this.ERC20SimpleSwap.liquidBalance(), + }; + const { logs } = await this.TestToken.transfer(this.ERC20SimpleSwap.address, amount, { from: 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()).bignumber.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()).bignumber.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()).bignumber.to.equal(this.preconditions.totalHardDeposit); + }); it('should emit a transfer event', async function () { - expectEvent.inLogs(this.logs, "Transfer", { + expectEvent.inLogs(this.logs, 'Transfer', { from: from, to: this.ERC20SimpleSwap.address, - value: amount - }) - }) + value: amount, + }); + }); } module.exports = { shouldDeploy, @@ -612,5 +678,4 @@ module.exports = { shouldWithdraw, shouldNotWithdraw, shouldDeposit, -} - +}; diff --git a/test/ERC20SimpleSwap.test.js b/test/ERC20SimpleSwap.test.js index 76dbe6f..aa64cdb 100755 --- a/test/ERC20SimpleSwap.test.js +++ b/test/ERC20SimpleSwap.test.js @@ -4,22 +4,22 @@ const { BigNumber } = ethers; const { shouldBehaveLikeERC20SimpleSwap } = require('./ERC20SimpleSwap.behavior'); const { shouldDeploy } = require('./ERC20SimpleSwap.should'); -describe('ERC20SimpleSwap', function() { +describe('ERC20SimpleSwap', function () { let issuer, alice, bob, carol; - before(async function() { + before(async function () { // Get signer accounts from Hardhat's ethers provider [issuer, alice, bob, carol] = await ethers.getSigners(); }); - describe("when we don't deposit while deploying", function() { + describe("when we don't deposit while deploying", function () { const defaultHardDepositTimeout = BigNumber.from(86400); const value = BigNumber.from(0); shouldDeploy(issuer, defaultHardDepositTimeout, issuer, value); shouldBehaveLikeERC20SimpleSwap([issuer, alice, bob, carol], defaultHardDepositTimeout); }); - describe('when we deposit while deploying', function() { + describe('when we deposit while deploying', function () { const defaultHardDepositTimeout = BigNumber.from(86400); const value = BigNumber.from(50); shouldDeploy(issuer, defaultHardDepositTimeout, issuer, value); diff --git a/test/SimpleSwapFactory.test.js b/test/SimpleSwapFactory.test.ts similarity index 80% rename from test/SimpleSwapFactory.test.js rename to test/SimpleSwapFactory.test.ts index cbc1b12..5d6b85e 100755 --- a/test/SimpleSwapFactory.test.js +++ b/test/SimpleSwapFactory.test.ts @@ -3,7 +3,7 @@ const { ethers } = require('hardhat'); const { constants } = require('@openzeppelin/test-helpers'); describe('SimpleSwapFactory', function () { - const salt = "0x000000000000000000000000000000000000000000000000000000000000abcd"; + const salt = '0x000000000000000000000000000000000000000000000000000000000000abcd'; let issuer, other; let simpleSwapFactory, testToken; @@ -13,6 +13,7 @@ describe('SimpleSwapFactory', function () { function shouldDeployERC20SimpleSwap(DEFAULT_HARDDEPOSIT_DECREASE_TIMEOUT, value) { beforeEach(async function () { + let deployer, other; [deployer, other] = await ethers.getSigners(); const TestToken = await ethers.getContractFactory('TestToken'); testToken = await TestToken.deploy(); @@ -22,9 +23,9 @@ describe('SimpleSwapFactory', function () { simpleSwapFactory = await SimpleSwapFactory.deploy(testToken.address); await simpleSwapFactory.deployed(); - const tx = await simpleSwapFactory.deploySimpleSwap(issuer.address, DEFAULT_HARDDEPOSIT_DECREASE_TIMEOUT, salt); + const tx = await simpleSwapFactory.deploySimpleSwap(deployer.address, DEFAULT_HARDDEPOSIT_DECREASE_TIMEOUT, salt); const receipt = await tx.wait(); - const event = receipt.events.find(e => e.event === 'SimpleSwapDeployed'); + const event = receipt.events.find((e) => e.event === 'SimpleSwapDeployed'); this.ERC20SimpleSwapAddress = event.args.contractAddress; const ERC20SimpleSwap = await ethers.getContractFactory('ERC20SimpleSwap'); @@ -37,7 +38,9 @@ describe('SimpleSwapFactory', function () { }); it('should allow other addresses to deploy with same salt', async function () { - await simpleSwapFactory.connect(other).deploySimpleSwap(issuer.address, DEFAULT_HARDDEPOSIT_DECREASE_TIMEOUT, salt); + await simpleSwapFactory + .connect(other) + .deploySimpleSwap(issuer.address, DEFAULT_HARDDEPOSIT_DECREASE_TIMEOUT, salt); }); it('should deploy with the right issuer', async function () { @@ -68,11 +71,11 @@ describe('SimpleSwapFactory', function () { shouldDeployERC20SimpleSwap(86400, ethers.constants.Zero); }); - describe("when we deposit while deploying SimpleSwap", function () { + describe('when we deposit while deploying SimpleSwap', function () { shouldDeployERC20SimpleSwap(86400, ethers.utils.parseUnits('10', 18)); }); - describe("when we deposit while issuer 0", function () { + describe('when we deposit while issuer 0', function () { it('should fail', async function () { const TestToken = await ethers.getContractFactory('TestToken'); testToken = await TestToken.deploy(); @@ -82,9 +85,9 @@ describe('SimpleSwapFactory', function () { simpleSwapFactory = await SimpleSwapFactory.deploy(testToken.address); await simpleSwapFactory.deployed(); - await expect( - simpleSwapFactory.deploySimpleSwap(constants.ZERO_ADDRESS, 86400, salt) - ).to.be.revertedWith('invalid issuer'); + await expect(simpleSwapFactory.deploySimpleSwap(constants.ZERO_ADDRESS, 86400, salt)).to.be.revertedWith( + 'invalid issuer' + ); }); }); }); diff --git a/test/swutils.js b/test/swutils.js index 2e6fbae..afc56fb 100755 --- a/test/swutils.js +++ b/test/swutils.js @@ -1,48 +1,50 @@ -const abi = require('ethereumjs-abi') +const abi = require('ethereumjs-abi'); const EIP712Domain = [ { name: 'name', type: 'string' }, { name: 'version', type: 'string' }, - { name: 'chainId', type: 'uint256' } -] + { name: 'chainId', type: 'uint256' }, +]; const ChequeType = [ { name: 'chequebook', type: 'address' }, { name: 'beneficiary', type: 'address' }, - { name: 'cumulativePayout', type: 'uint256' } -] + { name: 'cumulativePayout', type: 'uint256' }, +]; const CashoutType = [ { name: 'chequebook', type: 'address' }, { name: 'sender', type: 'address' }, { name: 'requestPayout', type: 'uint256' }, { name: 'recipient', type: 'address' }, - { name: 'callerPayout', type: 'uint256' } -] + { name: 'callerPayout', type: 'uint256' }, +]; const CustomDecreaseTimeoutType = [ { name: 'chequebook', type: 'address' }, { name: 'beneficiary', type: 'address' }, - { name: 'decreaseTimeout', type: 'uint256' } -] + { name: 'decreaseTimeout', type: 'uint256' }, +]; async function sign(hash, signer) { - let signature = await web3.eth.sign(hash, signer) + let signature = await web3.eth.sign(hash, signer); - let rs = signature.substr(0,130); - let v = parseInt(signature.substr(130, 2), 16) + 27 + let rs = signature.substr(0, 130); + let v = parseInt(signature.substr(130, 2), 16) + 27; - return rs + v.toString(16) + return rs + v.toString(16); } function signTypedData(eip712data, signee) { - return new Promise((resolve, reject) => - web3.currentProvider.send({ - method: 'eth_signTypedData_v4', - params: [signee, eip712data] - }, - (err, result) => err == null ? resolve(result.result) : reject(err)) - ) + return new Promise((resolve, reject) => + web3.currentProvider.send( + { + method: 'eth_signTypedData_v4', + params: [signee, eip712data], + }, + (err, result) => (err == null ? resolve(result.result) : reject(err)) + ) + ); } // the chainId is set to 31337 which is the hardhat default @@ -50,36 +52,36 @@ async function signCheque(swap, beneficiary, cumulativePayout, signee, chainId = const cheque = { chequebook: swap.address, beneficiary, - cumulativePayout: cumulativePayout.toNumber() - } + cumulativePayout: cumulativePayout.toNumber(), + }; const eip712data = { types: { EIP712Domain, - Cheque: ChequeType + Cheque: ChequeType, }, domain: { - name: "Chequebook", - version: "1.0", - chainId + name: 'Chequebook', + version: '1.0', + chainId, }, primaryType: 'Cheque', - message: cheque - } + message: cheque, + }; - return signTypedData(eip712data, signee) + return signTypedData(eip712data, signee); } async function signCashOut(swap, sender, cumulativePayout, beneficiaryAgent, callerPayout, signee, chainId = 31337) { const eip712data = { types: { EIP712Domain, - Cashout: CashoutType + Cashout: CashoutType, }, domain: { - name: "Chequebook", - version: "1.0", - chainId + name: 'Chequebook', + version: '1.0', + chainId, }, primaryType: 'Cashout', message: { @@ -87,37 +89,37 @@ async function signCashOut(swap, sender, cumulativePayout, beneficiaryAgent, cal sender, requestPayout: cumulativePayout.toNumber(), recipient: beneficiaryAgent, - callerPayout: callerPayout.toNumber() - } - } + callerPayout: callerPayout.toNumber(), + }, + }; - return signTypedData(eip712data, signee) + return signTypedData(eip712data, signee); } async function signCustomDecreaseTimeout(swap, beneficiary, decreaseTimeout, signee, chainId = 31337) { const eip712data = { types: { EIP712Domain, - CustomDecreaseTimeout: CustomDecreaseTimeoutType + CustomDecreaseTimeout: CustomDecreaseTimeoutType, }, domain: { - name: "Chequebook", - version: "1.0", - chainId + name: 'Chequebook', + version: '1.0', + chainId, }, primaryType: 'CustomDecreaseTimeout', message: { chequebook: swap.address, beneficiary, - decreaseTimeout: decreaseTimeout.toNumber() - } - } - return signTypedData(eip712data, signee) + decreaseTimeout: decreaseTimeout.toNumber(), + }, + }; + return signTypedData(eip712data, signee); } module.exports = { signCustomDecreaseTimeout, signCashOut, signCheque, - sign + sign, }; diff --git a/test/testutils.js b/test/testutils.js index 23d57f4..e028c5a 100755 --- a/test/testutils.js +++ b/test/testutils.js @@ -4,5 +4,5 @@ async function computeCost(receipt) { } module.exports = { - computeCost + computeCost, }; diff --git a/yarn.lock b/yarn.lock index acb065d..455f0c9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1025,6 +1025,11 @@ web3 "^1.2.5" web3-utils "^1.2.5" +"@pkgr/core@^0.1.0": + version "0.1.1" + resolved "https://registry.yarnpkg.com/@pkgr/core/-/core-0.1.1.tgz#1ec17e2edbec25c8306d424ecfbf13c7de1aaa31" + integrity sha512-cq8o4cWH0ibXh9VGi5P20Tu9XF/0fFXl9EUinr9QfTM7a7p0oTA4iJRCQWppXR1Pg8dSM0UCItCkPwsk9qWWYA== + "@pnpm/config.env-replace@^1.1.0": version "1.1.0" resolved "https://registry.yarnpkg.com/@pnpm/config.env-replace/-/config.env-replace-1.1.0.tgz#ab29da53df41e8948a00f2433f085f54de8b3a4c" @@ -3324,17 +3329,18 @@ escodegen@1.8.x: optionalDependencies: source-map "~0.2.0" -eslint-config-prettier@^8.7.0: - version "8.10.0" - resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-8.10.0.tgz#3a06a662130807e2502fc3ff8b4143d8a0658e11" - integrity sha512-SM8AMJdeQqRYT9O9zguiruQZaN7+z+E4eAP9oiLNGKMtomwaB1E9dcgUD6ZAn/eQAb52USbvezbiljfZUhbJcg== +eslint-config-prettier@^9.1.0: + version "9.1.0" + resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-9.1.0.tgz#31af3d94578645966c082fcb71a5846d3c94867f" + integrity sha512-NSWl5BFQWEPi1j4TjVNItzYV7dZXZ+wP6I6ZhrBGpChQhZRUaElihE9uRRkcbRnNb76UMKDF3r+WTmNcGPKsqw== -eslint-plugin-prettier@^4.2.1: - version "4.2.1" - resolved "https://registry.yarnpkg.com/eslint-plugin-prettier/-/eslint-plugin-prettier-4.2.1.tgz#651cbb88b1dab98bfd42f017a12fa6b2d993f94b" - integrity sha512-f/0rXLXUt0oFYs8ra4w49wYZBG5GKZpAYsJSm6rnYL5uVDjd+zowwMwVZHnAjf4edNrKpCDYfXDgmRE/Ak7QyQ== +eslint-plugin-prettier@^5.1.3: + version "5.1.3" + resolved "https://registry.yarnpkg.com/eslint-plugin-prettier/-/eslint-plugin-prettier-5.1.3.tgz#17cfade9e732cef32b5f5be53bd4e07afd8e67e1" + integrity sha512-C9GCVAs4Eq7ZC/XFQHITLiHJxQngdtraXaM+LoUFoFp/lHNl2Zn8f3WQbe9HvTBBQ9YnKFB0/2Ajdqwo5D1EAw== dependencies: prettier-linter-helpers "^1.0.0" + synckit "^0.8.6" eslint-scope@^5.1.1: version "5.1.1" @@ -7418,6 +7424,14 @@ sync-rpc@^1.2.1: dependencies: get-port "^3.1.0" +synckit@^0.8.6: + version "0.8.8" + resolved "https://registry.yarnpkg.com/synckit/-/synckit-0.8.8.tgz#fe7fe446518e3d3d49f5e429f443cf08b6edfcd7" + integrity sha512-HwOKAP7Wc5aRGYdKH+dw0PRRpbO841v2DENBtjnR5HFWoiNByAl7vrx3p0G/rCyYXQsrxqtX48TImFtPcIHSpQ== + dependencies: + "@pkgr/core" "^0.1.0" + tslib "^2.6.2" + table-layout@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/table-layout/-/table-layout-1.0.2.tgz#c4038a1853b0136d63365a734b6931cf4fad4a04" @@ -7578,6 +7592,11 @@ tslib@^1.8.1, tslib@^1.9.3: resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00" integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg== +tslib@^2.6.2: + version "2.6.2" + resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.6.2.tgz#703ac29425e7b37cd6fd456e92404d46d1f3e4ae" + integrity sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q== + tslog@^4.3.1, tslog@^4.4.0: version "4.9.2" resolved "https://registry.yarnpkg.com/tslog/-/tslog-4.9.2.tgz#35de3a073784dfe3849caeaa028010c7a62b7f4a"