Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
o-az committed Nov 8, 2024
1 parent 75df1e6 commit ef0ed7f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 59 deletions.
6 changes: 5 additions & 1 deletion typescript-sdk/.npmrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
git-checks = false

engine-strict=true
package-lock=false
shell-emulator=true
git-tag-version=false
auto-install-peers=true
node-options="--no-warnings"
node-options="--no-warnings NODE_NO_WARNINGS=1"

@jsr:registry=https://npm.jsr.io
Binary file modified typescript-sdk/bun.lockb
Binary file not shown.
58 changes: 25 additions & 33 deletions typescript-sdk/playground/move-to-union.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
#!/usr/bin/env bun

import { http } from "viem"
import { parseArgs } from "node:util"
import { consola } from "scripts/logger"
import { raise } from "#utilities/index.ts"
import { createUnionClient, type TransferAssetsParameters } from "#mod.ts"
import { Account, Ed25519PrivateKey } from "@aptos-labs/ts-sdk"
import { createUnionClient, type TransferAssetsParameters } from "#mod.ts"

/* npx tsx playground/move-to-union.ts --private-key "e3579557fd55ed8fab0d1e211eb1c05d56d74650e7070b703925493c38fe2aed" --chain-id "2" */
/* node --import=tsx playground/move-to-union.ts --private-key "e3579557fd55ed8fab0d1e211eb1c05d56d74650e7070b703925493c38fe2aed" */

const { values } = parseArgs({
args: process.argv.slice(2),
options: {
"private-key": { type: "string" }, // User's private key
"chain-id": { type: "string" }, // Chain ID for the Move chain
"rpc-url": { type: "string" } // Move RPC URL
"private-key": { type: "string" } // User's private key
}
})

const PRIVATE_KEY = values["private-key"]
const CHAIN_ID = values["chain-id"]
let RPC_URL = values["rpc-url"]

if (!PRIVATE_KEY) raise("Private key not found")
if (!CHAIN_ID) raise("Chain ID not provided")
if (!RPC_URL) RPC_URL = "https://api.testnet.aptoslabs.com/v1"

// Convert the hex string private key to a Uint8Array
const privateKeyBytes = Uint8Array.from(Buffer.from(PRIVATE_KEY, "hex"))
Expand All @@ -33,27 +26,26 @@ const moveAccount = Account.fromPrivateKey({
privateKey: new Ed25519PrivateKey(privateKeyBytes)
})

