Skip to content

Commit

Permalink
feat: aptos - allow passing signed payload from wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
o-az committed Oct 26, 2024
1 parent 323e8c5 commit a2e63c3
Show file tree
Hide file tree
Showing 8 changed files with 406 additions and 254 deletions.
19 changes: 12 additions & 7 deletions typescript-sdk/playground/aptos-to-aptos.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env bun
import { parseArgs } from "node:util"
import { consola } from "scripts/logger"
import { raise } from "#utilities/index.ts"
import { Account, Ed25519PrivateKey } from "@aptos-labs/ts-sdk"
import "#patch.ts"
import {
http,
createUnionClient,
hexStringToUint8Array,
type TransferAssetsParameters
} from "#mod.ts"
import { parseArgs } from "node:util"
import { consola } from "scripts/logger"
import { raise } from "#utilities/index.ts"
import { Account, Ed25519PrivateKey } from "@aptos-labs/ts-sdk"

/* node --import=tsx playground/aptos-to-union.ts --private-key $PRIVATE_KEY */

Expand All @@ -26,6 +27,7 @@ if (!PRIVATE_KEY) raise("Private key not found")
const aptosAccount = Account.fromPrivateKey({
privateKey: new Ed25519PrivateKey(hexStringToUint8Array(PRIVATE_KEY))
})
console.info(aptosAccount.accountAddress.toString())

