Skip to content

Commit

Permalink
887 Wallet custom config by chain (#898)
Browse files Browse the repository at this point in the history
* Wallet can handle custom config by chain

* Version update

* thorchain-amm version updated
  • Loading branch information
0xp3gasus authored Nov 7, 2023
1 parent 3f69599 commit f444b75
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 35 deletions.
6 changes: 6 additions & 0 deletions packages/xchain-thorchain-amm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# v0.7.13 (2023-11-05)

## Update

- Wallet can be initialised with a custom config by chain

# v0.7.12 (2023-11-05)

## Update
Expand Down
43 changes: 41 additions & 2 deletions packages/xchain-thorchain-amm/__e2e__/wallet.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cosmosclient from '@cosmos-client/core'
import { Network } from '@xchainjs/xchain-client'
import { ExplorerProvider, Network } from '@xchainjs/xchain-client'
import {
AssetBTC,
AssetETH,
Expand All @@ -13,7 +13,7 @@ import {
import { baseToAsset, formatAssetAmountCurrency, register9Rheader } from '@xchainjs/xchain-util'
import axios from 'axios'

import { Wallet } from '../src/wallet'
import { ChainConfigs, Wallet } from '../src/wallet'

require('dotenv').config()

Expand Down Expand Up @@ -127,4 +127,43 @@ describe('xchain-swap wallet Tests', () => {
console.error(e)
}
})

it(`Can init wallet with custom config`, async () => {
try {
const customConfig: ChainConfigs = {
[BTCChain]: {
explorerProviders: {
[Network.Mainnet]: new ExplorerProvider(
'https://custom.mainnet.provider',
'https://custom.mainnet.provider/address/%%ADDRESS%%',
'https://custom.mainnet.provider/tx/%%TX_ID%%',
),
[Network.Testnet]: new ExplorerProvider(
'https://custom.testnet.provider',
'https://custom.testnet.provider/address/%%ADDRESS%%',
'https://custom.testnet.provider/tx/%%TX_ID%%',
),
[Network.Stagenet]: new ExplorerProvider(
'https://custom.stagenet.provider',
'https://custom.stagenet.provider/address/%%ADDRESS%%',
'https://custom.stagenet.provider/tx/%%TX_ID%%',
),
},
dataProviders: [],
},
}
const wallet = new Wallet(
process.env.MAINNETPHRASE || 'you forgot to set the phrase',
thorchainQueryMainnet,
customConfig,
)
for (const [chain, client] of Object.entries(wallet.clients)) {
console.log(`${chain} config`)
console.log(`Network: ${client.getNetwork()}`)
console.log(`Explorer provider: ${client.getExplorerUrl()}`)
}
} catch (e) {
console.error(e)
}
})
})
2 changes: 1 addition & 1 deletion packages/xchain-thorchain-amm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@xchainjs/xchain-thorchain-amm",
"version": "0.7.12",
"version": "0.7.13",
"description": "module that exposes estimating & swappping cryptocurrency assets on thorchain",
"keywords": [
"THORChain",
Expand Down
71 changes: 39 additions & 32 deletions packages/xchain-thorchain-amm/src/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Client as AvaxClient, defaultAvaxParams } from '@xchainjs/xchain-avax'
import { Client as BnbClient } from '@xchainjs/xchain-binance'
import { BTCChain, Client as BtcClient } from '@xchainjs/xchain-bitcoin'
import { Client as BchClient } from '@xchainjs/xchain-bitcoincash'
import { Client as BscClient, defaultBscParams } from '@xchainjs/xchain-bsc'
import { FeeOption, Network, XChainClient } from '@xchainjs/xchain-client'
import { Client as CosmosClient } from '@xchainjs/xchain-cosmos'
import { Client as DogeClient } from '@xchainjs/xchain-doge'
import { Client as EthClient, defaultEthParams } from '@xchainjs/xchain-ethereum'
import { Client as LtcClient } from '@xchainjs/xchain-litecoin'
import { Client as MayaClient } from '@xchainjs/xchain-mayachain'
import { Client as ThorClient, THORChain, ThorchainClient } from '@xchainjs/xchain-thorchain'
import { AVAXChain, Client as AvaxClient, defaultAvaxParams } from '@xchainjs/xchain-avax'
import { BNBChain, Client as BnbClient } from '@xchainjs/xchain-binance'
import { BTCChain, Client as BtcClient, defaultBTCParams as defaultBtcParams } from '@xchainjs/xchain-bitcoin'
import { BCHChain, Client as BchClient, defaultBchParams } from '@xchainjs/xchain-bitcoincash'
import { BSCChain, Client as BscClient, defaultBscParams } from '@xchainjs/xchain-bsc'
import { FeeOption, Network, UtxoClientParams, XChainClient, XChainClientParams } from '@xchainjs/xchain-client'
import { Client as CosmosClient, GAIAChain } from '@xchainjs/xchain-cosmos'
import { Client as DogeClient, DOGEChain, defaultDogeParams } from '@xchainjs/xchain-doge'
import { Client as EthClient, ETHChain, defaultEthParams } from '@xchainjs/xchain-ethereum'
import { EVMClientParams } from '@xchainjs/xchain-evm'
import { Client as LtcClient, LTCChain, defaultLtcParams } from '@xchainjs/xchain-litecoin'
import { Client as MayaClient, MAYAChain, MayachainClientParams } from '@xchainjs/xchain-mayachain'
import { Client as ThorClient, THORChain, ThorchainClient, ThorchainClientParams } from '@xchainjs/xchain-thorchain'
import { CryptoAmount, ThorchainQuery } from '@xchainjs/xchain-thorchain-query'
import { Address, Asset, assetFromString } from '@xchainjs/xchain-util'

