Skip to content

Commit

Permalink
chore(wallet-mobile): impl explorers v1.0.1 (#3553)
Browse files Browse the repository at this point in the history
  • Loading branch information
stackchain authored Aug 22, 2024
1 parent 6c41444 commit de1145a
Show file tree
Hide file tree
Showing 17 changed files with 321 additions and 48 deletions.
2 changes: 1 addition & 1 deletion apps/wallet-mobile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
"@yoroi/api": "1.5.1",
"@yoroi/common": "1.5.3",
"@yoroi/exchange": "2.1.0",
"@yoroi/explorers": "1.0.0",
"@yoroi/explorers": "1.0.1",
"@yoroi/identicon": "1.0.0",
"@yoroi/links": "1.5.6",
"@yoroi/portfolio": "1.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@ export const buildPortfolioTokenManagers = () => {
const mainnetPortfolioTokenManager = buildPortfolioTokenManager({network: Chain.Network.Mainnet})
const preprodPortfolioTokenManager = buildPortfolioTokenManager({network: Chain.Network.Preprod})
const sanchoPortfolioTokenManager = buildPortfolioTokenManager({network: Chain.Network.Sancho})
const previewPortfolioTokenManager = buildPortfolioTokenManager({network: Chain.Network.Preview})

const tokenManagers: Readonly<{
[Chain.Network.Mainnet]: Portfolio.Manager.Token
[Chain.Network.Preprod]: Portfolio.Manager.Token
[Chain.Network.Sancho]: Portfolio.Manager.Token
[Chain.Network.Preview]: Portfolio.Manager.Token
}> = freeze(
{
[Chain.Network.Mainnet]: mainnetPortfolioTokenManager.tokenManager,
[Chain.Network.Preprod]: preprodPortfolioTokenManager.tokenManager,
[Chain.Network.Sancho]: sanchoPortfolioTokenManager.tokenManager,
[Chain.Network.Preview]: previewPortfolioTokenManager.tokenManager,
},
true,
)
Expand All @@ -47,11 +50,13 @@ export const buildPortfolioTokenManagers = () => {
[Chain.Network.Mainnet]: App.Storage<false, Portfolio.Token.Id>
[Chain.Network.Preprod]: App.Storage<false, Portfolio.Token.Id>
[Chain.Network.Sancho]: App.Storage<false, Portfolio.Token.Id>
[Chain.Network.Preview]: App.Storage<false, Portfolio.Token.Id>
}> = freeze(
{
[Chain.Network.Mainnet]: mainnetPortfolioTokenManager.storage,
[Chain.Network.Preprod]: preprodPortfolioTokenManager.storage,
[Chain.Network.Sancho]: sanchoPortfolioTokenManager.storage,
[Chain.Network.Preview]: previewPortfolioTokenManager.storage,
},
true,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {Chain} from '@yoroi/types'

import {buildPortfolioTokenManagers} from '../../../Portfolio/common/helpers/build-token-managers'
import {buildNetworkManagers, byronEraConfig, shelleyEraConfig, shelleyPreprodEraConfig} from '../network-manager'
import {dateToEpochInfo} from './date-to-epoch-info'
Expand All @@ -8,7 +10,7 @@ describe('dateToEpochInfo', () => {
tokenManagers: buildPortfolioTokenManagers().tokenManagers,
})
it('should return the correct epoch information', () => {
const convertDateToEpoch = dateToEpochInfo(networkManagers['mainnet'].eras)
const convertDateToEpoch = dateToEpochInfo(networkManagers[Chain.Network.Mainnet].eras)

const inputDate = new Date('2024-05-14T12:30:00Z')
const expectedOutput = {
Expand All @@ -24,15 +26,15 @@ describe('dateToEpochInfo', () => {
})

it('should throw an error for a date before the start of the known eras', () => {
const convertDateToEpoch = dateToEpochInfo(networkManagers['mainnet'].eras)
const convertDateToEpoch = dateToEpochInfo(networkManagers[Chain.Network.Mainnet].eras)

const inputDate = new Date('1998-12-31T23:59:59Z')

expect(() => convertDateToEpoch(inputDate)).toThrow('Date is before the start of the known eras')
})

it('should return the first byron epoch - 0', () => {
const convertDateToEpoch = dateToEpochInfo(networkManagers['mainnet'].eras)
const convertDateToEpoch = dateToEpochInfo(networkManagers[Chain.Network.Mainnet].eras)

const inputDate = new Date(1506203091000)
const expectedOutput = {
Expand All @@ -48,7 +50,7 @@ describe('dateToEpochInfo', () => {
})

it('should return the last byron epoch - 207', () => {
const convertDateToEpoch = dateToEpochInfo(networkManagers['mainnet'].eras)
const convertDateToEpoch = dateToEpochInfo(networkManagers[Chain.Network.Mainnet].eras)

const inputDate = new Date('2020-07-29T21:44:50.000Z')
const expectedOutput = {
Expand All @@ -64,7 +66,7 @@ describe('dateToEpochInfo', () => {
})

it('should return the first shelley epoch - 208', () => {
const convertDateToEpoch = dateToEpochInfo(networkManagers['mainnet'].eras)
const convertDateToEpoch = dateToEpochInfo(networkManagers[Chain.Network.Mainnet].eras)

const inputDate = new Date('2020-07-29T21:44:51.000Z')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import {WalletFactory} from '../../common/types'
const ShelleyWalletMainnet = makeCardanoWallet(networkManagers[Chain.Network.Mainnet], 'cardano-cip1852', MAINNET)
const ShelleyWalletTestnet = makeCardanoWallet(networkManagers[Chain.Network.Preprod], 'cardano-cip1852', TESTNET)
const ShelleySanchonetWallet = makeCardanoWallet(networkManagers[Chain.Network.Sancho], 'cardano-cip1852', SANCHONET)
const ShelleyPreviewWallet = makeCardanoWallet(networkManagers[Chain.Network.Preview], 'cardano-cip1852', SANCHONET)
const ByronWallet = makeCardanoWallet(networkManagers[Chain.Network.Mainnet], 'cardano-bip44', MAINNET)
const ByronWalletTestnet = makeCardanoWallet(networkManagers[Chain.Network.Preprod], 'cardano-bip44', TESTNET)
const ByronSanchonetWallet = makeCardanoWallet(networkManagers[Chain.Network.Sancho], 'cardano-bip44', SANCHONET)
const ByronPreviewWallet = makeCardanoWallet(networkManagers[Chain.Network.Preview], 'cardano-bip44', SANCHONET)

/**
* Retrieves the wallet factory based on the network and implementation ID
Expand Down Expand Up @@ -45,6 +47,10 @@ export function getWalletFactory({
'cardano-cip1852': ShelleySanchonetWallet,
'cardano-bip44': ByronSanchonetWallet,
},
[Chain.Network.Preview]: /* cardano preview */ {
'cardano-cip1852': ShelleyPreviewWallet,
'cardano-bip44': ByronPreviewWallet,
},
} as const)

const networkImplementations = walletMap[network]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,17 @@ export const networkConfigs: Readonly<Record<Chain.SupportedNetworks, Readonly<N
legacyApiBaseUrl: 'https://sanchonet-backend.yoroiwallet.com/api',
},
// NOTE: not supported yet on mobile
// [Chain.Network.Preview]: {
// network: Chain.Network.Preview,
// primaryTokenInfo: primaryTokenInfoAnyTestnet,
// chainId: 0,
// protocolMagic: 2,
// eras: [shelleyEraConfig],
// name: 'Preview',
// isMainnet: false,
// },
[Chain.Network.Preview]: {
network: Chain.Network.Preview,
primaryTokenInfo: primaryTokenInfoAnyTestnet,
chainId: 0,
protocolMagic: 2,
eras: [shelleyEraConfig],
name: 'Preview',
isMainnet: false,

legacyApiBaseUrl: 'https://preview-backend.emurgornd.com/api',
},
})

export function buildNetworkManagers({
Expand Down
1 change: 1 addition & 0 deletions apps/wallet-mobile/src/kernel/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const governaceAfterBlock = freeze({
// TODO: Add block number once known
[Chain.Network.Mainnet]: Infinity,
[Chain.Network.Preprod]: Infinity,
[Chain.Network.Preview]: Infinity,
})

export const agreementDate = 1691967600000
Expand Down
9 changes: 6 additions & 3 deletions packages/api/src/cardano/api/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import {Chain} from '@yoroi/types'
export const API_ENDPOINTS: Readonly<
Record<Chain.SupportedNetworks, {root: string}>
> = {
mainnet: {
[Chain.Network.Mainnet]: {
root: 'https://zero.yoroiwallet.com',
},
preprod: {
[Chain.Network.Preprod]: {
root: 'https://yoroi-backend-zero-preprod.emurgornd.com',
},
sancho: {
[Chain.Network.Sancho]: {
root: 'https://yoroi-backend-zero-sanchonet.emurgornd.com',
},
[Chain.Network.Preview]: {
root: 'https://yoroi-backend-zero-preview.emurgornd.com',
},
} as const
2 changes: 1 addition & 1 deletion packages/dapp-connector/src/adapters/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,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({network: Chain.Network.Preview} as any),
dappConnectorApiMaker({request: fakeFetchData as any}).getDApps({network: Chain.Network.Testnet} as any),
).rejects.toThrow()
})
})
Expand Down
11 changes: 6 additions & 5 deletions packages/dapp-connector/src/adapters/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import {AxiosRequestConfig} from 'axios'
import {z} from 'zod'
import {Chain} from '@yoroi/types'

const dappListHosts = {
mainnet: 'https://daehx1qv45z7c.cloudfront.net/data.json',
preprod: 'https://daehx1qv45z7c.cloudfront.net/preprod.json',
sancho: 'https://daehx1qv45z7c.cloudfront.net/sancho.json',
} as const
const dappListHosts: Readonly<Record<Chain.SupportedNetworks, string>> = freeze({
[Chain.Network.Mainnet]: 'https://daehx1qv45z7c.cloudfront.net/data.json',
[Chain.Network.Preprod]: 'https://daehx1qv45z7c.cloudfront.net/preprod.json',
[Chain.Network.Sancho]: 'https://daehx1qv45z7c.cloudfront.net/sancho.json',
[Chain.Network.Preview]: 'https://daehx1qv45z7c.cloudfront.net/preview.json',
})

const initialDeps = freeze({request: fetchData}, true)

Expand Down
126 changes: 125 additions & 1 deletion packages/explorers/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,125 @@
# Explorer package for Yoroi
# @yoroi/explorers

## Overview

The `@yoroi/explorers` is a TypeScript package designed to manage and interact with various blockchain explorers supported by the Yoroi Wallet. The package provides a consistent interface for generating URLs for different blockchain networks and explorers.

## Installation

To install the package, you can use npm or yarn:

```bash
npm install @yoroi/explorers
```

or

```bash
yarn add @yoroi/explorers
```

## Usage

To use `@yoroi/explorers`, you can import the necessary types and functions to generate URLs for tokens, addresses, transactions, pools, and stake keys.

Example

```typescript
import {Chain, Explorers} from '@yoroi/types'
import {explorerManager} from '@yoroi/explorers'

// Build the manager
const mainnetExplorer = explorerManager[Chain.Network.Mainnet]

// Generate URLs using CardanoScan explorer
const tokenUrl = mainnetExplorer[Explorers.Explorer.CardanoScan].token('fingerprint')
const addressUrl = mainnetExplorer[Explorers.Explorer.CardanoScan].address('address')
const txUrl = mainnetExplorer[Explorers.Explorer.CardanoScan].tx('txHash')
const poolUrl = mainnetExplorer[Explorers.Explorer.CardanoScan].pool('poolId')
const stakeUrl = mainnetExplorer[Explorers.Explorer.CardanoScan].stake('stakeAddress')

console.log(tokenUrl) // Output: https://cardanoscan.io/token/fingerprint
console.log(addressUrl) // Output: https://cardanoscan.io/address/address
console.log(txUrl) // Output: https://cardanoscan.io/transaction/txHash
console.log(poolUrl) // Output: https://cardanoscan.io/pool/poolId
console.log(stakeUrl) // Output: https://cardanoscan.io/stakeKey/stakeAddress
```

This example shows how to use the explorerManager object to generate URLs for different entities on the Mainnet using the CardanoScan explorer.

## About Supported Networks and Explorers

The following networks and explorers are supported:

Networks:

1. Mainnet: The live network for Cardano.
2. Preprod: The pre-production network for testing.
3. Sancho: The temporary Conway test network for governance.
4. Preview: A preview network for new features.

Explorers:

1. CardanoScan: A popular Cardano blockchain explorer.
1. [Twitter ![X](https://img.icons8.com/ios-filled/12/000000/x.png)](https://twitter.com/cardanoscan.io)
2. [Discord ![Discord](https://img.icons8.com/ios-filled/12/000000/discord-logo.png)](https://discord.gg/WQFPHNXcz8)
3. [Explorer](https://cardanoscan.io)

2. CExplorer: Another Cardano blockchain explorer.
1. [Explorer](https://cexplorer.io)

## URL Generation

For each network and explorer, the following URL generation methods are available:

`token(fingerprint: string)`

`address(address: string)`

`tx(txHash: string)`

`pool(poolId: string)`

`stake(stakeAddress: string)`

## Adding a New Explorer

If you are a developer working on a new Cardano explorer and want to add support for it in Yoroi, you can do so by following these steps:

1. Fork the Repository
Start by forking the `@yoroi/explorers` repository on GitHub.

2. Add Your Explorer URLs
In the codebase, navigate to the `explorer-manager.ts` file. Add your explorer under the appropriate network with the URL generation methods (token, address, tx, pool, stake, etc).

For example:

```typescript
[Chain.Network.Mainnet]: {
[Explorers.Explorer.YourExplorerName]: {
token: (fingerprint: string) => `https://yourexplorer.io/token/${fingerprint}`,
address: (address: string) => `https://yourexplorer.io/address/${address}`,
tx: (txHash: string) => `https://yourexplorer.io/transaction/${txHash}`,
pool: (poolId: string) => `https://yourexplorer.io/pool/${poolId}`,
stake: (stakeAddress: string) => `https://yourexplorer.io/stake/${stakeAddress}`,
},
},
```

3. Test Your Implementation
Write unit tests to ensure that the URLs generated for your explorer are correct. You can follow the existing test patterns in the explorerManager test suite, 100% coverage is expected.

Running Tests

```bash
yarn test
```

This will run the unit tests to ensure that everything is working as expected.

4. Submit a Pull Request (PR)
Once you've implemented and tested your explorer integration, submit a PR to the repository. The Yoroi development team will review your submission.

## Contributing

We welcome contributions from the community! If you find a bug or have a feature request, please open an issue or submit a pull request.
2 changes: 1 addition & 1 deletion packages/explorers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yoroi/explorers",
"version": "1.0.0",
"version": "1.0.1",
"description": "The Explorers package of Yoroi SDK",
"keywords": [
"yoroi",
Expand Down
Loading

0 comments on commit de1145a

Please sign in to comment.