From 340a278f63143201c175aef2c05bef6bd384b56f Mon Sep 17 00:00:00 2001 From: jeremy lee Date: Tue, 6 Aug 2024 14:50:18 -0400 Subject: [PATCH 1/2] add submit to skip api method --- v4-client-js/src/clients/noble-client.ts | 58 ++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/v4-client-js/src/clients/noble-client.ts b/v4-client-js/src/clients/noble-client.ts index 77e92a13..6f17d52f 100644 --- a/v4-client-js/src/clients/noble-client.ts +++ b/v4-client-js/src/clients/noble-client.ts @@ -1,3 +1,4 @@ +import { toBase64 } from '@cosmjs/encoding'; import { EncodeObject, Registry, Coin } from '@cosmjs/proto-signing'; import { calculateFee, @@ -8,18 +9,23 @@ import { SigningStargateClient, MsgTransferEncodeObject, } from '@cosmjs/stargate'; +import { TxRaw } from 'cosmjs-types/cosmos/tx/v1beta1/tx'; import { GAS_MULTIPLIER } from './constants'; +import { Response } from './lib/axios'; import { MsgDepositForBurn, MsgDepositForBurnWithCaller } from './lib/cctpProto'; import LocalWallet from './modules/local-wallet'; +import RestClient from './modules/rest'; -export class NobleClient { +export class NobleClient extends RestClient{ private wallet?: LocalWallet; private restEndpoint: string; private stargateClient?: SigningStargateClient; private defaultClientMemo?: string; + private defaultGasPrice: GasPrice = GasPrice.fromString('0.1uusdc'); - constructor(restEndpoint: string, defaultClientMemo?: string) { + constructor(restEndpoint: string, defaultClientMemo?: string, skipUrl: string = 'https://api.skip.money') { + super(skipUrl) this.restEndpoint = restEndpoint; this.defaultClientMemo = defaultClientMemo; } @@ -65,9 +71,53 @@ export class NobleClient { return tx; } + + async submitToSkipApi( + messages: EncodeObject[], + chainId: string, + gasPrice: GasPrice = this.defaultGasPrice, + memo: string = '', + ): Promise { + if (!this.stargateClient) { + throw new Error('NobleClient stargateClient not initialized'); + } + + if (this.wallet?.address === undefined) { + throw new Error('NobleClient wallet not initialized'); + } + // Simulate to get the gas estimate + const fee = await this.simulateTransaction(messages, gasPrice, memo ?? this.defaultClientMemo); + + // Sign and broadcast the transaction + const txHashObj = await this.stargateClient.sign( + this.wallet.address, + messages, + fee, + memo ?? this.defaultClientMemo, + ); + + const serializedTx = TxRaw.encode(txHashObj).finish() + const base64Tx = toBase64(serializedTx) + const skipSubmitResponse = await this.post( + `/v2/tx/submit`, + {}, + JSON.stringify({ + tx: base64Tx, + chain_id: chainId + }) + ) + const { data: { tx_hash: txHash }} = skipSubmitResponse; + const formattedResponse = { + transactionHash: txHash, + code: 0, + ...skipSubmitResponse.data + } + return formattedResponse + } + async send( messages: EncodeObject[], - gasPrice: GasPrice = GasPrice.fromString('0.1uusdc'), + gasPrice: GasPrice = this.defaultGasPrice, memo?: string, ): Promise { if (!this.stargateClient) { @@ -90,7 +140,7 @@ export class NobleClient { async simulateTransaction( messages: readonly EncodeObject[], - gasPrice: GasPrice = GasPrice.fromString('0.1uusdc'), + gasPrice: GasPrice = this.defaultGasPrice, memo?: string, ): Promise { if (!this.stargateClient) { From 14956e05f495c8fbea2d16503b284a20f2d1cca9 Mon Sep 17 00:00:00 2001 From: jeremy lee Date: Tue, 17 Sep 2024 12:30:28 -0400 Subject: [PATCH 2/2] feat: skip submit method --- v4-client-js/src/clients/native.ts | 7 +++---- v4-client-js/src/clients/noble-client.ts | 8 ++++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/v4-client-js/src/clients/native.ts b/v4-client-js/src/clients/native.ts index b412cd72..1485e1e0 100644 --- a/v4-client-js/src/clients/native.ts +++ b/v4-client-js/src/clients/native.ts @@ -1176,9 +1176,7 @@ export async function cctpMultiMsgWithdraw(cosmosPayload: string): Promise