Skip to content

Commit b115372

Browse files
committed
chore(app): creating aptos client according to wallet wip
Signed-off-by: Kaan Caglan <[email protected]>
1 parent c4ed99f commit b115372

File tree

1 file changed

+68
-37
lines changed

1 file changed

+68
-37
lines changed

typescript-sdk/src/aptos/client.ts

+68-37
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
81+
const account = await parameters.transport.getAccount?.() ||
82+
{ address: "" }
83+
if (!account.address) {
84+
throw new Error("No account address found from the wallet")
85+
}
86+
87+
// Create a signer by merging the wallet’s methods with the account address.
88+
const signer = Object.assign({}, parameters.transport, {
89+
accountAddress: account.address
90+
}) as unknown as AptosAccount // <== Force-cast to AptosAccount
91+
7692
return {
7793
authAccess: "wallet",
7894
aptos: new Aptos(config),
79-
signer: parameters.transport as AptosBrowserWallet
95+
signer: signer
8096
}
8197
}
98+
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,22 @@ 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+
console.info("create aptos client params:", clientParameters)
126+
if (clientParameters.account) {
127+
console.info("returning key one")
128+
return await getAptosClient({ ...clientParameters, authAccess: "wallet" })
129+
} else {
130+
console.info("returning auth one")
131+
return await getAptosClient({ ...clientParameters, authAccess: "wallet" })
132+
}
133+
}
112134
}))
113135
.extend(client => ({
114136
waitForTransactionReceipt: async ({ hash }: { hash: string }) => {
115-
const { aptos, signer } = await client.getAptosClient()
137+
const { aptos } = await client.getAptosClient()
116138
return await waitForTransactionReceipt({ aptos, hash })
117139
},
118140

@@ -130,10 +152,17 @@ export const createAptosClient = (clientParameters: AptosClientParameters) => {
130152
ucs03address
131153
}: TransferAssetParameters<AptosChainId>): Promise<Result<string, Error>> => {
132154
const { aptos, signer } = await client.getAptosClient()
133-
134-
const baseTokenHex = baseToken.startsWith("0x") ? baseToken.slice(2) : baseToken // Remove "0x" if it exists
135-
// let my_addr = AccountAddress.fromHex(baseToken)
136-
155+
console.info("aptos", aptos)
156+
console.info("signer", signer)
157+
console.info("baseAmount", baseAmount)
158+
console.info("baseToken", baseToken)
159+
console.info("quoteAmount", quoteAmount)
160+
console.info("quoteToken", quoteToken)
161+
console.info("receiver", receiver)
162+
console.info("sourceChannelId", sourceChannelId)
163+
console.info("ucs03address", ucs03address)
164+
165+
const baseTokenHex = baseToken.startsWith("0x") ? baseToken.slice(2) : baseToken
137166
const quoteTokenVec = MoveVector.U8(quoteToken)
138167
const receiverVec = MoveVector.U8(receiver)
139168

@@ -159,17 +188,19 @@ export const createAptosClient = (clientParameters: AptosClientParameters) => {
159188
]
160189
}
161190
})
162-
163-
try {
164-
const txn = await aptos.signAndSubmitTransaction({
165-
signer: signer,
166-
transaction: payload
167-
})
168-
const receipt = await waitForTransactionReceipt({ aptos, hash: txn.hash })
169-
return receipt
170-
} catch (error) {
171-
return err(new Error("failed to execute aptos call", { cause: error as Error }))
172-
}
191+
console.info("payload", payload)
192+
return err(new Error("not implemented"))
193+
194+
// try {
195+
// const txn = await aptos.signAndSubmitTransaction({
196+
// signer: signer,
197+
// transaction: payload
198+
// })
199+
// const receipt = await waitForTransactionReceipt({ aptos, hash: txn.hash })
200+
// return receipt
201+
// } catch (error) {
202+
// return err(new Error("failed to execute aptos call", { cause: error as Error }))
203+
// }
173204
}
174205
}))
175206
}

0 commit comments

Comments
 (0)