diff --git a/src/classes/clientHandler.ts b/src/classes/clientHandler.ts index b35fbf7..dc34931 100644 --- a/src/classes/clientHandler.ts +++ b/src/classes/clientHandler.ts @@ -33,7 +33,7 @@ import { IWrappedEncodeObject, } from '@/interfaces' import { TSockets, TSocketSet } from '@/types' -import { Coin, DeliverTxResponse, StdFee } from '@cosmjs/stargate' +import { DeliverTxResponse, StdFee } from '@cosmjs/stargate' export class ClientHandler implements IClientHandler { protected readonly jklQuery: TJackalQueryClient @@ -278,7 +278,8 @@ export class ClientHandler implements IClientHandler { /** * Create Wasm Handler session, defaults to Archway. - * @returns {Promise} + * @param {IWasmDetails} [details] + * @returns {Promise} */ async createWasmStorageHandler (details: IWasmDetails = {}): Promise { try { @@ -289,20 +290,20 @@ export class ClientHandler implements IClientHandler { } = details this.myCosmwasm = await WasmHandler.init(this) const ica = await this.myCosmwasm.getICAContractAddress(contract).catch(err => { - console.warn("can't get ica", err) + console.warn('can\'t get ica', err) }) - console.log("ICA: ", ica) + console.log('ICA: ', ica) if (ica) { - console.log("Set ICA correctly") + console.log('Set ICA correctly') this.myContractAddress = ica } else { - console.log("Failed to set ICA") + console.log('Failed to set ICA') await this.myCosmwasm.instantiateICA( contract, connIdA, connIdB, ) - await new Promise(r => setTimeout(r, 5000)); + await new Promise(r => setTimeout(r, 5000)) this.myContractAddress = await this.myCosmwasm.getICAContractAddress(contract) } console.log(this.myContractAddress) @@ -312,9 +313,9 @@ export class ClientHandler implements IClientHandler { const state = await this.myCosmwasm.getContractChannelState( this.myContractAddress, ) - console.log("State: ", state) + console.log('State: ', state) if (state === 'STATE_CLOSED') { - console.log("needs to re-open channel") + console.log('needs to re-open channel') await this.myCosmwasm.reOpenChannel( contract, connIdA, @@ -458,16 +459,22 @@ export class ClientHandler implements IClientHandler { return this.getJackalNetworkBalance(this.getICAJackalAddress()) } - async ibcSend (address: string, amount: Coin, sourceChannel: string): Promise { + /** + * + * @param {string} address + * @param {DCoin} amount + * @param {string} sourceChannel + * @returns {Promise} + */ + async ibcSend (address: string, amount: DCoin, sourceChannel: string): Promise { if (!this.hostSigner) { throw new Error(signerNotEnabled('ClientHandler', 'ibcSend')) } - - - const fee: Coin = {amount: "35774392000000000", denom: "aarch"} - const f: StdFee = {amount: [fee], gas: "200000"} try { - return await this.hostSigner.sendIbcTokens(this.hostAddress, address, amount, "transfer", sourceChannel, undefined, Date.now() * 1000 * 1000 + 2 * 60 * 60 * 1000 * 1000 * 1000, f, undefined) + const fee: DCoin = { amount: '35774392000000000', denom: 'aarch' } + const standardFee: StdFee = { amount: [fee], gas: '200000' } + + return await this.hostSigner.sendIbcTokens(this.hostAddress, address, amount, 'transfer', sourceChannel, undefined, Date.now() * 1000 * 1000 + 2 * 60 * 60 * 1000 * 1000 * 1000, standardFee, undefined) } catch (err) { throw warnError('clientHandler ibcSend()', err) } @@ -475,6 +482,7 @@ export class ClientHandler implements IClientHandler { /** * + * @param {string} address * @returns {Promise} */ async getJackalNetworkBalance (address: string): Promise { @@ -483,7 +491,7 @@ export class ClientHandler implements IClientHandler { } try { const res = await this.jklQuery.queries.bank.balance({ - address: address, + address, denom: 'ujkl', }) return res.balance as DCoin @@ -494,18 +502,16 @@ export class ClientHandler implements IClientHandler { /** * + * @param {string} address + * @param {string} denom * @returns {Promise} */ async getHostNetworkBalance (address: string, denom: string): Promise { - if (!this.jklSigner) { - throw new Error(signerNotEnabled('ClientHandler', 'getHostNetworkBalance')) - } if (!this.hostQuery) { throw new Error(signerNotEnabled('ClientHandler', 'getHostNetworkBalance')) } try { - const res = await this.hostQuery.getBalance(address,denom) - return res + return await this.hostQuery.getBalance(address, denom) } catch (err) { throw warnError('clientHandler getHostNetworkBalance()', err) } @@ -548,7 +554,7 @@ export class ClientHandler implements IClientHandler { * Returns true if ICA address has been set by wasm signer. * @returns {boolean} */ - wasmIsConnected(): boolean { + wasmIsConnected (): boolean { return this.getICAJackalAddress() !== this.jklAddress } @@ -621,8 +627,8 @@ export class ClientHandler implements IClientHandler { events, msgs, this.myIcaAddress || this.hostAddress, - queryOverride, socketOverrides, + queryOverride, ) console.log('connectionBundles:', connectionBundles) diff --git a/src/classes/encodingHandler.ts b/src/classes/encodingHandler.ts index 027cabf..63afc05 100644 --- a/src/classes/encodingHandler.ts +++ b/src/classes/encodingHandler.ts @@ -232,7 +232,7 @@ export class EncodingHandler { protected encodePostKey (key: string): DEncodeObject { const forKey: DMsgPostKey = { creator: this.jackalClient.getICAJackalAddress(), - key: key, + key, } return this.jackalSigner.txLibrary.fileTree.msgPostKey(forKey) } diff --git a/src/classes/storageHandler.ts b/src/classes/storageHandler.ts index 15f316b..4f09dfe 100644 --- a/src/classes/storageHandler.ts +++ b/src/classes/storageHandler.ts @@ -1517,7 +1517,7 @@ export class StorageHandler extends EncodingHandler implements IStorageHandler { // console.log(providers) return await this.findProviderIps(providers) } catch (err) { - throw warnError('storageHandler loadProvidersFromChain()', err) + throw warnError('storageHandler getAllProviders()', err) } } diff --git a/src/classes/wasmHandler.ts b/src/classes/wasmHandler.ts index 7cd9cc7..b8f32e5 100644 --- a/src/classes/wasmHandler.ts +++ b/src/classes/wasmHandler.ts @@ -29,17 +29,21 @@ export class WasmHandler extends EncodingHandler implements IWasmHandler { * @returns {Promise} - Instance of WasmHandler. */ static async init (client: IClientHandler): Promise { - const jackalSigner = client.getJackalSigner() - if (!jackalSigner) { - throw new Error(signerNotEnabled('WasmHandler', 'init')) - } - const hostSigner = client.getHostSigner() - if (!hostSigner) { - throw new Error(signerNotEnabled('WasmHandler', 'init')) + try { + const jackalSigner = client.getJackalSigner() + if (!jackalSigner) { + throw new Error(signerNotEnabled('WasmHandler', 'init')) + } + const hostSigner = client.getHostSigner() + if (!hostSigner) { + throw new Error(signerNotEnabled('WasmHandler', 'init')) + } + let dummyKey = await stringToShaHex('') + const keyPair = PrivateKey.fromHex(dummyKey) + return new WasmHandler(client, jackalSigner, hostSigner, keyPair) + } catch (err) { + throw warnError('wasmHandler init()', err) } - let dummyKey = await stringToShaHex('') - const keyPair = PrivateKey.fromHex(dummyKey) - return new WasmHandler(client, jackalSigner, hostSigner, keyPair) } /** @@ -52,18 +56,17 @@ export class WasmHandler extends EncodingHandler implements IWasmHandler { async instantiateICA ( contractAddress: string, connectionIdA: string, - connectionIdB: string + connectionIdB: string, ): Promise { try { - const msg = { create_outpost: { channel_open_init_options: { connection_id: connectionIdA, counterparty_connection_id: connectionIdB, - tx_encoding: "proto3" - } - } + tx_encoding: 'proto3', + }, + }, } const eo = this.jackalClient.getTxs().cosmwasm.msgExecuteContract({ @@ -75,18 +78,24 @@ export class WasmHandler extends EncodingHandler implements IWasmHandler { const wrapped: IWrappedEncodeObject = { encodedObject: eo, modifier: 0 } const postBroadcast = - //{queryOverride: `execute._contract_address = '${contractAddress}'`} - await this.jackalClient.broadcastAndMonitorMsgs(wrapped, {queryOverride: `message.action = '/cosmwasm.wasm.v1.MsgExecuteContract' AND message.sender = '${this.hostAddress}'`}) + 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) } } + /** + * + * @param {string} contractAddress + * @param {string} connectionIdA + * @param {string} connectionIdB + * @returns {Promise} + */ async reOpenChannel ( contractAddress: string, connectionIdA: string, - connectionIdB: string + connectionIdB: string, ): Promise { try { const msg = { @@ -94,9 +103,9 @@ export class WasmHandler extends EncodingHandler implements IWasmHandler { channel_open_init_options: { connection_id: connectionIdA, counterparty_connection_id: connectionIdB, - tx_encoding: "proto3" - } - } + tx_encoding: 'proto3', + }, + }, } const eo = this.jackalClient.getTxs().cosmwasm.msgExecuteContract({ @@ -106,9 +115,8 @@ export class WasmHandler extends EncodingHandler implements IWasmHandler { sender: this.jackalClient.getHostAddress(), }) 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: `execute._contract_address = '${contractAddress}'` }) return postBroadcast.txResponse } catch (err) { throw warnError('wasmHandler reOpenChannel()', err) @@ -122,28 +130,20 @@ export class WasmHandler extends EncodingHandler implements IWasmHandler { */ async getICAContractAddress (contractAddress: string): Promise { try { - const query = { get_user_outpost_address: { user_address: this.hostAddress, }, } - - const q = stringToUint8Array(JSON.stringify(query)) - - + const uintQuery = stringToUint8Array(JSON.stringify(query)) const req: DQuerySmartContractStateRequest = { address: contractAddress, - queryData: q, + queryData: uintQuery, } - const state = await this.hostSigner.queries.cosmwasm.smartContractState(req) - - const uint8Array = state.data instanceof Uint8Array ? state.data : new Uint8Array(state.data); - + const uint8Array = state.data instanceof Uint8Array ? state.data : new Uint8Array(state.data) return uintArrayToString(uint8Array).replaceAll('"', '') - } catch (err) { throw warnError('wasmHandler getICAContractAddress()', err) } @@ -169,51 +169,53 @@ 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 + try { + const retries = 30 + let attempt = 0 + while (attempt < retries) { + try { + const query = { get_contract_state: {} } + const req: DQuerySmartContractStateRequest = { + address: contractAddress, + queryData: stringToUint8Array(JSON.stringify(query)), + } + 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) } - attempt ++ - await new Promise(r => setTimeout(r, 5000)); - } catch (err) { - console.warn('wasmHandler getJackalAddressFromContract()', err) } + throw new Error('can\'t get details from contract') + } catch (err) { + throw warnError('wasmHandler getJackalAddressFromContract()', err) } - - throw warnError('wasmHandler getJackalAddressFromContract()', "can't get details from contract") } - + /** + * + * @param {string} contractAddress + * @returns {Promise} + */ async getContractChannelState (contractAddress: string): Promise { try { - const q = { get_channel: {} } - + const query = { get_channel: {} } const req: DQuerySmartContractStateRequest = { address: contractAddress, - queryData: stringToUint8Array(JSON.stringify(q)), + queryData: stringToUint8Array(JSON.stringify(query)), } - 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) - return data.channel_status } catch (err) { throw warnError('wasmHandler getContractChannelState()', err) diff --git a/src/utils/gas.ts b/src/utils/gas.ts index b470394..d6d09a3 100644 --- a/src/utils/gas.ts +++ b/src/utils/gas.ts @@ -5,7 +5,7 @@ const gasBaselineRate = 56 const gasFallbackTxCost = 142 const gasMap: Record = { /** Filetree */ - '/canine_chain.filetree.MsgPostFile': 570, + '/canine_chain.filetree.MsgPostFile': 270, '/canine_chain.filetree.MsgPostKey': 12, '/canine_chain.filetree.MsgDeleteFile': 9, /** Notifications */ diff --git a/src/utils/globalDefaults.ts b/src/utils/globalDefaults.ts index 5591a3c..8558858 100644 --- a/src/utils/globalDefaults.ts +++ b/src/utils/globalDefaults.ts @@ -67,32 +67,32 @@ export const sockets: Record = { jackal: { chainId: 'jackal-1', endpoint: 'wss://rpc.jackalprotocol.com', - gasMultiplier: 1100, + gasMultiplier: 1200, }, jackaltest: { chainId: 'lupulella-2', endpoint: 'wss://testnet-rpc.jackalprotocol.com', - gasMultiplier: 1100, + gasMultiplier: 1200, }, jackalv4: { chainId: 'mesomelas-1', endpoint: 'wss://jackal-testnet-v4-rpc.brocha.in', - gasMultiplier: 1100, + gasMultiplier: 1200, }, jackallocal: { chainId: 'puppy-1', endpoint: 'ws://localhost:62744', - gasMultiplier: 1100, + gasMultiplier: 1200, }, archway: { chainId: 'archway-1', endpoint: 'wss://rpc.mainnet.archway.io', - gasMultiplier: 2200, + gasMultiplier: 5500, }, archwaytest: { chainId: 'constantine-3', endpoint: 'wss://rpc.constantine.archway.io', - gasMultiplier: 2200, + gasMultiplier: 5500, }, wasm: { chainId: 'localwasm-1', diff --git a/src/utils/misc.ts b/src/utils/misc.ts index 19c0515..fa5ebeb 100644 --- a/src/utils/misc.ts +++ b/src/utils/misc.ts @@ -171,6 +171,7 @@ export function shuffleArray (source: T[]): T[] { * @param {DEncodeObject[]} msgs - Messages to broadcast to chain. * @param {string} addr - Address of signer. * @param {Record} socketOverrides - Object of possible override values for sockets. + * @param {string} [queryOverride] - Overrides generated query. * @returns {IIbcEngageBundle[]} - Broadcast result. */ export function makeConnectionBundles ( @@ -178,8 +179,8 @@ export function makeConnectionBundles ( feed: TxEvent[], msgs: DEncodeObject[], addr: string, - queryOverride: string | undefined, socketOverrides: TSocketSet, + queryOverride?: string, ): IIbcEngageBundle[] { const bundles: IIbcEngageBundle[] = []