diff --git a/tools/libraries/package.json b/tools/libraries/package.json index 0ffa2d744..fc667394c 100644 --- a/tools/libraries/package.json +++ b/tools/libraries/package.json @@ -1,6 +1,6 @@ { "name": "@airswap/libraries", - "version": "4.2.9", + "version": "4.2.10", "description": "AirSwap: Libraries for Developers", "repository": { "type": "git", diff --git a/tools/libraries/src/Registry.ts b/tools/libraries/src/Registry.ts index 96d55e824..97febf757 100644 --- a/tools/libraries/src/Registry.ts +++ b/tools/libraries/src/Registry.ts @@ -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' @@ -16,23 +15,29 @@ class ServerRegistry extends Contract { chainId: number, protocol: string, baseToken?: string, - quoteToken?: string + quoteToken?: string, + address = registryDeploys[chainId] ): Promise { - 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( @@ -41,21 +46,21 @@ class ServerRegistry extends Contract { protocol: string, baseToken?: string, quoteToken?: string, - options?: ServerOptions + address = registryDeploys[chainId] ): Promise> { 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], }) }) ) diff --git a/tools/libraries/test/Registry.test.ts b/tools/libraries/test/Registry.test.ts new file mode 100644 index 000000000..4a1315078 --- /dev/null +++ b/tools/libraries/test/Registry.test.ts @@ -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) + }) +})