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

1022 setNetwotk cosmos-sdk bug fix #1023

Merged
merged 3 commits into from
Jan 30, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/fresh-walls-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-cosmos-sdk': patch
---

getPrefix abstract function
5 changes: 5 additions & 0 deletions .changeset/smart-rice-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@xchainjs/xchain-cosmos-sdk': patch
---

setNetwork function bug fix
6 changes: 6 additions & 0 deletions .changeset/yellow-melons-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@xchainjs/xchain-thorchain': patch
'@xchainjs/xchain-kujira': patch
---

getPrefix protected function
19 changes: 16 additions & 3 deletions packages/xchain-cosmos-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export enum MsgTypes {
export default abstract class Client extends BaseXChainClient implements XChainClient {
private readonly defaultDecimals: number
private readonly defaultFee: BaseAmount
protected readonly startgateClient: CachedValue<StargateClient>
protected startgateClient: CachedValue<StargateClient>
protected prefix: string
protected readonly clientUrls: Record<Network, string>
protected readonly prefix: string
protected readonly baseDenom: string
protected readonly registry: Registry
/**
Expand All @@ -69,7 +69,7 @@ export default abstract class Client extends BaseXChainClient implements XChainC
constructor(params: CosmosSdkClientParams) {
super(params.chain, params)
this.clientUrls = params.clientUrls
this.prefix = params.prefix
this.prefix = this.getPrefix(this.getNetwork())
this.defaultDecimals = params.defaultDecimals
this.defaultFee = params.defaultFee
this.baseDenom = params.baseDenom
Expand All @@ -83,6 +83,18 @@ export default abstract class Client extends BaseXChainClient implements XChainC
return StargateClient.connect(clientUrl)
}

/**
* Set client network to work with.
*
* @param {Network} network
* @returns {void}
*/
public setNetwork(network: Network): void {
super.setNetwork(network)
this.startgateClient = new CachedValue<StargateClient>(() => this.connectClient(this.clientUrls[network]))
this.prefix = this.getPrefix(network)
}

/**
* @private
* Split on amount and denom strings with format 300000uatom
Expand Down Expand Up @@ -399,6 +411,7 @@ export default abstract class Client extends BaseXChainClient implements XChainC
abstract getDenom(asset: Asset): string | null
protected abstract getMsgTypeUrlByType(msgType: MsgTypes): string
protected abstract getStandardFee(asset: Asset): StdFee
protected abstract getPrefix(network: Network): string
}

export { Client }
9 changes: 9 additions & 0 deletions packages/xchain-kujira/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ export class Client extends CosmosSdkClient {
})
}

/**
* Get address prefix by network
* @param {Network} network The network of which return the prefix
* @returns the address prefix
*/
protected getPrefix(): string {
return 'kujira'
}

getAssetInfo(): AssetInfo {
const assetInfo: AssetInfo = {
asset: AssetKUJI,
Expand Down
14 changes: 12 additions & 2 deletions packages/xchain-thorchain/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,22 @@ describe('Thorchain client', () => {
expect(client.validateAddress('thor1k2e50ws3d9lce9ycr7ppaazx3ygaa7lxj8kkny')).toBeTruthy()
expect(client.validateAddress('0x42D5B09a92A31AfB875e1E40ae4b06f2A60890FC')).toBeFalsy()
})
it('Should change network and validate address', () => {
const client = new Client({ network: Network.Stagenet })
client.setNetwork(Network.Mainnet)
expect(client.validateAddress('thor1k2e50ws3d9lce9ycr7ppaazx3ygaa7lxj8kkny')).toBeTruthy()
})
})
describe('Stagenet', () => {
it('Should validate address', () => {
const client = new Client({ network: Network.Stagenet, prefix: 'sthor' })
const client = new Client({ network: Network.Stagenet })
expect(client.validateAddress('sthor17gw75axcnr8747pkanye45pnrwk7p9c3ve0wxj')).toBeTruthy()
})

it('Should change network and validate address', () => {
const client = new Client()
client.setNetwork(Network.Stagenet)
expect(client.validateAddress('sthor17gw75axcnr8747pkanye45pnrwk7p9c3ve0wxj')).toBeTruthy()
expect(client.validateAddress('0x42D5B09a92A31AfB875e1E40ae4b06f2A60890FC')).toBeFalsy()
})
})
})
Expand Down
18 changes: 17 additions & 1 deletion packages/xchain-thorchain/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
decodeTxRaw,
} from '@cosmjs/proto-signing'
import { SigningStargateClient } from '@cosmjs/stargate'
import { AssetInfo, PreparedTx, TxHash, TxParams } from '@xchainjs/xchain-client'
import { AssetInfo, Network, PreparedTx, TxHash, TxParams } from '@xchainjs/xchain-client'
import {
Client as CosmosSDKClient,
CosmosSdkClientParams,
Expand Down Expand Up @@ -64,6 +64,22 @@ export class Client extends CosmosSDKClient implements ThorchainClient {
})
}

/**
* Get address prefix by network
* @param {Network} network The network of which return the prefix
* @returns the address prefix
*/
protected getPrefix(network: Network): string {
switch (network) {
case Network.Mainnet:
return 'thor'
case Network.Stagenet:
return 'sthor'
case Network.Testnet:
return 'tthor'
}
}

/**
* Get client native asset
*
Expand Down
Loading