1
1
import {
2
2
type AptosAccount ,
3
- // (These below helpers remain as before)
4
3
waitForTransactionReceipt ,
5
4
type AptosPublicAccountInfo
6
5
} from "./transfer.ts"
@@ -9,12 +8,15 @@ import { Aptos, Network, AptosConfig, AccountAddress, MoveVector } from "@aptos-
9
8
import { createClient , fallback , type HttpTransport } from "viem"
10
9
import type { AptosBrowserWallet , AuthAccess } from "./wallet.ts"
11
10
11
+ // Define a unified signer type that always includes an accountAddress.
12
+ export type AptosSigner = AptosAccount | ( AptosBrowserWallet & { accountAddress : string } )
13
+
12
14
export type { AptosAccount , AptosBrowserWallet }
13
15
14
16
export const aptosChainId = [
15
- "2" , // aptos testnet
17
+ "2" , // aptos testnet
16
18
"177" , // movement porto
17
- "250" // movement bardock
19
+ "250" // movement bardock
18
20
] as const
19
21
export type AptosChainId = `${( typeof aptosChainId ) [ number ] } `
20
22
@@ -37,17 +39,17 @@ export type AptosClientParameters = {
37
39
*/
38
40
async function getAptosClient (
39
41
parameters : AptosClientParameters & { authAccess : "key" }
40
- ) : Promise < { authAccess : "key" ; aptos : Aptos ; signer : AptosAccount } >
42
+ ) : Promise < { authAccess : "key" ; aptos : Aptos ; signer : AptosSigner } >
41
43
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 } >
45
47
46
48
async function getAptosClient (
47
49
parameters : AptosClientParameters & { authAccess : AuthAccess }
48
50
) : 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 }
51
53
> {
52
54
if ( parameters . authAccess === "key" ) {
53
55
if ( typeof parameters . transport !== "function" ) {
@@ -62,7 +64,7 @@ async function getAptosClient(
62
64
return {
63
65
authAccess : "key" ,
64
66
aptos : new Aptos ( config ) ,
65
- signer : parameters . account as AptosAccount
67
+ signer : parameters . account as AptosAccount // AptosAccount is assumed to have accountAddress
66
68
}
67
69
}
68
70
@@ -71,20 +73,35 @@ async function getAptosClient(
71
73
throw new Error ( "Invalid Aptos transport" )
72
74
}
73
75
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
75
78
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
+
76
92
return {
77
93
authAccess : "wallet" ,
78
94
aptos : new Aptos ( config ) ,
79
- signer : parameters . transport as AptosBrowserWallet
95
+ signer : signer
80
96
}
81
97
}
98
+
99
+
82
100
throw new Error ( "Invalid Aptos transport" )
83
101
}
84
102
85
103
/**
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.
88
105
*/
89
106
export interface TransferAssetParameters < AptosChainId > {
90
107
baseAmount : bigint
@@ -102,17 +119,22 @@ export interface TransferAssetParameters<AptosChainId> {
102
119
*/
103
120
export const createAptosClient = ( clientParameters : AptosClientParameters ) => {
104
121
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
+ }
112
134
} ) )
113
135
. extend ( client => ( {
114
136
waitForTransactionReceipt : async ( { hash } : { hash : string } ) => {
115
- const { aptos, signer } = await client . getAptosClient ( )
137
+ const { aptos } = await client . getAptosClient ( )
116
138
return await waitForTransactionReceipt ( { aptos, hash } )
117
139
} ,
118
140
@@ -130,10 +152,17 @@ export const createAptosClient = (clientParameters: AptosClientParameters) => {
130
152
ucs03address
131
153
} : TransferAssetParameters < AptosChainId > ) : Promise < Result < string , Error > > => {
132
154
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
137
166
const quoteTokenVec = MoveVector . U8 ( quoteToken )
138
167
const receiverVec = MoveVector . U8 ( receiver )
139
168
@@ -159,17 +188,19 @@ export const createAptosClient = (clientParameters: AptosClientParameters) => {
159
188
]
160
189
}
161
190
} )
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
+ // }
173
204
}
174
205
} ) )
175
206
}
0 commit comments