const client = createUnionClient({
chainId: "2",
Expand All @@ -36,9 +38,12 @@ const client = createUnionClient({
const transferPayload = {
memo: "",
amount: 1n,
receiver: "0x2fb6eaaff3f29cedbcc89129a01aa60b2e4712ffd264b255244168a1bddea9ec",
denomAddress: "0x9935a6a334e070bcecf5b1abb1c842c123572e63e70f0539d79610c32954c06c",
destinationChainId: "2"
simulate: false,
authAccess: "key",
account: aptosAccount,
destinationChainId: "2",
receiver: "0xe3579557fd55ed8fab0d1e211eb1c05d56d74650e7070b703925493c38fe2aed",
denomAddress: "0x9935a6a334e070bcecf5b1abb1c842c123572e63e70f0539d79610c32954c06c"
} satisfies TransferAssetsParameters<"2">

const simulateResult = await client.simulateTransaction(transferPayload)
Expand Down
21 changes: 12 additions & 9 deletions typescript-sdk/playground/aptos-to-union.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#!/usr/bin/env bun
import { parseArgs } from "node:util"
import { consola } from "scripts/logger"
import { raise } from "#utilities/index.ts"
import { Account, Ed25519PrivateKey } from "@aptos-labs/ts-sdk"
import "#patch.ts"
import {
http,
createUnionClient,
type TransferAssetsParameters,
hexStringToUint8Array
hexStringToUint8Array,
type TransferAssetsParameters
} from "#mod.ts"
import { parseArgs } from "node:util"
import { consola } from "scripts/logger"
import { raise } from "#utilities/index.ts"
import { Account, Ed25519PrivateKey } from "@aptos-labs/ts-sdk"

/* node --import=tsx playground/aptos-to-union.ts --private-key $PRIVATE_KEY */

Expand Down Expand Up @@ -38,10 +39,12 @@ const client = createUnionClient({
const transferPayload = {
memo: "",
amount: 1n,
// receiver: "1363462745291c711144011c1305e737dd74ace69a5576612745e29a2e4fa1b5",
simulate: false,
authAccess: "key",
account: aptosAccount,
destinationChainId: "union-testnet-8",
receiver: "union17ttpfu2xsmfxu6shl756mmxyqu33l5ljs5j6md",
denomAddress: "0x9935a6a334e070bcecf5b1abb1c842c123572e63e70f0539d79610c32954c06c",
destinationChainId: "union-testnet-8"
denomAddress: "0x9935a6a334e070bcecf5b1abb1c842c123572e63e70f0539d79610c32954c06c"
} satisfies TransferAssetsParameters<"2">

const simulateResult = await client.simulateTransaction(transferPayload)
Expand Down
148 changes: 87 additions & 61 deletions typescript-sdk/src/client/aptos.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,86 @@
import {
type AptosAccount,
type AptosAuthAccess,
aptosTransferSimulate,
aptosSameChainTransfer,
transferAssetFromAptos,
aptosSameChainTransferSimulate,
transferAssetFromAptosSimulate
type AptosBrowserWallet,
type AptosTransferParams,
waitForTransactionReceipt,
type AptosPublicAccountInfo
} from "../transfer/aptos.ts"
import { cosmosChainId } from "./cosmos.ts"
import { err, type Result } from "neverthrow"
import type { Account } from "@aptos-labs/ts-sdk"
import { bech32AddressToHex } from "../convert.ts"
import type { TransferAssetsParameters } from "./types.ts"
import { createPfmMemo, getHubbleChainDetails } from "../pfm.ts"
import { Aptos, Network, AptosConfig } from "@aptos-labs/ts-sdk"
import { createClient, fallback, type HttpTransport } from "viem"

export type {
AptosAccount,
AptosAuthAccess,
AptosBrowserWallet,
AptosTransferParams,
AptosPublicAccountInfo
}

export const aptosChainId = ["2"] as const

export type AptosChainId = `${(typeof aptosChainId)[number]}`

export interface AptosClientParameters {
account?: Account
account?: AptosAccount
chainId: AptosChainId
transport: HttpTransport
}

export const createAptosClient = (parameters: AptosClientParameters) =>
createClient({ transport: fallback([]) }).extend(_ => ({
transferAsset: async ({
memo,
amount,
receiver,
simulate,
denomAddress,
destinationChainId,
relayContractAddress,
account = parameters.account
}: TransferAssetsParameters<AptosChainId>): Promise<Result<string, Error>> => {
const rpcUrl = parameters.transport({}).value?.url

if (!rpcUrl) return err(new Error("No Aptos RPC URL found"))
if (!account) return err(new Error("No Aptos account found"))

if (parameters.chainId === destinationChainId) {
export const createAptosClient = (clientParameters: AptosClientParameters) => {
const rpcUrl = clientParameters.transport({}).value?.url

if (!rpcUrl) throw new Error("No Aptos RPC URL found")

const config = new AptosConfig({ fullnode: rpcUrl, network: Network.TESTNET })
const aptos = new Aptos(config)

return createClient({ transport: fallback([]) }).extend(_ => ({
waitForTransactionReceipt: async ({ hash }: { hash: string }) =>
waitForTransactionReceipt({ aptos, hash }),
transferAsset: async (
transferParameters: TransferAssetsParameters<AptosChainId>
): Promise<Result<string, Error>> => {
let {
memo,
amount,
simulate,
receiver,
denomAddress,
destinationChainId,
relayContractAddress
} = transferParameters

if (!transferParameters.account) return err(new Error("No Aptos account found"))
if (!destinationChainId) return err(new Error("destinationChainId missing"))

const account = transferParameters.account || clientParameters.account

if (clientParameters.chainId === destinationChainId) {
// @ts-expect-error TODO: fix account type
const transfer = await aptosSameChainTransfer({
...transferParameters,
aptos,
amount,
account,
simulate,
receiver,
denomAddress,
baseUrl: rpcUrl
denomAddress
})
return transfer
}

const chainDetails = await getHubbleChainDetails({
destinationChainId,
sourceChainId: parameters.chainId
sourceChainId: clientParameters.chainId
})
if (chainDetails.isErr()) return err(chainDetails.error)

Expand All @@ -76,50 +104,49 @@ export const createAptosClient = (parameters: AptosClientParameters) =>
const sourceChannel = chainDetails.value.sourceChannel
relayContractAddress ??= chainDetails.value.relayContractAddress

const result = await transferAssetFromAptos({
// @ts-expect-error TODO: fix account type
return await transferAssetFromAptos({
...transferParameters,
memo,
aptos,
amount,
account,
receiver,
simulate,
receiver,
denomAddress,
sourceChannel,
baseUrl: rpcUrl,
destinationChainId,
relayContractAddress
})
if (result.isErr()) return err(new Error(`Aptos transfer failed: ${result.error.message}`))

return result
},
simulateTransaction: async ({
memo,
amount,
receiver,
denomAddress,
destinationChainId,
relayContractAddress,
account = parameters.account
}: TransferAssetsParameters<AptosChainId>): Promise<Result<string, Error>> => {
const rpcUrl = parameters.transport({}).value?.url

if (!rpcUrl) return err(new Error("No Aptos RPC URL found"))
if (!account) return err(new Error("No Aptos account found"))

if (parameters.chainId === destinationChainId) {
return await aptosSameChainTransferSimulate({
amount,
account,
receiver,
denomAddress,
baseUrl: rpcUrl
simulateTransaction: async (
transferParameters: TransferAssetsParameters<AptosChainId>
): Promise<Result<string, Error>> => {
let {
memo,
amount,
receiver,
denomAddress,
autoApprove: _,
destinationChainId,
relayContractAddress
} = transferParameters

if (!transferParameters.account) return err(new Error("No Aptos account found"))
if (!destinationChainId) return err(new Error("destinationChainId missing"))

if (clientParameters.chainId === destinationChainId) {
return await aptosTransferSimulate({
aptos,
path: "SAME_CHAIN",
...transferParameters
})
}

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

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

if (chainDetails.value.transferType === "pfm") {
Expand All @@ -138,22 +165,21 @@ export const createAptosClient = (parameters: AptosClientParameters) =>
if (pfmMemo.isErr()) return err(pfmMemo.error)
memo = pfmMemo.value
}

const sourceChannel = chainDetails.value.sourceChannel
relayContractAddress ??= chainDetails.value.relayContractAddress

const result = await transferAssetFromAptosSimulate({
return await aptosTransferSimulate({
...transferParameters,
path: "CROSS_CHAIN",
memo,
aptos,
amount,
account,
receiver,
denomAddress,
sourceChannel,
baseUrl: rpcUrl,
destinationChainId,
relayContractAddress
})
if (!result) return err(new Error(`Aptos transfer failed`))

return result
}
}))
}
1 change: 0 additions & 1 deletion typescript-sdk/src/client/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ export const createEvmClient = (parameters: EvmClientParameters) => {
autoApprove,
denomAddress,
sourceChannel,
destinationChainId,
relayContractAddress
})
},
Expand Down
25 changes: 20 additions & 5 deletions typescript-sdk/src/client/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import type {
aptosChainId,
AptosChainId,
AptosAccount,
AptosBrowserWallet,
AptosPublicAccountInfo
} from "./aptos.ts"
import type { OfflineSigner } from "../types.ts"
import type { evmChainId, EvmChainId } from "./evm.ts"
import type { Account as ViemAccount, Address } from "viem"
import type { aptosChainId, AptosChainId } from "./aptos.ts"
import type { cosmosChainId, CosmosChainId } from "./cosmos.ts"
import type { Account as AptosAccount } from "@aptos-labs/ts-sdk"

export type { evmChainId, EvmChainId, cosmosChainId, CosmosChainId, aptosChainId, AptosChainId }

Expand All @@ -29,15 +34,25 @@ export type TransferAssetsParameters<CHAIN_ID extends EvmChainId | CosmosChainId
? {
simulate?: boolean
denomAddress: Address
account?: ViemAccount | undefined
relayContractAddress?: Address
account?: ViemAccount | undefined
}
: CHAIN_ID extends AptosChainId
? {
simulate?: boolean
denomAddress: string
account?: AptosAccount
relayContractAddress?: string
gasPrice?: { amount: string; denom: string }
simulate?: boolean
}
} & (
| {
authAccess: "key"
account?: AptosAccount
}
| {
authAccess: "wallet"
account?: AptosPublicAccountInfo
sign: AptosBrowserWallet["signTransaction"]
}
)
: undefined)
Loading

0 comments on commit a2e63c3

Please sign in to comment.