Skip to content

Commit

Permalink
feat: foxy/market-service inject provider
Browse files Browse the repository at this point in the history
  • Loading branch information
gomesalexandre committed Feb 15, 2024
1 parent d4ac096 commit ab15bed
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/lib/investor/investor-foxy/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type EthereumChainReference =
export type ConstructorArgs = {
adapter: EvmBaseAdapter<KnownChainIds.EthereumMainnet>
providerUrl: string
provider: ethers.providers.StaticJsonRpcProvider
foxyAddresses: FoxyAddressesType
chainReference?: EthereumChainReference
}
Expand All @@ -84,7 +85,6 @@ export class FoxyApi {
public adapter: EvmBaseAdapter<KnownChainIds.EthereumMainnet>
public provider: ethers.providers.StaticJsonRpcProvider
private providerUrl: string
public jsonRpcProvider: ethers.providers.StaticJsonRpcProvider
private foxyStakingContracts: ethers.Contract[]
private liquidityReserveContracts: ethers.Contract[]
private readonly ethereumChainReference: ChainReference
Expand All @@ -95,10 +95,10 @@ export class FoxyApi {
providerUrl,
foxyAddresses,
chainReference = CHAIN_REFERENCE.EthereumMainnet,
provider,
}: ConstructorArgs) {
this.adapter = adapter
this.provider = new ethers.providers.StaticJsonRpcProvider(providerUrl)
this.jsonRpcProvider = new ethers.providers.StaticJsonRpcProvider(providerUrl)
this.provider = provider
this.foxyStakingContracts = foxyAddresses.map(
addresses => new ethers.Contract(addresses.staking, foxyStakingAbi, this.provider),
)
Expand Down
4 changes: 4 additions & 0 deletions src/lib/investor/investor-foxy/foxycli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NativeHDWallet } from '@shapeshiftoss/hdwallet-native'
import { WithdrawType } from '@shapeshiftoss/types'
import * as unchained from '@shapeshiftoss/unchained-client'
import dotenv from 'dotenv'
import { ethers } from 'ethers'
import readline from 'readline-sync'
import { bnOrZero } from 'lib/bignumber/bignumber'

Expand Down Expand Up @@ -54,6 +55,9 @@ const main = async (): Promise<void> => {
adapter: ethChainAdapter,
providerUrl: process.env.ARCHIVE_NODE || 'http://127.0.0.1:8545/',
foxyAddresses,
provider: new ethers.providers.StaticJsonRpcProvider(
process.env.ARCHIVE_NODE || 'http://127.0.0.1:8545/',
),
})

const accountNumber = 0
Expand Down
2 changes: 2 additions & 0 deletions src/lib/market-service/foxy/foxy.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { HistoryTimeframe } from '@shapeshiftoss/types'
import type { AxiosInstance } from 'axios'
import { ethers } from 'ethers'
import { beforeAll, describe, expect, it, vi } from 'vitest'
import { bn } from 'lib/bignumber/bignumber'

import { FOXY_ASSET_ID, FoxyMarketService } from './foxy'
import { fox, mockFoxyMarketData } from './foxyMockData'