try {
// Create the Move client
const client = createUnionClient({
account: moveAccount,
chainId: CHAIN_ID as "2", // Adjust according to your setup
transport: http(RPC_URL)
})

const transferResult = await client.transferAsset({
memo: "",
amount: 1n,
receiver: "1363462745291c711144011c1305e737dd74ace69a5576612745e29a2e4fa1b5",
denomAddress: "0x9935a6a334e070bcecf5b1abb1c842c123572e63e70f0539d79610c32954c06c",
destinationChainId: "union-testnet-8"
} satisfies TransferAssetsParameters<"2">)

consola.success("Transfer result:", transferResult)
consola.success("Move Client created successfully", client)
} catch (error) {
const errorMessage = error instanceof Error ? error.message : error
consola.error("Error creating Move client:", errorMessage)
} finally {
process.exit(0)
// Create the Move client
const client = createUnionClient({
chainId: "2",
account: moveAccount,
transport: http("https://api.testnet.aptoslabs.com/v1")
})

const transferResult = await client.transferAsset({
memo: "",
amount: 1n,
receiver: "1363462745291c711144011c1305e737dd74ace69a5576612745e29a2e4fa1b5",
denomAddress: "0x9935a6a334e070bcecf5b1abb1c842c123572e63e70f0539d79610c32954c06c",
destinationChainId: "union-testnet-8"
} satisfies TransferAssetsParameters<"2">)

// consola.success("Move Client created successfully", client)

if (transferResult.isErr()) {
consola.error("Error transferring asset:", transferResult.error)
process.exit(1)
}

consola.success("Transfer result:", transferResult)
21 changes: 8 additions & 13 deletions typescript-sdk/src/client/move.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { createClient, fallback, type HttpTransport } from "viem"
import { err, type Result } from "neverthrow"
import type { TransferAssetsParameters } from "./types.ts"
import { consola } from "scripts/logger"
import { transferAssetFromMove } from "src/transfer/move.ts" // Import the Move transfer function
import type { Account } from "@aptos-labs/ts-sdk"
import type { TransferAssetsParameters } from "./types.ts"
import { transferAssetFromMove } from "../transfer/move.ts"
import { createClient, fallback, type HttpTransport } from "viem"

// Define the list of supported Move chains
export const moveChainId = ["2"] as const
Expand All @@ -18,22 +17,20 @@ export interface MoveClientParameters {

/* trunk-ignore(biome/lint/nursery/useExplicitFunctionReturnType) */
export const createMoveClient = (parameters: MoveClientParameters) =>
createClient({ transport: fallback([]) }).extend(client => ({
createClient({ transport: fallback([]) }).extend(_ => ({
transferAsset: async ({
memo,
amount,
receiver,
denomAddress,
destinationChainId,
account = parameters.account,
gasPrice = parameters.gasPrice
destinationChainId: _destinationChainId,
relayContractAddress = "0x52570c4292730a9d81aead22ac75d4bfca3f23d788f679ce72a11ca3fa7d6762",
account = parameters.account
}: TransferAssetsParameters<MoveChainId>): Promise<Result<string, Error>> => {
const rpcUrl = parameters.transport({}).value?.url

if (!rpcUrl) return err(new Error("No Move RPC URL found"))
if (!account) return err(new Error("No Move account found"))
consola.info(`Move client created for chainId: ${parameters.chainId}`)
consola.info(`RPC URL: ${rpcUrl}`)
consola.info(`account: ${account}`)

// const chainDetails = await getHubbleChainDetails({
// destinationChainId,
Expand All @@ -59,8 +56,6 @@ export const createMoveClient = (parameters: MoveClientParameters) =>
// const sourceChannel = chainDetails.value.sourceChannel
// const relayContractAddress = chainDetails.value.relayContractAddress
const sourceChannel = "channel-0"
const relayContractAddress =
"0x52570c4292730a9d81aead22ac75d4bfca3f23d788f679ce72a11ca3fa7d6762"

// priv key: 0xe992615114d70429d2920c9d106ac55ec16d9d36a5a017f14f9ee77a85f02467
// account addr: 0xe3579557fd55ed8fab0d1e211eb1c05d56d74650e7070b703925493c38fe2aed
Expand Down
18 changes: 6 additions & 12 deletions typescript-sdk/src/transfer/move.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { err, ok, type Result } from "neverthrow"
import { type Account, Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk"
import consola from "consola"
import { raise } from "#utilities/index.ts"
import { Hex } from "node_modules/@aptos-labs/ts-sdk/dist/common"
import { type Account, Aptos, AptosConfig, Network } from "@aptos-labs/ts-sdk"

export type TransferAssetFromMoveParams = {
memo?: string
Expand Down Expand Up @@ -51,20 +49,16 @@ export async function transferAssetFromMove({
}: TransferAssetFromMoveParams): Promise<Result<string, Error>> {
try {
// Ensure the baseUrl is provided and valid
if (!baseUrl) {
return err(new Error("Base URL for Aptos node not provided"))
}
if (!baseUrl) return err(new Error("Base URL for Aptos node not provided"))

// TODO: Handle simulation scenario
if (simulate) {
raise("Simulation not implemented")
}
if (simulate) return err(new Error("Simulation not implemented"))

// Setup the Aptos client with the correct network and base URL
const config = new AptosConfig({ fullnode: baseUrl, network: Network.TESTNET })
const aptos = new Aptos(config)

consola.info(`Using Aptos fullnode at: ${baseUrl}`)
console.info(`Using Aptos fullnode at: ${baseUrl}`)

// Build the transaction using the IBC `send` function (similar to EVM)
const transaction = await aptos.transaction.build.simple({
Expand All @@ -85,7 +79,7 @@ export async function transferAssetFromMove({
}
})

consola.info("Transaction built successfully")
console.info("Transaction built successfully")

// Sign and submit the transaction
const senderAuthenticator = aptos.transaction.sign({
Expand All @@ -95,7 +89,7 @@ export async function transferAssetFromMove({

const pendingTxn = await aptos.transaction.submit.simple({ transaction, senderAuthenticator })

consola.info(`Transaction executed! Hash: ${pendingTxn.hash}`)
console.info(`Transaction executed! Hash: ${pendingTxn.hash}`)

return ok(pendingTxn.hash) // Return the transaction hash
} catch (error) {
Expand Down

0 comments on commit ef0ed7f

Please sign in to comment.