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

refactor: imports #734

Open
wants to merge 1 commit into
base: staging
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
4 changes: 2 additions & 2 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
"@cosmjs/proto-signing": "0.32.4",
"@cosmjs/stargate": "0.32.4",
"@cosmjs/tendermint-rpc": "0.32.4",
"@injectivelabs/core-proto-ts": "0.0.21",
"@injectivelabs/sdk-ts": "1.14.5",
"@injectivelabs/core-proto-ts": "1.13.5",
"@injectivelabs/sdk-ts": "1.14.40",
"@keplr-wallet/unit": "^0.12.143",
"@solana/wallet-adapter-base": "^0.9.23",
"axios": "1.x",
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { accountParser } from "./registry";
import {
ChainRestAuthApi,
ChainRestTendermintApi,
} from "@injectivelabs/sdk-ts/dist/cjs/client/chain/rest";
} from "@injectivelabs/sdk-ts";
import {
BigNumberInBase,
DEFAULT_BLOCK_TIMEOUT_HEIGHT,
Expand Down
85 changes: 11 additions & 74 deletions packages/client/src/injective/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
import { StdFee } from "@cosmjs/amino";
import {
CosmosTxSigningV1Beta1Signing,
CosmosTxV1Beta1Tx,
} from "@injectivelabs/core-proto-ts/cjs";
import {
createAuthInfo,
createBody,
createFee,
createSignDoc,
createSigners,
} from "@injectivelabs/sdk-ts/dist/cjs/core/modules/tx/utils/tx";
createTransactionWithSigners,
CreateTransactionArgs as CreateTransactionArgsInjective,
} from "@injectivelabs/sdk-ts";
import { DEFAULT_STD_FEE } from "@injectivelabs/utils";

import createKeccakHash from 'keccak';

