Skip to content

Commit

Permalink
fix(typescript-sdk): hardcode less
Browse files Browse the repository at this point in the history
  • Loading branch information
cor committed Jan 7, 2025
1 parent 3b7562f commit 855c24c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 29 deletions.
76 changes: 76 additions & 0 deletions typescript-sdk/playground/holesky-to-sepolia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/env bun
import { fallback, http } from "viem"
import { parseArgs } from "node:util"
import { consola } from "scripts/logger"
import { raise } from "#utilities/index.ts"
import { privateKeyToAccount } from "viem/accounts"
import { holesky, sepolia } from "viem/chains"
import { createUnionClient, type TransferAssetsParameters } from "#mod.ts"

/* `bun playground/sepolia-to-holesky.ts --private-key "..."` */

const { values } = parseArgs({
args: process.argv.slice(2),
options: {
"private-key": { type: "string" },
"estimate-gas": { type: "boolean", default: false }
}
})

const PRIVATE_KEY = values["private-key"]
if (!PRIVATE_KEY) raise("Private key not found")
const ONLY_ESTIMATE_GAS = values["estimate-gas"] ?? false

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

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

try {
const client = createUnionClient({
chainId: "17000",
account: evmAccount,
transport: fallback([
http("https://rpc.holesky.sepolia.chain.kitchen"),
http(holesky?.rpcUrls.default.http.at(0))
])
})

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

const gasEstimationResponse = await client.simulateTransaction(transactionPayload)

if (gasEstimationResponse.isErr()) {
consola.error(gasEstimationResponse.error)
process.exit(1)
}

consola.success("Sepolia to Holesky gas cost:", gasEstimationResponse.value)

if (ONLY_ESTIMATE_GAS) process.exit(0)

if (gasEstimationResponse.isErr()) {
console.info("Transaction simulation failed", gasEstimationResponse.error)
process.exit(1)
}

const transfer = await client.transferAsset(transactionPayload)

if (transfer.isErr()) {
console.error(transfer.error)
process.exit(1)
}

consola.info(transfer.value)
} catch (error) {
const errorMessage = error instanceof Error ? error.message : error
console.error(errorMessage)
} finally {
process.exit(0)
}
2 changes: 1 addition & 1 deletion typescript-sdk/playground/sepolia-to-holesky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ try {
chainId: "11155111",
account: evmAccount,
transport: fallback([
http("https://rpc.17000.sepolia.chain.kitchen"),
http("https://rpc.11155111.sepolia.chain.kitchen"),
http(sepolia?.rpcUrls.default.http.at(0))
])
})
Expand Down
37 changes: 11 additions & 26 deletions typescript-sdk/src/evm/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,12 @@ export const createEvmClient = (parameters: EvmClientParameters) => {
return ok(transfer.value)
}

// TODO: don't hardcode chain details
// const chainDetails2 = await getHubbleChainDetails({
// destinationChainId,
// sourceChainId: parameters.chainId
// })

const chainDetails = {
value: {
sourceChannel: "3",
relayContractAddress: "0x84F074C15513F15baeA0fbEd3ec42F0Bd1fb3efa",
transferType: "direct",
destinationChainId
}
}
// const chainDetails = await getHubbleChainDetails({
// destinationChainId,
// sourceChainId: parameters.chainId
// })
const chainDetails = await getHubbleChainDetails({
destinationChainId,
sourceChainId: parameters.chainId
})

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

// if (chainDetails.value.transferType === "pfm") {
// if (!chainDetails.value.port) return err(new Error("Port not found in hubble"))
Expand All @@ -124,7 +110,6 @@ export const createEvmClient = (parameters: EvmClientParameters) => {
// memo = pfmMemo.value
// }

destinationChainId ??= chainDetails.value.destinationChainId
const sourceChannel = chainDetails.value.sourceChannel
relayContractAddress ??= getAddress(chainDetails.value.relayContractAddress)

Expand Down Expand Up @@ -154,12 +139,12 @@ export const createEvmClient = (parameters: EvmClientParameters) => {
// otherwise, it's the relayer contract address from ucs config
if (parameters.chainId !== destinationChainId) {
// TODO: don't hardcode
// const ucsDetails = await getHubbleChainDetails({
// destinationChainId,
// sourceChainId: parameters.chainId
// })
// if (ucsDetails.isErr()) return err(ucsDetails.error)
_receiver = "0x84F074C15513F15baeA0fbEd3ec42F0Bd1fb3efa"
const ucsDetails = await getHubbleChainDetails({
destinationChainId,
sourceChainId: parameters.chainId
})
if (ucsDetails.isErr()) return err(ucsDetails.error)
_receiver = ucsDetails.value.relayContractAddress as `0x${string}`
} else _receiver = getAddress(receiver)

return await evmApproveTransferAsset(client, {
Expand Down
9 changes: 7 additions & 2 deletions typescript-sdk/src/evm/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type EvmTransferParams = {
account?: Account
simulate?: boolean
autoApprove?: boolean
sourceChannel: string
sourceChannel: number
denomAddress: HexAddress
relayContractAddress: HexAddress
}
Expand Down Expand Up @@ -73,6 +73,11 @@ 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 @@ -102,7 +107,7 @@ export async function transferAssetFromEvm(
* bool onlyMaker
*/
args: [
Number(sourceChannel), // TODO: make typesafe
sourceChannel,
0n, // TODO: customize timeoutheight
"0x000000000000000000000000000000000000000000000000fffffffffffffffa", // TODO: make non-hexencoded timestamp
toHex(salt),
Expand Down

0 comments on commit 855c24c

Please sign in to comment.