@stableflow/wallet-aptos 3.0.0-beta-02a59f37a0-20260515
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -0
- package/dist/index.d.mts +74 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.js +516 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +507 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# @stableflow/wallet-aptos
|
|
2
|
+
|
|
3
|
+
Aptos wallet adapter for the StableFlow SDK.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @stableflow/wallet-aptos @stableflow/core @stableflow/bridges
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
**Peer dependencies:**
|
|
12
|
+
|
|
13
|
+
- `@aptos-labs/ts-sdk` ^6.3.1
|
|
14
|
+
|
|
15
|
+
## When to use
|
|
16
|
+
|
|
17
|
+
Aptos chain transfers and quotes in `BridgeSFA` flows.
|
|
18
|
+
|
|
19
|
+
## Main exports
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import AptosWallet, { AptosWallet as Named } from '@stableflow/wallet-aptos';
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## See also
|
|
26
|
+
|
|
27
|
+
- [Developer Guide](../../DEVELOPER_GUIDE.md)
|
|
28
|
+
- [@stableflow/bridges](../bridges/README.md)
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Service, SendType } from '@stableflow/core';
|
|
2
|
+
|
|
3
|
+
declare class AptosWallet {
|
|
4
|
+
connection: any;
|
|
5
|
+
private account;
|
|
6
|
+
private aptos;
|
|
7
|
+
private signAndSubmitTransaction;
|
|
8
|
+
private csl;
|
|
9
|
+
constructor(options: {
|
|
10
|
+
account: any | null;
|
|
11
|
+
signAndSubmitTransaction: any;
|
|
12
|
+
});
|
|
13
|
+
transferAPT(to: string, amount: string): Promise<string>;
|
|
14
|
+
transferToken(contractAddress: string, to: string, amount: string): Promise<string>;
|
|
15
|
+
transfer(data: {
|
|
16
|
+
originAsset: string;
|
|
17
|
+
depositAddress: string;
|
|
18
|
+
amount: string;
|
|
19
|
+
}): Promise<string>;
|
|
20
|
+
getAPTBalance(account: string, options?: {
|
|
21
|
+
isCatchError?: boolean;
|
|
22
|
+
}): Promise<string>;
|
|
23
|
+
getTokenBalance(contractAddress: string, account: string, options?: {
|
|
24
|
+
isCatchError?: boolean;
|
|
25
|
+
}): Promise<string>;
|
|
26
|
+
getBalance(token: any, account: string, options?: {
|
|
27
|
+
isCatchError?: boolean;
|
|
28
|
+
}): Promise<string>;
|
|
29
|
+
balanceOf(token: any, account: string, options?: {
|
|
30
|
+
isCatchError?: boolean;
|
|
31
|
+
}): Promise<string>;
|
|
32
|
+
/**
|
|
33
|
+
* Estimate gas limit for transfer transaction
|
|
34
|
+
* @param data Transfer data
|
|
35
|
+
* @returns Gas limit estimate, gas price, and estimated gas cost
|
|
36
|
+
*/
|
|
37
|
+
estimateTransferGas(data: {
|
|
38
|
+
fromToken: any;
|
|
39
|
+
depositAddress: string;
|
|
40
|
+
amount: string;
|
|
41
|
+
}): Promise<{
|
|
42
|
+
gasLimit: bigint;
|
|
43
|
+
gasPrice: bigint;
|
|
44
|
+
estimateGas: bigint;
|
|
45
|
+
}>;
|
|
46
|
+
getEstimateGas(params: any): Promise<{
|
|
47
|
+
gasPrice: any;
|
|
48
|
+
usd: string;
|
|
49
|
+
wei: bigint;
|
|
50
|
+
amount: string;
|
|
51
|
+
}>;
|
|
52
|
+
estimateTransaction(params: any): Promise<{
|
|
53
|
+
estimateSourceGasLimit: bigint;
|
|
54
|
+
estimateSourceGas: bigint;
|
|
55
|
+
estimateSourceGasUsd: string;
|
|
56
|
+
}>;
|
|
57
|
+
checkTransactionStatus(signature: string): Promise<boolean>;
|
|
58
|
+
quoteOneClickProxy(params: any): Promise<any>;
|
|
59
|
+
sendTransaction(params: any): Promise<any>;
|
|
60
|
+
/**
|
|
61
|
+
* Unified quote method that routes to specific quote methods based on type
|
|
62
|
+
* @param type Service type from Service
|
|
63
|
+
* @param params Parameters for the quote
|
|
64
|
+
*/
|
|
65
|
+
quote(type: Service, params: any): Promise<any>;
|
|
66
|
+
/**
|
|
67
|
+
* Unified send method that routes to specific send methods based on type
|
|
68
|
+
* @param type Send type from SendTypeSENDm
|
|
69
|
+
* @param params Parameters for the send transaction
|
|
70
|
+
*/
|
|
71
|
+
send(type: SendType, params: any): Promise<any>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export { AptosWallet, AptosWallet as default };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Service, SendType } from '@stableflow/core';
|
|
2
|
+
|
|
3
|
+
declare class AptosWallet {
|
|
4
|
+
connection: any;
|
|
5
|
+
private account;
|
|
6
|
+
private aptos;
|
|
7
|
+
private signAndSubmitTransaction;
|
|
8
|
+
private csl;
|
|
9
|
+
constructor(options: {
|
|
10
|
+
account: any | null;
|
|
11
|
+
signAndSubmitTransaction: any;
|
|
12
|
+
});
|
|
13
|
+
transferAPT(to: string, amount: string): Promise<string>;
|
|
14
|
+
transferToken(contractAddress: string, to: string, amount: string): Promise<string>;
|
|
15
|
+
transfer(data: {
|
|
16
|
+
originAsset: string;
|
|
17
|
+
depositAddress: string;
|
|
18
|
+
amount: string;
|
|
19
|
+
}): Promise<string>;
|
|
20
|
+
getAPTBalance(account: string, options?: {
|
|
21
|
+
isCatchError?: boolean;
|
|
22
|
+
}): Promise<string>;
|
|
23
|
+
getTokenBalance(contractAddress: string, account: string, options?: {
|
|
24
|
+
isCatchError?: boolean;
|
|
25
|
+
}): Promise<string>;
|
|
26
|
+
getBalance(token: any, account: string, options?: {
|
|
27
|
+
isCatchError?: boolean;
|
|
28
|
+
}): Promise<string>;
|
|
29
|
+
balanceOf(token: any, account: string, options?: {
|
|
30
|
+
isCatchError?: boolean;
|
|
31
|
+
}): Promise<string>;
|
|
32
|
+
/**
|
|
33
|
+
* Estimate gas limit for transfer transaction
|
|
34
|
+
* @param data Transfer data
|
|
35
|
+
* @returns Gas limit estimate, gas price, and estimated gas cost
|
|
36
|
+
*/
|
|
37
|
+
estimateTransferGas(data: {
|
|
38
|
+
fromToken: any;
|
|
39
|
+
depositAddress: string;
|
|
40
|
+
amount: string;
|
|
41
|
+
}): Promise<{
|
|
42
|
+
gasLimit: bigint;
|
|
43
|
+
gasPrice: bigint;
|
|
44
|
+
estimateGas: bigint;
|
|
45
|
+
}>;
|
|
46
|
+
getEstimateGas(params: any): Promise<{
|
|
47
|
+
gasPrice: any;
|
|
48
|
+
usd: string;
|
|
49
|
+
wei: bigint;
|
|
50
|
+
amount: string;
|
|
51
|
+
}>;
|
|
52
|
+
estimateTransaction(params: any): Promise<{
|
|
53
|
+
estimateSourceGasLimit: bigint;
|
|
54
|
+
estimateSourceGas: bigint;
|
|
55
|
+
estimateSourceGasUsd: string;
|
|
56
|
+
}>;
|
|
57
|
+
checkTransactionStatus(signature: string): Promise<boolean>;
|
|
58
|
+
quoteOneClickProxy(params: any): Promise<any>;
|
|
59
|
+
sendTransaction(params: any): Promise<any>;
|
|
60
|
+
/**
|
|
61
|
+
* Unified quote method that routes to specific quote methods based on type
|
|
62
|
+
* @param type Service type from Service
|
|
63
|
+
* @param params Parameters for the quote
|
|
64
|
+
*/
|
|
65
|
+
quote(type: Service, params: any): Promise<any>;
|
|
66
|
+
/**
|
|
67
|
+
* Unified send method that routes to specific send methods based on type
|
|
68
|
+
* @param type Send type from SendTypeSENDm
|
|
69
|
+
* @param params Parameters for the send transaction
|
|
70
|
+
*/
|
|
71
|
+
send(type: SendType, params: any): Promise<any>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export { AptosWallet, AptosWallet as default };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var tsSdk = require('@aptos-labs/ts-sdk');
|
|
6
|
+
var Big = require('big.js');
|
|
7
|
+
var core = require('@stableflow/core');
|
|
8
|
+
|
|
9
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
|
+
|
|
11
|
+
var Big__default = /*#__PURE__*/_interopDefault(Big);
|
|
12
|
+
|
|
13
|
+
// src/aptos.ts
|
|
14
|
+
var DEFAULT_GAS_LIMIT = 5000n;
|
|
15
|
+
var AptosWallet = class {
|
|
16
|
+
constructor(options) {
|
|
17
|
+
const config = new tsSdk.AptosConfig({
|
|
18
|
+
network: tsSdk.Network.MAINNET
|
|
19
|
+
});
|
|
20
|
+
const aptos = new tsSdk.Aptos(config);
|
|
21
|
+
this.aptos = aptos;
|
|
22
|
+
this.signAndSubmitTransaction = options.signAndSubmitTransaction;
|
|
23
|
+
this.account = options.account;
|
|
24
|
+
const cs = new core.Csl(core.OpenAPI.DEBUG);
|
|
25
|
+
this.csl = cs.log;
|
|
26
|
+
}
|
|
27
|
+
// Transfer APT
|
|
28
|
+
async transferAPT(to, amount) {
|
|
29
|
+
if (!this.account) {
|
|
30
|
+
throw new Error("Wallet not connected");
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
const amountInOctas = Math.floor(parseFloat(amount) * 1e8);
|
|
34
|
+
const result = await this.signAndSubmitTransaction({
|
|
35
|
+
data: {
|
|
36
|
+
function: "0x1::coin::transfer",
|
|
37
|
+
typeArguments: ["0x1::aptos_coin::AptosCoin"],
|
|
38
|
+
functionArguments: [to, amountInOctas.toString()]
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
const executedTransaction = await this.aptos.waitForTransaction({ transactionHash: typeof result === "string" ? result : result.hash });
|
|
42
|
+
if (executedTransaction.success !== true) {
|
|
43
|
+
this.csl("Aptos transferAPT", "red-500", "Transfer APT token failed: %o", executedTransaction);
|
|
44
|
+
throw new Error("Transfer token failed");
|
|
45
|
+
}
|
|
46
|
+
return typeof result === "string" ? result : result.hash;
|
|
47
|
+
} catch (error) {
|
|
48
|
+
this.csl("Aptos transferAPT", "red-500", "Transfer APT failed: %o", error);
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
// Transfer Fungible Asset token
|
|
53
|
+
async transferToken(contractAddress, to, amount) {
|
|
54
|
+
if (!this.account) {
|
|
55
|
+
throw new Error("Wallet not connected");
|
|
56
|
+
}
|
|
57
|
+
try {
|
|
58
|
+
const result = await this.signAndSubmitTransaction({
|
|
59
|
+
data: {
|
|
60
|
+
function: "0x1::primary_fungible_store::transfer",
|
|
61
|
+
typeArguments: ["0x1::fungible_asset::Metadata"],
|
|
62
|
+
functionArguments: [contractAddress, to, amount],
|
|
63
|
+
abi: faTransferAbi
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
const executedTransaction = await this.aptos.waitForTransaction({ transactionHash: typeof result === "string" ? result : result.hash });
|
|
67
|
+
if (executedTransaction.success !== true) {
|
|
68
|
+
this.csl("Aptos transferToken", "red-500", "Transfer token failed: %o", executedTransaction);
|
|
69
|
+
throw new Error("Transfer token failed");
|
|
70
|
+
}
|
|
71
|
+
return typeof result === "string" ? result : result.hash;
|
|
72
|
+
} catch (error) {
|
|
73
|
+
this.csl("Aptos transferToken", "red-500", "Transfer token failed: %o", error);
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
// Generic transfer method
|
|
78
|
+
async transfer(data) {
|
|
79
|
+
const { originAsset, depositAddress, amount } = data;
|
|
80
|
+
if (originAsset === "APT" || originAsset === "apt") {
|
|
81
|
+
return await this.transferAPT(depositAddress, amount);
|
|
82
|
+
}
|
|
83
|
+
const result = await this.transferToken(
|
|
84
|
+
originAsset,
|
|
85
|
+
depositAddress,
|
|
86
|
+
amount
|
|
87
|
+
);
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
async getAPTBalance(account, options) {
|
|
91
|
+
const { isCatchError = false } = options || {};
|
|
92
|
+
try {
|
|
93
|
+
const accountAPTAmount = await this.aptos.getAccountAPTAmount({
|
|
94
|
+
accountAddress: account
|
|
95
|
+
});
|
|
96
|
+
return accountAPTAmount.toString();
|
|
97
|
+
} catch (error) {
|
|
98
|
+
this.csl("Aptos getAPTBalance", "red-500", "Get APT balance failed: %o", error);
|
|
99
|
+
if (isCatchError) {
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
return "0";
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
async getTokenBalance(contractAddress, account, options) {
|
|
106
|
+
const { isCatchError = false } = options || {};
|
|
107
|
+
try {
|
|
108
|
+
const balance = await this.aptos.getBalance({
|
|
109
|
+
accountAddress: account,
|
|
110
|
+
asset: contractAddress
|
|
111
|
+
});
|
|
112
|
+
return balance.toString();
|
|
113
|
+
} catch (error) {
|
|
114
|
+
this.csl("Aptos getTokenBalance", "red-500", "Get token balance failed: %o", error);
|
|
115
|
+
if (isCatchError) {
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
return "0";
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
async getBalance(token, account, options) {
|
|
122
|
+
if (token.symbol === "APT" || token.symbol === "apt" || token.symbol === "native") {
|
|
123
|
+
return await this.getAPTBalance(account, options);
|
|
124
|
+
}
|
|
125
|
+
return await this.getTokenBalance(token.contractAddress, account, options);
|
|
126
|
+
}
|
|
127
|
+
async balanceOf(token, account, options) {
|
|
128
|
+
return await this.getBalance(token, account, options);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Estimate gas limit for transfer transaction
|
|
132
|
+
* @param data Transfer data
|
|
133
|
+
* @returns Gas limit estimate, gas price, and estimated gas cost
|
|
134
|
+
*/
|
|
135
|
+
async estimateTransferGas(data) {
|
|
136
|
+
if (!this.account) {
|
|
137
|
+
throw new Error("Wallet not connected");
|
|
138
|
+
}
|
|
139
|
+
const { fromToken, depositAddress, amount } = data;
|
|
140
|
+
const originAsset = fromToken.contractAddress;
|
|
141
|
+
const isOriginNative = originAsset === "APT" || originAsset === "apt";
|
|
142
|
+
const sender = this.account?.address?.toString();
|
|
143
|
+
if (!sender) {
|
|
144
|
+
throw new Error("Invalid sender address");
|
|
145
|
+
}
|
|
146
|
+
let signerPublicKey;
|
|
147
|
+
if (this.account.publicKey) {
|
|
148
|
+
signerPublicKey = this.account.publicKey;
|
|
149
|
+
} else if (this.account.address) {
|
|
150
|
+
signerPublicKey = this.account.address;
|
|
151
|
+
} else {
|
|
152
|
+
throw new Error("Unable to get signer public key");
|
|
153
|
+
}
|
|
154
|
+
let simulationAmount = amount;
|
|
155
|
+
let useMinimalAmount = false;
|
|
156
|
+
try {
|
|
157
|
+
if (isOriginNative) {
|
|
158
|
+
try {
|
|
159
|
+
const balance = await this.aptos.getAccountAPTAmount({
|
|
160
|
+
accountAddress: sender
|
|
161
|
+
});
|
|
162
|
+
const amountBigInt = BigInt(amount);
|
|
163
|
+
if (amountBigInt > balance) {
|
|
164
|
+
simulationAmount = "1";
|
|
165
|
+
useMinimalAmount = true;
|
|
166
|
+
}
|
|
167
|
+
} catch (error) {
|
|
168
|
+
simulationAmount = "1";
|
|
169
|
+
useMinimalAmount = true;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
} catch (error) {
|
|
173
|
+
}
|
|
174
|
+
let rawTxn;
|
|
175
|
+
if (isOriginNative) {
|
|
176
|
+
rawTxn = await this.aptos.transaction.build.simple({
|
|
177
|
+
sender,
|
|
178
|
+
data: {
|
|
179
|
+
function: "0x1::coin::transfer",
|
|
180
|
+
typeArguments: ["0x1::aptos_coin::AptosCoin"],
|
|
181
|
+
functionArguments: [depositAddress, simulationAmount]
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
} else {
|
|
185
|
+
rawTxn = await this.aptos.transaction.build.simple({
|
|
186
|
+
sender,
|
|
187
|
+
data: {
|
|
188
|
+
function: "0x1::primary_fungible_store::transfer",
|
|
189
|
+
typeArguments: ["0x1::fungible_asset::Metadata"],
|
|
190
|
+
functionArguments: [originAsset, depositAddress, simulationAmount]
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
let simulation;
|
|
195
|
+
try {
|
|
196
|
+
const simulationResult = await this.aptos.transaction.simulate.simple({
|
|
197
|
+
signerPublicKey,
|
|
198
|
+
transaction: rawTxn,
|
|
199
|
+
options: {
|
|
200
|
+
estimateGasUnitPrice: true,
|
|
201
|
+
estimateMaxGasAmount: true,
|
|
202
|
+
estimatePrioritizedGasUnitPrice: true
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
simulation = simulationResult[0];
|
|
206
|
+
} catch (error) {
|
|
207
|
+
if (!useMinimalAmount && (error.message?.includes("INSUFFICIENT_BALANCE") || error.message?.includes("EINSUFFICIENT_BALANCE"))) {
|
|
208
|
+
simulationAmount = isOriginNative ? "1" : "1";
|
|
209
|
+
if (isOriginNative) {
|
|
210
|
+
rawTxn = await this.aptos.transaction.build.simple({
|
|
211
|
+
sender,
|
|
212
|
+
data: {
|
|
213
|
+
function: "0x1::coin::transfer",
|
|
214
|
+
typeArguments: ["0x1::aptos_coin::AptosCoin"],
|
|
215
|
+
functionArguments: [depositAddress, simulationAmount]
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
} else {
|
|
219
|
+
rawTxn = await this.aptos.transaction.build.simple({
|
|
220
|
+
sender,
|
|
221
|
+
data: {
|
|
222
|
+
function: "0x1::primary_fungible_store::transfer",
|
|
223
|
+
typeArguments: ["0x1::fungible_asset::Metadata"],
|
|
224
|
+
functionArguments: [originAsset, depositAddress, simulationAmount]
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
const simulationResult = await this.aptos.transaction.simulate.simple({
|
|
229
|
+
signerPublicKey,
|
|
230
|
+
transaction: rawTxn,
|
|
231
|
+
options: {
|
|
232
|
+
estimateGasUnitPrice: true,
|
|
233
|
+
estimateMaxGasAmount: true,
|
|
234
|
+
estimatePrioritizedGasUnitPrice: true
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
simulation = simulationResult[0];
|
|
238
|
+
} else {
|
|
239
|
+
throw error;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (!simulation.success) {
|
|
243
|
+
const defaultGasLimit = isOriginNative ? 2400n : 6000n;
|
|
244
|
+
const defaultGasPrice = 100n;
|
|
245
|
+
const defaultEstimateGas = defaultGasLimit * defaultGasPrice;
|
|
246
|
+
this.csl("Aptos getTokenBalance", "red-500", `Simulation failed: ${simulation.vm_status}, using default gas estimates`);
|
|
247
|
+
return {
|
|
248
|
+
gasLimit: defaultGasLimit,
|
|
249
|
+
gasPrice: defaultGasPrice,
|
|
250
|
+
estimateGas: defaultEstimateGas
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
const gasUsed = BigInt(simulation.gas_used || 0);
|
|
254
|
+
const gasLimit = gasUsed * 150n / 100n;
|
|
255
|
+
const gasPrice = BigInt(simulation.gas_unit_price || 100);
|
|
256
|
+
const estimateGas = gasLimit * gasPrice;
|
|
257
|
+
return {
|
|
258
|
+
gasLimit,
|
|
259
|
+
gasPrice,
|
|
260
|
+
estimateGas
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
async getEstimateGas(params) {
|
|
264
|
+
const { gasLimit, price, nativeToken, gasPrice } = params;
|
|
265
|
+
let finalGasPrice = gasPrice;
|
|
266
|
+
if (!finalGasPrice) {
|
|
267
|
+
const feeData = await this.aptos.getGasPriceEstimation();
|
|
268
|
+
finalGasPrice = feeData.gas_estimate || feeData.prioritized_gas_estimate || BigInt("100");
|
|
269
|
+
}
|
|
270
|
+
const estimateGas = BigInt(gasLimit) * BigInt(finalGasPrice);
|
|
271
|
+
const estimateGasAmount = Big__default.default(estimateGas.toString()).div(10 ** nativeToken.decimals);
|
|
272
|
+
const estimateGasUsd = Big__default.default(estimateGasAmount).times(price || 1);
|
|
273
|
+
return {
|
|
274
|
+
gasPrice: finalGasPrice,
|
|
275
|
+
usd: core.numberRemoveEndZero(Big__default.default(estimateGasUsd).toFixed(20)),
|
|
276
|
+
wei: estimateGas,
|
|
277
|
+
amount: core.numberRemoveEndZero(Big__default.default(estimateGasAmount).toFixed(nativeToken.decimals))
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
async estimateTransaction(params) {
|
|
281
|
+
const {
|
|
282
|
+
dry,
|
|
283
|
+
function: functionId,
|
|
284
|
+
typeArguments,
|
|
285
|
+
functionArguments,
|
|
286
|
+
fromToken,
|
|
287
|
+
prices,
|
|
288
|
+
defaultGasLimit = DEFAULT_GAS_LIMIT
|
|
289
|
+
} = params;
|
|
290
|
+
const nativeTokenPrice = core.getPrice(prices, fromToken.nativeToken.symbol);
|
|
291
|
+
const result = {
|
|
292
|
+
estimateSourceGasLimit: dry ? 4000000n : DEFAULT_GAS_LIMIT,
|
|
293
|
+
estimateSourceGas: 0n,
|
|
294
|
+
estimateSourceGasUsd: "0"
|
|
295
|
+
};
|
|
296
|
+
const setDefaultGasLimit = async () => {
|
|
297
|
+
const { usd, wei } = await this.getEstimateGas({
|
|
298
|
+
gasLimit: DEFAULT_GAS_LIMIT,
|
|
299
|
+
price: nativeTokenPrice,
|
|
300
|
+
nativeToken: fromToken.nativeToken,
|
|
301
|
+
gasPrice: dry ? "100" : void 0
|
|
302
|
+
});
|
|
303
|
+
result.estimateSourceGas = wei;
|
|
304
|
+
result.estimateSourceGasUsd = usd;
|
|
305
|
+
};
|
|
306
|
+
let finalGasLimit = defaultGasLimit;
|
|
307
|
+
if (dry) {
|
|
308
|
+
await setDefaultGasLimit();
|
|
309
|
+
return result;
|
|
310
|
+
}
|
|
311
|
+
const zeroPublicKey = new tsSdk.Ed25519PublicKey(new Uint8Array(32));
|
|
312
|
+
let signerPublicKey;
|
|
313
|
+
if (this.account.publicKey) {
|
|
314
|
+
signerPublicKey = this.account.publicKey;
|
|
315
|
+
} else if (this.account.address) {
|
|
316
|
+
signerPublicKey = this.account.address;
|
|
317
|
+
} else {
|
|
318
|
+
signerPublicKey = zeroPublicKey;
|
|
319
|
+
}
|
|
320
|
+
let sender = this.account?.address?.toString();
|
|
321
|
+
if (!sender) {
|
|
322
|
+
const authKey = tsSdk.AuthenticationKey.fromPublicKey({ publicKey: zeroPublicKey });
|
|
323
|
+
sender = authKey.derivedAddress();
|
|
324
|
+
}
|
|
325
|
+
let simulation;
|
|
326
|
+
try {
|
|
327
|
+
const rawTxn = await this.aptos.transaction.build.simple({
|
|
328
|
+
sender,
|
|
329
|
+
data: {
|
|
330
|
+
function: functionId,
|
|
331
|
+
typeArguments,
|
|
332
|
+
functionArguments
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
const simulationResult = await this.aptos.transaction.simulate.simple({
|
|
336
|
+
signerPublicKey,
|
|
337
|
+
transaction: rawTxn,
|
|
338
|
+
options: {
|
|
339
|
+
estimateGasUnitPrice: true,
|
|
340
|
+
estimateMaxGasAmount: true,
|
|
341
|
+
estimatePrioritizedGasUnitPrice: true
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
simulation = simulationResult[0];
|
|
345
|
+
if (!simulation.success) {
|
|
346
|
+
throw new Error(`Simulation failed: ${simulation}`);
|
|
347
|
+
}
|
|
348
|
+
const gasUsed = BigInt(simulation.gas_used || 0);
|
|
349
|
+
finalGasLimit = gasUsed * 150n / 100n;
|
|
350
|
+
result.estimateSourceGasLimit = finalGasLimit;
|
|
351
|
+
const gasPrice = BigInt(simulation.gas_unit_price || 100);
|
|
352
|
+
const { usd, wei } = await this.getEstimateGas({
|
|
353
|
+
gasLimit: finalGasLimit,
|
|
354
|
+
price: nativeTokenPrice,
|
|
355
|
+
nativeToken: fromToken.nativeToken,
|
|
356
|
+
gasPrice
|
|
357
|
+
});
|
|
358
|
+
result.estimateSourceGas = wei;
|
|
359
|
+
result.estimateSourceGasUsd = usd;
|
|
360
|
+
} catch (error) {
|
|
361
|
+
this.csl("Aptos estimateTransaction", "red-500", "simulation failed: %o", error);
|
|
362
|
+
await setDefaultGasLimit();
|
|
363
|
+
}
|
|
364
|
+
return result;
|
|
365
|
+
}
|
|
366
|
+
async checkTransactionStatus(signature) {
|
|
367
|
+
try {
|
|
368
|
+
const transaction = await this.aptos.getTransactionByHash({
|
|
369
|
+
transactionHash: signature
|
|
370
|
+
});
|
|
371
|
+
if (!transaction) {
|
|
372
|
+
return false;
|
|
373
|
+
}
|
|
374
|
+
return transaction.success === true || transaction.status === "success";
|
|
375
|
+
} catch (error) {
|
|
376
|
+
this.csl("Aptos checkTransactionStatus", "red-500", "Check transaction status failed: %o", error);
|
|
377
|
+
return false;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
async quoteOneClickProxy(params) {
|
|
381
|
+
const {
|
|
382
|
+
dry,
|
|
383
|
+
proxyAddress,
|
|
384
|
+
fromToken,
|
|
385
|
+
depositAddress,
|
|
386
|
+
amountWei,
|
|
387
|
+
prices
|
|
388
|
+
} = params;
|
|
389
|
+
const execTime = new core.ExecTime({ type: "OneClick Aptos", logStyle: "sky-200", isDebug: core.OpenAPI.DEBUG });
|
|
390
|
+
const result = { fees: {} };
|
|
391
|
+
if (!this.account) {
|
|
392
|
+
throw new Error("Wallet not connected");
|
|
393
|
+
}
|
|
394
|
+
if (!depositAddress) {
|
|
395
|
+
throw new Error("depositAddress is required");
|
|
396
|
+
}
|
|
397
|
+
try {
|
|
398
|
+
const sender = this.account?.address?.toString();
|
|
399
|
+
if (!sender) {
|
|
400
|
+
throw new Error("Invalid sender address");
|
|
401
|
+
}
|
|
402
|
+
let signerPublicKey;
|
|
403
|
+
if (this.account.publicKey) {
|
|
404
|
+
signerPublicKey = this.account.publicKey;
|
|
405
|
+
} else if (this.account.address) {
|
|
406
|
+
signerPublicKey = this.account.address;
|
|
407
|
+
} else {
|
|
408
|
+
throw new Error("Unable to get signer public key");
|
|
409
|
+
}
|
|
410
|
+
const typeArgument = `0x1::fungible_asset::Metadata`;
|
|
411
|
+
const functionId = `${proxyAddress}::stableflow_proxy::proxy_transfer_fa`;
|
|
412
|
+
const functionArguments = [fromToken.contractAddress, depositAddress, amountWei];
|
|
413
|
+
execTime.breakpoint();
|
|
414
|
+
const ett = await this.estimateTransaction({
|
|
415
|
+
dry,
|
|
416
|
+
function: functionId,
|
|
417
|
+
typeArguments: [typeArgument],
|
|
418
|
+
functionArguments,
|
|
419
|
+
fromToken,
|
|
420
|
+
prices
|
|
421
|
+
});
|
|
422
|
+
execTime.log("estimateTransaction");
|
|
423
|
+
result.fees.estimateGasUsd = ett.estimateSourceGasUsd;
|
|
424
|
+
result.estimateSourceGas = ett.estimateSourceGas;
|
|
425
|
+
result.totalEstimateSourceGas = ett.estimateSourceGas;
|
|
426
|
+
result.estimateSourceGasUsd = ett.estimateSourceGasUsd;
|
|
427
|
+
result.sendParam = {
|
|
428
|
+
function: functionId,
|
|
429
|
+
typeArguments: [typeArgument],
|
|
430
|
+
functionArguments
|
|
431
|
+
};
|
|
432
|
+
} catch (error) {
|
|
433
|
+
this.csl("Aptos quoteOneClickProxy", "red-500", "oneclick quote proxy failed: %o", error);
|
|
434
|
+
const defaultGasLimit = 5000n;
|
|
435
|
+
const defaultGasPrice = 100n;
|
|
436
|
+
const defaultEstimateGas = defaultGasLimit * defaultGasPrice;
|
|
437
|
+
const estimateGasUsd = Big__default.default(defaultEstimateGas.toString()).div(10 ** fromToken.nativeToken.decimals).times(core.getPrice(prices, fromToken.nativeToken.symbol));
|
|
438
|
+
result.fees.estimateGasUsd = core.numberRemoveEndZero(Big__default.default(estimateGasUsd).toFixed(20));
|
|
439
|
+
result.estimateSourceGas = defaultEstimateGas;
|
|
440
|
+
result.totalEstimateSourceGas = defaultEstimateGas;
|
|
441
|
+
result.estimateSourceGasUsd = core.numberRemoveEndZero(Big__default.default(estimateGasUsd).toFixed(20));
|
|
442
|
+
}
|
|
443
|
+
execTime.logTotal("quoteOneClickProxy");
|
|
444
|
+
return result;
|
|
445
|
+
}
|
|
446
|
+
async sendTransaction(params) {
|
|
447
|
+
if (!this.account) {
|
|
448
|
+
throw new Error("Wallet not connected");
|
|
449
|
+
}
|
|
450
|
+
const { function: functionId, typeArguments, functionArguments } = params;
|
|
451
|
+
if (!functionId || !typeArguments || !functionArguments) {
|
|
452
|
+
throw new Error("Invalid sendParam: function, typeArguments, and functionArguments are required");
|
|
453
|
+
}
|
|
454
|
+
try {
|
|
455
|
+
const sender = this.account?.address?.toString();
|
|
456
|
+
if (!sender) {
|
|
457
|
+
throw new Error("Invalid sender address");
|
|
458
|
+
}
|
|
459
|
+
const result = await this.signAndSubmitTransaction({
|
|
460
|
+
data: {
|
|
461
|
+
function: functionId,
|
|
462
|
+
typeArguments,
|
|
463
|
+
functionArguments
|
|
464
|
+
}
|
|
465
|
+
});
|
|
466
|
+
const executedTransaction = await this.aptos.waitForTransaction({
|
|
467
|
+
transactionHash: typeof result === "string" ? result : result.hash
|
|
468
|
+
});
|
|
469
|
+
if (executedTransaction.success !== true) {
|
|
470
|
+
this.csl("Aptos sendTransaction", "red-500", "Proxy transfer failed: %o", executedTransaction);
|
|
471
|
+
throw new Error("Proxy transfer failed");
|
|
472
|
+
}
|
|
473
|
+
return typeof result === "string" ? result : result.hash;
|
|
474
|
+
} catch (error) {
|
|
475
|
+
this.csl("Aptos sendTransaction", "red-500", "Send transaction failed: %o", error);
|
|
476
|
+
throw error;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* Unified quote method that routes to specific quote methods based on type
|
|
481
|
+
* @param type Service type from Service
|
|
482
|
+
* @param params Parameters for the quote
|
|
483
|
+
*/
|
|
484
|
+
async quote(type, params) {
|
|
485
|
+
switch (type) {
|
|
486
|
+
case core.Service.OneClick:
|
|
487
|
+
return await this.quoteOneClickProxy(params);
|
|
488
|
+
default:
|
|
489
|
+
throw new Error(`Unsupported quote type: ${type}`);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* Unified send method that routes to specific send methods based on type
|
|
494
|
+
* @param type Send type from SendTypeSENDm
|
|
495
|
+
* @param params Parameters for the send transaction
|
|
496
|
+
*/
|
|
497
|
+
async send(type, params) {
|
|
498
|
+
switch (type) {
|
|
499
|
+
case core.SendType.SEND:
|
|
500
|
+
return await this.sendTransaction(params);
|
|
501
|
+
case core.SendType.TRANSFER:
|
|
502
|
+
return await this.transfer(params);
|
|
503
|
+
default:
|
|
504
|
+
throw new Error(`Unsupported send type: ${type}`);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
var faTransferAbi = {
|
|
509
|
+
typeParameters: [{ constraints: [] }],
|
|
510
|
+
parameters: [tsSdk.parseTypeTag("0x1::object::Object"), new tsSdk.TypeTagAddress(), new tsSdk.TypeTagU64()]
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
exports.AptosWallet = AptosWallet;
|
|
514
|
+
exports.default = AptosWallet;
|
|
515
|
+
//# sourceMappingURL=index.js.map
|
|
516
|
+
//# sourceMappingURL=index.js.map
|