-
Notifications
You must be signed in to change notification settings - Fork 33
refactor: use types from sdk-cosmos npm package #416
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
base: main
Are you sure you want to change the base?
Changes from all commits
284091f
cc08985
c8b3656
3aed65c
8fc3d3a
95a40a6
d64a3c2
ad2af49
4562885
88e53a3
e83da2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,14 @@ | ||
| import { QueryAllForeignCoinsResponseSDKType } from "@zetachain/sdk-cosmos/zetachain/zetacore/fungible/query"; | ||
| import axios from "axios"; | ||
|
|
||
| import { ForeignCoinsResponse } from "../../../types/foreignCoins.types"; | ||
| import { ZetaChainClient } from "./client"; | ||
|
|
||
| export const getForeignCoins = async function (this: ZetaChainClient) { | ||
| const api = this.getEndpoint("cosmos-http", `zeta_${this.network}`); | ||
| const endpoint = `${api}/zeta-chain/fungible/foreign_coins`; | ||
| const { data } = await axios.get<ForeignCoinsResponse>(endpoint); | ||
| const { data } = await axios.get<QueryAllForeignCoinsResponseSDKType>( | ||
| endpoint | ||
| ); | ||
|
|
||
| return data.foreignCoins; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import { parseUnits } from "@ethersproject/units"; | |
| import UniswapV2RouterABI from "@uniswap/v2-periphery/build/IUniswapV2Router02.json"; | ||
| import { getAddress } from "@zetachain/protocol-contracts"; | ||
| import ZRC20 from "@zetachain/protocol-contracts/abi/ZRC20.sol/ZRC20.json"; | ||
| import { CoinType } from "@zetachain/sdk-cosmos/zetachain/zetacore/pkg/coin/coin"; | ||
| import { ethers } from "ethers"; | ||
|
|
||
| import { | ||
|
|
@@ -78,8 +79,8 @@ export const getZRC20GasToken = async function ( | |
| const foreignCoins = await this.getForeignCoins(); | ||
| const token = foreignCoins.find((foreignCoin) => { | ||
| return ( | ||
| foreignCoin.foreign_chain_id === chainID && | ||
| foreignCoin.coin_type === "Gas" | ||
| String(foreignCoin.foreign_chain_id) === chainID && | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do understand why we need to do this (the new sdk type says
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The way I see it we could:
export interface ForeignCoinsRuntimeType extends Omit<ForeignCoinsSDKType, 'foreign_chain_id'> {
foreign_chain_id: string;
}
|
||
| foreignCoin.coin_type === CoinType.Gas | ||
| ); | ||
| }); | ||
| return token?.zrc20_contract_address; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,14 @@ | ||
| import { QuerySupportedChainsResponseSDKType } from "@zetachain/sdk-cosmos/zetachain/zetacore/observer/query"; | ||
| import axios from "axios"; | ||
|
|
||
| import { ObserverSupportedChainsResponse } from "../../../types/supportedChains.types"; | ||
| import { ZetaChainClient } from "./client"; | ||
|
|
||
| export const getSupportedChains = async function (this: ZetaChainClient) { | ||
| const api = this.getEndpoint("cosmos-http", `zeta_${this.network}`); | ||
| const endpoint = `${api}/zeta-chain/observer/supportedChains`; | ||
| const { data } = await axios.get<ObserverSupportedChainsResponse>(endpoint); | ||
| const { data } = await axios.get<QuerySupportedChainsResponseSDKType>( | ||
| endpoint | ||
| ); | ||
hernan-clich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return data.chains; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,3 +1,12 @@ | ||||||||||||||||||||||||||||||||
| import type { CrossChainTxSDKType } from "@zetachain/sdk-cosmos/zetachain/zetacore/crosschain/cross_chain_tx"; | ||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||
| CctxStatus, | ||||||||||||||||||||||||||||||||
| cctxStatusToJSON, | ||||||||||||||||||||||||||||||||
| } from "@zetachain/sdk-cosmos/zetachain/zetacore/crosschain/cross_chain_tx"; | ||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||
| CoinType, | ||||||||||||||||||||||||||||||||
| coinTypeToJSON, | ||||||||||||||||||||||||||||||||
| } from "@zetachain/sdk-cosmos/zetachain/zetacore/pkg/coin/coin"; | ||||||||||||||||||||||||||||||||
| import { Command } from "commander"; | ||||||||||||||||||||||||||||||||
| import { ethers } from "ethers"; | ||||||||||||||||||||||||||||||||
| import EventEmitter from "eventemitter3"; | ||||||||||||||||||||||||||||||||
|
|
@@ -9,30 +18,34 @@ import { | |||||||||||||||||||||||||||||||
| DEFAULT_TIMEOUT, | ||||||||||||||||||||||||||||||||
| } from "../../../../src/constants/commands/cctx"; | ||||||||||||||||||||||||||||||||
| import { cctxOptionsSchema } from "../../../../src/schemas/commands/cctx"; | ||||||||||||||||||||||||||||||||
| import type { CrossChainTx } from "../../../../types/trackCCTX.types"; | ||||||||||||||||||||||||||||||||
| import { fetchFromApi, sleep } from "../../../../utils"; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * Event map: | ||||||||||||||||||||||||||||||||
| * - `cctx` → emitted every polling round with the **entire** array so far. | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| interface CctxEvents { | ||||||||||||||||||||||||||||||||
| cctx: (allSoFar: CrossChainTx[]) => void; | ||||||||||||||||||||||||||||||||
| cctx: (allSoFar: CrossChainTxSDKType[]) => void; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| export const cctxEmitter = new EventEmitter<CctxEvents>(); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| type CctxOptions = z.infer<typeof cctxOptionsSchema>; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| interface CctxResponse { | ||||||||||||||||||||||||||||||||
| CrossChainTxs: CrossChainTx[]; | ||||||||||||||||||||||||||||||||
| CrossChainTxs: CrossChainTxSDKType[]; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * True if the CCTX is still in-flight and may mutate on‑chain. | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| const isPending = (tx: CrossChainTx): boolean => | ||||||||||||||||||||||||||||||||
| ["PendingOutbound", "PendingRevert"].includes(tx.cctx_status.status); | ||||||||||||||||||||||||||||||||
| const isPending = (tx: CrossChainTxSDKType): boolean => { | ||||||||||||||||||||||||||||||||
| if (!tx.cctx_status) { | ||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| const status = cctxStatusToJSON(tx.cctx_status.status); | ||||||||||||||||||||||||||||||||
| return ["PendingOutbound", "PendingRevert"].includes(status); | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * Poll indefinitely until user terminates. | ||||||||||||||||||||||||||||||||
|
|
@@ -49,7 +62,7 @@ const gatherCctxs = async ( | |||||||||||||||||||||||||||||||
| ): Promise<void> => { | ||||||||||||||||||||||||||||||||
| const startTime = Date.now(); | ||||||||||||||||||||||||||||||||
| // Latest copy of each CCTX keyed by index | ||||||||||||||||||||||||||||||||
| const results = new Map<string, CrossChainTx>(); | ||||||||||||||||||||||||||||||||
| const results = new Map<string, CrossChainTxSDKType>(); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // Hashes we need to query in the upcoming round | ||||||||||||||||||||||||||||||||
| const frontier = new Set<string>([rootHash]); | ||||||||||||||||||||||||||||||||
|
|
@@ -91,7 +104,9 @@ const gatherCctxs = async ( | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // Keep querying while pending | ||||||||||||||||||||||||||||||||
| if (isPending(tx)) { | ||||||||||||||||||||||||||||||||
| nextFrontier.add(tx.inbound_params.observed_hash); | ||||||||||||||||||||||||||||||||
| if (tx.inbound_params) { | ||||||||||||||||||||||||||||||||
| nextFrontier.add(tx.inbound_params.observed_hash); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| queriedOnce.add(tx.index); | ||||||||||||||||||||||||||||||||
|
|
@@ -113,7 +128,7 @@ const gatherCctxs = async ( | |||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const formatCCTX = (cctx: CrossChainTx) => { | ||||||||||||||||||||||||||||||||
| const formatCCTX = (cctx: CrossChainTxSDKType) => { | ||||||||||||||||||||||||||||||||
| let output = ""; | ||||||||||||||||||||||||||||||||
| const { | ||||||||||||||||||||||||||||||||
| index, | ||||||||||||||||||||||||||||||||
|
|
@@ -123,6 +138,8 @@ const formatCCTX = (cctx: CrossChainTx) => { | |||||||||||||||||||||||||||||||
| revert_options, | ||||||||||||||||||||||||||||||||
| relayed_message, | ||||||||||||||||||||||||||||||||
| } = cctx; | ||||||||||||||||||||||||||||||||
| if (!inbound_params || !cctx_status) return ""; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const { sender_chain_id, sender, amount, coin_type } = inbound_params; | ||||||||||||||||||||||||||||||||
| const { receiver_chainId, receiver } = outbound_params[0]; | ||||||||||||||||||||||||||||||||
| const { status, status_message, error_message = "" } = cctx_status; | ||||||||||||||||||||||||||||||||
|
|
@@ -135,18 +152,25 @@ const formatCCTX = (cctx: CrossChainTx) => { | |||||||||||||||||||||||||||||||
| } = revert_options; | ||||||||||||||||||||||||||||||||
| let mainStatusIcon = "🔄"; | ||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||
| status === "OutboundMined" || | ||||||||||||||||||||||||||||||||
| (status === "PendingOutbound" && outbound_params[0].hash !== "") | ||||||||||||||||||||||||||||||||
| String(status) === cctxStatusToJSON(CctxStatus.OutboundMined) || | ||||||||||||||||||||||||||||||||
| (String(status) === cctxStatusToJSON(CctxStatus.PendingOutbound) && | ||||||||||||||||||||||||||||||||
| outbound_params[0].hash !== "") | ||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||
|
Comment on lines
154
to
158
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Use firstOutbound.hash after guarding array access Avoid direct indexing; rely on the guarded alias. - (String(status) === cctxStatusToJSON(CctxStatus.PendingOutbound) &&
- outbound_params[0].hash !== "")
+ (String(status) === cctxStatusToJSON(CctxStatus.PendingOutbound) &&
+ firstOutbound.hash !== "")📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
| mainStatusIcon = "✅"; | ||||||||||||||||||||||||||||||||
| } else if (status === "PendingOutbound" || status === "PendingRevert") { | ||||||||||||||||||||||||||||||||
| } else if ( | ||||||||||||||||||||||||||||||||
| String(status) === cctxStatusToJSON(CctxStatus.PendingOutbound) || | ||||||||||||||||||||||||||||||||
| String(status) === cctxStatusToJSON(CctxStatus.PendingRevert) | ||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||
| mainStatusIcon = "🔄"; | ||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||
| mainStatusIcon = "❌"; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| let mainStatus; | ||||||||||||||||||||||||||||||||
| if (status === "PendingOutbound" && outbound_params[0].hash !== "") { | ||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||
| String(status) === cctxStatusToJSON(CctxStatus.PendingOutbound) && | ||||||||||||||||||||||||||||||||
| outbound_params[0].hash !== "" | ||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||
| mainStatus = "PendingOutbound (transaction broadcasted to target chain)"; | ||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||
| mainStatus = status; | ||||||||||||||||||||||||||||||||
|
|
@@ -169,7 +193,7 @@ Receiver: ${receiver} | |||||||||||||||||||||||||||||||
| mainTx += `Message: ${relayed_message}\n`; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if (coin_type !== "NoAssetCall") { | ||||||||||||||||||||||||||||||||
| if (String(coin_type) !== coinTypeToJSON(CoinType.NoAssetCall)) { | ||||||||||||||||||||||||||||||||
| mainTx += `Amount: ${amount} ${coin_type} tokens\n`; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+196
to
198
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix coin type comparison and display String(coin_type) will not match JSON names; use coinTypeToJSON consistently. - if (String(coin_type) !== coinTypeToJSON(CoinType.NoAssetCall)) {
- mainTx += `Amount: ${amount} ${coin_type} tokens\n`;
- }
+ if (coinTypeToJSON(coin_type) !== coinTypeToJSON(CoinType.NoAssetCall)) {
+ mainTx += `Amount: ${amount} ${coinTypeToJSON(coin_type)} tokens\n`;
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
|
@@ -188,11 +212,12 @@ Receiver: ${receiver} | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||
| outbound_params[1] && | ||||||||||||||||||||||||||||||||
| ["Reverted", "PendingRevert", "Aborted"].includes(status) | ||||||||||||||||||||||||||||||||
| ["Reverted", "PendingRevert", "Aborted"].includes(String(status)) | ||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||
| const isReverted = status === "Reverted"; | ||||||||||||||||||||||||||||||||
| const isPendingRevert = status === "PendingRevert"; | ||||||||||||||||||||||||||||||||
| const isAborted = status === "Aborted"; | ||||||||||||||||||||||||||||||||
| const isReverted = String(status) === cctxStatusToJSON(CctxStatus.Reverted); | ||||||||||||||||||||||||||||||||
| const isPendingRevert = | ||||||||||||||||||||||||||||||||
| String(status) === cctxStatusToJSON(CctxStatus.PendingRevert); | ||||||||||||||||||||||||||||||||
| const isAborted = String(status) === cctxStatusToJSON(CctxStatus.Aborted); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
|
Comment on lines
214
to
221
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert/abort block never executes due to wrong status includes String(status) won’t equal "Reverted"/"PendingRevert"/"Aborted". Compute flags via cctxStatusToJSON and gate on them. - if (
- outbound_params[1] &&
- ["Reverted", "PendingRevert", "Aborted"].includes(String(status))
- ) {
- const isReverted = String(status) === cctxStatusToJSON(CctxStatus.Reverted);
- const isPendingRevert =
- String(status) === cctxStatusToJSON(CctxStatus.PendingRevert);
- const isAborted = String(status) === cctxStatusToJSON(CctxStatus.Aborted);
+ const isReverted = cctxStatusToJSON(status) === cctxStatusToJSON(CctxStatus.Reverted);
+ const isPendingRevert = cctxStatusToJSON(status) === cctxStatusToJSON(CctxStatus.PendingRevert);
+ const isAborted = cctxStatusToJSON(status) === cctxStatusToJSON(CctxStatus.Aborted);
+ if (outbound_params[1] && (isReverted || isPendingRevert || isAborted)) {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||
| const statusIcon = isPendingRevert ? "🔄" : "✅"; | ||||||||||||||||||||||||||||||||
| let statusMessage = "Unknown"; | ||||||||||||||||||||||||||||||||
|
|
@@ -210,7 +235,7 @@ Receiver: ${receiver} | |||||||||||||||||||||||||||||||
| : revert_address; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const revertMessage = revert_message | ||||||||||||||||||||||||||||||||
| ? Buffer.from(revert_message, "base64").toString("hex") | ||||||||||||||||||||||||||||||||
| ? Buffer.from(String(revert_message), "base64").toString("hex") | ||||||||||||||||||||||||||||||||
| : "null"; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| let chainDetails = "Unknown"; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||

Uh oh!
There was an error while loading. Please reload this page.