From 986a6583e631d0a5aed41208746610cc906362c2 Mon Sep 17 00:00:00 2001 From: marston Date: Mon, 14 Oct 2024 10:26:53 -0400 Subject: [PATCH] updating ica workflow --- package-lock.json | 14 ++-- package.json | 2 +- src/classes/clientHandler.ts | 18 +++++- src/classes/wasmHandler.ts | 72 +++++++++++++++++++-- src/interfaces/classes/IWasmHandler.ts | 8 +++ src/interfaces/options/IBroadcastOptions.ts | 1 + src/utils/misc.ts | 3 +- 7 files changed, 105 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index acf6238..e5a93a6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,7 @@ "@cosmjs/stargate": "^0.32.2", "@jackallabs/bech32": "^1.2.0", "@jackallabs/dogwood-tree": "^1.0.1", - "@jackallabs/jackal.js-protos": "2.3.0", + "@jackallabs/jackal.js-protos": "file:jackallabs-jackal.js-protos-2.3.0.tgz", "@karnthis/plzsu": "^1.0.0", "@keplr-wallet/types": "^0.12.44", "browserify-aes": "^1.2.0", @@ -929,8 +929,9 @@ }, "node_modules/@jackallabs/banshee": { "version": "1.0.1", - "resolved": "https://registry.npmjs.org/@jackallabs/banshee/-/banshee-1.0.1.tgz", - "integrity": "sha512-ouhmRnjUK1JDoB2+wcEadUnsTm0y5obslTZyotzGXzfxHi5G8RWtfUuDAC2VQFWP6pbFkv+lJRZUXWyGnXXvTA==", + "resolved": "file:jackallabs-banshee-1.0.1.tgz", + "integrity": "sha512-s9R+9if9i+sawwrys2rbTgWFYCduu/7XQgsI1b9/ogvvwR3ZIFaF3lBBBtXtIvb5bVdW3Q4emyYHc4vKTgObAQ==", + "license": "MIT", "dependencies": { "@cosmjs/amino": "^0.32.3", "@cosmjs/launchpad": "^0.27.1", @@ -1020,8 +1021,9 @@ }, "node_modules/@jackallabs/jackal.js-protos": { "version": "2.3.0", - "resolved": "https://registry.npmjs.org/@jackallabs/jackal.js-protos/-/jackal.js-protos-2.3.0.tgz", - "integrity": "sha512-3fw8nmYfkxIzBWMzhvrWO14yxeoxEznCyon/F4l18+J4/yunKXPdvOYmsY0seTmv0hwdWl+UtToxIAqEYBi5zw==", + "resolved": "file:jackallabs-jackal.js-protos-2.3.0.tgz", + "integrity": "sha512-qup7B9QhycWflFskU7LMblq83i3SrqTkOvXFE76JmnVvYml500bvssF4Y2AhovFzsLKzds2LCXbeYlNIwvDLig==", + "license": "MIT", "dependencies": { "@cosmjs/amino": "^0.32.3", "@cosmjs/encoding": "^0.32.3", @@ -1031,7 +1033,7 @@ "@cosmjs/stargate": "^0.32.3", "@cosmjs/tendermint-rpc": "^0.32.3", "@cosmjs/utils": "^0.32.3", - "@jackallabs/banshee": "1.0.1", + "@jackallabs/banshee": "file:jackallabs-banshee-1.0.1.tgz", "cosmjs-types": "^0.8.0", "grpc-web": "^1.5.0", "protobufjs": "^7.2.5", diff --git a/package.json b/package.json index 9f9c114..9f94b45 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "@cosmjs/stargate": "^0.32.2", "@jackallabs/bech32": "^1.2.0", "@jackallabs/dogwood-tree": "^1.0.1", - "@jackallabs/jackal.js-protos": "2.3.0", + "@jackallabs/jackal.js-protos": "file:jackallabs-jackal.js-protos-2.3.0.tgz", "@karnthis/plzsu": "^1.0.0", "@keplr-wallet/types": "^0.12.44", "browserify-aes": "^1.2.0", diff --git a/src/classes/clientHandler.ts b/src/classes/clientHandler.ts index 377dad4..b35fbf7 100644 --- a/src/classes/clientHandler.ts +++ b/src/classes/clientHandler.ts @@ -285,7 +285,7 @@ export class ClientHandler implements IClientHandler { const { connIdA = 'connection-18', connIdB = 'connection-50', - contract = 'archway1wzv0qsr6wrunw5x650gc8q9r54my0r8vl7n723377ss7jq0mjlts33gpgj', + contract = 'archway1z0wngtg6qwweqh3sfcz6l42srkhqly2guxp8enf9y6sny08c4gqq35jdc4', } = details this.myCosmwasm = await WasmHandler.init(this) const ica = await this.myCosmwasm.getICAContractAddress(contract).catch(err => { @@ -302,12 +302,26 @@ export class ClientHandler implements IClientHandler { connIdA, connIdB, ) + await new Promise(r => setTimeout(r, 5000)); this.myContractAddress = await this.myCosmwasm.getICAContractAddress(contract) } console.log(this.myContractAddress) this.myIcaAddress = await this.myCosmwasm.getJackalAddressFromContract( this.myContractAddress, ) + const state = await this.myCosmwasm.getContractChannelState( + this.myContractAddress, + ) + console.log("State: ", state) + if (state === 'STATE_CLOSED') { + console.log("needs to re-open channel") + await this.myCosmwasm.reOpenChannel( + contract, + connIdA, + connIdB, + ) + } + return await StorageHandler.init(this, { accountAddress: this.myIcaAddress, }) @@ -594,6 +608,7 @@ export class ClientHandler implements IClientHandler { broadcastTimeoutHeight, monitorTimeout = 30, socketOverrides = {} as TSocketSet, + queryOverride, } = options const events: TxEvent[] = [] const ready: IWrappedEncodeObject[] = @@ -606,6 +621,7 @@ export class ClientHandler implements IClientHandler { events, msgs, this.myIcaAddress || this.hostAddress, + queryOverride, socketOverrides, ) console.log('connectionBundles:', connectionBundles) diff --git a/src/classes/wasmHandler.ts b/src/classes/wasmHandler.ts index 3bc788c..36d3e35 100644 --- a/src/classes/wasmHandler.ts +++ b/src/classes/wasmHandler.ts @@ -75,13 +75,46 @@ export class WasmHandler extends EncodingHandler implements IWasmHandler { const wrapped: IWrappedEncodeObject = { encodedObject: eo, modifier: 0 } const postBroadcast = - await this.jackalClient.broadcastAndMonitorMsgs(wrapped) + //{queryOverride: `execute._contract_address = '${contractAddress}'`} + await this.jackalClient.broadcastAndMonitorMsgs(wrapped, {queryOverride: `message.action = '/cosmwasm.wasm.v1.MsgExecuteContract' AND message.sender = '${this.hostAddress}'`}) return postBroadcast.txResponse } catch (err) { throw warnError('wasmHandler instantiateICA()', err) } } + async reOpenChannel ( + contractAddress: string, + connectionIdA: string, + connectionIdB: string + ): Promise { + try { + const msg = { + create_channel: { + channel_open_init_options: { + connection_id: connectionIdA, + counterparty_connection_id: connectionIdB, + tx_encoding: "proto3" + } + } + } + + const eo = this.jackalClient.getTxs().cosmwasm.msgExecuteContract({ + contract: contractAddress, + msg: stringToUint8Array(JSON.stringify(msg)), + funds: [], + sender: this.jackalClient.getHostAddress(), + }) + const wrapped: IWrappedEncodeObject = { encodedObject: eo, modifier: 0 } + + const postBroadcast = + await this.jackalClient.broadcastAndMonitorMsgs(wrapped, {queryOverride: `execute._contract_address = '${contractAddress}'`}) + return postBroadcast.txResponse + } catch (err) { + throw warnError('wasmHandler reOpenChannel()', err) + } + } + /** * Get Interchain wasm contract address. * @param {string} contractAddress - Contract to query from @@ -136,8 +169,39 @@ export class WasmHandler extends EncodingHandler implements IWasmHandler { * @returns {Promise} - Jkl address. */ async getJackalAddressFromContract (contractAddress: string): Promise { + const retries = 30 + let attempt = 0 + while (attempt < retries) { + try { + const q = { get_contract_state: {} } + + const req: DQuerySmartContractStateRequest = { + address: contractAddress, + queryData: stringToUint8Array(JSON.stringify(q)), + } + + console.log(req) + const res = await this.hostSigner.queries.cosmwasm.smartContractState(req) + const str = uintArrayToString(res.data as Uint8Array) + console.log(str) + const data = JSON.parse(str) + if ("ica_info" in data) { + return data.ica_info.ica_address + } + attempt ++ + await new Promise(r => setTimeout(r, 5000)); + } catch (err) { + console.warn('wasmHandler getJackalAddressFromContract()', err) + } + } + + throw warnError('wasmHandler getJackalAddressFromContract()', "can't get details from contract") + } + + + async getContractChannelState (contractAddress: string): Promise { try { - const q = { get_contract_state: {} } + const q = { get_channel: {} } const req: DQuerySmartContractStateRequest = { address: contractAddress, @@ -150,9 +214,9 @@ export class WasmHandler extends EncodingHandler implements IWasmHandler { console.log(str) const data = JSON.parse(str) - return data.ica_info.ica_address + return data.channel_status } catch (err) { - throw warnError('wasmHandler getJackalAddressFromContract()', err) + throw warnError('wasmHandler getContractChannelState()', err) } } diff --git a/src/interfaces/classes/IWasmHandler.ts b/src/interfaces/classes/IWasmHandler.ts index 72bfaff..9f6dfca 100644 --- a/src/interfaces/classes/IWasmHandler.ts +++ b/src/interfaces/classes/IWasmHandler.ts @@ -7,11 +7,19 @@ export interface IWasmHandler { connectionIdB: string ): Promise + reOpenChannel ( + contractAddress: string, + connectionIdA: string, + connectionIdB: string + ): Promise + getICAContractAddress (contractAddress: string): Promise getICAJackalAddress (contractAddress: string): Promise getJackalAddressFromContract (contractAddress: string): Promise + getContractChannelState (contractAddress: string): Promise + wrapEncodeObjectsForBroadcast (contract: string, msgs: DEncodeObject[]): DEncodeObject[] } diff --git a/src/interfaces/options/IBroadcastOptions.ts b/src/interfaces/options/IBroadcastOptions.ts index 254e10d..0610ab1 100644 --- a/src/interfaces/options/IBroadcastOptions.ts +++ b/src/interfaces/options/IBroadcastOptions.ts @@ -6,4 +6,5 @@ export interface IBroadcastOptions { broadcastTimeoutHeight?: bigint monitorTimeout?: number socketOverrides?: TSocketSet + queryOverride?: string } diff --git a/src/utils/misc.ts b/src/utils/misc.ts index d2bcea7..19c0515 100644 --- a/src/utils/misc.ts +++ b/src/utils/misc.ts @@ -178,6 +178,7 @@ export function makeConnectionBundles ( feed: TxEvent[], msgs: DEncodeObject[], addr: string, + queryOverride: string | undefined, socketOverrides: TSocketSet, ): IIbcEngageBundle[] { const bundles: IIbcEngageBundle[] = [] @@ -200,7 +201,7 @@ export function makeConnectionBundles ( } } - const query = `${findQueryKey(url)} = '${addr}'` + const query = queryOverride || `${findQueryKey(url)} = '${addr}'` for (let id of networks) { const { chainId, endpoint } = socketOverrides[id] || sockets[id] console.log('makeConnectionBundles')