From a72609c5c029bfc9c819d7bd68eacd34336f7b17 Mon Sep 17 00:00:00 2001 From: 0xp3gasus <0xp3gasus@proton.me> Date: Tue, 30 Jan 2024 12:15:04 +0100 Subject: [PATCH 1/3] cosmos-sdk setNetwork bug fix --- packages/xchain-cosmos-sdk/src/client.ts | 19 ++++++++++++++++--- packages/xchain-kujira/src/client.ts | 9 +++++++++ .../xchain-thorchain/__tests__/client.test.ts | 14 ++++++++++++-- packages/xchain-thorchain/src/client.ts | 18 +++++++++++++++++- 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/packages/xchain-cosmos-sdk/src/client.ts b/packages/xchain-cosmos-sdk/src/client.ts index da893f5b9..5068361ac 100644 --- a/packages/xchain-cosmos-sdk/src/client.ts +++ b/packages/xchain-cosmos-sdk/src/client.ts @@ -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 + protected startgateClient: CachedValue + protected prefix: string protected readonly clientUrls: Record - protected readonly prefix: string protected readonly baseDenom: string protected readonly registry: Registry /** @@ -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.getNetworkPrefix(this.getNetwork()) this.defaultDecimals = params.defaultDecimals this.defaultFee = params.defaultFee this.baseDenom = params.baseDenom @@ -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(() => this.connectClient(this.clientUrls[network])) + this.prefix = this.getNetworkPrefix(network) + } + /** * @private * Split on amount and denom strings with format 300000uatom @@ -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 getNetworkPrefix(network: Network): string } export { Client } diff --git a/packages/xchain-kujira/src/client.ts b/packages/xchain-kujira/src/client.ts index 9b2adb96b..7a83dddca 100644 --- a/packages/xchain-kujira/src/client.ts +++ b/packages/xchain-kujira/src/client.ts @@ -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 getNetworkPrefix(): string { + return 'kujira' + } + getAssetInfo(): AssetInfo { const assetInfo: AssetInfo = { asset: AssetKUJI, diff --git a/packages/xchain-thorchain/__tests__/client.test.ts b/packages/xchain-thorchain/__tests__/client.test.ts index f9162d18e..0103ba267 100644 --- a/packages/xchain-thorchain/__tests__/client.test.ts +++ b/packages/xchain-thorchain/__tests__/client.test.ts @@ -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() }) }) }) diff --git a/packages/xchain-thorchain/src/client.ts b/packages/xchain-thorchain/src/client.ts index 5a1b2ac78..8b5de05f9 100644 --- a/packages/xchain-thorchain/src/client.ts +++ b/packages/xchain-thorchain/src/client.ts @@ -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, @@ -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 getNetworkPrefix(network: Network): string { + switch (network) { + case Network.Mainnet: + return 'thor' + case Network.Stagenet: + return 'sthor' + case Network.Testnet: + return 'tthor' + } + } + /** * Get client native asset * From cc09bf4ce3c5432dea0e2cb0f117b057366f0170 Mon Sep 17 00:00:00 2001 From: 0xp3gasus <0xp3gasus@proton.me> Date: Tue, 30 Jan 2024 12:19:32 +0100 Subject: [PATCH 2/3] Changeset version files --- .changeset/fresh-walls-shop.md | 5 +++++ .changeset/smart-rice-try.md | 5 +++++ .changeset/yellow-melons-push.md | 6 ++++++ 3 files changed, 16 insertions(+) create mode 100644 .changeset/fresh-walls-shop.md create mode 100644 .changeset/smart-rice-try.md create mode 100644 .changeset/yellow-melons-push.md diff --git a/.changeset/fresh-walls-shop.md b/.changeset/fresh-walls-shop.md new file mode 100644 index 000000000..27e6e5252 --- /dev/null +++ b/.changeset/fresh-walls-shop.md @@ -0,0 +1,5 @@ +--- +'@xchainjs/xchain-cosmos-sdk': patch +--- + +getNetworkPrefix abstract function diff --git a/.changeset/smart-rice-try.md b/.changeset/smart-rice-try.md new file mode 100644 index 000000000..49e56e00a --- /dev/null +++ b/.changeset/smart-rice-try.md @@ -0,0 +1,5 @@ +--- +'@xchainjs/xchain-cosmos-sdk': patch +--- + +setNetwork function bug fix diff --git a/.changeset/yellow-melons-push.md b/.changeset/yellow-melons-push.md new file mode 100644 index 000000000..e5a911cea --- /dev/null +++ b/.changeset/yellow-melons-push.md @@ -0,0 +1,6 @@ +--- +'@xchainjs/xchain-thorchain': patch +'@xchainjs/xchain-kujira': patch +--- + +getNetworkPrefix protected function From 60b1588df2b05a0d7e7c9a3de9d0539f52eb9e6a Mon Sep 17 00:00:00 2001 From: 0xp3gasus <0xp3gasus@proton.me> Date: Tue, 30 Jan 2024 12:29:46 +0100 Subject: [PATCH 3/3] getNetworkPrefix renamed to getPrefix --- .changeset/fresh-walls-shop.md | 2 +- .changeset/yellow-melons-push.md | 2 +- packages/xchain-cosmos-sdk/src/client.ts | 6 +++--- packages/xchain-kujira/src/client.ts | 2 +- packages/xchain-thorchain/src/client.ts | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.changeset/fresh-walls-shop.md b/.changeset/fresh-walls-shop.md index 27e6e5252..a44915253 100644 --- a/.changeset/fresh-walls-shop.md +++ b/.changeset/fresh-walls-shop.md @@ -2,4 +2,4 @@ '@xchainjs/xchain-cosmos-sdk': patch --- -getNetworkPrefix abstract function +getPrefix abstract function diff --git a/.changeset/yellow-melons-push.md b/.changeset/yellow-melons-push.md index e5a911cea..50d56eb1a 100644 --- a/.changeset/yellow-melons-push.md +++ b/.changeset/yellow-melons-push.md @@ -3,4 +3,4 @@ '@xchainjs/xchain-kujira': patch --- -getNetworkPrefix protected function +getPrefix protected function diff --git a/packages/xchain-cosmos-sdk/src/client.ts b/packages/xchain-cosmos-sdk/src/client.ts index 5068361ac..ff38e6c65 100644 --- a/packages/xchain-cosmos-sdk/src/client.ts +++ b/packages/xchain-cosmos-sdk/src/client.ts @@ -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 = this.getNetworkPrefix(this.getNetwork()) + this.prefix = this.getPrefix(this.getNetwork()) this.defaultDecimals = params.defaultDecimals this.defaultFee = params.defaultFee this.baseDenom = params.baseDenom @@ -92,7 +92,7 @@ export default abstract class Client extends BaseXChainClient implements XChainC public setNetwork(network: Network): void { super.setNetwork(network) this.startgateClient = new CachedValue(() => this.connectClient(this.clientUrls[network])) - this.prefix = this.getNetworkPrefix(network) + this.prefix = this.getPrefix(network) } /** @@ -411,7 +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 getNetworkPrefix(network: Network): string + protected abstract getPrefix(network: Network): string } export { Client } diff --git a/packages/xchain-kujira/src/client.ts b/packages/xchain-kujira/src/client.ts index 7a83dddca..5b014cceb 100644 --- a/packages/xchain-kujira/src/client.ts +++ b/packages/xchain-kujira/src/client.ts @@ -25,7 +25,7 @@ export class Client extends CosmosSdkClient { * @param {Network} network The network of which return the prefix * @returns the address prefix */ - protected getNetworkPrefix(): string { + protected getPrefix(): string { return 'kujira' } diff --git a/packages/xchain-thorchain/src/client.ts b/packages/xchain-thorchain/src/client.ts index 8b5de05f9..d88348677 100644 --- a/packages/xchain-thorchain/src/client.ts +++ b/packages/xchain-thorchain/src/client.ts @@ -69,7 +69,7 @@ export class Client extends CosmosSDKClient implements ThorchainClient { * @param {Network} network The network of which return the prefix * @returns the address prefix */ - protected getNetworkPrefix(network: Network): string { + protected getPrefix(network: Network): string { switch (network) { case Network.Mainnet: return 'thor'