export interface CreateTransactionArgs {
fee?: StdFee; // the fee to include in the transaction
memo?: string; // the memo to include in the transaction
chainId: string; // the chain id of the chain that the transaction is going to be broadcasted to
export interface CreateTransactionArgs extends CreateTransactionArgsInjective {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
message: any | any[]; // the message that should be packed into the transaction
pubKey: string; // the pubKey of the signer of the transaction in base64
sequence: number; // the sequence (nonce) of the signer of the transaction
accountNumber: number; // the account number of the signer of the transaction
signMode?: CosmosTxSigningV1Beta1Signing.SignMode;
timeoutHeight?: number; // the height at which the transaction should be considered invalid
}

export function createTransaction({
Expand All @@ -44,58 +26,13 @@ export function createTransaction({
sequence: sequence,
};

const actualSigners = Array.isArray(signers) ? signers : [signers];
const [signer] = actualSigners;

const body = createBody({ message, memo, timeoutHeight });

if (!fee.amount[0]) {
throw new Error("createTransaction error: unable to get fee amount");
}

const feeMessage = createFee({
fee: fee.amount[0],
payer: fee.payer,
granter: fee.granter,
gasLimit: parseInt(fee.gas, 10),
});

const signInfo = createSigners({
chainId,
mode: signMode,
signers: actualSigners,
});

const authInfo = createAuthInfo({
signerInfo: signInfo,
fee: feeMessage,
});

const bodyBytes = CosmosTxV1Beta1Tx.TxBody.encode(body).finish();
const authInfoBytes = CosmosTxV1Beta1Tx.AuthInfo.encode(authInfo).finish();

const signDoc = createSignDoc({
return createTransactionWithSigners({
fee,
memo,
message,
signers,
chainId,
bodyBytes: bodyBytes,
authInfoBytes: authInfoBytes,
accountNumber: signer.accountNumber,
signMode,
timeoutHeight,
});

const signDocBytes = CosmosTxV1Beta1Tx.SignDoc.encode(signDoc).finish();
const toSignBytes = Buffer.from(signDocBytes);
const toSignHash = createKeccakHash('keccak256').update(toSignBytes).digest();
const txRaw = CosmosTxV1Beta1Tx.TxRaw.create();
txRaw.authInfoBytes = authInfoBytes;
txRaw.bodyBytes = bodyBytes;

return {
txRaw,
signDoc,
signers,
signer,
signBytes: toSignBytes,
signHashedBytes: toSignHash,
bodyBytes: bodyBytes,
authInfoBytes: authInfoBytes,
};
}
40 changes: 23 additions & 17 deletions packages/client/src/registry.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,59 @@
import { AccountParser, accountFromAny } from '@cosmjs/stargate';
import { AccountParser, accountFromAny } from "@cosmjs/stargate";
import { assert } from "@cosmjs/utils";
import { StridePeriodicVestingAccount } from './stride';
import { BaseAccount } from './codegen/cosmos/auth/v1beta1/auth';
import { EthAccount } from '@injectivelabs/core-proto-ts/cjs/injective/types/v1beta1/account';
import { StridePeriodicVestingAccount } from "./stride";
import { BaseAccount } from "./codegen/cosmos/auth/v1beta1/auth";
import { InjectiveTypesV1Beta1Account } from "@injectivelabs/core-proto-ts";

export const accountParser: AccountParser = (acc) => {
switch (acc.typeUrl) {
case "/stride.vesting.StridePeriodicVestingAccount":
case "/stride.vesting.StridePeriodicVestingAccount": {
const baseAccount = StridePeriodicVestingAccount.decode(acc.value)
.baseVestingAccount?.baseAccount;
assert(baseAccount);
return accountFromAny({
typeUrl: "/cosmos.auth.v1beta1.BaseAccount",
value: BaseAccount.encode(baseAccount).finish(),
});
case "/injective.types.v1beta1.EthAccount":
const injAccount = EthAccount.decode(acc.value as Uint8Array);
}
case "/injective.types.v1beta1.EthAccount": {
const injAccount = InjectiveTypesV1Beta1Account.EthAccount.decode(
acc.value as Uint8Array,
);
const baseInjAccount = injAccount.baseAccount!;
const pubKey = baseInjAccount.pubKey;

return {
address: baseInjAccount.address,
pubkey: pubKey
? {
type: "/injective.crypto.v1beta1.ethsecp256k1.PubKey",
value: Buffer.from(pubKey.value).toString("base64"),
}
type: "/injective.crypto.v1beta1.ethsecp256k1.PubKey",
value: Buffer.from(pubKey.value).toString("base64"),
}
: null,
accountNumber: Number(baseInjAccount.accountNumber),
sequence: Number(baseInjAccount.sequence),
};

case "/ethermint.types.v1.EthAccount":
const account = EthAccount.decode(acc.value as Uint8Array);
}
case "/ethermint.types.v1.EthAccount": {
const account = InjectiveTypesV1Beta1Account.EthAccount.decode(
acc.value as Uint8Array,
);
const baseEthAccount = account.baseAccount!;
const pubKeyEth = baseEthAccount.pubKey;

return {
address: baseEthAccount.address,
pubkey: pubKeyEth
? {
type: "/ethermint.crypto.v1.ethsecp256k1.PubKey",
value: Buffer.from(pubKeyEth.value).toString("base64"),
}
type: "/ethermint.crypto.v1.ethsecp256k1.PubKey",
value: Buffer.from(pubKeyEth.value).toString("base64"),
}
: null,
accountNumber: Number(baseEthAccount.accountNumber),
sequence: Number(baseEthAccount.sequence),
};
}
default:
return accountFromAny(acc);
}
};
};
84 changes: 43 additions & 41 deletions packages/client/src/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
import { toUtf8 } from '@cosmjs/encoding';
import { EncodeObject } from '@cosmjs/proto-signing';
import MsgTransferInjective from '@injectivelabs/sdk-ts/dist/cjs/core/modules/ibc/msgs/MsgTransfer';
import { Msgs } from '@injectivelabs/sdk-ts/dist/cjs/core/modules/msgs';
import MsgExecuteContractInjective from '@injectivelabs/sdk-ts/dist/cjs/core/modules/wasm/msgs/MsgExecuteContract';
import { MsgSend } from 'cosmjs-types/cosmos/bank/v1beta1/tx';
import { MsgExecuteContract } from 'cosmjs-types/cosmwasm/wasm/v1/tx';
import { MsgTransfer } from 'cosmjs-types/ibc/applications/transfer/v1/tx';

import { CosmosMsg } from './types';
import { toUtf8 } from "@cosmjs/encoding";
import { EncodeObject } from "@cosmjs/proto-signing";
import {
type Msgs,
MsgTransfer as MsgTransferInjective,
MsgExecuteContract as MsgExecuteContractInjective,
} from "@injectivelabs/sdk-ts";
import { MsgSend } from "cosmjs-types/cosmos/bank/v1beta1/tx";
import { MsgExecuteContract } from "cosmjs-types/cosmwasm/wasm/v1/tx";
import { MsgTransfer } from "cosmjs-types/ibc/applications/transfer/v1/tx";

import { CosmosMsg } from "./types";
import {
MsgDepositForBurn,
MsgDepositForBurnWithCaller,
} from './codegen/circle/cctp/v1/tx';
import { SigningStargateClient } from '@cosmjs/stargate';
import { MsgExecute } from './codegen/initia/move/v1/tx';
} from "./codegen/circle/cctp/v1/tx";
import { SigningStargateClient } from "@cosmjs/stargate";
import { MsgExecute } from "./codegen/initia/move/v1/tx";

import { MsgInitiateTokenDeposit } from './codegen/opinit/ophost/v1/tx';
import { ClawbackVestingAccount } from './codegen/evmos/vesting/v2/vesting';
import { MsgInitiateTokenDeposit } from "./codegen/opinit/ophost/v1/tx";
import { ClawbackVestingAccount } from "./codegen/evmos/vesting/v2/vesting";

export const DEFAULT_GAS_MULTIPLIER = 1.5;

export function getEncodeObjectFromCosmosMessage(
message: CosmosMsg
message: CosmosMsg,
): EncodeObject {
const msgJson = JSON.parse(message.msg);

if (message.msgTypeURL === '/ibc.applications.transfer.v1.MsgTransfer') {
if (message.msgTypeURL === "/ibc.applications.transfer.v1.MsgTransfer") {
return {
typeUrl: '/ibc.applications.transfer.v1.MsgTransfer',
typeUrl: "/ibc.applications.transfer.v1.MsgTransfer",
value: MsgTransfer.fromJSON({
sourcePort: msgJson.source_port,
sourceChannel: msgJson.source_channel,
Expand All @@ -41,7 +43,7 @@ export function getEncodeObjectFromCosmosMessage(
};
}

if (message.msgTypeURL === '/cosmwasm.wasm.v1.MsgExecuteContract') {
if (message.msgTypeURL === "/cosmwasm.wasm.v1.MsgExecuteContract") {
return {
typeUrl: message.msgTypeURL,
value: MsgExecuteContract.fromPartial({
Expand All @@ -53,7 +55,7 @@ export function getEncodeObjectFromCosmosMessage(
};
}

if (message.msgTypeURL === '/cosmos.bank.v1beta1.MsgSend') {
if (message.msgTypeURL === "/cosmos.bank.v1beta1.MsgSend") {
return {
typeUrl: message.msgTypeURL,
value: MsgSend.fromPartial({
Expand All @@ -64,21 +66,21 @@ export function getEncodeObjectFromCosmosMessage(
};
}

if (message.msgTypeURL === '/circle.cctp.v1.MsgDepositForBurn') {
if (message.msgTypeURL === "/circle.cctp.v1.MsgDepositForBurn") {
return {
typeUrl: message.msgTypeURL,
value: MsgDepositForBurn.fromAmino(msgJson),
};
}

if (message.msgTypeURL === '/circle.cctp.v1.MsgDepositForBurnWithCaller') {
if (message.msgTypeURL === "/circle.cctp.v1.MsgDepositForBurnWithCaller") {
return {
typeUrl: message.msgTypeURL,
value: MsgDepositForBurnWithCaller.fromAmino(msgJson),
};
}

if (message.msgTypeURL === '/initia.move.v1.MsgExecute') {
if (message.msgTypeURL === "/initia.move.v1.MsgExecute") {
return {
typeUrl: message.msgTypeURL,
value: MsgExecute.fromPartial({
Expand All @@ -91,7 +93,7 @@ export function getEncodeObjectFromCosmosMessage(
};
}

if (message.msgTypeURL === '/opinit.ophost.v1.MsgInitiateTokenDeposit') {
if (message.msgTypeURL === "/opinit.ophost.v1.MsgInitiateTokenDeposit") {
return {
typeUrl: message.msgTypeURL,
value: MsgInitiateTokenDeposit.fromPartial({
Expand All @@ -103,7 +105,7 @@ export function getEncodeObjectFromCosmosMessage(
};
}

if (message.msgTypeURL === '/evmos.vesting.v2.ClawbackVestingAccount') {
if (message.msgTypeURL === "/evmos.vesting.v2.ClawbackVestingAccount") {
return {
typeUrl: message.msgTypeURL,
value: ClawbackVestingAccount.fromPartial({
Expand All @@ -123,11 +125,11 @@ export function getEncodeObjectFromCosmosMessage(
}

export function getEncodeObjectFromCosmosMessageInjective(
message: CosmosMsg
message: CosmosMsg,
): Msgs {
const msgJson = JSON.parse(message.msg);

if (message.msgTypeURL === '/ibc.applications.transfer.v1.MsgTransfer') {
if (message.msgTypeURL === "/ibc.applications.transfer.v1.MsgTransfer") {
return MsgTransferInjective.fromJSON({
port: msgJson.source_port,
channelId: msgJson.source_channel,
Expand All @@ -139,7 +141,7 @@ export function getEncodeObjectFromCosmosMessageInjective(
});
}

if (message.msgTypeURL === '/cosmwasm.wasm.v1.MsgExecuteContract') {
if (message.msgTypeURL === "/cosmwasm.wasm.v1.MsgExecuteContract") {
return MsgExecuteContractInjective.fromJSON({
sender: msgJson.sender,
contractAddress: msgJson.contract,
Expand All @@ -148,7 +150,7 @@ export function getEncodeObjectFromCosmosMessageInjective(
});
}

throw new Error('Unsupported message type');
throw new Error("Unsupported message type");
}

export async function getCosmosGasAmountForMessage(
Expand All @@ -157,36 +159,36 @@ export async function getCosmosGasAmountForMessage(
chainID: string,
messages?: CosmosMsg[],
encodedMsgs?: EncodeObject[],
multiplier: number = DEFAULT_GAS_MULTIPLIER
multiplier: number = DEFAULT_GAS_MULTIPLIER,
) {
if (!messages && !encodedMsgs) {
throw new Error('Either message or encodedMsg must be provided');
throw new Error("Either message or encodedMsg must be provided");
}
const _encodedMsgs = messages?.map((message) =>
getEncodeObjectFromCosmosMessage(message)
getEncodeObjectFromCosmosMessage(message),
);
encodedMsgs = encodedMsgs || _encodedMsgs;

if (!encodedMsgs) {
throw new Error('Either message or encodedMsg must be provided');
throw new Error("Either message or encodedMsg must be provided");
}
if (
chainID.includes('evmos') ||
chainID.includes('injective') ||
chainID.includes('dymension') ||
process?.env.NODE_ENV === 'test'
chainID.includes("evmos") ||
chainID.includes("injective") ||
chainID.includes("dymension") ||
process?.env.NODE_ENV === "test"
) {
if (
messages?.find(
(i) => i.msgTypeURL === '/cosmwasm.wasm.v1.MsgExecuteContract'
(i) => i.msgTypeURL === "/cosmwasm.wasm.v1.MsgExecuteContract",
)
) {
return '2400000';
return "2400000";
}
return '280000';
return "280000";
}

const estimatedGas = await client.simulate(signerAddress, encodedMsgs, '');
const estimatedGas = await client.simulate(signerAddress, encodedMsgs, "");

const estimatedGasWithBuffer = estimatedGas * multiplier;

Expand Down
Loading
Loading