Expand All @@ -28,6 +29,19 @@ import { EvmHelper } from './utils/evm-helper'

export type NodeUrls = Record<Network, string>

export type ChainConfigs = Partial<{
[BTCChain]: Omit<UtxoClientParams, 'phrase' | 'network'>
[BCHChain]: Omit<UtxoClientParams, 'phrase' | 'network'>
[LTCChain]: Omit<UtxoClientParams, 'phrase' | 'network'>
[DOGEChain]: Omit<UtxoClientParams, 'phrase' | 'network'>
[ETHChain]: Omit<EVMClientParams, 'phrase' | 'network'>
[AVAXChain]: Omit<EVMClientParams, 'phrase' | 'network'>
[BSCChain]: Omit<EVMClientParams, 'phrase' | 'network'>
[GAIAChain]: Omit<XChainClientParams, 'phrase' | 'network'>
[BNBChain]: Omit<XChainClientParams, 'phrase' | 'network'>
[THORChain]: Omit<XChainClientParams & ThorchainClientParams, 'phrase' | 'network'>
[MAYAChain]: Omit<XChainClientParams & MayachainClientParams, 'phrase' | 'network'>
}>
/**
* Wallet Class for managing all xchain-* wallets with a mnemonic seed.
*/
Expand All @@ -40,33 +54,26 @@ export class Wallet {
*
* @param phrase - mnemonic phrase
* @param thorchainCache - an instance of the ThorchainCache (could be pointing to stagenet,testnet,mainnet)
* @param chainConfigs - Config by chain
* @returns Wallet
*/
constructor(phrase: string, thorchainQuery: ThorchainQuery) {
constructor(phrase: string, thorchainQuery: ThorchainQuery, chainConfigs: ChainConfigs = {}) {
this.thorchainQuery = thorchainQuery

const settings = { network: this.thorchainQuery.thorchainCache.midgardQuery.midgardCache.midgard.network, phrase }
this.clients = {
BCH: new BchClient(),
BTC: new BtcClient(),
DOGE: new DogeClient(),
LTC: new LtcClient(),
THOR: new ThorClient(settings),
BNB: new BnbClient(settings),
GAIA: new CosmosClient(settings),
MAYA: new MayaClient(settings),
ETH: new EthClient({ ...defaultEthParams, network: settings.network, phrase }),
AVAX: new AvaxClient({ ...defaultAvaxParams, network: settings.network, phrase }),
BSC: new BscClient({ ...defaultBscParams, network: settings.network, phrase }),
BCH: new BchClient({ ...defaultBchParams, ...chainConfigs[BCHChain], ...settings }),
BTC: new BtcClient({ ...defaultBtcParams, ...chainConfigs[BTCChain], ...settings }),
DOGE: new DogeClient({ ...defaultDogeParams, ...chainConfigs[DOGEChain], ...settings }),
LTC: new LtcClient({ ...defaultLtcParams, ...chainConfigs[LTCChain], ...settings }),
THOR: new ThorClient({ ...chainConfigs[THORChain], ...settings }),
BNB: new BnbClient({ ...chainConfigs[BNBChain], ...settings }),
GAIA: new CosmosClient({ ...chainConfigs[GAIAChain], ...settings }),
MAYA: new MayaClient({ ...chainConfigs[MAYAChain], ...settings }),
ETH: new EthClient({ ...defaultEthParams, ...chainConfigs[ETHChain], ...settings }),
AVAX: new AvaxClient({ ...defaultAvaxParams, ...chainConfigs[AVAXChain], ...settings }),
BSC: new BscClient({ ...defaultBscParams, ...chainConfigs[BSCChain], ...settings }),
}
this.clients.BCH.setNetwork(settings.network)
this.clients.BCH.setPhrase(settings.phrase, 0)
this.clients.BTC.setNetwork(settings.network)
this.clients.BTC.setPhrase(settings.phrase, 0)
this.clients.DOGE.setNetwork(settings.network)
this.clients.DOGE.setPhrase(settings.phrase, 0)
this.clients.LTC.setNetwork(settings.network)
this.clients.LTC.setPhrase(settings.phrase, 0)

this.evmHelpers = {
ETH: new EvmHelper(this.clients.ETH, this.thorchainQuery.thorchainCache),
Expand Down

0 comments on commit f444b75

Please sign in to comment.