const foxyMarketService = new FoxyMarketService({
provider: new ethers.providers.StaticJsonRpcProvider(''),
providerUrls: {
jsonRpcProviderUrl: 'dummy',
unchainedEthereumHttpUrl: '',
Expand Down
14 changes: 12 additions & 2 deletions src/lib/market-service/foxy/foxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
PriceHistoryArgs,
} from '@shapeshiftoss/types'
import * as unchained from '@shapeshiftoss/unchained-client'
import type { ethers } from 'ethers'
import { foxyAddresses, FoxyApi } from 'lib/investor/investor-foxy'

import type { MarketService } from '../api'
Expand All @@ -19,11 +20,19 @@ const FOXY_ASSET_PRECISION = '18'

export class FoxyMarketService extends CoinGeckoMarketService implements MarketService {
providerUrls: ProviderUrls

constructor({ providerUrls }: { providerUrls: ProviderUrls }) {
provider: ethers.providers.StaticJsonRpcProvider

constructor({
providerUrls,
provider,
}: {
providerUrls: ProviderUrls
provider: ethers.providers.StaticJsonRpcProvider
}) {
super()

this.providerUrls = providerUrls
this.provider = provider
}

async findAll() {
Expand Down Expand Up @@ -71,6 +80,7 @@ export class FoxyMarketService extends CoinGeckoMarketService implements MarketS
adapter: ethChainAdapter,
providerUrl: this.providerUrls.jsonRpcProviderUrl,
foxyAddresses,
provider: this.provider,
})

const tokenContractAddress = foxyAddresses[0].foxy
Expand Down
6 changes: 4 additions & 2 deletions src/lib/market-service/market-service-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
MarketDataArgs,
PriceHistoryArgs,
} from '@shapeshiftoss/types'
import type { ethers } from 'ethers'
import { AssetService } from 'lib/asset-service'

// import { Yearn } from '@yfi/sdk'
Expand All @@ -25,14 +26,15 @@ export type ProviderUrls = {
export type MarketServiceManagerArgs = {
yearnChainReference: 1 | 250 | 1337 | 42161 // from @yfi/sdk
providerUrls: ProviderUrls
provider: ethers.providers.StaticJsonRpcProvider
}

export class MarketServiceManager {
marketProviders: MarketService[]
assetService: AssetService

constructor(args: MarketServiceManagerArgs) {
const { providerUrls } = args
const { providerUrls, provider } = args

// TODO(0xdef1cafe): after chain agnosticism, we need to dependency inject a chainReference here
// YearnVaultMarketCapService deps
Expand All @@ -48,7 +50,7 @@ export class MarketServiceManager {
// Yearn is currently borked upstream
// new YearnVaultMarketCapService({ yearnSdk }),
// new YearnTokenMarketCapService({ yearnSdk }),
new FoxyMarketService({ providerUrls }),
new FoxyMarketService({ providerUrls, provider }),
]

this.assetService = new AssetService()
Expand Down
2 changes: 2 additions & 0 deletions src/lib/market-service/market-service.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { HistoryTimeframe } from '@shapeshiftoss/types'
import { ethers } from 'ethers'
import { describe, expect, it, vi } from 'vitest'

import { CoinGeckoMarketService } from './coingecko/coingecko'
Expand Down Expand Up @@ -99,6 +100,7 @@ describe('market service', () => {
const marketServiceManagerArgs = {
coinGeckoAPIKey: 'dummyCoingeckoApiKey',
yearnChainReference: 1 as const,
provider: new ethers.providers.StaticJsonRpcProvider(''),
providerUrls: {
jsonRpcProviderUrl: '',
unchainedEthereumWsUrl: '',
Expand Down
2 changes: 2 additions & 0 deletions src/state/apis/foxy/foxyApiSingleton.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { EvmBaseAdapter } from '@shapeshiftoss/chain-adapters'
import { KnownChainIds } from '@shapeshiftoss/types'
import { getConfig } from 'config'
import { getEthersProvider } from 'lib/ethersProviderSingleton'
import { foxyAddresses, FoxyApi } from 'lib/investor/investor-foxy'
import { assertGetEvmChainAdapter } from 'lib/utils/evm'

Expand All @@ -19,6 +20,7 @@ export const getFoxyApi = (): FoxyApi => {
) as EvmBaseAdapter<KnownChainIds.EthereumMainnet>,
providerUrl: getConfig()[RPC_PROVIDER_ENV],
foxyAddresses,
provider: getEthersProvider(KnownChainIds.EthereumMainnet),
})

_foxyApi = foxyApi
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// do not directly use or export, singleton
import { KnownChainIds } from '@shapeshiftoss/types'
import { getConfig } from 'config'
import { getEthersProvider } from 'lib/ethersProviderSingleton'
import { MarketServiceManager } from 'lib/market-service'

let _marketServiceManager: MarketServiceManager | undefined
Expand All @@ -11,6 +13,7 @@ export const getMarketServiceManager: GetMarketServiceManager = () => {
if (!_marketServiceManager) {
_marketServiceManager = new MarketServiceManager({
yearnChainReference: 1, // CHAIN_REFERENCE.EthereumMainnet is '1', yearn requires strict number union
provider: getEthersProvider(KnownChainIds.EthereumMainnet),
providerUrls: {
jsonRpcProviderUrl: config.REACT_APP_ETHEREUM_NODE_URL,
unchainedEthereumHttpUrl: config.REACT_APP_UNCHAINED_ETHEREUM_HTTP_URL,
Expand Down

0 comments on commit ab15bed

Please sign in to comment.