Skip to content

Commit

Permalink
fix getting timeout height for intermediate IBC transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
jonator committed Aug 6, 2024
1 parent 9f1cb1d commit ff74a4e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 33 deletions.
19 changes: 8 additions & 11 deletions packages/bridge/src/axelar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class AxelarBridgeProvider implements BridgeProvider {
protected _queryClient: AxelarQueryAPI | null = null;
protected _assetTransferClient: AxelarAssetTransfer | null = null;
protected protoRegistry = new Registry(ibcProtoRegistry);
protected axelarChainId: string;

protected readonly axelarScanBaseUrl: string;
protected readonly axelarApiBaseUrl: string;
Expand All @@ -64,6 +65,8 @@ export class AxelarBridgeProvider implements BridgeProvider {
this.ctx.env === "mainnet"
? "https://api.axelarscan.io"
: "https://testnet.api.axelarscan.io";
this.axelarChainId =
ctx.env === "mainnet" ? "axelar-dojo-1" : "axelar-testnet-lisbon-3";
}

async getQuote(params: GetBridgeQuoteParams): Promise<BridgeQuote> {
Expand Down Expand Up @@ -568,7 +571,7 @@ export class AxelarBridgeProvider implements BridgeProvider {
});

const timeoutHeight = await this.ctx.getTimeoutHeight({
chainId: toChain.chainId.toString(),
chainId: this.axelarChainId,
});

const ibcAsset = getAssetFromAssetList({
Expand All @@ -578,23 +581,17 @@ export class AxelarBridgeProvider implements BridgeProvider {
});

if (!ibcAsset) {
throw new BridgeQuoteError({
bridgeId: AxelarBridgeProvider.ID,
errorType: "CreateCosmosTxError",
message: "Could not find IBC asset info",
});
throw new Error("Could not find IBC asset info: " + fromAsset.denom);
}

const ibcTransferMethod = ibcAsset.rawAsset.transferMethods.find(
({ type }) => type === "ibc"
) as IbcTransferMethod | undefined;

if (!ibcTransferMethod) {
throw new BridgeQuoteError({
bridgeId: AxelarBridgeProvider.ID,
errorType: "CreateCosmosTxError",
message: "Could not find IBC asset transfer info",
});
throw new Error(
"Could not find IBC asset transfer info: " + ibcAsset.symbol
);
}

const { typeUrl, value: msg } = cosmosMsgOpts.ibcTransfer.messageComposer(
Expand Down
8 changes: 6 additions & 2 deletions packages/bridge/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ export interface BridgeProviderContext {
assetLists: AssetList[];
chainList: Chain[];

/** Provides current timeout height for a chain of given chainId. */
getTimeoutHeight(params: { chainId: string }): Promise<{
/** Provides current timeout height for a chain of given chainId.
* If a destination address is provided, the bech32Prefix will be used to get the chain. */
getTimeoutHeight(params: {
chainId?: string;
destinationAddress?: string;
}): Promise<{
revisionNumber: string | undefined;
revisionHeight: string;
}>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,11 @@ describe("SkipBridgeProvider", () => {

const txRequest = (await provider.createTransaction(
"1",
"osmosis-1",
{
chainId: "osmosis-1",
chainName: "osmosis",
chainType: "cosmos",
},
"0xabc",
messages
)) as EvmBridgeTransactionRequest;
Expand Down
19 changes: 12 additions & 7 deletions packages/bridge/src/skip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ export class SkipBridgeProvider implements BridgeProvider {

const transactionRequest = await this.createTransaction(
fromChain.chainId.toString(),
toChain.chainId.toString(),
toChain,
fromAddress as Address,
msgs
);
Expand Down Expand Up @@ -444,7 +444,7 @@ export class SkipBridgeProvider implements BridgeProvider {

async createTransaction(
fromChainId: string,
toChainId: string,
toChain: BridgeChain,
address: Address,
messages: SkipMsg[]
) {
Expand All @@ -459,15 +459,15 @@ export class SkipBridgeProvider implements BridgeProvider {

if ("multi_chain_msg" in message) {
return await this.createCosmosTransaction(
toChainId,
toChain,
message.multi_chain_msg
);
}
}
}

async createCosmosTransaction(
toChainId: string,
toChain: BridgeChain,
message: SkipMultiChainMsg
): Promise<CosmosBridgeTransactionRequest & { fallbackGasLimit?: number }> {
const messageData = JSON.parse(message.msg);
Expand Down Expand Up @@ -502,9 +502,14 @@ export class SkipBridgeProvider implements BridgeProvider {
} else {
// is an ibc transfer

const timeoutHeight = await this.ctx.getTimeoutHeight({
chainId: toChainId,
});
// If toChain is not cosmos, this IBC transfer is an
// intermediary IBC transfer where we need to get the
// timeout from the bech32 prefix of the receiving address
const timeoutHeight = await this.ctx.getTimeoutHeight(
toChain.chainType === "cosmos"
? toChain
: { destinationAddress: messageData.receiver }
);

const { typeUrl, value } = cosmosMsgOpts.ibcTransfer.messageComposer({
sourcePort: messageData.source_port,
Expand Down
24 changes: 13 additions & 11 deletions packages/bridge/src/squid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export class SquidBridgeProvider implements BridgeProvider {
: await this.createCosmosTransaction(
transactionRequest.data,
fromAddress,
toChain.chainId.toString(),
toChain,
{ denom: fromAsset.address, amount: fromAmount }
// TODO: uncomment when we're able to find a way to get gas limit from Squid
// or get it ourselves
Expand Down Expand Up @@ -495,7 +495,7 @@ export class SquidBridgeProvider implements BridgeProvider {
async createCosmosTransaction(
data: string,
fromAddress: string,
toChainId: string,
toChain: BridgeChain,
fromCoin: {
denom: string;
amount: string;
Expand Down Expand Up @@ -533,9 +533,14 @@ export class SquidBridgeProvider implements BridgeProvider {
};
};

const timeoutHeight = await this.ctx.getTimeoutHeight({
chainId: toChainId,
});
// If toChain is not cosmos, this IBC transfer is an
// intermediary IBC transfer where we need to get the
// timeout from the bech32 prefix of the receiving address
const timeoutHeight = await this.ctx.getTimeoutHeight(
toChain.chainType === "cosmos"
? toChain
: { destinationAddress: ibcData.msg.receiver }
);

const { typeUrl, value: msg } =
cosmosMsgOpts.ibcTransfer.messageComposer({
Expand Down Expand Up @@ -587,12 +592,9 @@ export class SquidBridgeProvider implements BridgeProvider {
};
}

throw new BridgeQuoteError({
bridgeId: SquidBridgeProvider.ID,
errorType: "CreateCosmosTxError",
message:
"Unknown message type. Osmosis FrontEnd only supports the IBC transfer and cosmwasm executeMsg message type",
});
throw new Error(
"Unknown message type. Osmosis FrontEnd only supports the IBC transfer and cosmwasm executeMsg message type"
);
} catch (e) {
const error = e as Error | BridgeQuoteError;

Expand Down
9 changes: 8 additions & 1 deletion packages/server/src/queries/complex/get-timeout-height.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ import { queryRPCStatus } from "../../queries/cosmos";
export async function getTimeoutHeight({
chainList,
chainId,
destinationAddress,
}: {
chainList: Chain[];
chainId: string;
chainId?: string;
/**
* WARNING: bech32 prefix may be the same across different chains,
* retulting in the use of an unintended chain.
*/
destinationAddress?: string;
}) {
const destinationCosmosChain = getChain({
chainList,
chainId,
destinationAddress,
});

if (!destinationCosmosChain) {
Expand Down

0 comments on commit ff74a4e

Please sign in to comment.