Skip to content

Commit

Permalink
Changes for Honeyswap (#1)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Vega <[email protected]>
  • Loading branch information
danivega1912 and danitangocode authored May 10, 2021
1 parent 2037bfd commit ce98cf5
Show file tree
Hide file tree
Showing 12 changed files with 1,699 additions and 1,473 deletions.
Binary file added .DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"@makerdao/multicall": "^0.11.0",
"big.js": "^5.2.2",
"decimal.js-light": "^2.5.0",
"dxswap-core": "git://github.com/levelkdev/dxswap-core.git#v0.3.7",
"dxswap-periphery": "git://github.com/levelkdev/dxswap-periphery.git#v0.3.8",
"dxswap-core": "git://github.com/danivega1912/dxswap-core#useMatic",
"dxswap-periphery": "git://github.com/danivega1912/dxswap-periphery#useMatic",
"jsbi": "^3.1.1",
"node-fetch": "^2.6.1",
"tiny-invariant": "^1.1.0",
Expand Down
28 changes: 19 additions & 9 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ import {
mainnet as coreMainnet,
arbitrumTestnetV3 as coreArbitrumTestnetV3,
sokol as coreSokol,
xdai as coreXDai
xdai as coreXDai,
matic as coreMatic
} from 'dxswap-core/.contracts.json'
import {
rinkeby as peripheryRinkeby,
mainnet as peripheryMainnet,
arbitrumTestnetV3 as peripheryArbitrumTestnetV3,
sokol as peripherySokol,
xdai as peripheryXDai
xdai as peripheryXDai,
matic as peripheryMatic
} from 'dxswap-periphery/.contracts.json'

// exports for external consumption
Expand All @@ -26,7 +28,8 @@ export enum ChainId {
RINKEBY = 4,
ARBITRUM_TESTNET_V3 = 79377087078960,
SOKOL = 77,
XDAI = 100
XDAI = 100,
MATIC = 137
}

export enum TradeType {
Expand All @@ -47,39 +50,45 @@ export const FACTORY_ADDRESS: { [chainId: number]: string } = {
[ChainId.RINKEBY]: coreRinkeby.factory,
[ChainId.ARBITRUM_TESTNET_V3]: coreArbitrumTestnetV3.factory,
[ChainId.SOKOL]: coreSokol.factory,
[ChainId.XDAI]: coreXDai.factory
[ChainId.XDAI]: coreXDai.factory,
[ChainId.MATIC]: coreMatic.factory
}

export const ROUTER_ADDRESS: { [chainId in ChainId]?: string } = {
[ChainId.RINKEBY]: peripheryRinkeby.router,
[ChainId.MAINNET]: peripheryMainnet.router,
[ChainId.ARBITRUM_TESTNET_V3]: peripheryArbitrumTestnetV3.router,
[ChainId.SOKOL]: peripherySokol.router,
[ChainId.XDAI]: peripheryXDai.router
[ChainId.XDAI]: peripheryXDai.router,
[ChainId.MATIC]: peripheryMatic.router

}

export const STAKING_REWARDS_FACTORY_ADDRESS: { [chainId: number]: string } = {
[ChainId.MAINNET]: '0x0000000000000000000000000000000000001234',
[ChainId.RINKEBY]: '0xDcA39454f0B83c6f0807708b0E6a620dd9BA6808',
[ChainId.ARBITRUM_TESTNET_V3]: '0xB95Ad562EDE8DD78BBFC287fA18150e802b09D9F',
[ChainId.SOKOL]: '0xD436e756Cf41318ADeC62E8dCbEF2608753Ae068',
[ChainId.XDAI]: '0xCD2A45F36464FdB1065160e03A2353996Ea8Ff57'
[ChainId.XDAI]: '0xCD2A45F36464FdB1065160e03A2353996Ea8Ff57',
[ChainId.MATIC]: '',
}

export const TOKEN_REGISTRY_ADDRESS: { [chainId: number]: string } = {
[ChainId.MAINNET]: '0x93DB90445B76329e9ed96ECd74e76D8fbf2590d8',
[ChainId.RINKEBY]: '0x815d1b18f6baaeb3853b0f637475a5c2b28e2253',
[ChainId.ARBITRUM_TESTNET_V3]: '0x9d6f6d86b81289e40e07fcda805c06f6e9b8f629',
[ChainId.SOKOL]: '0x681c3836a5589b933062ACA4fd846c1287a2865F',
[ChainId.XDAI]: '0x85E001DfFF16F388Bc32Cd18009ceDF8F9b62C9E'
[ChainId.XDAI]: '0x85E001DfFF16F388Bc32Cd18009ceDF8F9b62C9E',
[ChainId.MATIC]: '',
}

export const DXSWAP_TOKEN_LIST_ID: { [chainId: number]: number } = {
[ChainId.MAINNET]: 1,
[ChainId.RINKEBY]: 1,
[ChainId.ARBITRUM_TESTNET_V3]: 1,
[ChainId.SOKOL]: 1,
[ChainId.XDAI]: 5
[ChainId.XDAI]: 5,
[ChainId.MATIC]: 137
}

export const INIT_CODE_HASH = '0xd306a548755b9295ee49cc729e13ca4a45e00199bbd890fa146da43a50571776'
Expand Down Expand Up @@ -118,7 +127,8 @@ const PERMISSIVE_MULTICALL_ADDRESS: { [chainId: number]: string } = {
[ChainId.RINKEBY]: '0x798d8ced4dff8f054a5153762187e84751a73344',
[ChainId.ARBITRUM_TESTNET_V3]: '0x73a08DC74eF4ed2c360199244bb69F1464204E7C',
[ChainId.SOKOL]: '0x4D97Bd8eFaCf46b33c4438Ed0B7B6AABfa2359FB',
[ChainId.XDAI]: '0x4E75068ED2338fCa56631E740B0723A6dbc1d5CD'
[ChainId.XDAI]: '0x4E75068ED2338fCa56631E740B0723A6dbc1d5CD',
[ChainId.MATIC]: '0x95028E5B8a734bb7E2071F96De89BABe75be9C8E'
}

export {
Expand Down
5 changes: 4 additions & 1 deletion src/entities/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ export class Currency {
public static readonly ETHER: Currency = new Currency(18, 'ETH', 'Ether')
public static readonly SPOA: Currency = new Currency(18, 'SPOA', 'Sokol POA')
public static readonly XDAI: Currency = new Currency(18, 'XDAI', 'xDAI')
public static readonly MATIC: Currency = new Currency(18, 'MATIC', 'MATIC')

private static readonly NATIVE_CURRENCY: { [chainId in ChainId]: Currency } = {
[ChainId.MAINNET]: Currency.ETHER,
[ChainId.RINKEBY]: Currency.ETHER,
[ChainId.ARBITRUM_TESTNET_V3]: Currency.ETHER,
[ChainId.SOKOL]: Currency.SPOA,
[ChainId.XDAI]: Currency.XDAI
[ChainId.XDAI]: Currency.XDAI,
[ChainId.MATIC]: Currency.MATIC
}

/**
Expand Down Expand Up @@ -58,3 +60,4 @@ export const USD = Currency.USD
export const ETHER = Currency.ETHER
export const SPOA = Currency.SPOA
export const XDAI = Currency.XDAI
export const MATIC = Currency.MATIC
4 changes: 4 additions & 0 deletions src/entities/liquidity-mining-campaign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const MINIMUM_STAKED_AMOUNT_NATIVE_CURRENCY: { [chainId in ChainId]: CurrencyAmo
[ChainId.ARBITRUM_TESTNET_V3]: CurrencyAmount.nativeCurrency(
utils.parseUnits('0.05', Token.getNative(ChainId.ARBITRUM_TESTNET_V3).decimals).toString(),
ChainId.ARBITRUM_TESTNET_V3
),
[ChainId.MATIC]: CurrencyAmount.nativeCurrency(
utils.parseUnits('1000', Token.getNative(ChainId.MATIC).decimals).toString(),
ChainId.MATIC
)
}

Expand Down
3 changes: 2 additions & 1 deletion src/entities/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const INITIAL_CACHE_STATE = {
[ChainId.RINKEBY]: {},
[ChainId.ARBITRUM_TESTNET_V3]: {},
[ChainId.SOKOL]: {},
[ChainId.XDAI]: {}
[ChainId.XDAI]: {},
[ChainId.MATIC]: {}
}

let PAIR_ADDRESS_CACHE: {
Expand Down
9 changes: 9 additions & 0 deletions src/entities/routable-platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ const SUSHISWAP_FACTORY_ADDRESS = '0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac'
const HONEYSWAP_FACTORY_ADDRESS = '0xA818b4F111Ccac7AA31D0BCc0806d64F2E0737D7'
const BAOSWAP_FACTORY_ADDRESS = '0x45de240fbe2077dd3e711299538a09854fae9c9b'
const LEVINSWAP_FACTORY_ADDRESS = '0x965769C9CeA8A7667246058504dcdcDb1E2975A5'
const QUICKSWAP_FACTORY_ADDRESS = '0x5757371414417b8C6CAad45bAeF941aBc7d3Ab32'

const UNISWAP_ROUTER_ADDRESS = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D'
const SUSHISWAP_ROUTER_ADDRESS = '0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F'
const HONEYSWAP_ROUTER_ADDRESS = '0x1C232F01118CB8B424793ae03F870aa7D0ac7f77'
const BAOSWAP_ROUTER_ADDRESS = '0x6093AeBAC87d62b1A5a4cEec91204e35020E38bE'
const LEVINSWAP_ROUTER_ADDRESS = '0xb18d4f69627F8320619A696202Ad2C430CeF7C53'
const QUICKSWAP_ROUTER_ADDRESS = '0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff'


/**
Expand Down Expand Up @@ -65,6 +67,13 @@ export class RoutablePlatform {
'0x4955fd9146732ca7a64d43c7a8d65fe6db1acca27e9c5b3bee7c3abe5849f441',
_30
)
public static readonly QUICKSWAP = new RoutablePlatform(
'Quickswap',
{ [ChainId.MATIC]: QUICKSWAP_FACTORY_ADDRESS },
{ [ChainId.MATIC]: QUICKSWAP_ROUTER_ADDRESS },
'0x96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f',
_30
)

public constructor(
name: string,
Expand Down
15 changes: 14 additions & 1 deletion src/entities/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ export class Token extends Currency {
18,
'WETH',
'Wrapped Ether on xDai'
),
[ChainId.MATIC]: new Token(
ChainId.MATIC,
'0x8cc8538d60901d19692F5ba22684732Bc28F54A3',
18,
'WETH',
'Wrapped Ether on Matic'
)
}

Expand All @@ -56,6 +63,10 @@ export class Token extends Currency {
[ChainId.XDAI]: new Token(ChainId.XDAI, '0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d', 18, 'WXDAI', 'Wrapped xDAI')
}

public static readonly WMATIC: { [key: number]: Token } = {
[ChainId.MATIC]: new Token(ChainId.MATIC, '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270', 18, 'WMATIC', 'Wrapped Matic')
}

public static readonly DXD: { [key: number]: Token } = {
[ChainId.MAINNET]: new Token(ChainId.MAINNET, '0xa1d65E8fB6e87b60FECCBc582F7f97804B725521', 18, 'DXD', 'DXdao'),
[ChainId.RINKEBY]: new Token(ChainId.RINKEBY, '0x554898A0BF98aB0C03ff86C7DccBE29269cc4d29', 18, 'DXD', 'DXdao'),
Expand All @@ -73,7 +84,8 @@ export class Token extends Currency {
[ChainId.RINKEBY]: Token.WETH[ChainId.RINKEBY],
[ChainId.ARBITRUM_TESTNET_V3]: Token.WETH[ChainId.ARBITRUM_TESTNET_V3],
[ChainId.SOKOL]: Token.WSPOA[ChainId.SOKOL],
[ChainId.XDAI]: Token.WXDAI[ChainId.XDAI]
[ChainId.XDAI]: Token.WXDAI[ChainId.XDAI],
[ChainId.MATIC]: Token.WMATIC[ChainId.MATIC]
}

public constructor(chainId: ChainId, address: string, decimals: number, symbol?: string, name?: string) {
Expand Down Expand Up @@ -135,3 +147,4 @@ export const WETH = Token.WETH
export const WSPOA = Token.WSPOA
export const DXD = Token.DXD
export const WXDAI = Token.WXDAI
export const WMATIC = Token.WMATIC
93 changes: 63 additions & 30 deletions src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import IDXswapPair from 'dxswap-core/build/IDXswapPair.json'
import IDXswapFactory from 'dxswap-core/build/IDXswapFactory.json'
import invariant from 'tiny-invariant'
import ERC20Abi from './abis/ERC20.json'
import TokenRegistryAbi from './abis/token-registry.json'
// import TokenRegistryAbi from './abis/token-registry.json'
import {
ChainId,
BigintIsh,
FACTORY_ADDRESS,
PERMISSIVE_MULTICALL_ADDRESS,
PERMISSIVE_MULTICALL_ABI,
TOKEN_REGISTRY_ADDRESS,
DXSWAP_TOKEN_LIST_ID
PERMISSIVE_MULTICALL_ABI
} from './constants'
import { Token } from './entities/token'
import { Currency } from './entities/currency'
Expand All @@ -40,7 +38,8 @@ const TOKEN_LOGO_URI_CACHE: {
[ChainId.XDAI]: {},
[ChainId.SOKOL]: {},
[ChainId.ARBITRUM_TESTNET_V3]: {},
[ChainId.RINKEBY]: {}
[ChainId.RINKEBY]: {},
[ChainId.MATIC]: {}
}

/**
Expand Down Expand Up @@ -376,53 +375,87 @@ export abstract class Fetcher {
chainId: ChainId,
provider = getDefaultProvider(getNetwork(chainId))
): Promise<TokenList> {
const tokenRegistryContract = new Contract(TOKEN_REGISTRY_ADDRESS[chainId], TokenRegistryAbi, provider)
const tokenAddresses = await tokenRegistryContract.getTokens(DXSWAP_TOKEN_LIST_ID[chainId])
const tokens = await this.fetchMultipleTokensData(chainId, tokenAddresses, provider)

const tokenListUrl = new Map([
[1, 'https://tokens.coingecko.com/uniswap/all.json'],
[100, 'https://tokens.honeyswap.org'],
[137, 'https://unpkg.com/[email protected]/build/quickswap-default.tokenlist.json']
])

// const tokenRegistryContract = new Contract(TOKEN_REGISTRY_ADDRESS[chainId], TokenRegistryAbi, provider)
// const tokenAddresses = await tokenRegistryContract.getTokens(DXSWAP_TOKEN_LIST_ID[chainId])
// const tokens = await this.fetchMultipleTokensData(chainId, tokenAddresses, provider)

console.log(provider);

const chainTokenURL = tokenListUrl.get(chainId) ?? ''

const response = await fetch(chainTokenURL, {
method: 'GET',
headers: {
'content-type': 'application/json;charset=UTF-8',
}
})

const tokenList = []

if (!response.ok) {
return {
name: 'default token list',
tokens: []
}
}

const { tokens }: { tokens: TokenInfo[] } = await response.json()


if (tokens.length != 0) {
await this.fetchTokenLogoUri(chainId, tokens)
}

for (const token of tokens) {
tokenList.push({
chainId,
address: token.address,
name: token.name!,
decimals: token.decimals,
symbol: token.symbol!,
logoURI: await this.fetchTokenLogoUri(token)
logoURI: token.logoURI
})
}


return {
name: 'DXswap default token list',
name: 'swapr default token list',
tokens: tokenList
}
}

private static async fetchTokenLogoUri(token: Token): Promise<string> {
const chainId = token.chainId
if (chainId !== ChainId.MAINNET && chainId !== ChainId.XDAI) {
return '' // token logos not fully supported for testnets
private static async fetchTokenLogoUri(tokenChainId: number, tokens: TokenInfo[]): Promise<void> {
const chainId = tokenChainId
if (chainId !== ChainId.MAINNET && chainId !== ChainId.XDAI && chainId !== ChainId.MATIC) {
return // token logos not fully supported for testnets
}

if (Object.keys(TOKEN_LOGO_URI_CACHE[chainId]).length === 0) {
await this.populateTokenLogoCache(chainId)
await this.populateTokenLogoCache(chainId, tokens)
}
return TOKEN_LOGO_URI_CACHE[chainId][token.address.toLowerCase()]
return
}

public static async populateTokenLogoCache(chainId: ChainId): Promise<void> {
if (chainId !== ChainId.MAINNET && chainId !== ChainId.XDAI) {
return
public static async checkTokenLogoCache(chainId: ChainId): Promise<void> {
if (chainId !== ChainId.MAINNET && chainId !== ChainId.XDAI && chainId !== ChainId.MATIC) {
return // token logos not fully supported for testnets
}
let tokenListURL = ''
if (chainId == ChainId.MAINNET) {
tokenListURL = 'https://tokens.coingecko.com/uniswap/all.json' // coingecko list used for mainnet
} else {
tokenListURL = 'https://tokens.honeyswap.org' // honeyswap list used for xdai
}
const response = await fetch(tokenListURL)
if (!response.ok) {
console.warn(`could not fetch token list at ${tokenListURL}`)
if (Object.keys(TOKEN_LOGO_URI_CACHE[chainId]).length === 0) {
// populate cache
await this.fetchDxDaoTokenList(chainId)
return
}
const { tokens }: { tokens: TokenInfo[] } = await response.json()
return
}

public static async populateTokenLogoCache(chainId: ChainId, tokens: TokenInfo[]): Promise<void> {
TOKEN_LOGO_URI_CACHE[chainId] = tokens.reduce((cache: { [tokenAddress: string]: string }, token) => {
cache[token.address.toLowerCase()] = token.logoURI
return cache
Expand All @@ -431,7 +464,7 @@ export abstract class Fetcher {

public static getCachedTokenLogo(token: Token): string {
const { chainId } = token
if (chainId !== ChainId.MAINNET && chainId !== ChainId.XDAI) {
if (chainId !== ChainId.MAINNET && chainId !== ChainId.XDAI && chainId !== ChainId.MATIC) {
return ''
}
return TOKEN_LOGO_URI_CACHE[chainId][token.address.toLowerCase()] || ''
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import invariant from 'tiny-invariant'
import warning from 'tiny-warning'
// import warning from 'tiny-warning'
import JSBI from 'jsbi'
import { getAddress } from '@ethersproject/address'

Expand All @@ -14,7 +14,7 @@ export function validateSolidityTypeInstance(value: JSBI, solidityType: Solidity
export function validateAndParseAddress(address: string): string {
try {
const checksummedAddress = getAddress(address)
warning(address === checksummedAddress, `${address} is not checksummed.`)
// warning(address === checksummedAddress, `${address} is not checksummed.`)
return checksummedAddress
} catch (error) {
invariant(false, `${address} is not a valid address.`)
Expand Down
2 changes: 2 additions & 0 deletions test/fees.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { ZERO_ADDRESS } from '../src/constants'
import { rinkeby } from 'dxswap-core/.contracts.json'
import { getAddress } from '@ethersproject/address'
import { TEST_TOKENS } from './commons'
import { getNetwork } from '@ethersproject/networks'
import { getDefaultProvider } from '@ethersproject/providers'

describe('fees', () => {
// skip because uses old implementations, update tests with new local deployment
Expand Down
Loading

0 comments on commit ce98cf5

Please sign in to comment.