Skip to content

Commit

Permalink
feat(typescript-sdk): use predicted counterparty denom
Browse files Browse the repository at this point in the history
  • Loading branch information
cor committed Jan 7, 2025
1 parent 85a322f commit 7b97072
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
6 changes: 3 additions & 3 deletions typescript-sdk/playground/holesky-to-sepolia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const ONLY_ESTIMATE_GAS = values["estimate-gas"] ?? false

const evmAccount = privateKeyToAccount(`0x${PRIVATE_KEY}`)

const LINK_CONTRACT_ADDRESS = "0x779877A7B0D9E8603169DdbD7836e478b4624789"
const LINK_CONTRACT_ADDRESS = "0x685cE6742351ae9b618F383883D6d1e0c5A31B4B"
const RECEIVER = "0x153919669Edc8A5D0c8D1E4507c9CE60435A1177"

try {
Expand All @@ -37,12 +37,12 @@ try {
})

const transactionPayload = {
amount: 420n,
amount: 421n,
destinationChainId: `${sepolia.id}`,
receiver: RECEIVER,
denomAddress: LINK_CONTRACT_ADDRESS,
autoApprove: true
} satisfies TransferAssetsParameters<"11155111">
} satisfies TransferAssetsParameters<"17000">

const gasEstimationResponse = await client.simulateTransaction(transactionPayload)

Expand Down
28 changes: 25 additions & 3 deletions typescript-sdk/src/evm/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
type HttpTransport,
createWalletClient,
type CustomTransport,
type FallbackTransport
type FallbackTransport,
createPublicClient,
http
} from "viem"
import {
evmSameChainTransfer,
Expand All @@ -26,6 +28,8 @@ import {
berachainTestnetbArtio
} from "viem/chains"
import type { TransferAssetsParameters, LooseAutocomplete, Hex, HexAddress } from "../types.ts"
import { getChainId } from "viem/actions"
import { ucs03ZkgmAbi } from "#abi/ucs-03.ts"
export { sepolia, scrollSepolia, arbitrumSepolia, berachainTestnetbArtio }

export const evmChains = [
Expand Down Expand Up @@ -90,12 +94,29 @@ export const createEvmClient = (parameters: EvmClientParameters) => {
}

const chainDetails = await getHubbleChainDetails({
destinationChainId,
sourceChainId: parameters.chainId
sourceChainId: parameters.chainId,
destinationChainId
})

if (chainDetails.isErr()) return err(chainDetails.error)

// TODO: make resillient
const destinationChainClient = createPublicClient({
chain: evmChainFromChainId(destinationChainId),
transport: http()
})

// We need to predict the askToken denom based on the sentToken (denomAddress in the transferAssetFromEvm args)
// we do this by calling the ucs03 instance on the counterparty chain.
const [askToken, _] = (await destinationChainClient.readContract({
address: chainDetails.value.destinationUCS03Address as `0x${string}`,
abi: ucs03ZkgmAbi,
functionName: "predictWrappedToken",
args: [0, chainDetails.value.destinationChannel, denomAddress]
})) as ["0x${string}", string]

console.log({ sentToken: denomAddress, askToken }) // useful for debugging app

// if (chainDetails.value.transferType === "pfm") {
// if (!chainDetails.value.port) return err(new Error("Port not found in hubble"))
// const pfmMemo = createPfmMemo({
Expand All @@ -121,6 +142,7 @@ export const createEvmClient = (parameters: EvmClientParameters) => {
receiver,
autoApprove,
denomAddress,
askToken,
sourceChannel,
relayContractAddress
})
Expand Down
21 changes: 8 additions & 13 deletions typescript-sdk/src/evm/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ import {
type Account,
type WalletClient,
type PublicActions,
toHex
toHex,
createClient,
createPublicClient
} from "viem"
import { holesky } from "viem/chains"

export type EvmTransferParams = {
memo?: string
askToken: HexAddress
amount: bigint
receiver: string
account?: Account
Expand Down Expand Up @@ -48,6 +52,7 @@ export async function transferAssetFromEvm(
account,
receiver,
denomAddress,
askToken,
sourceChannel,
simulate = true,
autoApprove = false,
Expand All @@ -73,11 +78,6 @@ export async function transferAssetFromEvm(

memo ??= timestamp()

// We need to predict the askToken denom based on the sentToken (denomAddress in the transferAssetFromEvm args)
// we do this by calling the ucs03 instance on the counterparty chain.
//
await client.readContract

// add a salt to each transfer to prevent hash collisions
// important because ibc-union does not use sequence numbers
// such that intents are possible based on deterministic packet hashes
Expand Down Expand Up @@ -112,16 +112,11 @@ export async function transferAssetFromEvm(
"0x000000000000000000000000000000000000000000000000fffffffffffffffa", // TODO: make non-hexencoded timestamp
toHex(salt),
receiver.startsWith("0x") ? getAddress(receiver) : bech32AddressToHex({ address: receiver }),
denomAddress, // TODO: customize sentToken
denomAddress,
amount,
"0xd1b482d1b947a96e96c9b76d15de34f7f70a20a1", // TODO: customize askToken
askToken,
amount, // we want the same amount on dest as we send on the source
false
//
// [{ denom: denomAddress, amount }],
// memo,
// { revision_number: 9n, revision_height: BigInt(999_999_999) + 100n },
// 0n
]
} as const
if (!simulate) {
Expand Down
4 changes: 3 additions & 1 deletion typescript-sdk/src/pfm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export async function getHubbleChainDetails({
sourceChannel: number
destinationChannel: number
relayContractAddress: string
destinationUCS03Address: string
transferType: "direct" | "pfm"
},
Error
Expand Down Expand Up @@ -117,7 +118,8 @@ export async function getHubbleChainDetails({
transferType: "direct",
sourceChannel,
destinationChannel,
relayContractAddress: sourceChain.ucs3_config.address
relayContractAddress: sourceChain.ucs3_config.address,
destinationUCS03Address: destinationChain.ucs3_config.address
})
// }

Expand Down

0 comments on commit 7b97072

Please sign in to comment.