@dynamic-labs/sui 4.15.0 → 4.17.0
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/CHANGELOG.md +24 -0
- package/package.cjs +1 -1
- package/package.js +1 -1
- package/package.json +8 -8
- package/src/SuiWalletConnector.cjs +27 -0
- package/src/SuiWalletConnector.d.ts +3 -1
- package/src/SuiWalletConnector.js +27 -0
- package/src/index.cjs +2 -0
- package/src/index.d.ts +1 -0
- package/src/index.js +1 -0
- package/src/utils/SuiUiTransaction/SuiUiTransaction.cjs +125 -0
- package/src/utils/SuiUiTransaction/SuiUiTransaction.d.ts +47 -0
- package/src/utils/SuiUiTransaction/SuiUiTransaction.js +121 -0
- package/src/utils/SuiUiTransaction/index.d.ts +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,28 @@
|
|
|
1
1
|
|
|
2
|
+
## [4.17.0](https://github.com/dynamic-labs/dynamic-auth/compare/v4.16.0...v4.17.0) (2025-05-06)
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
### Features
|
|
6
|
+
|
|
7
|
+
* add chain id option to create kernel client in react-native ([#8650](https://github.com/dynamic-labs/dynamic-auth/issues/8650)) ([1450bea](https://github.com/dynamic-labs/dynamic-auth/commit/1450bea6f979a82ed4880700aff14fa1ead9dc60))
|
|
8
|
+
* add sendCalls method to EthereumWallet ([#8634](https://github.com/dynamic-labs/dynamic-auth/issues/8634)) ([7c9aef9](https://github.com/dynamic-labs/dynamic-auth/commit/7c9aef95382b4823a40279bebfb084de32c25610))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* 7702 do not override existing old aa ([#8630](https://github.com/dynamic-labs/dynamic-auth/issues/8630)) ([a731d9e](https://github.com/dynamic-labs/dynamic-auth/commit/a731d9e77ecd9e1dec3a7704a63989b5f2741a57))
|
|
14
|
+
* improve error message when email login fails ([#8649](https://github.com/dynamic-labs/dynamic-auth/issues/8649)) ([e1c03ac](https://github.com/dynamic-labs/dynamic-auth/commit/e1c03ac6dc83386c63d63db59d31355bc4fe2959))
|
|
15
|
+
* improve react-native stability on android ([#8652](https://github.com/dynamic-labs/dynamic-auth/issues/8652)) ([1617546](https://github.com/dynamic-labs/dynamic-auth/commit/1617546e6053c2ab32f7d385336c47df185d077f))
|
|
16
|
+
|
|
17
|
+
## [4.16.0](https://github.com/dynamic-labs/dynamic-auth/compare/v4.15.0...v4.16.0) (2025-05-03)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
* (GVTY-3020) add send balance to waas sui connector ([#8638](https://github.com/dynamic-labs/dynamic-auth/issues/8638)) ([97f0af0](https://github.com/dynamic-labs/dynamic-auth/commit/97f0af0504a0079ebf0958fde1f8edc34c035927))
|
|
23
|
+
* add isAtomicSupported and isPaymasterServiceSupported methods to EthereumWallet ([#8627](https://github.com/dynamic-labs/dynamic-auth/issues/8627)) ([df82b9e](https://github.com/dynamic-labs/dynamic-auth/commit/df82b9e86eafea9dd6c505227722450bc56d7c57))
|
|
24
|
+
* add solana mpc to global wallets ([#8605](https://github.com/dynamic-labs/dynamic-auth/issues/8605)) ([6261bf9](https://github.com/dynamic-labs/dynamic-auth/commit/6261bf9bec45b1d1afa13869cf5d10cda215783e))
|
|
25
|
+
|
|
2
26
|
## [4.15.0](https://github.com/dynamic-labs/dynamic-auth/compare/v4.14.0...v4.15.0) (2025-04-30)
|
|
3
27
|
|
|
4
28
|
|
package/package.cjs
CHANGED
package/package.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dynamic-labs/sui",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.17.0",
|
|
4
4
|
"description": "A React SDK for implementing wallet web3 authentication and authorization to your website.",
|
|
5
5
|
"author": "Dynamic Labs, Inc.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,13 +22,13 @@
|
|
|
22
22
|
"@mysten/wallet-standard": "0.13.29",
|
|
23
23
|
"@mysten/sui": "1.24.0",
|
|
24
24
|
"text-encoding": "0.7.0",
|
|
25
|
-
"@dynamic-labs/assert-package-version": "4.
|
|
26
|
-
"@dynamic-labs/logger": "4.
|
|
27
|
-
"@dynamic-labs/rpc-providers": "4.
|
|
28
|
-
"@dynamic-labs/types": "4.
|
|
29
|
-
"@dynamic-labs/utils": "4.
|
|
30
|
-
"@dynamic-labs/wallet-book": "4.
|
|
31
|
-
"@dynamic-labs/wallet-connector-core": "4.
|
|
25
|
+
"@dynamic-labs/assert-package-version": "4.17.0",
|
|
26
|
+
"@dynamic-labs/logger": "4.17.0",
|
|
27
|
+
"@dynamic-labs/rpc-providers": "4.17.0",
|
|
28
|
+
"@dynamic-labs/types": "4.17.0",
|
|
29
|
+
"@dynamic-labs/utils": "4.17.0",
|
|
30
|
+
"@dynamic-labs/wallet-book": "4.17.0",
|
|
31
|
+
"@dynamic-labs/wallet-connector-core": "4.17.0"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {}
|
|
34
34
|
}
|
|
@@ -10,6 +10,7 @@ var utils = require('@dynamic-labs/utils');
|
|
|
10
10
|
var logger = require('@dynamic-labs/logger');
|
|
11
11
|
var SuiWallet = require('./wallet/SuiWallet.cjs');
|
|
12
12
|
var networkHelpers = require('./utils/network/networkHelpers.cjs');
|
|
13
|
+
var SuiUiTransaction = require('./utils/SuiUiTransaction/SuiUiTransaction.cjs');
|
|
13
14
|
|
|
14
15
|
class SuiWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
15
16
|
constructor(name, opts) {
|
|
@@ -239,6 +240,32 @@ class SuiWalletConnector extends walletConnectorCore.WalletConnectorBase {
|
|
|
239
240
|
return this.account;
|
|
240
241
|
});
|
|
241
242
|
}
|
|
243
|
+
/** Function used to create transactions in the SDK interface */
|
|
244
|
+
createUiTransaction(from) {
|
|
245
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
246
|
+
yield this.validateActiveWallet(from);
|
|
247
|
+
const suiClient = yield this.getSuiClient();
|
|
248
|
+
if (!suiClient) {
|
|
249
|
+
throw new utils.DynamicError('No Sui client available');
|
|
250
|
+
}
|
|
251
|
+
return new SuiUiTransaction.SuiUiTransaction({
|
|
252
|
+
client: suiClient,
|
|
253
|
+
from,
|
|
254
|
+
onSubmit: (transaction) => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
255
|
+
var _a, _b;
|
|
256
|
+
if (!this.account || !transaction) {
|
|
257
|
+
throw new utils.DynamicError('No account or transaction found');
|
|
258
|
+
}
|
|
259
|
+
const result = yield ((_b = (_a = this.getFeatures()) === null || _a === void 0 ? void 0 : _a['sui:signAndExecuteTransaction']) === null || _b === void 0 ? void 0 : _b.signAndExecuteTransaction({
|
|
260
|
+
account: this.account,
|
|
261
|
+
chain: this.account.chains[0],
|
|
262
|
+
transaction,
|
|
263
|
+
}));
|
|
264
|
+
return result;
|
|
265
|
+
}),
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
}
|
|
242
269
|
teardownEventListeners() {
|
|
243
270
|
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
244
271
|
if (this.eventsUnsubscribeHandler) {
|
|
@@ -2,7 +2,7 @@ import { SuiWalletFeatures, WalletAccount, WalletWithFeatures } from '@mysten/wa
|
|
|
2
2
|
import { SuiClient } from '@mysten/sui/client';
|
|
3
3
|
import { Chain, WalletConnectorBase } from '@dynamic-labs/wallet-connector-core';
|
|
4
4
|
import { Logger } from '@dynamic-labs/logger';
|
|
5
|
-
import { GenericNetwork } from '@dynamic-labs/types';
|
|
5
|
+
import { GenericNetwork, IUITransaction } from '@dynamic-labs/types';
|
|
6
6
|
import { SuiWallet } from './wallet/SuiWallet';
|
|
7
7
|
import { SuiWalletConnectorProps, SuiWalletStandardEventHandler } from './types';
|
|
8
8
|
export declare abstract class SuiWalletConnector extends WalletConnectorBase<typeof SuiWallet> {
|
|
@@ -64,6 +64,8 @@ export declare abstract class SuiWalletConnector extends WalletConnectorBase<typ
|
|
|
64
64
|
getBalance(address: string): Promise<string | undefined>;
|
|
65
65
|
signMessage(messageToSign: string): Promise<string | undefined>;
|
|
66
66
|
getWalletAccount(): Promise<WalletAccount | undefined>;
|
|
67
|
+
/** Function used to create transactions in the SDK interface */
|
|
68
|
+
createUiTransaction(from: string): Promise<IUITransaction>;
|
|
67
69
|
teardownEventListeners(): Promise<void>;
|
|
68
70
|
endSession(): Promise<void>;
|
|
69
71
|
getEnabledNetworks(): GenericNetwork[];
|
|
@@ -6,6 +6,7 @@ import { DynamicError } from '@dynamic-labs/utils';
|
|
|
6
6
|
import { Logger } from '@dynamic-labs/logger';
|
|
7
7
|
import { SuiWallet } from './wallet/SuiWallet.js';
|
|
8
8
|
import { getSuiNetworkIdFromName, getPreferredRpcUrl } from './utils/network/networkHelpers.js';
|
|
9
|
+
import { SuiUiTransaction } from './utils/SuiUiTransaction/SuiUiTransaction.js';
|
|
9
10
|
|
|
10
11
|
class SuiWalletConnector extends WalletConnectorBase {
|
|
11
12
|
constructor(name, opts) {
|
|
@@ -235,6 +236,32 @@ class SuiWalletConnector extends WalletConnectorBase {
|
|
|
235
236
|
return this.account;
|
|
236
237
|
});
|
|
237
238
|
}
|
|
239
|
+
/** Function used to create transactions in the SDK interface */
|
|
240
|
+
createUiTransaction(from) {
|
|
241
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
242
|
+
yield this.validateActiveWallet(from);
|
|
243
|
+
const suiClient = yield this.getSuiClient();
|
|
244
|
+
if (!suiClient) {
|
|
245
|
+
throw new DynamicError('No Sui client available');
|
|
246
|
+
}
|
|
247
|
+
return new SuiUiTransaction({
|
|
248
|
+
client: suiClient,
|
|
249
|
+
from,
|
|
250
|
+
onSubmit: (transaction) => __awaiter(this, void 0, void 0, function* () {
|
|
251
|
+
var _a, _b;
|
|
252
|
+
if (!this.account || !transaction) {
|
|
253
|
+
throw new DynamicError('No account or transaction found');
|
|
254
|
+
}
|
|
255
|
+
const result = yield ((_b = (_a = this.getFeatures()) === null || _a === void 0 ? void 0 : _a['sui:signAndExecuteTransaction']) === null || _b === void 0 ? void 0 : _b.signAndExecuteTransaction({
|
|
256
|
+
account: this.account,
|
|
257
|
+
chain: this.account.chains[0],
|
|
258
|
+
transaction,
|
|
259
|
+
}));
|
|
260
|
+
return result;
|
|
261
|
+
}),
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
}
|
|
238
265
|
teardownEventListeners() {
|
|
239
266
|
return __awaiter(this, void 0, void 0, function* () {
|
|
240
267
|
if (this.eventsUnsubscribeHandler) {
|
package/src/index.cjs
CHANGED
|
@@ -10,6 +10,7 @@ var constants = require('./utils/constants/constants.cjs');
|
|
|
10
10
|
var SuiWalletConnector = require('./SuiWalletConnector.cjs');
|
|
11
11
|
var SuiWallet = require('./wallet/SuiWallet.cjs');
|
|
12
12
|
var isSuiWallet = require('./wallet/isSuiWallet/isSuiWallet.cjs');
|
|
13
|
+
var SuiUiTransaction = require('./utils/SuiUiTransaction/SuiUiTransaction.cjs');
|
|
13
14
|
|
|
14
15
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
15
16
|
assertPackageVersion.assertPackageVersion('@dynamic-labs/sui', _package.version);
|
|
@@ -22,4 +23,5 @@ exports.MIST_PER_SUI = constants.MIST_PER_SUI;
|
|
|
22
23
|
exports.SuiWalletConnector = SuiWalletConnector.SuiWalletConnector;
|
|
23
24
|
exports.SuiWallet = SuiWallet.SuiWallet;
|
|
24
25
|
exports.isSuiWallet = isSuiWallet.isSuiWallet;
|
|
26
|
+
exports.SuiUiTransaction = SuiUiTransaction.SuiUiTransaction;
|
|
25
27
|
exports.SuiWalletConnectors = SuiWalletConnectors;
|
package/src/index.d.ts
CHANGED
|
@@ -4,3 +4,4 @@ export { SuiWalletConnector } from './SuiWalletConnector';
|
|
|
4
4
|
export { SuiWallet } from './wallet/SuiWallet';
|
|
5
5
|
export { isSuiWallet } from './wallet/isSuiWallet';
|
|
6
6
|
export type { SuiWalletConnectorProps, SuiSendBalanceProps } from './types';
|
|
7
|
+
export { SuiUiTransaction } from './utils/SuiUiTransaction/SuiUiTransaction';
|
package/src/index.js
CHANGED
|
@@ -6,6 +6,7 @@ export { MIST_PER_SUI } from './utils/constants/constants.js';
|
|
|
6
6
|
export { SuiWalletConnector } from './SuiWalletConnector.js';
|
|
7
7
|
export { SuiWallet } from './wallet/SuiWallet.js';
|
|
8
8
|
export { isSuiWallet } from './wallet/isSuiWallet/isSuiWallet.js';
|
|
9
|
+
export { SuiUiTransaction } from './utils/SuiUiTransaction/SuiUiTransaction.js';
|
|
9
10
|
|
|
10
11
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
11
12
|
assertPackageVersion('@dynamic-labs/sui', version);
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../../_virtual/_tslib.cjs');
|
|
7
|
+
var transactions = require('@mysten/sui/transactions');
|
|
8
|
+
var utils$1 = require('@mysten/sui/utils');
|
|
9
|
+
var utils = require('@dynamic-labs/utils');
|
|
10
|
+
var constants = require('../constants/constants.cjs');
|
|
11
|
+
|
|
12
|
+
class SuiUiTransaction {
|
|
13
|
+
constructor({ onSubmit, from, client }) {
|
|
14
|
+
this.chain = 'SUI';
|
|
15
|
+
this.data = undefined;
|
|
16
|
+
this.fee = { gas: undefined };
|
|
17
|
+
this.formatNonNativeToken = (value, decimals) => (Number(value) / Number(Math.pow(10, decimals))).toString();
|
|
18
|
+
this.from = from;
|
|
19
|
+
this.onSubmit = onSubmit;
|
|
20
|
+
this.client = client;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Fetches the gas fee for the transaction by executing a dry run of the
|
|
24
|
+
* transaction and calculating the gas used.
|
|
25
|
+
*
|
|
26
|
+
* @returns The gas fee for the transaction
|
|
27
|
+
*/
|
|
28
|
+
fetchFee() {
|
|
29
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
30
|
+
if (this.fee.gas)
|
|
31
|
+
return;
|
|
32
|
+
const transaction = yield this.createTransactionSafe();
|
|
33
|
+
if (!transaction) {
|
|
34
|
+
throw new Error('Could not create a valid SUI transaction, check your address and value');
|
|
35
|
+
}
|
|
36
|
+
const transactionBlock = yield transaction.build({ client: this.client });
|
|
37
|
+
const dryRunResult = yield this.client.dryRunTransactionBlock({
|
|
38
|
+
transactionBlock,
|
|
39
|
+
});
|
|
40
|
+
if (!dryRunResult) {
|
|
41
|
+
this.fee.gas = undefined;
|
|
42
|
+
throw new Error('Error accessing SUI network to calculate gas, please try again later');
|
|
43
|
+
}
|
|
44
|
+
const gasObject = dryRunResult.effects.gasUsed;
|
|
45
|
+
// net_gas_fees = computation_gas_fee + storage_gas_fee - storage_rebate
|
|
46
|
+
// https://docs.sui.io/concepts/tokenomics/gas-in-sui
|
|
47
|
+
const calculatedGas = BigInt(gasObject.computationCost) +
|
|
48
|
+
BigInt(gasObject.storageCost) -
|
|
49
|
+
BigInt(gasObject.storageRebate);
|
|
50
|
+
this.fee.gas = calculatedGas;
|
|
51
|
+
if (this.fee.gas <= BigInt(0)) {
|
|
52
|
+
this.fee.gas = undefined;
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
isGasSponsored() {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
parse(input) {
|
|
60
|
+
const floatValue = parseFloat(input);
|
|
61
|
+
const mist = Math.round(floatValue * constants.MIST_PER_SUI);
|
|
62
|
+
return BigInt(mist);
|
|
63
|
+
}
|
|
64
|
+
parseNonNativeToken(input, decimals) {
|
|
65
|
+
return BigInt(Math.floor(Number(input) * Math.pow(10, decimals)));
|
|
66
|
+
}
|
|
67
|
+
format(value, { precision } = {}) {
|
|
68
|
+
const suiValue = Number(value) / constants.MIST_PER_SUI;
|
|
69
|
+
const decimalString = suiValue.toLocaleString('fullwide', {
|
|
70
|
+
maximumFractionDigits: 20,
|
|
71
|
+
minimumFractionDigits: 0,
|
|
72
|
+
useGrouping: false,
|
|
73
|
+
});
|
|
74
|
+
return utils.formatNumberText(decimalString, { precision });
|
|
75
|
+
}
|
|
76
|
+
submit() {
|
|
77
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
const sendTransaction = (yield this.createTransaction());
|
|
79
|
+
return this.onSubmit(sendTransaction);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
getBalance() {
|
|
83
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
const balance = yield this.client.getBalance({
|
|
85
|
+
coinType: '0x2::sui::SUI',
|
|
86
|
+
owner: this.from,
|
|
87
|
+
});
|
|
88
|
+
return BigInt(balance.totalBalance);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
validateAddressFormat(address) {
|
|
92
|
+
if (address === 'dyn_send_transaction.multiple_recipients') {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
return utils$1.isValidSuiAddress(address);
|
|
96
|
+
}
|
|
97
|
+
createTransaction() {
|
|
98
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
const { value, to } = this;
|
|
100
|
+
if (!to) {
|
|
101
|
+
throw new Error('Destination is required');
|
|
102
|
+
}
|
|
103
|
+
if (!value) {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
const sendTransaction = new transactions.Transaction();
|
|
107
|
+
sendTransaction.setSender(this.from);
|
|
108
|
+
const [coin] = sendTransaction.splitCoins(sendTransaction.gas, [value]);
|
|
109
|
+
sendTransaction.transferObjects([coin], to);
|
|
110
|
+
return sendTransaction;
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
createTransactionSafe() {
|
|
114
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
115
|
+
try {
|
|
116
|
+
return yield this.createTransaction();
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
exports.SuiUiTransaction = SuiUiTransaction;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Transaction } from '@mysten/sui/transactions';
|
|
2
|
+
import { SuiClient } from '@mysten/sui/client';
|
|
3
|
+
import { IUITransaction, IUITransactionFormatOptions } from '@dynamic-labs/types';
|
|
4
|
+
type SuiUiTransactionProps = {
|
|
5
|
+
/** SUI Address of the sender */
|
|
6
|
+
from: string;
|
|
7
|
+
/** The [SuiClient] instance, needed to estimate gas fees */
|
|
8
|
+
client: SuiClient;
|
|
9
|
+
/** The function to call when the transaction is submitted */
|
|
10
|
+
onSubmit: (transaction?: Transaction) => Promise<any>;
|
|
11
|
+
};
|
|
12
|
+
export declare class SuiUiTransaction implements IUITransaction {
|
|
13
|
+
to: string | undefined;
|
|
14
|
+
from: string;
|
|
15
|
+
value: bigint | undefined;
|
|
16
|
+
chain: string;
|
|
17
|
+
receipt: string | undefined;
|
|
18
|
+
data: undefined;
|
|
19
|
+
fee: {
|
|
20
|
+
gas: bigint | undefined;
|
|
21
|
+
};
|
|
22
|
+
nonNativeAddress?: string;
|
|
23
|
+
nonNativeDecimal?: number;
|
|
24
|
+
nonNativeValue?: bigint;
|
|
25
|
+
nativePrice?: number;
|
|
26
|
+
private onSubmit;
|
|
27
|
+
private client;
|
|
28
|
+
constructor({ onSubmit, from, client }: SuiUiTransactionProps);
|
|
29
|
+
/**
|
|
30
|
+
* Fetches the gas fee for the transaction by executing a dry run of the
|
|
31
|
+
* transaction and calculating the gas used.
|
|
32
|
+
*
|
|
33
|
+
* @returns The gas fee for the transaction
|
|
34
|
+
*/
|
|
35
|
+
fetchFee(): Promise<void>;
|
|
36
|
+
isGasSponsored(): boolean;
|
|
37
|
+
parse(input: string): bigint;
|
|
38
|
+
parseNonNativeToken(input: string, decimals: number): bigint;
|
|
39
|
+
formatNonNativeToken: (value: bigint, decimals: number) => string;
|
|
40
|
+
format(value: bigint, { precision }?: IUITransactionFormatOptions): string;
|
|
41
|
+
submit(): Promise<any>;
|
|
42
|
+
getBalance(): Promise<bigint>;
|
|
43
|
+
validateAddressFormat(address: string): boolean;
|
|
44
|
+
private createTransaction;
|
|
45
|
+
private createTransactionSafe;
|
|
46
|
+
}
|
|
47
|
+
export {};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { __awaiter } from '../../../_virtual/_tslib.js';
|
|
3
|
+
import { Transaction } from '@mysten/sui/transactions';
|
|
4
|
+
import { isValidSuiAddress } from '@mysten/sui/utils';
|
|
5
|
+
import { formatNumberText } from '@dynamic-labs/utils';
|
|
6
|
+
import { MIST_PER_SUI } from '../constants/constants.js';
|
|
7
|
+
|
|
8
|
+
class SuiUiTransaction {
|
|
9
|
+
constructor({ onSubmit, from, client }) {
|
|
10
|
+
this.chain = 'SUI';
|
|
11
|
+
this.data = undefined;
|
|
12
|
+
this.fee = { gas: undefined };
|
|
13
|
+
this.formatNonNativeToken = (value, decimals) => (Number(value) / Number(Math.pow(10, decimals))).toString();
|
|
14
|
+
this.from = from;
|
|
15
|
+
this.onSubmit = onSubmit;
|
|
16
|
+
this.client = client;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Fetches the gas fee for the transaction by executing a dry run of the
|
|
20
|
+
* transaction and calculating the gas used.
|
|
21
|
+
*
|
|
22
|
+
* @returns The gas fee for the transaction
|
|
23
|
+
*/
|
|
24
|
+
fetchFee() {
|
|
25
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
+
if (this.fee.gas)
|
|
27
|
+
return;
|
|
28
|
+
const transaction = yield this.createTransactionSafe();
|
|
29
|
+
if (!transaction) {
|
|
30
|
+
throw new Error('Could not create a valid SUI transaction, check your address and value');
|
|
31
|
+
}
|
|
32
|
+
const transactionBlock = yield transaction.build({ client: this.client });
|
|
33
|
+
const dryRunResult = yield this.client.dryRunTransactionBlock({
|
|
34
|
+
transactionBlock,
|
|
35
|
+
});
|
|
36
|
+
if (!dryRunResult) {
|
|
37
|
+
this.fee.gas = undefined;
|
|
38
|
+
throw new Error('Error accessing SUI network to calculate gas, please try again later');
|
|
39
|
+
}
|
|
40
|
+
const gasObject = dryRunResult.effects.gasUsed;
|
|
41
|
+
// net_gas_fees = computation_gas_fee + storage_gas_fee - storage_rebate
|
|
42
|
+
// https://docs.sui.io/concepts/tokenomics/gas-in-sui
|
|
43
|
+
const calculatedGas = BigInt(gasObject.computationCost) +
|
|
44
|
+
BigInt(gasObject.storageCost) -
|
|
45
|
+
BigInt(gasObject.storageRebate);
|
|
46
|
+
this.fee.gas = calculatedGas;
|
|
47
|
+
if (this.fee.gas <= BigInt(0)) {
|
|
48
|
+
this.fee.gas = undefined;
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
isGasSponsored() {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
parse(input) {
|
|
56
|
+
const floatValue = parseFloat(input);
|
|
57
|
+
const mist = Math.round(floatValue * MIST_PER_SUI);
|
|
58
|
+
return BigInt(mist);
|
|
59
|
+
}
|
|
60
|
+
parseNonNativeToken(input, decimals) {
|
|
61
|
+
return BigInt(Math.floor(Number(input) * Math.pow(10, decimals)));
|
|
62
|
+
}
|
|
63
|
+
format(value, { precision } = {}) {
|
|
64
|
+
const suiValue = Number(value) / MIST_PER_SUI;
|
|
65
|
+
const decimalString = suiValue.toLocaleString('fullwide', {
|
|
66
|
+
maximumFractionDigits: 20,
|
|
67
|
+
minimumFractionDigits: 0,
|
|
68
|
+
useGrouping: false,
|
|
69
|
+
});
|
|
70
|
+
return formatNumberText(decimalString, { precision });
|
|
71
|
+
}
|
|
72
|
+
submit() {
|
|
73
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
74
|
+
const sendTransaction = (yield this.createTransaction());
|
|
75
|
+
return this.onSubmit(sendTransaction);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
getBalance() {
|
|
79
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
const balance = yield this.client.getBalance({
|
|
81
|
+
coinType: '0x2::sui::SUI',
|
|
82
|
+
owner: this.from,
|
|
83
|
+
});
|
|
84
|
+
return BigInt(balance.totalBalance);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
validateAddressFormat(address) {
|
|
88
|
+
if (address === 'dyn_send_transaction.multiple_recipients') {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
return isValidSuiAddress(address);
|
|
92
|
+
}
|
|
93
|
+
createTransaction() {
|
|
94
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
95
|
+
const { value, to } = this;
|
|
96
|
+
if (!to) {
|
|
97
|
+
throw new Error('Destination is required');
|
|
98
|
+
}
|
|
99
|
+
if (!value) {
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
const sendTransaction = new Transaction();
|
|
103
|
+
sendTransaction.setSender(this.from);
|
|
104
|
+
const [coin] = sendTransaction.splitCoins(sendTransaction.gas, [value]);
|
|
105
|
+
sendTransaction.transferObjects([coin], to);
|
|
106
|
+
return sendTransaction;
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
createTransactionSafe() {
|
|
110
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
111
|
+
try {
|
|
112
|
+
return yield this.createTransaction();
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export { SuiUiTransaction };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './SuiUiTransaction';
|