Skip to content

Commit

Permalink
fix filtering for getServerURLs; add test file for Registry
Browse files Browse the repository at this point in the history
  • Loading branch information
dmosites committed Feb 15, 2024
1 parent f02a764 commit 7a0c9cf
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 19 deletions.
43 changes: 24 additions & 19 deletions tools/libraries/src/Registry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ethers } from 'ethers'
import { ServerOptions } from '@airswap/utils'
import { Registry__factory } from '@airswap/registry/typechain/factories/contracts'
import registryDeploys from '@airswap/registry/deploys.js'
import registryBlocks from '@airswap/registry/deploys-blocks.js'
Expand All @@ -16,23 +15,29 @@ class ServerRegistry extends Contract {
chainId: number,
protocol: string,
baseToken?: string,
quoteToken?: string
quoteToken?: string,
address = registryDeploys[chainId]
): Promise<string[]> {
const contract = Registry__factory.connect(
registryDeploys[chainId],
providerOrSigner
)
const contract = Registry__factory.connect(address, providerOrSigner)
const protocolStakers: string[] = await contract.getStakersForProtocol(
protocol
)
const stakers = protocolStakers.filter(async (staker) => {
const tokens = await contract.getTokensForStaker(staker)
let include = false
if (!tokens.length) include = true
else if (baseToken) include = tokens.includes(baseToken)
else if (quoteToken) include = tokens.includes(quoteToken)
return include
})
const results = await Promise.all(
protocolStakers.map(async (staker) => {
const tokens = await contract.getTokensForStaker(staker)
let toInclude = false
if (baseToken)
toInclude = tokens
.map((token) => token.toLowerCase())
.includes(baseToken.toLowerCase())
else if (quoteToken)
toInclude = tokens
.map((token) => token.toLowerCase())
.includes(quoteToken.toLowerCase())
return toInclude
})
)
const stakers = protocolStakers.filter((_v, index) => results[index])
return await contract.getServerURLsForStakers(stakers)
}
public async getServers(
Expand All @@ -41,21 +46,21 @@ class ServerRegistry extends Contract {
protocol: string,
baseToken?: string,
quoteToken?: string,
options?: ServerOptions
address = registryDeploys[chainId]
): Promise<Array<Server>> {
const urls = await this.getServerURLs(
providerOrSigner,
chainId,
protocol,
baseToken,
quoteToken
quoteToken,
address
)
const serverPromises = await Promise.allSettled(
urls.map((url) => {
return Server.at(url, {
swapContract: options?.swapContract || SwapERC20.addresses[chainId],
chainId: chainId,
initializeTimeout: options?.initializeTimeout,
chainId,
swapContract: SwapERC20.addresses[chainId],
})
})
)
Expand Down
101 changes: 101 additions & 0 deletions tools/libraries/test/Registry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { ADDRESS_ZERO } from '@airswap/utils'
import { expect } from 'chai'

const { ChainIds, ProtocolIds } = require('@airswap/utils')
const { ethers } = require('hardhat')
const ERC20PresetFixedSupply = require('@openzeppelin/contracts/build/contracts/ERC20PresetFixedSupply.json')
const RegistryABI = require('@airswap/registry/build/contracts/Registry.sol/Registry.json')

const { Registry } = require('../index')

let deployer
let staker
let stakingToken
let baseToken
let quoteToken
let registryFactory
let registry

const SERVER_URL = 'https://maker.com/xyz'
const STAKING_COST = 1000
const SUPPORT_COST = 10
const TOKEN_BALANCE = 10000

describe('Registry', async () => {
before(async () => {
;[deployer, staker] = await ethers.getSigners()
stakingToken = await (
await ethers.getContractFactory(
ERC20PresetFixedSupply.abi,
ERC20PresetFixedSupply.bytecode
)
).deploy('TestERC20', 'TERC20', TOKEN_BALANCE, staker.address)
baseToken = await (
await ethers.getContractFactory(
ERC20PresetFixedSupply.abi,
ERC20PresetFixedSupply.bytecode
)
).deploy('TestERC20', 'TERC20', TOKEN_BALANCE, staker.address)
quoteToken = await (
await ethers.getContractFactory(
ERC20PresetFixedSupply.abi,
ERC20PresetFixedSupply.bytecode
)
).deploy('TestERC20', 'TERC20', TOKEN_BALANCE, staker.address)
registryFactory = await ethers.getContractFactory(
RegistryABI.abi,
RegistryABI.bytecode
)
registry = await registryFactory
.connect(deployer)
.deploy(stakingToken.address, STAKING_COST, SUPPORT_COST)
await registry.deployed()

stakingToken.connect(staker).approve(registry.address, STAKING_COST * 2)

await registry.connect(staker).setServerURL(SERVER_URL)
await registry
.connect(staker)
.addProtocols([ProtocolIds.RequestForQuoteERC20])
await registry
.connect(staker)
.addTokens([quoteToken.address, baseToken.address])
})

it('get URLs: succeeds', async () => {
const servers = await Registry.getServerURLs(
ethers.provider,
ChainIds.HARDHAT,
ProtocolIds.RequestForQuoteERC20,
baseToken.address,
quoteToken.address,
registry.address
)
expect(servers.length).to.be.equal(1)
expect(servers[0]).to.be.equal(SERVER_URL)
})

it('get URLs: none for incorrect protocol', async () => {
const servers = await Registry.getServerURLs(
ethers.provider,
ChainIds.HARDHAT,
ProtocolIds.LastLookERC20,
baseToken.address,
quoteToken.address,
registry.address
)
expect(servers.length).to.be.equal(0)
})

it('get URLs: none for incorrect tokens', async () => {
const servers = await Registry.getServerURLs(
ethers.provider,
ChainIds.HARDHAT,
ProtocolIds.RequestForQuoteERC20,
ADDRESS_ZERO,
ADDRESS_ZERO,
registry.address
)
expect(servers.length).to.be.equal(0)
})
})

0 comments on commit 7a0c9cf

Please sign in to comment.