Skip to content

Commit

Permalink
Consolidate into utils; update Indexing/ERC20 protocols (#1276)
Browse files Browse the repository at this point in the history
* consolidate constants, merkle, types into utils; update protocols

* merkle naming; function ordering; filename casing
  • Loading branch information
dmosites authored Jan 28, 2024
1 parent dabfb1a commit beb2a45
Show file tree
Hide file tree
Showing 85 changed files with 455 additions and 837 deletions.
2 changes: 1 addition & 1 deletion hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {
apiUrls,
explorerUrls,
explorerApiUrls,
} = require('@airswap/constants')
} = require('@airswap/utils')

/**
* @type import('hardhat/config').HardhatUserConfig
Expand Down
4 changes: 2 additions & 2 deletions scripts/owners-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const {
chainNames,
ownerAddresses,
apiUrls,
} = require('@airswap/constants')
const { ADDRESS_ZERO } = require('@airswap/constants')
} = require('@airswap/utils')
const { ADDRESS_ZERO } = require('@airswap/utils')

const contracts = [
['pool', 'Pool'],
Expand Down
2 changes: 1 addition & 1 deletion scripts/owners-update.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Confirm = require('prompt-confirm')
const { ethers } = require('hardhat')
const { chainNames, ChainIds, ownerAddresses } = require('@airswap/constants')
const { chainNames, ChainIds, ownerAddresses } = require('@airswap/utils')
const { getReceiptUrl } = require('@airswap/utils')

const CONFIRMATIONS = 2
Expand Down
4 changes: 2 additions & 2 deletions scripts/receivers-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const {
chainNames,
apiUrls,
protocolFeeReceiverAddresses,
} = require('@airswap/constants')
} = require('@airswap/utils')
const poolDeploys = require('@airswap/pool/deploys.js')
const { ADDRESS_ZERO } = require('@airswap/constants')
const { ADDRESS_ZERO } = require('@airswap/utils')

const contracts = [
['swap', 'Swap'],
Expand Down
3 changes: 1 addition & 2 deletions source/batch-call/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
"@airswap/swap-erc20": "4.2.0"
},
"devDependencies": {
"@airswap/constants": "4.2.0",
"@airswap/utils": "4.2.0",
"@airswap/utils": "4.2.1",
"prompt-confirm": "^2.0.4"
},
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion source/batch-call/scripts/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs')
const prettier = require('prettier')
const Confirm = require('prompt-confirm')
const { ethers, run } = require('hardhat')
const { ChainIds, chainNames, chainLabels } = require('@airswap/constants')
const { ChainIds, chainNames, chainLabels } = require('@airswap/utils')
const { getReceiptUrl } = require('@airswap/utils')
const batchCallDeploys = require('../deploys.js')
const batchCallBlocks = require('../deploys-blocks.js')
Expand Down
2 changes: 1 addition & 1 deletion source/batch-call/scripts/verify.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */
const { ethers, run } = require('hardhat')
const { chainNames } = require('@airswap/constants')
const { chainNames } = require('@airswap/utils')
const batchCallDeploys = require('../deploys.js')

async function main() {
Expand Down
2 changes: 1 addition & 1 deletion source/batch-call/test/BatchCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const {
createOrderERC20,
createOrderERC20Signature,
} = require('@airswap/utils')
const { TokenKinds } = require('@airswap/constants')
const { TokenKinds } = require('@airswap/utils')

const CHAIN_ID = 31337
const PROTOCOL_FEE = '30'
Expand Down
4 changes: 1 addition & 3 deletions source/pool/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@
"@openzeppelin/contracts": "^4.8.3"
},
"devDependencies": {
"@airswap/constants": "4.2.0",
"@airswap/metadata": "4.1.17",
"@airswap/types": "4.2.0",
"@airswap/utils": "4.2.0",
"@airswap/utils": "4.2.1",
"prompt-confirm": "^2.0.4"
},
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion source/pool/scripts/balances.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { ethers } = require('hardhat')
const { getKnownTokens } = require('@airswap/metadata')
const { chainNames, ChainIds } = require('@airswap/constants')
const { chainNames, ChainIds } = require('@airswap/utils')
const BatchCall = require('@airswap/batch-call/build/contracts/BatchCall.sol/BatchCall.json')
const batchCallDeploys = require('@airswap/batch-call/deploys.js')
const poolDeploys = require('../deploys.js')
Expand Down
2 changes: 1 addition & 1 deletion source/pool/scripts/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs')
const prettier = require('prettier')
const Confirm = require('prompt-confirm')
const { ethers, run } = require('hardhat')
const { chainLabels, chainNames, ChainIds } = require('@airswap/constants')
const { chainLabels, chainNames, ChainIds } = require('@airswap/utils')
const { getReceiptUrl } = require('@airswap/utils')
const poolDeploys = require('../deploys.js')
const poolBlocks = require('../deploys-blocks.js')
Expand Down
2 changes: 1 addition & 1 deletion source/pool/scripts/migrate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Confirm = require('prompt-confirm')
const { ethers } = require('hardhat')
const { chainNames, ChainIds } = require('@airswap/constants')
const { chainNames, ChainIds } = require('@airswap/utils')
const { getReceiptUrl } = require('@airswap/utils')

