Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Pocket Network as a default infra provider #284

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/build.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ export default {
]
},
infuraApiKey: 'da99ebc8c0964bb8bb757b6f8cc40f1f',
pocketApiKey: {
1: '61439bb937e52c00344102a5',
3: '6143ee6737e52c00344170b5',
56: '6143eefb37e52c00344175f9',
137: '6143eebc37e52c0034417396'
},
exploraApis: {
testnet: 'https://liquality.io/testnet/electrs',
mainnet: 'https://api-mainnet-bitcoin-electrs.liquality.io'
Expand Down
11 changes: 7 additions & 4 deletions src/store/factory/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ import cryptoassets from '@/utils/cryptoassets'
import buildConfig from '../../build.config'
import { ChainNetworks } from '@/utils/networks'

// Only bsc mainnet, ropsten testnet, eth mainnet & polygon mainnet are supported
import { evmChainToRpcProviders } from '@utils/pocket-rpc'

function createBtcClient (network, mnemonic, accountType, derivationPath) {
const isTestnet = network === 'testnet'
const bitcoinNetwork = ChainNetworks.bitcoin[network]
Expand Down Expand Up @@ -130,11 +133,11 @@ function createEthereumClient (
function createEthClient (asset, network, mnemonic, accountType, derivationPath) {
const isTestnet = network === 'testnet'
const ethereumNetwork = ChainNetworks.ethereum[network]
const infuraApi = isTestnet ? `https://ropsten.infura.io/v3/${buildConfig.infuraApiKey}` : `https://mainnet.infura.io/v3/${buildConfig.infuraApiKey}`
const pocketApi = isTestnet ? evmChainToRpcProviders[3] : evmChainToRpcProviders[1]
const scraperApi = isTestnet ? 'https://liquality.io/eth-ropsten-api' : 'https://liquality.io/eth-mainnet-api'
const feeProvider = isTestnet ? new EthereumRpcFeeProvider() : new EthereumGasNowFeeProvider()

return createEthereumClient(asset, network, ethereumNetwork, infuraApi, scraperApi, feeProvider, mnemonic, accountType, derivationPath)
return createEthereumClient(asset, network, ethereumNetwork, pocketApi, scraperApi, feeProvider, mnemonic, accountType, derivationPath)
}

function createNearClient (network, mnemonic, derivationPath) {
Expand Down Expand Up @@ -184,7 +187,7 @@ function createRskClient (asset, network, mnemonic, accountType, derivationPath)
function createBSCClient (asset, network, mnemonic, derivationPath) {
const isTestnet = network === 'testnet'
const bnbNetwork = ChainNetworks.bsc[network]
const rpcApi = isTestnet ? 'https://data-seed-prebsc-1-s1.binance.org:8545' : 'https://bsc-dataseed.binance.org'
const rpcApi = isTestnet ? 'https://data-seed-prebsc-1-s1.binance.org:8545' : evmChainToRpcProviders[56]
const scraperApi = isTestnet ? 'https://liquality.io/bsc-testnet-api' : 'https://liquality.io/bsc-mainnet-api'
const feeProvider = new EthereumRpcFeeProvider({ slowMultiplier: 1, averageMultiplier: 1, fastMultiplier: 1.25 })

Expand All @@ -194,7 +197,7 @@ function createBSCClient (asset, network, mnemonic, derivationPath) {
function createPolygonClient (asset, network, mnemonic, derivationPath) {
const isTestnet = network === 'testnet'
const polygonNetwork = ChainNetworks.polygon[network]
const rpcApi = isTestnet ? 'https://rpc-mumbai.maticvigil.com' : 'https://rpc-mainnet.maticvigil.com'
const rpcApi = isTestnet ? 'https://rpc-mumbai.maticvigil.com' : evmChainToRpcProviders[137]
const scraperApi = isTestnet ? 'https://liquality.io/polygon-testnet-api' : 'https://liquality.io/polygon-mainnet-api'
const feeProvider = new EthereumRpcFeeProvider({ slowMultiplier: 1, averageMultiplier: 1, fastMultiplier: 1.25 })

Expand Down
17 changes: 7 additions & 10 deletions src/swaps/oneinch/OneinchSwapProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ import { prettyBalance } from '../../utils/coinFormatter'
import { isEthereumChain, isERC20 } from '@/utils/asset'
import cryptoassets from '@/utils/cryptoassets'
import * as ethers from 'ethers'
import buildConfig from '../../build.config'
import ERC20 from '@uniswap/v2-core/build/ERC20.json'

// Only bsc mainnet, ropsten testnet, eth mainnet & polygon mainnet are supported
import { evmChainToRpcProviders } from '@utils/pocket-rpc'

const nativeAssetAddress = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
const slippagePercentage = 0.5
const chainToRpcProviders = {
1: `https://mainnet.infura.io/v3/${buildConfig.infuraApiKey}`,
56: 'https://bsc-dataseed.binance.org',
137: 'https://rpc-mainnet.matic.network'
}

class OneinchSwapProvider extends SwapProvider {
async getSupportedPairs () {
Expand All @@ -43,7 +40,7 @@ class OneinchSwapProvider extends SwapProvider {
const fromAmountInUnit = BN(currencyToUnit(cryptoassets[from], BN(amount)))
const chainIdFrom = ChainNetworks[cryptoassets[from].chain][network].chainId
const chainIdTo = ChainNetworks[cryptoassets[to].chain][network].chainId
if (chainIdFrom !== chainIdTo || !chainToRpcProviders[chainIdFrom]) return null
if (chainIdFrom !== chainIdTo || !evmChainToRpcProviders[chainIdFrom]) return null

const trade = await this._getQuote(chainIdFrom, from, to, fromAmountInUnit.toNumber())
const toAmountInUnit = BN(trade.data.toTokenAmount)
Expand All @@ -60,9 +57,9 @@ class OneinchSwapProvider extends SwapProvider {
const fromChain = cryptoassets[quote.from].chain
const toChain = cryptoassets[quote.to].chain
const chainId = ChainNetworks[fromChain][network].chainId
if (fromChain !== toChain || !chainToRpcProviders[chainId]) return null
if (fromChain !== toChain || !evmChainToRpcProviders[chainId]) return null

const api = new ethers.providers.StaticJsonRpcProvider(chainToRpcProviders[chainId])
const api = new ethers.providers.StaticJsonRpcProvider(evmChainToRpcProviders[chainId])
const erc20 = new ethers.Contract(cryptoassets[quote.from].contractAddress, ERC20.abi, api)
const fromAddressRaw = await this.getSwapAddress(network, walletId, quote.from, quote.toAccountId)
const fromAddress = chains[fromChain].formatAddress(fromAddressRaw)
Expand Down Expand Up @@ -95,7 +92,7 @@ class OneinchSwapProvider extends SwapProvider {
const toChain = cryptoassets[quote.to].chain
const fromChain = cryptoassets[quote.from].chain
const chainId = ChainNetworks[toChain][network].chainId
if (toChain !== fromChain || !chainToRpcProviders[chainId]) return null
if (toChain !== fromChain || !evmChainToRpcProviders[chainId]) return null

const account = this.getAccount(quote.fromAccountId)
const client = this.getClient(network, walletId, quote.from, account?.type)
Expand Down
8 changes: 5 additions & 3 deletions src/swaps/thorchain/ThorchainSwapProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as ethers from 'ethers'
import { v4 as uuidv4 } from 'uuid'
import ERC20 from '@uniswap/v2-core/build/ERC20.json'

import buildConfig from '../../build.config'
import { chains, currencyToUnit, unitToCurrency } from '@liquality/cryptoassets'
import cryptoassets from '@/utils/cryptoassets'
import { isERC20 } from '../../utils/asset'
Expand All @@ -17,6 +16,9 @@ import { SwapProvider } from '../SwapProvider'
import { getTxFee } from '../../utils/fees'
import { mapValues } from 'lodash-es'

// Only bsc mainnet, ropsten testnet, eth mainnet & polygon mainnet are supported
import { evmChainToRpcProviders } from '@utils/pocket-rpc'

// Pool balances are denominated with 8 decimals
const THORCHAIN_DECIMAL = 8

Expand Down Expand Up @@ -130,7 +132,7 @@ class ThorchainSwapProvider extends SwapProvider {
const fromChain = cryptoassets[quote.from].chain
const chainId = ChainNetworks[fromChain][network].chainId

const api = new ethers.providers.InfuraProvider(chainId, buildConfig.infuraApiKey)
const api = new ethers.providers.StaticJsonRpcProvider(evmChainToRpcProviders[chainId])
const erc20 = new ethers.Contract(cryptoassets[quote.from].contractAddress, ERC20.abi, api)

const inboundAddresses = await this._getInboundAddresses()
Expand Down Expand Up @@ -184,7 +186,7 @@ class ThorchainSwapProvider extends SwapProvider {
const routerAddress = inboundAddresses.find(inbound => inbound.chain === fromThorchainAsset.chain).router

const chainId = ChainNetworks[cryptoassets[quote.from].chain][network].chainId
const api = new ethers.providers.InfuraProvider(chainId, buildConfig.infuraApiKey)
const api = new ethers.providers.StaticJsonRpcProvider(evmChainToRpcProviders[chainId])
const tokenAddress = isERC20(quote.from) ? cryptoassets[quote.from].contractAddress : '0x0000000000000000000000000000000000000000'
const thorchainRouter = new ethers.Contract(routerAddress, ['function deposit(address payable vault, address asset, uint amount, string memory memo) external payable'], api)

Expand Down
6 changes: 4 additions & 2 deletions src/swaps/uniswap/UniswapSwapProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import UniswapV2Pair from '@uniswap/v2-core/build/IUniswapV2Pair.json'
import UniswapV2Router from '@uniswap/v2-periphery/build/IUniswapV2Router02.json'
import * as ethers from 'ethers'

import buildConfig from '../../build.config'
import { chains, currencyToUnit, unitToCurrency } from '@liquality/cryptoassets'
import cryptoassets from '@/utils/cryptoassets'
import { isEthereumChain, isERC20 } from '../../utils/asset'
Expand All @@ -18,6 +17,9 @@ import { ChainNetworks } from '@/utils/networks'
import { withInterval, withLock } from '../../store/actions/performNextAction/utils'
import { SwapProvider } from '../SwapProvider'

// Only bsc mainnet, ropsten testnet, eth mainnet & polygon mainnet are supported
import { evmChainToRpcProviders } from '@utils/pocket-rpc'

const SWAP_DEADLINE = 30 * 60 // 30 minutes

class UniswapSwapProvider extends SwapProvider {
Expand All @@ -36,7 +38,7 @@ class UniswapSwapProvider extends SwapProvider {
if (chainId in this._apiCache) {
return this._apiCache[chainId]
} else {
const api = new ethers.providers.InfuraProvider(chainId, buildConfig.infuraApiKey)
const api = new ethers.providers.StaticJsonRpcProvider(evmChainToRpcProviders[chainId])
this._apiCache[chainId] = api
return api
}
Expand Down
10 changes: 6 additions & 4 deletions src/utils/asset.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
import cryptoassets from '@/utils/cryptoassets'
import * as ethers from 'ethers'
import tokenABI from './tokenABI.json'
import buildConfig from '../build.config'

// Only bsc mainnet, ropsten testnet, eth mainnet & polygon mainnet are supported
import { evmChainToRpcProviders } from '@utils/pocket-rpc'

const EXPLORERS = {
ethereum: {
Expand Down Expand Up @@ -165,12 +167,12 @@ export const getExplorerTransactionHash = (asset, hash) => {
export const tokenDetailProviders = {
ethereum: {
async getDetails (contractAddress) {
return await fetchTokenDetails(contractAddress, `https://mainnet.infura.io/v3/${buildConfig.infuraApiKey}`)
return await fetchTokenDetails(contractAddress, evmChainToRpcProviders[1])
}
},
polygon: {
async getDetails (contractAddress) {
return await fetchTokenDetails(contractAddress, 'https://rpc-mainnet.matic.network/')
return await fetchTokenDetails(contractAddress, evmChainToRpcProviders[137])
}
},
rsk: {
Expand All @@ -180,7 +182,7 @@ export const tokenDetailProviders = {
},
bsc: {
async getDetails (contractAddress) {
return await fetchTokenDetails(contractAddress, 'https://bsc-dataseed.binance.org')
return await fetchTokenDetails(contractAddress, evmChainToRpcProviders[56])
}
},
arbitrum: {
Expand Down
10 changes: 10 additions & 0 deletions src/utils/pocket-rpc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import buildConfig from '../../build.config'

const evmChainToRpcProviders = {
1: `https://eth-mainnet.gateway.pokt.network/v1/lb/${buildConfig.pocketApiKey[1]}`,
3: `https://eth-ropsten.gateway.pokt.network/v1/lb/${buildConfig.pocketApiKey[3]}`,
56: `https://bsc-mainnet.gateway.pokt.network/v1/lb/${buildConfig.pocketApiKey[56]}`,
137: `https://poly-mainnet.gateway.pokt.network/v1/lb/${buildConfig.pocketApiKey[137]}`
}

export { evmChainToRpcProviders }