Skip to content

Commit 8532e74

Browse files
committed
chore(app): creating aptos client according to wallet wip
Signed-off-by: Kaan Caglan <caglankaan@gmail.com>
1 parent c4ed99f commit 8532e74

File tree

1 file changed

+42
-25
lines changed

1 file changed

+42
-25
lines changed

typescript-sdk/src/aptos/client.ts

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
type AptosAccount,
3-
// (These below helpers remain as before)
43
waitForTransactionReceipt,
54
type AptosPublicAccountInfo
65
} from "./transfer.ts"
@@ -9,12 +8,15 @@ import { Aptos, Network, AptosConfig, AccountAddress, MoveVector } from "@aptos-
98
import { createClient, fallback, type HttpTransport } from "viem"
109
import type { AptosBrowserWallet, AuthAccess } from "./wallet.ts"
1110

11+
// Define a unified signer type that always includes an accountAddress.
12+
export type AptosSigner = AptosAccount | (AptosBrowserWallet & { accountAddress: string })
13+
1214
export type { AptosAccount, AptosBrowserWallet }
1315

1416
export const aptosChainId = [
15-
"2", // aptos testnet
17+
"2", // aptos testnet
1618
"177", // movement porto
17-
"250" // movement bardock
19+
"250" // movement bardock
1820
] as const
1921
export type AptosChainId = `${(typeof aptosChainId)[number]}`
2022

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

42-
// async function getAptosClient(
43-
// parameters: AptosClientParameters & { authAccess: "wallet" }
44-
// ): Promise<{ authAccess: "wallet"; aptos: Aptos; signer: AptosBrowserWallet }>
44+
async function getAptosClient(
45+
parameters: AptosClientParameters & { authAccess: "wallet" }
46+
): Promise<{ authAccess: "wallet"; aptos: Aptos; signer: AptosSigner }>
4547

4648
async function getAptosClient(
4749
parameters: AptosClientParameters & { authAccess: AuthAccess }
4850
): Promise<
49-
| { authAccess: "key"; aptos: Aptos; signer: AptosAccount }
50-
| { authAccess: "wallet"; aptos: Aptos; signer: AptosBrowserWallet }
51+
| { authAccess: "key"; aptos: Aptos; signer: AptosSigner }
52+
| { authAccess: "wallet"; aptos: Aptos; signer: AptosSigner }
5153
> {
5254
if (parameters.authAccess === "key") {
5355
if (typeof parameters.transport !== "function") {
@@ -62,7 +64,7 @@ async function getAptosClient(
6264
return {
6365
authAccess: "key",
6466
aptos: new Aptos(config),
65-
signer: parameters.account as AptosAccount
67+
signer: parameters.account as AptosAccount // AptosAccount is assumed to have accountAddress
6668
}
6769
}
6870

@@ -71,20 +73,35 @@ async function getAptosClient(
7173
throw new Error("Invalid Aptos transport")
7274
}
7375
const networkInfo = await parameters.transport.getNetwork()
74-
const network = networkInfo.name.toLowerCase() === "mainnet" ? Network.MAINNET : Network.TESTNET
76+
const network =
77+
networkInfo.name.toLowerCase() === "mainnet" ? Network.MAINNET : Network.TESTNET
7578
const config = new AptosConfig({ fullnode: networkInfo.url, network })
79+
80+
// Get the connected account; this may require calling a dedicated method
81+
const account = await parameters.transport.getAccount?.() ||
82+
// Or retrieve from your store if already connected:
83+
{ address: "" }
84+
if (!account.address) {
85+
throw new Error("No account address found from the wallet")
86+
}
87+
88+
// Create a signer by merging the wallet’s methods with the account address.
89+
const signer = Object.assign({}, parameters.transport, {
90+
accountAddress: account.address
91+
}) as AptosBrowserWallet & { accountAddress: string }
92+
7693
return {
7794
authAccess: "wallet",
7895
aptos: new Aptos(config),
79-
signer: parameters.transport as AptosBrowserWallet
96+
signer: signer
8097
}
8198
}
99+
82100
throw new Error("Invalid Aptos transport")
83101
}
84102

85103
/**
86-
* New unified transfer parameters for Aptos,
87-
* matching the Cosmos & EVM clients.
104+
* New unified transfer parameters for Aptos, matching the Cosmos & EVM clients.
88105
*/
89106
export interface TransferAssetParameters<AptosChainId> {
90107
baseAmount: bigint
@@ -102,17 +119,19 @@ export interface TransferAssetParameters<AptosChainId> {
102119
*/
103120
export const createAptosClient = (clientParameters: AptosClientParameters) => {
104121
return createClient({ transport: fallback([]) })
105-
.extend(_ => ({
106-
// A helper to get the underlying Aptos client.
107-
// We default to "key" if an account was provided.
108-
getAptosClient: async () => await getAptosClient({ ...clientParameters, authAccess: "key" })
109-
// clientParameters.account
110-
// ? await getAptosClient({ ...clientParameters, authAccess: "key" })
111-
// : await getAptosClient({ ...clientParameters, authAccess: "wallet" })
122+
.extend(() => ({
123+
// Choose authAccess based on whether a key-based account was provided.
124+
getAptosClient: async () => {
125+
if (clientParameters.account) {
126+
return await getAptosClient({ ...clientParameters, authAccess: "key" })
127+
} else {
128+
return await getAptosClient({ ...clientParameters, authAccess: "wallet" })
129+
}
130+
}
112131
}))
113132
.extend(client => ({
114133
waitForTransactionReceipt: async ({ hash }: { hash: string }) => {
115-
const { aptos, signer } = await client.getAptosClient()
134+
const { aptos } = await client.getAptosClient()
116135
return await waitForTransactionReceipt({ aptos, hash })
117136
},
118137

@@ -131,9 +150,7 @@ export const createAptosClient = (clientParameters: AptosClientParameters) => {
131150
}: TransferAssetParameters<AptosChainId>): Promise<Result<string, Error>> => {
132151
const { aptos, signer } = await client.getAptosClient()
133152

134-
const baseTokenHex = baseToken.startsWith("0x") ? baseToken.slice(2) : baseToken // Remove "0x" if it exists
135-
// let my_addr = AccountAddress.fromHex(baseToken)
136-
153+
const baseTokenHex = baseToken.startsWith("0x") ? baseToken.slice(2) : baseToken
137154
const quoteTokenVec = MoveVector.U8(quoteToken)
138155
const receiverVec = MoveVector.U8(receiver)
139156

0 commit comments

Comments
 (0)