Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: skip submit method for cctp withdrawals [OTE-640] #241

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions v4-client-js/src/clients/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1176,9 +1176,7 @@ export async function cctpMultiMsgWithdraw(cosmosPayload: string): Promise<strin
typeUrl, // '/circle.cctp.v1.MsgDepositForBurnWithCaller', '/cosmos.bank.v1beta1.MsgSend'
value,
}));

const fee = await client.simulateTransaction(ibcMsgs);

// take out fee from amount before sweeping
const amount =
parseInt(ibcMsgs[0].value.amount, 10) -
Expand All @@ -1189,8 +1187,9 @@ export async function cctpMultiMsgWithdraw(cosmosPayload: string): Promise<strin
}

ibcMsgs[0].value.amount = amount.toString();

const tx = await client.send(ibcMsgs);

// TODO: maybe make chainID a function input?
const tx = await client.submitToSkipApi(ibcMsgs, 'noble-1');

return encodeJson(tx);
} catch (error) {
Expand Down
62 changes: 58 additions & 4 deletions v4-client-js/src/clients/noble-client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { toBase64 } from '@cosmjs/encoding';
import { EncodeObject, Registry, Coin } from '@cosmjs/proto-signing';
import {
calculateFee,
Expand All @@ -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;
}
Expand Down Expand Up @@ -65,9 +71,57 @@ export class NobleClient {
return tx;
}

/**
* This method is a PoC for converting from custom blockchain broadcasting to utilizing skip API
* The Skip API carries benefits of built-in tenacity and transaction tracking
*
* If we decide this pattern is superior, we can explore moving this function to a SkipClient
*/
async submitToSkipApi(
messages: EncodeObject[],
chainId: string,
gasPrice: GasPrice = this.defaultGasPrice,
memo: string = '',
): Promise<Response> {
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<DeliverTxResponse> {
if (!this.stargateClient) {
Expand All @@ -90,7 +144,7 @@ export class NobleClient {

async simulateTransaction(
messages: readonly EncodeObject[],
gasPrice: GasPrice = GasPrice.fromString('0.1uusdc'),
gasPrice: GasPrice = this.defaultGasPrice,
memo?: string,
): Promise<StdFee> {
if (!this.stargateClient) {
Expand Down
Loading