Skip to content

Commit

Permalink
chore(app): creating aptos client according to wallet wip
Browse files Browse the repository at this point in the history
Signed-off-by: Kaan Caglan <[email protected]>
  • Loading branch information
Caglankaan committed Feb 28, 2025
1 parent c4ed99f commit 7779ece
Showing 1 changed file with 68 additions and 37 deletions.
105 changes: 68 additions & 37 deletions typescript-sdk/src/aptos/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
type AptosAccount,
// (These below helpers remain as before)
waitForTransactionReceipt,
type AptosPublicAccountInfo
} from "./transfer.ts"
Expand All @@ -9,12 +8,15 @@ import { Aptos, Network, AptosConfig, AccountAddress, MoveVector } from "@aptos-
import { createClient, fallback, type HttpTransport } from "viem"
import type { AptosBrowserWallet, AuthAccess } from "./wallet.ts"

// Define a unified signer type that always includes an accountAddress.
export type AptosSigner = AptosAccount | (AptosBrowserWallet & { accountAddress: string })

export type { AptosAccount, AptosBrowserWallet }

export const aptosChainId = [
"2", // aptos testnet
"2", // aptos testnet
"177", // movement porto
"250" // movement bardock
"250" // movement bardock
] as const
export type AptosChainId = `${(typeof aptosChainId)[number]}`

Expand All @@ -37,17 +39,17 @@ export type AptosClientParameters = {
*/
async function getAptosClient(
parameters: AptosClientParameters & { authAccess: "key" }
): Promise<{ authAccess: "key"; aptos: Aptos; signer: AptosAccount }>
): Promise<{ authAccess: "key"; aptos: Aptos; signer: AptosSigner }>

// async function getAptosClient(
// parameters: AptosClientParameters & { authAccess: "wallet" }
// ): Promise<{ authAccess: "wallet"; aptos: Aptos; signer: AptosBrowserWallet }>
async function getAptosClient(
parameters: AptosClientParameters & { authAccess: "wallet" }
): Promise<{ authAccess: "wallet"; aptos: Aptos; signer: AptosSigner }>

async function getAptosClient(
parameters: AptosClientParameters & { authAccess: AuthAccess }
): Promise<
| { authAccess: "key"; aptos: Aptos; signer: AptosAccount }
| { authAccess: "wallet"; aptos: Aptos; signer: AptosBrowserWallet }
| { authAccess: "key"; aptos: Aptos; signer: AptosSigner }
| { authAccess: "wallet"; aptos: Aptos; signer: AptosSigner }
> {
if (parameters.authAccess === "key") {
if (typeof parameters.transport !== "function") {
Expand All @@ -62,7 +64,7 @@ async function getAptosClient(
return {
authAccess: "key",
aptos: new Aptos(config),
signer: parameters.account as AptosAccount
signer: parameters.account as AptosAccount // AptosAccount is assumed to have accountAddress
}
}

Expand All @@ -71,20 +73,35 @@ async function getAptosClient(
throw new Error("Invalid Aptos transport")
}
const networkInfo = await parameters.transport.getNetwork()
const network = networkInfo.name.toLowerCase() === "mainnet" ? Network.MAINNET : Network.TESTNET
const network =
networkInfo.name.toLowerCase() === "mainnet" ? Network.MAINNET : Network.TESTNET
const config = new AptosConfig({ fullnode: networkInfo.url, network })

// Get the connected account
const account = await parameters.transport.getAccount?.() ||
{ address: "" }
if (!account.address) {
throw new Error("No account address found from the wallet")
}

// Create a signer by merging the wallet’s methods with the account address.
const signer = Object.assign({}, parameters.transport, {
accountAddress: account.address
}) as unknown as AptosAccount // <== Force-cast to AptosAccount

return {
authAccess: "wallet",
aptos: new Aptos(config),
signer: parameters.transport as AptosBrowserWallet
signer: signer
}
}


throw new Error("Invalid Aptos transport")
}

/**
* New unified transfer parameters for Aptos,
* matching the Cosmos & EVM clients.
* New unified transfer parameters for Aptos, matching the Cosmos & EVM clients.
*/
export interface TransferAssetParameters<AptosChainId> {
baseAmount: bigint
Expand All @@ -102,17 +119,22 @@ export interface TransferAssetParameters<AptosChainId> {
*/
export const createAptosClient = (clientParameters: AptosClientParameters) => {
return createClient({ transport: fallback([]) })
.extend(_ => ({
// A helper to get the underlying Aptos client.
// We default to "key" if an account was provided.
getAptosClient: async () => await getAptosClient({ ...clientParameters, authAccess: "key" })
// clientParameters.account
// ? await getAptosClient({ ...clientParameters, authAccess: "key" })
// : await getAptosClient({ ...clientParameters, authAccess: "wallet" })
.extend(() => ({
// Choose authAccess based on whether a key-based account was provided.
getAptosClient: async () => {
console.info("create aptos client params:", clientParameters)
if (clientParameters.account) {
console.info("returning key one")
return await getAptosClient({ ...clientParameters, authAccess: "key" })
} else {
console.info("returning auth one")
return await getAptosClient({ ...clientParameters, authAccess: "wallet" })
}
}
}))
.extend(client => ({
waitForTransactionReceipt: async ({ hash }: { hash: string }) => {
const { aptos, signer } = await client.getAptosClient()
const { aptos } = await client.getAptosClient()
return await waitForTransactionReceipt({ aptos, hash })
},

Expand All @@ -130,10 +152,17 @@ export const createAptosClient = (clientParameters: AptosClientParameters) => {
ucs03address
}: TransferAssetParameters<AptosChainId>): Promise<Result<string, Error>> => {
const { aptos, signer } = await client.getAptosClient()

const baseTokenHex = baseToken.startsWith("0x") ? baseToken.slice(2) : baseToken // Remove "0x" if it exists
// let my_addr = AccountAddress.fromHex(baseToken)

console.info("aptos", aptos)
console.info("signer", signer)
console.info("baseAmount", baseAmount)
console.info("baseToken", baseToken)
console.info("quoteAmount", quoteAmount)
console.info("quoteToken", quoteToken)
console.info("receiver", receiver)
console.info("sourceChannelId", sourceChannelId)
console.info("ucs03address", ucs03address)

const baseTokenHex = baseToken.startsWith("0x") ? baseToken.slice(2) : baseToken
const quoteTokenVec = MoveVector.U8(quoteToken)
const receiverVec = MoveVector.U8(receiver)

Expand All @@ -159,17 +188,19 @@ export const createAptosClient = (clientParameters: AptosClientParameters) => {
]
}
})

try {
const txn = await aptos.signAndSubmitTransaction({
signer: signer,
transaction: payload
})
const receipt = await waitForTransactionReceipt({ aptos, hash: txn.hash })
return receipt
} catch (error) {
return err(new Error("failed to execute aptos call", { cause: error as Error }))
}
console.info("payload", payload)
return err(new Error("not implemented"))

// try {
// const txn = await aptos.signAndSubmitTransaction({
// signer: signer,
// transaction: payload
// })
// const receipt = await waitForTransactionReceipt({ aptos, hash: txn.hash })
// return receipt
// } catch (error) {
// return err(new Error("failed to execute aptos call", { cause: error as Error }))
// }
}
}))
}

0 comments on commit 7779ece

Please sign in to comment.