const { Pool__factory } = require('../typechain/factories/contracts')
Expand Down
2 changes: 1 addition & 1 deletion source/pool/scripts/verify.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console */
const { ethers, run } = require('hardhat')
const poolDeploys = require('../deploys.js')
const { chainNames } = require('@airswap/constants')
const { chainNames } = require('@airswap/utils')

async function main() {
await run('compile')
Expand Down
78 changes: 47 additions & 31 deletions source/pool/test/Pool.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const { expect } = require('chai')
const { toAtomicString } = require('@airswap/utils')
const { generateTreeFromData, getRoot, getProof } = require('@airswap/merkle')
const {
toAtomicString,
generateMerkleTreeFromData,
getMerkleRoot,
getMerkleProof,
} = require('@airswap/utils')
const { soliditySha3 } = require('web3-utils')

const { ethers, waffle } = require('hardhat')
const { deployMockContract } = waffle
const IERC20 = require('@openzeppelin/contracts/build/contracts/IERC20.json')
const STAKING = require('@airswap/staking/build/contracts/Staking.sol/Staking.json')
const { ADDRESS_ZERO } = require('@airswap/constants')
const { ADDRESS_ZERO } = require('@airswap/utils')

function toWei(value, places) {
return toAtomicString(value, places || 18)
Expand Down Expand Up @@ -71,13 +75,13 @@ describe('Pool Unit', () => {
).deploy(CLAIM_SCALE, CLAIM_MAX)
await pool.deployed()

tree = generateTreeFromData({
tree = generateMerkleTreeFromData({
[alice.address]: ALICE_SCORE,
[bob.address]: BOB_SCORE,
[carol.address]: CAROL_SCORE,
})

newTree = generateTreeFromData({
newTree = generateMerkleTreeFromData({
[alice.address]: ALICE_SCORE,
[bob.address]: BOB_NEW_SCORE,
[carol.address]: CAROL_SCORE,
Expand Down Expand Up @@ -118,7 +122,7 @@ describe('Pool Unit', () => {

describe('admin functions', async () => {
it('enable a claim for a merkle root suceeds', async () => {
const root = getRoot(tree)
const root = getMerkleRoot(tree)
await pool.setAdmin(alice.address)
expect(await pool.connect(alice).enable(TREE, root)).to.emit(
pool,
Expand All @@ -127,17 +131,17 @@ describe('Pool Unit', () => {
})

it('enable a claim for a merkle root fails when not admin', async () => {
const root = getRoot(tree)
const root = getMerkleRoot(tree)
await expect(pool.connect(bob).enable(TREE, root)).to.be.revertedWith(
'Unauthorized'
)
})

it('enable a with the same tree overrwrites the previous root', async () => {
const root = getRoot(tree)
const root = getMerkleRoot(tree)
await pool.setAdmin(alice.address)
await pool.connect(alice).enable(TREE, root)
const newRoot = getRoot(newTree)
const newRoot = getMerkleRoot(newTree)
await expect(pool.connect(alice).enable(TREE, newRoot)).to.be.emit(
pool,
`Enable`
Expand All @@ -151,9 +155,9 @@ describe('Pool Unit', () => {
await feeToken.mock.transfer.returns(true)

await pool.setAdmin(alice.address)
const root = getRoot(tree)
const root = getMerkleRoot(tree)
await pool.connect(alice).enable(TREE, root)
const proof = getProof(tree, soliditySha3(bob.address, BOB_SCORE))
const proof = getMerkleProof(tree, soliditySha3(bob.address, BOB_SCORE))

await expect(
pool.connect(bob).withdraw(
Expand All @@ -179,7 +183,7 @@ describe('Pool Unit', () => {
await feeToken.mock.transfer.returns(true)

await pool.setAdmin(alice.address)
const root = getRoot(tree)
const root = getMerkleRoot(tree)
await pool.connect(alice).enable(TREE, root)

await expect(
Expand All @@ -197,7 +201,7 @@ describe('Pool Unit', () => {
await feeToken.mock.transfer.returns(true)

await pool.setAdmin(alice.address)
const proof = getProof(tree, soliditySha3(bob.address, BOB_SCORE))
const proof = getMerkleProof(tree, soliditySha3(bob.address, BOB_SCORE))

await expect(
pool.connect(bob).withdraw(
Expand Down Expand Up @@ -225,9 +229,9 @@ describe('Pool Unit', () => {
await feeToken.mock.transfer.returns(true)

await pool.setAdmin(alice.address)
const root = getRoot(tree)
const root = getMerkleRoot(tree)
await pool.connect(alice).enable(TREE, root)
const proof = getProof(tree, soliditySha3(bob.address, BOB_SCORE))
const proof = getMerkleProof(tree, soliditySha3(bob.address, BOB_SCORE))

await expect(
pool.connect(bob).withdraw(
Expand Down Expand Up @@ -267,9 +271,9 @@ describe('Pool Unit', () => {
score = 0

await pool.setAdmin(alice.address)
const root = getRoot(tree)
const root = getMerkleRoot(tree)
await pool.connect(alice).enable(TREE, root)
const proof = getProof(tree, soliditySha3(bob.address, BOB_SCORE))
const proof = getMerkleProof(tree, soliditySha3(bob.address, BOB_SCORE))

await expect(
pool.connect(bob).withdraw(
Expand All @@ -296,9 +300,12 @@ describe('Pool Unit', () => {
await feeToken.mock.balanceOf.returns('100000')
;(await feeToken.mock.transfer.returns(true)) -
(await pool.setAdmin(alice.address))
const root = getRoot(tree)
const root = getMerkleRoot(tree)
await pool.connect(alice).enable(TREE, root)
const proof = getProof(tree, soliditySha3(alice.address, ALICE_SCORE))
const proof = getMerkleProof(
tree,
soliditySha3(alice.address, ALICE_SCORE)
)
const withdrawMinimum = 496

const amount = await pool
Expand Down Expand Up @@ -331,11 +338,11 @@ describe('Pool Unit', () => {
await feeToken.mock.transfer.returns(true)

await pool.setAdmin(alice.address)
const root = getRoot(tree)
const newRoot = getRoot(newTree)
const root = getMerkleRoot(tree)
const newRoot = getMerkleRoot(newTree)
await pool.connect(alice).enable(TREE, root)
await pool.connect(alice).enable(NEW_TREE, newRoot)
const proof = getProof(tree, soliditySha3(bob.address, BOB_SCORE))
const proof = getMerkleProof(tree, soliditySha3(bob.address, BOB_SCORE))

await pool.connect(bob).withdraw(
[
Expand Down Expand Up @@ -368,29 +375,38 @@ describe('Pool Unit', () => {
describe('Verify', async () => {
it('verification is valid', async () => {
await pool.setAdmin(alice.address)
const root = getRoot(tree)
const root = getMerkleRoot(tree)
await pool.connect(alice).enable(TREE, root)
const proof = getProof(tree, soliditySha3(alice.address, ALICE_SCORE))
const proof = getMerkleProof(
tree,
soliditySha3(alice.address, ALICE_SCORE)
)

const isValid = await pool.verify(alice.address, root, ALICE_SCORE, proof)
expect(isValid).to.be.equal(true)
})

it('verification is invalid with wrong participant', async () => {
await pool.setAdmin(alice.address)
const root = getRoot(tree)
const root = getMerkleRoot(tree)
await pool.connect(alice).enable(TREE, root)
const proof = getProof(tree, soliditySha3(alice.address, ALICE_SCORE))
const proof = getMerkleProof(
tree,
soliditySha3(alice.address, ALICE_SCORE)
)

const isValid = await pool.verify(bob.address, root, ALICE_SCORE, proof)
expect(isValid).to.be.equal(false)
})

it('verification is invalid with wrong scroe', async () => {
await pool.setAdmin(alice.address)
const root = getRoot(tree)
const root = getMerkleRoot(tree)
await pool.connect(alice).enable(TREE, root)
const proof = getProof(tree, soliditySha3(alice.address, ALICE_SCORE))
const proof = getMerkleProof(
tree,
soliditySha3(alice.address, ALICE_SCORE)
)

const isValid = await pool.verify(alice.address, root, BOB_SCORE, proof)
expect(isValid).to.be.equal(false)
Expand Down Expand Up @@ -484,10 +500,10 @@ describe('Pool Unit', () => {
await feeToken.mock.balanceOf.returns('100000')
await await pool.connect(deployer).setAdmin(alice.address)

const root = getRoot(tree)
const root = getMerkleRoot(tree)
await pool.connect(alice).enableAndSetClaimed(TREE, root, [bob.address])

const proof = getProof(tree, soliditySha3(bob.address, BOB_SCORE))
const proof = getMerkleProof(tree, soliditySha3(bob.address, BOB_SCORE))
await expect(
pool.connect(bob).withdraw(
[
Expand All @@ -507,7 +523,7 @@ describe('Pool Unit', () => {
it('set claimed with non-owner reverts', async () => {
await feeToken.mock.balanceOf.returns('100000')

const root = getRoot(tree)
const root = getMerkleRoot(tree)
await expect(
pool.connect(bob).enableAndSetClaimed(TREE, root, [bob.address])
).to.be.revertedWith('Unauthorized')
Expand Down
14 changes: 9 additions & 5 deletions source/pool/test/PoolIntegration.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const { expect } = require('chai')
const { toAtomicString } = require('@airswap/utils')
const { generateTreeFromData, getRoot, getProof } = require('@airswap/merkle')
const {
toAtomicString,
generateMerkleTreeFromData,
getMerkleRoot,
getMerkleProof,
} = require('@airswap/utils')
const { soliditySha3 } = require('web3-utils')

const { ethers } = require('hardhat')
Expand Down Expand Up @@ -73,7 +77,7 @@ describe('Pool Integration', () => {

await pool.setAdmin(deployer.address)

tree = generateTreeFromData({
tree = generateMerkleTreeFromData({
[alice.address]: ALICE_SCORE,
[bob.address]: BOB_SCORE,
[carol.address]: CAROL_SCORE,
Expand All @@ -82,12 +86,12 @@ describe('Pool Integration', () => {

describe('withdraw increase the staker balance', async () => {
it('transfers the claimed funds to the staker', async () => {
const root = getRoot(tree)
const root = getMerkleRoot(tree)
expect(await pool.connect(deployer).enable(TREE_ID, root)).to.emit(
pool,
'Enable'
)
const proof = getProof(tree, soliditySha3(bob.address, BOB_SCORE))
const proof = getMerkleProof(tree, soliditySha3(bob.address, BOB_SCORE))
await pool.connect(bob).withdraw(
[
{
Expand Down
3 changes: 1 addition & 2 deletions source/registry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
"access": "public"
},
"devDependencies": {
"@airswap/constants": "4.2.0",
"@airswap/utils": "4.2.0",
"@airswap/utils": "4.2.1",
"prompt-confirm": "^2.0.4"
}
}
2 changes: 1 addition & 1 deletion source/registry/scripts/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {
ChainIds,
stakingTokenAddresses,
ADDRESS_ZERO,
} = require('@airswap/constants')
} = require('@airswap/utils')
const wrappedTokenAddresses = require('@airswap/wrapper/deploys-weth')

module.exports = {
Expand Down
Loading

0 comments on commit beb2a45

Please sign in to comment.