Skip to content

Commit

Permalink
updating ica workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMarstonConnell committed Oct 14, 2024
1 parent b62404e commit 986a658
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 13 deletions.
14 changes: 8 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 17 additions & 1 deletion src/classes/clientHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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,
})
Expand Down Expand Up @@ -594,6 +608,7 @@ export class ClientHandler implements IClientHandler {
broadcastTimeoutHeight,
monitorTimeout = 30,
socketOverrides = {} as TSocketSet,
queryOverride,
} = options
const events: TxEvent[] = []
const ready: IWrappedEncodeObject[] =
Expand All @@ -606,6 +621,7 @@ export class ClientHandler implements IClientHandler {
events,
msgs,
this.myIcaAddress || this.hostAddress,
queryOverride,
socketOverrides,
)
console.log('connectionBundles:', connectionBundles)
Expand Down
72 changes: 68 additions & 4 deletions src/classes/wasmHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DDeliverTxResponse> {
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
Expand Down Expand Up @@ -136,8 +169,39 @@ export class WasmHandler extends EncodingHandler implements IWasmHandler {
* @returns {Promise<string>} - Jkl address.
*/
async getJackalAddressFromContract (contractAddress: string): Promise<string> {
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<string> {
try {
const q = { get_contract_state: {} }
const q = { get_channel: {} }

const req: DQuerySmartContractStateRequest = {
address: contractAddress,
Expand All @@ -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)
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/interfaces/classes/IWasmHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ export interface IWasmHandler {
connectionIdB: string
): Promise<DDeliverTxResponse>

reOpenChannel (
contractAddress: string,
connectionIdA: string,
connectionIdB: string
): Promise<DDeliverTxResponse>

getICAContractAddress (contractAddress: string): Promise<string>

getICAJackalAddress (contractAddress: string): Promise<string>

getJackalAddressFromContract (contractAddress: string): Promise<string>

getContractChannelState (contractAddress: string): Promise<string>

wrapEncodeObjectsForBroadcast (contract: string, msgs: DEncodeObject[]): DEncodeObject[]
}
1 change: 1 addition & 0 deletions src/interfaces/options/IBroadcastOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface IBroadcastOptions {
broadcastTimeoutHeight?: bigint
monitorTimeout?: number
socketOverrides?: TSocketSet
queryOverride?: string
}
3 changes: 2 additions & 1 deletion src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ export function makeConnectionBundles (
feed: TxEvent[],
msgs: DEncodeObject[],
addr: string,
queryOverride: string | undefined,
socketOverrides: TSocketSet,
): IIbcEngageBundle<TxEvent>[] {
const bundles: IIbcEngageBundle<TxEvent>[] = []
Expand All @@ -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')
Expand Down

0 comments on commit 986a658

Please sign in to comment.