Skip to content

Commit

Permalink
Refactor to use network
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljscript committed Aug 2, 2024
1 parent 69a1884 commit 2826a73
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 30 deletions.
1 change: 1 addition & 0 deletions apps/wallet-mobile/src/features/Discover/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export const createDappConnector = (options: CreateDappConnectorOptions) => {
: undefined

const handlerWallet: ResolverWallet = {
network: wallet.networkManager.network,
id: wallet.id,
networkId: wallet.networkManager.chainId,
getUsedAddresses: (params) => cip30.getUsedAddresses(params),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const networkConfigs: Readonly<Record<Chain.SupportedNetworks, Readonly<N
[Chain.Network.Sancho]: {
network: Chain.Network.Sancho,
primaryTokenInfo: primaryTokenInfoAnyTestnet,
chainId: 450,
chainId: 0,
protocolMagic: 4,
eras: [shelleyEraConfig],
name: 'Sancho',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const BACKEND = {
FILTER_USED_MAX_ADDRESSES: 50,
TX_HISTORY_RESPONSE_LIMIT: 50,
} as const
export const CHAIN_NETWORK_ID = 450
export const CHAIN_NETWORK_ID = 0

export const IS_MAINNET = false

Expand Down
23 changes: 16 additions & 7 deletions packages/dapp-connector/src/adapters/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
import {dappConnectorApiMaker} from './api'
import {managerMock} from '../manager.mocks'
import {Chain} from '@yoroi/types'

describe('dappConnectorApiMaker', () => {
describe('getDApps', () => {
it('should throw error if fetching fails', async () => {
const fakeFetchData = () => Promise.reject(new Error('fake error'))
await expect(() => dappConnectorApiMaker({request: fakeFetchData}).getDApps({chainId: 1})).rejects.toThrow()
await expect(() =>
dappConnectorApiMaker({request: fakeFetchData}).getDApps({network: Chain.Network.Mainnet}),
).rejects.toThrow()
})

it('should throw error if response is invalid', async () => {
const fakeResult = {tag: 'right' as const, value: {data: {dapps: 1}}} as const
const fakeFetchData = () => Promise.resolve(fakeResult)
await expect(() =>
dappConnectorApiMaker({request: fakeFetchData as any}).getDApps({chainId: 1}),
dappConnectorApiMaker({request: fakeFetchData as any}).getDApps({network: Chain.Network.Mainnet}),
).rejects.toThrow()
})

it('should throw error if response is left', async () => {
const fakeResult = {tag: 'left' as const, error: {status: 404, message: 'Not found', responseData: null}} as const
const fakeFetchData = () => Promise.resolve(fakeResult)
await expect(() =>
dappConnectorApiMaker({request: fakeFetchData as any}).getDApps({chainId: 1}),
dappConnectorApiMaker({request: fakeFetchData as any}).getDApps({network: Chain.Network.Mainnet}),
).rejects.toThrow()
})

it('should return data if response is valid', async () => {
const fakeResult = {tag: 'right' as const, value: {data: await managerMock.getDAppList()}} as const
const fakeFetchData = () => Promise.resolve(fakeResult)
const result = await dappConnectorApiMaker({request: fakeFetchData as any}).getDApps({chainId: 1})
const result = await dappConnectorApiMaker({request: fakeFetchData as any}).getDApps({
network: Chain.Network.Mainnet,
})
expect(result).toEqual({
...fakeResult.value.data,
dapps: fakeResult.value.data.dapps.map((d) => ({...d, logo: 'https://daehx1qv45z7c.cloudfront.net/icon.png'})),
Expand All @@ -37,7 +42,9 @@ describe('dappConnectorApiMaker', () => {
it('should support preprod', async () => {
const fakeResult = {tag: 'right' as const, value: {data: await managerMock.getDAppList()}} as const
const fakeFetchData = () => Promise.resolve(fakeResult)
const result = await dappConnectorApiMaker({request: fakeFetchData as any}).getDApps({chainId: 0})
const result = await dappConnectorApiMaker({request: fakeFetchData as any}).getDApps({
network: Chain.Network.Preprod,
})
expect(result).toEqual({
...fakeResult.value.data,
dapps: fakeResult.value.data.dapps.map((d) => ({...d, logo: 'https://daehx1qv45z7c.cloudfront.net/icon.png'})),
Expand All @@ -47,7 +54,9 @@ describe('dappConnectorApiMaker', () => {
it('should support sancho', async () => {
const fakeResult = {tag: 'right' as const, value: {data: await managerMock.getDAppList()}} as const
const fakeFetchData = () => Promise.resolve(fakeResult)
const result = await dappConnectorApiMaker({request: fakeFetchData as any}).getDApps({chainId: 450})
const result = await dappConnectorApiMaker({request: fakeFetchData as any}).getDApps({
network: Chain.Network.Sancho,
})
expect(result).toEqual({
...fakeResult.value.data,
dapps: fakeResult.value.data.dapps.map((d) => ({...d, logo: 'https://daehx1qv45z7c.cloudfront.net/icon.png'})),
Expand All @@ -58,7 +67,7 @@ describe('dappConnectorApiMaker', () => {
const fakeResult = {tag: 'right' as const, value: {data: await managerMock.getDAppList()}} as const
const fakeFetchData = () => Promise.resolve(fakeResult)
await expect(() =>
dappConnectorApiMaker({request: fakeFetchData as any}).getDApps({chainId: -1}),
dappConnectorApiMaker({request: fakeFetchData as any}).getDApps({network: Chain.Network.Preview} as any),
).rejects.toThrow()
})
})
Expand Down
18 changes: 3 additions & 15 deletions packages/dapp-connector/src/adapters/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {FetchData, fetchData, getApiError, isLeft, createTypeGuardFromSchema} fr
import {freeze} from 'immer'
import {AxiosRequestConfig} from 'axios'
import {z} from 'zod'
import {Chain} from '@yoroi/types'

const dappListHosts = {
mainnet: 'https://daehx1qv45z7c.cloudfront.net/data.json',
Expand All @@ -12,7 +13,7 @@ const dappListHosts = {
const initialDeps = freeze({request: fetchData}, true)

type GetDAppsOptions = {
chainId: number
network: Chain.SupportedNetworks
}

export type Api = {
Expand All @@ -21,7 +22,7 @@ export type Api = {

export const dappConnectorApiMaker = ({request}: {request: FetchData} = initialDeps): Api => {
const getDApps = async (options: GetDAppsOptions, fetcherConfig?: AxiosRequestConfig): Promise<DappListResponse> => {
const url = dappListHosts[getNetworkNameByChainId(options.chainId)]
const url = dappListHosts[options.network]

const response = await request<unknown>({url}, fetcherConfig)

Expand Down Expand Up @@ -81,16 +82,3 @@ interface DappResponse {
uri: string
origins: string[]
}

const getNetworkNameByChainId = (chainId: number): 'mainnet' | 'preprod' | 'sancho' => {
switch (chainId) {
case 1:
return 'mainnet'
case 0:
return 'preprod'
case 450:
return 'sancho'
default:
throw new Error('Invalid chain id')
}
}
2 changes: 2 additions & 0 deletions packages/dapp-connector/src/dapp-connector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {Api, dappConnectorApiMaker} from './adapters/api'
import {mockedDAppList} from './manager.mocks'
import {ResolverWallet} from './resolver'
import {init} from '@emurgo/cross-csl-nodejs'
import {Chain} from '@yoroi/types'

const getDappConnector = (wallet = mockWallet) => {
const storage = connectionStorageMaker({storage: storageMock})
Expand Down Expand Up @@ -615,6 +616,7 @@ const mockWallet: ResolverWallet = {
signTx: () => Promise.resolve(CSL.TransactionWitnessSet.new()),
signData: () => Promise.resolve({key: '', signature: ''}),
id: walletId,
network: Chain.Network.Mainnet,
networkId: 1,
confirmConnection: async () => true,
getBalance: () => CSL.Value.fromHex('1a062ea8a0'),
Expand Down
9 changes: 5 additions & 4 deletions packages/dapp-connector/src/dapp-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {resolverHandleEvent, ResolverWallet} from './resolver'
import {connectWallet} from './connector'
import {DappConnection, Storage} from './adapters/async-storage'
import {Api, DappListResponse} from './adapters/api'
import {Chain} from '@yoroi/types'

export type DappConnectorManager = {
getDAppList(): Promise<DappListResponse>
Expand All @@ -15,7 +16,7 @@ export type DappConnectorManager = {
trustedUrl: string,
sendMessage: (id: string, result: unknown, error?: Error) => void,
): Promise<void>
readonly chainId: number
readonly network: Chain.SupportedNetworks
readonly walletId: string
}

Expand All @@ -24,15 +25,15 @@ export const dappConnectorMaker = (storage: Storage, wallet: ResolverWallet, api
}

export class DappConnector implements DappConnectorManager {
chainId: number
network: Chain.SupportedNetworks
walletId: string
constructor(private storage: Storage, private wallet: ResolverWallet, private api: Api) {
this.chainId = wallet.networkId
this.network = wallet.network
this.walletId = wallet.id
}

async getDAppList() {
return this.api.getDApps({chainId: this.wallet.networkId})
return this.api.getDApps({network: this.wallet.network})
}

async listAllConnections() {
Expand Down
3 changes: 2 additions & 1 deletion packages/dapp-connector/src/manager.mocks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {DappConnectorManager} from './dapp-connector'
import {DappListResponse} from './adapters/api'
import {Chain} from '@yoroi/types'

export const managerMock: DappConnectorManager = {
chainId: 1,
network: Chain.Network.Mainnet,
walletId: 'walletId',
getDAppList(): Promise<DappListResponse> {
return Promise.resolve(mockedDAppList)
Expand Down
2 changes: 2 additions & 0 deletions packages/dapp-connector/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Storage} from './adapters/async-storage'
import {z} from 'zod'
import {Address, TransactionUnspentOutput, TransactionWitnessSet, Value} from '@emurgo/cross-csl-core'
import BigNumber from 'bignumber.js'
import {Chain} from '@yoroi/types'

type Context = {
browserOrigin: string
Expand Down Expand Up @@ -336,6 +337,7 @@ export const resolverHandleEvent = async (
export type ResolverWallet = {
id: string
networkId: number
network: Chain.SupportedNetworks
confirmConnection: (dappOrigin: string) => Promise<boolean>
getBalance: (tokenId?: string) => Promise<Value>
getUnusedAddresses: () => Promise<Address[]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const useDappList = (
options?: UseQueryOptions<DappListResponse, Error, DappListResponse, [string, 'dappList', string]>,
) => {
const {manager} = useDappConnector()
return useQuery([manager.walletId, 'dappList', String(manager.chainId)], {
return useQuery([manager.walletId, 'dappList', String(manager.network)], {
queryFn: () => manager.getDAppList(),
refetchOnMount: false,
refetchInterval: ONE_DAY,
Expand Down

0 comments on commit 2826a73

Please sign in to comment.