Skip to content

Commit

Permalink
feat: use tx message composers
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseRFelix committed Aug 12, 2024
1 parent 230991b commit 4593983
Show file tree
Hide file tree
Showing 23 changed files with 181 additions and 216 deletions.
32 changes: 15 additions & 17 deletions packages/bridge/src/axelar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
import { Registry } from "@cosmjs/proto-signing";
import { CoinPretty, Dec, IntPretty } from "@keplr-wallet/unit";
import { ibcProtoRegistry } from "@osmosis-labs/proto-codecs";
import { cosmosMsgOpts, estimateGasFee } from "@osmosis-labs/tx";
import { estimateGasFee, makeIBCTransferMsg } from "@osmosis-labs/tx";
import type { IbcTransferMethod } from "@osmosis-labs/types";
import {
EthereumChainInfo,
Expand Down Expand Up @@ -427,7 +427,7 @@ export class AxelarBridgeProvider implements BridgeProvider {
],
},
bech32Address: params.fromAddress,
fallbackGasLimit: cosmosMsgOpts.ibcTransfer.gas,
fallbackGasLimit: makeIBCTransferMsg.gas,
}).catch((e) => {
if (
e instanceof Error &&
Expand Down Expand Up @@ -594,21 +594,19 @@ export class AxelarBridgeProvider implements BridgeProvider {
);
}

const { typeUrl, value: msg } = cosmosMsgOpts.ibcTransfer.messageComposer(
{
receiver: depositAddress,
sender: fromAddress,
sourceChannel: ibcTransferMethod.chain.channelId,
sourcePort: "transfer",
timeoutTimestamp: "0" as any,
// @ts-ignore
timeoutHeight,
token: {
amount: fromAmount,
denom: fromAsset.address,
},
}
);
const { typeUrl, value: msg } = makeIBCTransferMsg({
receiver: depositAddress,
sender: fromAddress,
sourceChannel: ibcTransferMethod.chain.channelId,
sourcePort: "transfer",
timeoutTimestamp: "0" as any,
// @ts-ignore
timeoutHeight,
token: {
amount: fromAmount,
denom: fromAsset.address,
},
});

return {
type: "cosmos",
Expand Down
6 changes: 3 additions & 3 deletions packages/bridge/src/ibc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
} from "@osmosis-labs/server";
import {
calcAverageBlockTimeMs,
cosmosMsgOpts,
estimateGasFee,
makeIBCTransferMsg,
} from "@osmosis-labs/tx";
import { IbcTransferMethod } from "@osmosis-labs/types";
import cachified from "cachified";
Expand Down Expand Up @@ -149,7 +149,7 @@ export class IbcBridgeProvider implements BridgeProvider {
chainId: params.toChain.chainId.toString(),
});

const { typeUrl, value: msg } = cosmosMsgOpts.ibcTransfer.messageComposer({
const { typeUrl, value: msg } = makeIBCTransferMsg({
receiver: params.toAddress,
sender: params.fromAddress,
sourceChannel,
Expand All @@ -175,7 +175,7 @@ export class IbcBridgeProvider implements BridgeProvider {
],
},
bech32Address: params.fromAddress,
fallbackGasLimit: cosmosMsgOpts.ibcTransfer.gas,
fallbackGasLimit: makeIBCTransferMsg.gas,
}).catch((e) => {
if (
e instanceof Error &&
Expand Down
23 changes: 11 additions & 12 deletions packages/bridge/src/skip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import {
ibcProtoRegistry,
} from "@osmosis-labs/proto-codecs";
import {
cosmosMsgOpts,
cosmwasmMsgOpts,
estimateGasFee,
makeExecuteCosmwasmContractMsg,
makeIBCTransferMsg,
} from "@osmosis-labs/tx";
import { CosmosCounterparty, EVMCounterparty } from "@osmosis-labs/types";
import {
Expand Down Expand Up @@ -485,19 +485,18 @@ export class SkipBridgeProvider implements BridgeProvider {
}[];
};

const { typeUrl, value: msg } =
cosmwasmMsgOpts.executeWasm.messageComposer({
sender: cosmwasmData.sender,
contract: cosmwasmData.contract,
msg: Buffer.from(JSON.stringify(cosmwasmData.msg)),
funds: cosmwasmData.funds,
});
const { typeUrl, value: msg } = makeExecuteCosmwasmContractMsg({
sender: cosmwasmData.sender,
contract: cosmwasmData.contract,
msg: Buffer.from(JSON.stringify(cosmwasmData.msg)),
funds: cosmwasmData.funds,
});

return {
type: "cosmos",
msgTypeUrl: typeUrl,
msg,
fallbackGasLimit: cosmwasmMsgOpts.executeWasm.gas,
fallbackGasLimit: makeExecuteCosmwasmContractMsg.gas,
};
} else {
// is an ibc transfer
Expand All @@ -511,7 +510,7 @@ export class SkipBridgeProvider implements BridgeProvider {
: { destinationAddress: messageData.receiver }
);

const { typeUrl, value } = cosmosMsgOpts.ibcTransfer.messageComposer({
const { typeUrl, value } = makeIBCTransferMsg({
sourcePort: messageData.source_port,
sourceChannel: messageData.source_channel,
token: {
Expand All @@ -530,7 +529,7 @@ export class SkipBridgeProvider implements BridgeProvider {
type: "cosmos",
msgTypeUrl: typeUrl,
msg: value,
fallbackGasLimit: cosmosMsgOpts.ibcTransfer.gas,
fallbackGasLimit: makeIBCTransferMsg.gas,
};
}
}
Expand Down
49 changes: 25 additions & 24 deletions packages/bridge/src/squid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
type TransactionRequest,
} from "@0xsquid/sdk";
import { Dec } from "@keplr-wallet/unit";
import { cosmosMsgOpts, cosmwasmMsgOpts } from "@osmosis-labs/tx";
import {
makeExecuteCosmwasmContractMsg,
makeIBCTransferMsg,
} from "@osmosis-labs/tx";
import { CosmosCounterparty, EVMCounterparty } from "@osmosis-labs/types";
import {
apiClient,
Expand Down Expand Up @@ -542,22 +545,21 @@ export class SquidBridgeProvider implements BridgeProvider {
: { destinationAddress: ibcData.msg.receiver }
);

const { typeUrl, value: msg } =
cosmosMsgOpts.ibcTransfer.messageComposer({
memo: ibcData.msg.memo,
receiver: ibcData.msg.receiver,
sender: ibcData.msg.sender,
sourceChannel: ibcData.msg.sourceChannel,
sourcePort: ibcData.msg.sourcePort,
timeoutTimestamp: new Long(
ibcData.msg.timeoutTimestamp.low,
ibcData.msg.timeoutTimestamp.high,
ibcData.msg.timeoutTimestamp.unsigned
).toString() as any,
// @ts-ignore
timeoutHeight,
token: ibcData.msg.token,
});
const { typeUrl, value: msg } = makeIBCTransferMsg({
memo: ibcData.msg.memo,
receiver: ibcData.msg.receiver,
sender: ibcData.msg.sender,
sourceChannel: ibcData.msg.sourceChannel,
sourcePort: ibcData.msg.sourcePort,
timeoutTimestamp: new Long(
ibcData.msg.timeoutTimestamp.low,
ibcData.msg.timeoutTimestamp.high,
ibcData.msg.timeoutTimestamp.unsigned
).toString() as any,
// @ts-ignore
timeoutHeight,
token: ibcData.msg.token,
});

return {
type: "cosmos",
Expand All @@ -576,13 +578,12 @@ export class SquidBridgeProvider implements BridgeProvider {
};
};

const { typeUrl, value: msg } =
cosmwasmMsgOpts.executeWasm.messageComposer({
sender: fromAddress,
contract: cosmwasmData.msg.wasm.contract,
msg: Buffer.from(JSON.stringify(cosmwasmData.msg.wasm.msg)),
funds: [fromCoin],
});
const { typeUrl, value: msg } = makeExecuteCosmwasmContractMsg({
sender: fromAddress,
contract: cosmwasmData.msg.wasm.contract,
msg: Buffer.from(JSON.stringify(cosmwasmData.msg.wasm.msg)),
funds: [fromCoin],
});

return {
type: "cosmos",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,35 @@ import {
IQueriesStore,
txEventsWithPreOnFulfill,
} from "@osmosis-labs/keplr-stores";
import deepmerge from "deepmerge";
import Long from "long";
import { DeepPartial } from "utility-types";
import { makeIBCTransferMsg } from "@osmosis-labs/tx";

import {
AccountStore,
CosmwasmAccount,
DeliverTxResponse,
OsmosisAccount,
} from "../../account";
import { OsmosisQueries } from "../../queries";
import { cosmosMsgOpts } from "./types";
} from "../account";
import { OsmosisQueries } from "../queries";

export interface CosmosAccount {
cosmos: CosmosAccountImpl;
}

export const CosmosAccount = {
use(options: {
msgOptsCreator?: (
chainId: string
) => DeepPartial<typeof cosmosMsgOpts> | undefined;
queriesStore: IQueriesStore<CosmosQueries & OsmosisQueries>;
}): (
base: AccountStore<[OsmosisAccount, CosmosAccount, CosmwasmAccount]>,
chainGetter: ChainGetter,
chainId: string
) => CosmosAccount {
return (base, chainGetter, chainId) => {
const msgOptsFromCreator = options.msgOptsCreator
? options.msgOptsCreator(chainId)
: undefined;

return {
cosmos: new CosmosAccountImpl(
base,
chainGetter,
chainId,
options.queriesStore,
deepmerge<typeof cosmosMsgOpts, DeepPartial<typeof cosmosMsgOpts>>(
cosmosMsgOpts,
msgOptsFromCreator ? msgOptsFromCreator : {}
)
options.queriesStore
),
};
};
Expand All @@ -65,22 +51,22 @@ export class CosmosAccountImpl {
protected readonly chainId: string,
protected readonly queriesStore: IQueriesStore<
CosmosQueries & OsmosisQueries
>,
readonly msgOpts: typeof cosmosMsgOpts
>
) {}

private get address() {
return this.base.getWallet(this.chainId)?.address ?? "";
}

/**
* Send a IBC transfer transaction.
* Send an IBC transfer transaction.
*
* @param channel the channel to send the IBC transfer transaction.
* @param amount the amount to send.
* @param currency the currency to send.
* @param recipient the recipient address.
* @param onTxEvents the callback function to handle the transaction events.
* @param channel - The channel information for the IBC transfer.
* @param amount - The amount to send.
* @param currency - The currency to send.
* @param recipient - The recipient address.
* @param onTxEvents - Optional callback function or object to handle transaction events.
* @param memo - Optional memo to include with the transaction.
*/
async sendIBCTransferMsg(
channel: {
Expand Down Expand Up @@ -129,7 +115,7 @@ export class CosmosAccountImpl {
destinationInfo.network
).version.toString();

const msg = this.msgOpts.ibcTransfer.messageComposer({
const msg = makeIBCTransferMsg({
sourcePort: channel.portId,
sourceChannel: channel.channelId,
token: {
Expand All @@ -139,14 +125,8 @@ export class CosmosAccountImpl {
receiver: recipient,
sender: this.address,
timeoutHeight: {
/**
* Omit the revision_number if the chain's version is 0.
* Sending the value as 0 will cause the transaction to fail.
*/
revisionNumber:
revisionNumber !== "0"
? Long.fromString(revisionNumber)
: (undefined as any),
revisionNumber !== "0" ? BigInt(revisionNumber) : undefined,
revisionHeight: BigInt(
destinationInfo.latestBlockHeight.add(new Int("150")).toString()
),
Expand Down Expand Up @@ -183,5 +163,3 @@ export class CosmosAccountImpl {
);
}
}

export * from "./types";
11 changes: 0 additions & 11 deletions packages/stores/src/account/cosmos/types.ts

This file was deleted.

Loading

0 comments on commit 4593983

Please sign in to comment.