@zofai/zo-sdk 0.2.9 → 0.2.11
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/dist/abstract/BaseAPI.cjs +7 -1
- package/dist/abstract/BaseAPI.cjs.map +1 -1
- package/dist/abstract/BaseAPI.d.cts +11 -10
- package/dist/abstract/BaseAPI.d.cts.map +1 -1
- package/dist/abstract/BaseAPI.d.mts +11 -10
- package/dist/abstract/BaseAPI.d.mts.map +1 -1
- package/dist/abstract/BaseAPI.mjs +7 -1
- package/dist/abstract/BaseAPI.mjs.map +1 -1
- package/dist/abstract/BaseDataAPI.d.cts +1 -1
- package/dist/abstract/BaseDataAPI.d.cts.map +1 -1
- package/dist/abstract/BaseDataAPI.d.mts +1 -1
- package/dist/abstract/BaseDataAPI.d.mts.map +1 -1
- package/dist/api.cjs +12 -12
- package/dist/api.cjs.map +1 -1
- package/dist/api.d.cts +1 -1
- package/dist/api.d.cts.map +1 -1
- package/dist/api.d.mts +1 -1
- package/dist/api.d.mts.map +1 -1
- package/dist/api.mjs +12 -12
- package/dist/api.mjs.map +1 -1
- package/dist/bcs.d.cts +41 -71
- package/dist/bcs.d.cts.map +1 -1
- package/dist/bcs.d.mts +41 -71
- package/dist/bcs.d.mts.map +1 -1
- package/dist/coins.cjs +74 -0
- package/dist/coins.cjs.map +1 -0
- package/dist/coins.d.cts +37 -0
- package/dist/coins.d.cts.map +1 -0
- package/dist/coins.d.mts +37 -0
- package/dist/coins.d.mts.map +1 -0
- package/dist/coins.mjs +67 -0
- package/dist/coins.mjs.map +1 -0
- package/dist/data.d.cts +6 -6
- package/dist/data.d.cts.map +1 -1
- package/dist/data.d.mts +6 -6
- package/dist/data.d.mts.map +1 -1
- package/dist/factory/SDKFactory.d.cts +1 -1
- package/dist/factory/SDKFactory.d.cts.map +1 -1
- package/dist/factory/SDKFactory.d.mts +1 -1
- package/dist/factory/SDKFactory.d.mts.map +1 -1
- package/dist/implementations/SLPAPI.cjs +192 -100
- package/dist/implementations/SLPAPI.cjs.map +1 -1
- package/dist/implementations/SLPAPI.d.cts +24 -24
- package/dist/implementations/SLPAPI.d.cts.map +1 -1
- package/dist/implementations/SLPAPI.d.mts +24 -24
- package/dist/implementations/SLPAPI.d.mts.map +1 -1
- package/dist/implementations/SLPAPI.mjs +192 -100
- package/dist/implementations/SLPAPI.mjs.map +1 -1
- package/dist/implementations/SLPDataAPI.cjs +2 -2
- package/dist/implementations/SLPDataAPI.cjs.map +1 -1
- package/dist/implementations/SLPDataAPI.d.cts +5 -5
- package/dist/implementations/SLPDataAPI.d.cts.map +1 -1
- package/dist/implementations/SLPDataAPI.d.mts +5 -5
- package/dist/implementations/SLPDataAPI.d.mts.map +1 -1
- package/dist/implementations/SLPDataAPI.mjs +2 -2
- package/dist/implementations/SLPDataAPI.mjs.map +1 -1
- package/dist/implementations/USDZAPI.cjs +176 -92
- package/dist/implementations/USDZAPI.cjs.map +1 -1
- package/dist/implementations/USDZAPI.d.cts +22 -22
- package/dist/implementations/USDZAPI.d.cts.map +1 -1
- package/dist/implementations/USDZAPI.d.mts +22 -22
- package/dist/implementations/USDZAPI.d.mts.map +1 -1
- package/dist/implementations/USDZAPI.mjs +176 -92
- package/dist/implementations/USDZAPI.mjs.map +1 -1
- package/dist/implementations/USDZDataAPI.d.cts +5 -5
- package/dist/implementations/USDZDataAPI.d.cts.map +1 -1
- package/dist/implementations/USDZDataAPI.d.mts +5 -5
- package/dist/implementations/USDZDataAPI.d.mts.map +1 -1
- package/dist/implementations/ZBTCVCAPI.cjs +84 -46
- package/dist/implementations/ZBTCVCAPI.cjs.map +1 -1
- package/dist/implementations/ZBTCVCAPI.d.cts +12 -12
- package/dist/implementations/ZBTCVCAPI.d.cts.map +1 -1
- package/dist/implementations/ZBTCVCAPI.d.mts +12 -12
- package/dist/implementations/ZBTCVCAPI.d.mts.map +1 -1
- package/dist/implementations/ZBTCVCAPI.mjs +84 -46
- package/dist/implementations/ZBTCVCAPI.mjs.map +1 -1
- package/dist/implementations/ZBTCVCDataAPI.cjs.map +1 -1
- package/dist/implementations/ZBTCVCDataAPI.d.cts +5 -5
- package/dist/implementations/ZBTCVCDataAPI.d.cts.map +1 -1
- package/dist/implementations/ZBTCVCDataAPI.d.mts +5 -5
- package/dist/implementations/ZBTCVCDataAPI.d.mts.map +1 -1
- package/dist/implementations/ZBTCVCDataAPI.mjs.map +1 -1
- package/dist/implementations/ZLPAPI.cjs +192 -100
- package/dist/implementations/ZLPAPI.cjs.map +1 -1
- package/dist/implementations/ZLPAPI.d.cts +24 -24
- package/dist/implementations/ZLPAPI.d.cts.map +1 -1
- package/dist/implementations/ZLPAPI.d.mts +24 -24
- package/dist/implementations/ZLPAPI.d.mts.map +1 -1
- package/dist/implementations/ZLPAPI.mjs +192 -100
- package/dist/implementations/ZLPAPI.mjs.map +1 -1
- package/dist/implementations/ZLPDataAPI.cjs.map +1 -1
- package/dist/implementations/ZLPDataAPI.d.cts +5 -5
- package/dist/implementations/ZLPDataAPI.d.cts.map +1 -1
- package/dist/implementations/ZLPDataAPI.d.mts +5 -5
- package/dist/implementations/ZLPDataAPI.d.mts.map +1 -1
- package/dist/implementations/ZLPDataAPI.mjs.map +1 -1
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -1
- package/dist/interfaces/base.d.cts +26 -22
- package/dist/interfaces/base.d.cts.map +1 -1
- package/dist/interfaces/base.d.mts +26 -22
- package/dist/interfaces/base.d.mts.map +1 -1
- package/dist/interfaces/slp.d.cts +1 -1
- package/dist/interfaces/slp.d.cts.map +1 -1
- package/dist/interfaces/slp.d.mts +1 -1
- package/dist/interfaces/slp.d.mts.map +1 -1
- package/dist/interfaces/usdz.d.cts +1 -1
- package/dist/interfaces/usdz.d.cts.map +1 -1
- package/dist/interfaces/usdz.d.mts +1 -1
- package/dist/interfaces/usdz.d.mts.map +1 -1
- package/dist/interfaces/zbtcvc.d.cts +1 -1
- package/dist/interfaces/zbtcvc.d.cts.map +1 -1
- package/dist/interfaces/zbtcvc.d.mts +1 -1
- package/dist/interfaces/zbtcvc.d.mts.map +1 -1
- package/dist/interfaces/zlp.d.cts +1 -1
- package/dist/interfaces/zlp.d.cts.map +1 -1
- package/dist/interfaces/zlp.d.mts +1 -1
- package/dist/interfaces/zlp.d.mts.map +1 -1
- package/dist/oracle.cjs +27 -22
- package/dist/oracle.cjs.map +1 -1
- package/dist/oracle.d.cts +14 -2
- package/dist/oracle.d.cts.map +1 -1
- package/dist/oracle.d.mts +14 -2
- package/dist/oracle.d.mts.map +1 -1
- package/dist/oracle.mjs +27 -22
- package/dist/oracle.mjs.map +1 -1
- package/dist/suiClient.cjs +8 -0
- package/dist/suiClient.cjs.map +1 -0
- package/dist/suiClient.d.cts +8 -0
- package/dist/suiClient.d.cts.map +1 -0
- package/dist/suiClient.d.mts +8 -0
- package/dist/suiClient.d.mts.map +1 -0
- package/dist/suiClient.mjs +2 -0
- package/dist/suiClient.mjs.map +1 -0
- package/dist/utils.cjs +7 -3
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +2 -2
- package/dist/utils.d.cts.map +1 -1
- package/dist/utils.d.mts +2 -2
- package/dist/utils.d.mts.map +1 -1
- package/dist/utils.mjs +7 -3
- package/dist/utils.mjs.map +1 -1
- package/package.json +4 -4
- package/src/abstract/BaseAPI.ts +11 -9
- package/src/abstract/BaseDataAPI.ts +1 -1
- package/src/api.ts +13 -17
- package/src/coins.ts +116 -0
- package/src/data.ts +1 -1
- package/src/factory/SDKFactory.ts +1 -1
- package/src/implementations/SLPAPI.ts +200 -116
- package/src/implementations/SLPDataAPI.ts +3 -3
- package/src/implementations/USDZAPI.ts +185 -95
- package/src/implementations/USDZDataAPI.ts +1 -1
- package/src/implementations/ZBTCVCAPI.ts +99 -53
- package/src/implementations/ZBTCVCDataAPI.ts +1 -1
- package/src/implementations/ZLPAPI.ts +200 -111
- package/src/implementations/ZLPDataAPI.ts +1 -1
- package/src/index.ts +3 -1
- package/src/interfaces/base.ts +14 -22
- package/src/interfaces/slp.ts +0 -1
- package/src/interfaces/usdz.ts +0 -1
- package/src/interfaces/zbtcvc.ts +0 -1
- package/src/interfaces/zlp.ts +0 -1
- package/src/oracle.ts +70 -30
- package/src/suiClient.ts +11 -0
- package/src/utils.ts +9 -3
- package/tsconfig.json +8 -1
package/src/api.ts
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
/* eslint-disable class-methods-use-this */
|
|
3
3
|
import type { KioskClient, KioskOwnerCap } from '@mysten/kiosk'
|
|
4
4
|
import { KioskTransaction } from '@mysten/kiosk'
|
|
5
|
-
import type { SuiClient } from '@mysten/sui/client'
|
|
6
5
|
import { Transaction } from '@mysten/sui/transactions'
|
|
7
6
|
import { SUI_CLOCK_OBJECT_ID } from '@mysten/sui/utils'
|
|
8
7
|
|
|
@@ -10,6 +9,7 @@ import type { Network } from './consts'
|
|
|
10
9
|
import { ALLOW_TRADE_CAN_TRADE, ALLOW_TRADE_MUST_TRADE, ALLOW_TRADE_NO_TRADE } from './consts'
|
|
11
10
|
import type { ICredential } from './data'
|
|
12
11
|
import { DataAPI } from './data'
|
|
12
|
+
import type { SuiClient } from './suiClient'
|
|
13
13
|
import { joinSymbol } from './utils'
|
|
14
14
|
|
|
15
15
|
export class API extends DataAPI {
|
|
@@ -64,7 +64,7 @@ export class API extends DataAPI {
|
|
|
64
64
|
sender = '',
|
|
65
65
|
) => {
|
|
66
66
|
let tx = new Transaction()
|
|
67
|
-
tx = await this.initOracleTxb(Object.keys(this.consts.pythFeeder.feeder), tx)
|
|
67
|
+
tx = (await this.initOracleTxb(Object.keys(this.consts.pythFeeder.feeder), tx)).tx
|
|
68
68
|
if (referralAddress && !(await this.hasReferral(sender || ''))) {
|
|
69
69
|
tx = await this.addReferral(referralAddress, tx)
|
|
70
70
|
}
|
|
@@ -90,7 +90,7 @@ export class API extends DataAPI {
|
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
withdraw = async (coin: string, zlpCoinObjects: string[], amount: number, minAmountOut = 0) => {
|
|
93
|
-
const tx = await this.initOracleTxb(Object.keys(this.consts.pythFeeder.feeder))
|
|
93
|
+
const { tx } = await this.initOracleTxb(Object.keys(this.consts.pythFeeder.feeder))
|
|
94
94
|
const zlpCoinObject = this.#processCoins(tx, 'zlp', zlpCoinObjects)
|
|
95
95
|
const [withdrawObject] = tx.splitCoins(zlpCoinObject, [tx.pure.u64(amount)])
|
|
96
96
|
|
|
@@ -133,7 +133,7 @@ export class API extends DataAPI {
|
|
|
133
133
|
if (referralAddress && !(await this.hasReferral(sender || ''))) {
|
|
134
134
|
tx = await this.addReferral(referralAddress, tx)
|
|
135
135
|
}
|
|
136
|
-
tx = await this.initOracleTxb([collateralToken, indexToken], tx)
|
|
136
|
+
tx = (await this.initOracleTxb([collateralToken, indexToken], tx)).tx
|
|
137
137
|
const coinObject = this.#processCoins(tx, collateralToken, coinObjects)
|
|
138
138
|
const [depositObject, feeObject] = tx.splitCoins(coinObject, [
|
|
139
139
|
tx.pure.u64(collateralAmount),
|
|
@@ -203,7 +203,7 @@ export class API extends DataAPI {
|
|
|
203
203
|
if (referralAddress && !(await this.hasReferral(sender || ''))) {
|
|
204
204
|
tx = await this.addReferral(referralAddress, tx)
|
|
205
205
|
}
|
|
206
|
-
tx = await this.initOracleTxb([collateralToken, indexToken], tx)
|
|
206
|
+
tx = (await this.initOracleTxb([collateralToken, indexToken], tx)).tx
|
|
207
207
|
const coinObject = this.#processCoins(tx, collateralToken, coinObjects)
|
|
208
208
|
const [depositObject, feeObject] = tx.splitCoins(coinObject, [
|
|
209
209
|
tx.pure.u64(collateralAmount),
|
|
@@ -276,7 +276,7 @@ export class API extends DataAPI {
|
|
|
276
276
|
coinObjects: string[],
|
|
277
277
|
long: boolean,
|
|
278
278
|
) => {
|
|
279
|
-
const tx = await this.initOracleTxb([collateralToken, indexToken])
|
|
279
|
+
const { tx } = await this.initOracleTxb([collateralToken, indexToken])
|
|
280
280
|
const coinObject = this.#processCoins(tx, collateralToken, coinObjects)
|
|
281
281
|
const [depositObject] = tx.splitCoins(coinObject, [tx.pure.u64(amount)])
|
|
282
282
|
|
|
@@ -300,7 +300,7 @@ export class API extends DataAPI {
|
|
|
300
300
|
amount: number,
|
|
301
301
|
long: boolean,
|
|
302
302
|
) => {
|
|
303
|
-
const tx = await this.initOracleTxb([collateralToken, indexToken])
|
|
303
|
+
const { tx } = await this.initOracleTxb([collateralToken, indexToken])
|
|
304
304
|
const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
|
|
305
305
|
|
|
306
306
|
tx.moveCall({
|
|
@@ -342,7 +342,7 @@ export class API extends DataAPI {
|
|
|
342
342
|
collateralSlippage = 0.5,
|
|
343
343
|
relayerFee = BigInt(0.5),
|
|
344
344
|
) => {
|
|
345
|
-
const tx = await this.initOracleTxb([collateralToken, indexToken])
|
|
345
|
+
const { tx } = await this.initOracleTxb([collateralToken, indexToken])
|
|
346
346
|
const symbol = joinSymbol(long ? 'long' : 'short', indexToken)
|
|
347
347
|
const coinObject = this.#processCoins(tx, collateralToken, coinObjects)
|
|
348
348
|
const feeObject = tx.splitCoins(coinObject, [tx.pure.u64(relayerFee)])
|
|
@@ -358,7 +358,6 @@ export class API extends DataAPI {
|
|
|
358
358
|
isTakeProfitOrder = true
|
|
359
359
|
}
|
|
360
360
|
|
|
361
|
-
|
|
362
361
|
tx.moveCall({
|
|
363
362
|
target: `${this.consts.zoCore.upgradedPackage}::market::decrease_position`,
|
|
364
363
|
typeArguments: [
|
|
@@ -407,7 +406,7 @@ export class API extends DataAPI {
|
|
|
407
406
|
collateralSlippage = 0.5,
|
|
408
407
|
relayerFee = BigInt(0.5),
|
|
409
408
|
) => {
|
|
410
|
-
const tx = await this.initOracleTxb([collateralToken, indexToken])
|
|
409
|
+
const { tx } = await this.initOracleTxb([collateralToken, indexToken])
|
|
411
410
|
|
|
412
411
|
const kioskTx = new KioskTransaction({
|
|
413
412
|
transaction: tx,
|
|
@@ -435,7 +434,6 @@ export class API extends DataAPI {
|
|
|
435
434
|
isTakeProfitOrder = true
|
|
436
435
|
}
|
|
437
436
|
|
|
438
|
-
|
|
439
437
|
tx.moveCall({
|
|
440
438
|
target: `${this.consts.zoCore.upgradedPackage}::market::decrease_position_with_scard`,
|
|
441
439
|
typeArguments: [
|
|
@@ -491,7 +489,7 @@ export class API extends DataAPI {
|
|
|
491
489
|
if (!tx) {
|
|
492
490
|
tx = new Transaction()
|
|
493
491
|
}
|
|
494
|
-
tx = await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx)
|
|
492
|
+
tx = (await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx)).tx
|
|
495
493
|
|
|
496
494
|
for (const position of positions) {
|
|
497
495
|
const {
|
|
@@ -525,7 +523,6 @@ export class API extends DataAPI {
|
|
|
525
523
|
innerIsTakeProfitOrder = true
|
|
526
524
|
}
|
|
527
525
|
|
|
528
|
-
|
|
529
526
|
tx.moveCall({
|
|
530
527
|
target: `${this.consts.zoCore.upgradedPackage}::market::decrease_position`,
|
|
531
528
|
typeArguments: [
|
|
@@ -573,7 +570,7 @@ export class API extends DataAPI {
|
|
|
573
570
|
if (!tx) {
|
|
574
571
|
tx = new Transaction()
|
|
575
572
|
}
|
|
576
|
-
tx = await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx)
|
|
573
|
+
tx = (await this.initOracleTxb(positions.flatMap(position => [position.collateralToken, position.indexToken]), tx)).tx
|
|
577
574
|
|
|
578
575
|
const kioskTx = new KioskTransaction({
|
|
579
576
|
transaction: tx,
|
|
@@ -618,7 +615,6 @@ export class API extends DataAPI {
|
|
|
618
615
|
innerIsTakeProfitOrder = true
|
|
619
616
|
}
|
|
620
617
|
|
|
621
|
-
|
|
622
618
|
tx.moveCall({
|
|
623
619
|
target: `${this.consts.zoCore.upgradedPackage}::market::decrease_position_with_scard`,
|
|
624
620
|
typeArguments: [
|
|
@@ -748,7 +744,7 @@ export class API extends DataAPI {
|
|
|
748
744
|
fromAmount: bigint,
|
|
749
745
|
fromCoinObjects: string[],
|
|
750
746
|
) => {
|
|
751
|
-
const tx = await this.initOracleTxb(Object.keys(this.consts.zoCore.vaults))
|
|
747
|
+
const { tx } = await this.initOracleTxb(Object.keys(this.consts.zoCore.vaults))
|
|
752
748
|
const fromCoinObject = this.#processCoins(tx, fromToken, fromCoinObjects)
|
|
753
749
|
const [fromDepositObject] = tx.splitCoins(fromCoinObject, [tx.pure.u64(fromAmount)])
|
|
754
750
|
const vaultsValuation = this.valuateVaults(tx)
|
|
@@ -901,7 +897,7 @@ export class API extends DataAPI {
|
|
|
901
897
|
|
|
902
898
|
// admin methods
|
|
903
899
|
adminUpdatePriceFeed = async (collateralToken: string, indexToken: string) => {
|
|
904
|
-
const tx = await this.initOracleTxb([collateralToken, indexToken])
|
|
900
|
+
const { tx } = await this.initOracleTxb([collateralToken, indexToken])
|
|
905
901
|
return tx
|
|
906
902
|
}
|
|
907
903
|
|
package/src/coins.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import type { Transaction, TransactionObjectArgument } from '@mysten/sui/transactions'
|
|
2
|
+
import { coinWithBalance } from '@mysten/sui/transactions'
|
|
3
|
+
import { SUI_TYPE_ARG } from '@mysten/sui/utils'
|
|
4
|
+
|
|
5
|
+
import type { SuiClient } from './suiClient'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Sui wallets can hold funds in two places for the same coin type:
|
|
9
|
+
* - coin objects (what getCoins returns)
|
|
10
|
+
* - address balance (funds held at the address until redeemed in a tx)
|
|
11
|
+
*/
|
|
12
|
+
export interface ICoinBalanceBreakdown {
|
|
13
|
+
coinType: string
|
|
14
|
+
totalBalance: bigint
|
|
15
|
+
coinObjectBalance: bigint
|
|
16
|
+
addressBalance: bigint
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ICoinFundingOptions {
|
|
20
|
+
/** bigint values matching each split in processCoinSplitting (sum = total spend). */
|
|
21
|
+
splitAmounts?: bigint[]
|
|
22
|
+
/** Use coinWithBalance (redeems address balance and/or merges coin objects). */
|
|
23
|
+
useAddressBalance?: boolean
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function sumSplitAmounts(amounts: bigint[]): bigint {
|
|
27
|
+
return amounts.reduce((total, amount) => total + amount, 0n)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function getCoinBalanceBreakdown(
|
|
31
|
+
provider: SuiClient,
|
|
32
|
+
owner: string,
|
|
33
|
+
coinType: string,
|
|
34
|
+
): Promise<ICoinBalanceBreakdown> {
|
|
35
|
+
const balance = await provider.getBalance({ owner, coinType })
|
|
36
|
+
const totalBalance = BigInt(balance.totalBalance)
|
|
37
|
+
|
|
38
|
+
const extended = balance as { coinBalance?: string, addressBalance?: string }
|
|
39
|
+
if (extended.coinBalance !== undefined && extended.addressBalance !== undefined) {
|
|
40
|
+
return {
|
|
41
|
+
coinType: balance.coinType,
|
|
42
|
+
totalBalance,
|
|
43
|
+
coinObjectBalance: BigInt(extended.coinBalance),
|
|
44
|
+
addressBalance: BigInt(extended.addressBalance),
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
let coinObjectBalance = 0n
|
|
49
|
+
let cursor: string | null | undefined = null
|
|
50
|
+
do {
|
|
51
|
+
const page = await provider.getCoins({ owner, coinType, cursor })
|
|
52
|
+
for (const coin of page.data) {
|
|
53
|
+
coinObjectBalance += BigInt(coin.balance)
|
|
54
|
+
}
|
|
55
|
+
cursor = page.hasNextPage ? page.nextCursor : null
|
|
56
|
+
} while (cursor)
|
|
57
|
+
|
|
58
|
+
const addressBalance = totalBalance > coinObjectBalance ? totalBalance - coinObjectBalance : 0n
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
coinType: balance.coinType,
|
|
62
|
+
totalBalance,
|
|
63
|
+
coinObjectBalance,
|
|
64
|
+
addressBalance,
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Prefer coinWithBalance when the wallet has address balance and/or coin objects alone
|
|
70
|
+
* cannot cover `amount`. processCoins / processCoinSplitting only spend explicit object IDs otherwise.
|
|
71
|
+
*/
|
|
72
|
+
export function needsCoinWithBalanceFunding(
|
|
73
|
+
breakdown: ICoinBalanceBreakdown,
|
|
74
|
+
amount: bigint,
|
|
75
|
+
): boolean {
|
|
76
|
+
if (breakdown.totalBalance < amount) {
|
|
77
|
+
return false
|
|
78
|
+
}
|
|
79
|
+
if (breakdown.addressBalance > 0n) {
|
|
80
|
+
return true
|
|
81
|
+
}
|
|
82
|
+
return breakdown.coinObjectBalance < amount
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Creates a spendable Coin<T> inside an open transaction.
|
|
87
|
+
* At build time Mysten resolves this via coin::redeem_funds (address balance) and/or merging coin objects.
|
|
88
|
+
*/
|
|
89
|
+
export function createSpendableCoin(
|
|
90
|
+
tx: Transaction,
|
|
91
|
+
coinType: string,
|
|
92
|
+
amount: bigint,
|
|
93
|
+
useGasCoin = false,
|
|
94
|
+
): TransactionObjectArgument {
|
|
95
|
+
return coinWithBalance({ balance: amount, type: coinType, useGasCoin })(tx)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Resolves a spendable SUI coin for sponsored transactions.
|
|
100
|
+
* Uses coinWithBalance so address balance and coin objects are both supported.
|
|
101
|
+
*/
|
|
102
|
+
export async function resolveSpendableSuiCoin(
|
|
103
|
+
tx: Transaction,
|
|
104
|
+
provider: SuiClient,
|
|
105
|
+
sender: string,
|
|
106
|
+
amount: bigint,
|
|
107
|
+
): Promise<TransactionObjectArgument> {
|
|
108
|
+
const breakdown = await getCoinBalanceBreakdown(provider, sender, SUI_TYPE_ARG)
|
|
109
|
+
if (breakdown.totalBalance < amount) {
|
|
110
|
+
throw new Error(
|
|
111
|
+
`Insufficient SUI balance: need ${amount} MIST, have ${breakdown.totalBalance} MIST `
|
|
112
|
+
+ `(coin objects: ${breakdown.coinObjectBalance}, address balance: ${breakdown.addressBalance})`,
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
return createSpendableCoin(tx, SUI_TYPE_ARG, amount, false)
|
|
116
|
+
}
|
package/src/data.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/* eslint-disable class-methods-use-this */
|
|
2
2
|
|
|
3
|
-
import type { DynamicFieldInfo, SuiClient } from '
|
|
3
|
+
import type { DynamicFieldInfo, SuiClient } from './suiClient'
|
|
4
4
|
import type { Transaction } from '@mysten/sui/transactions'
|
|
5
5
|
import { SUI_CLOCK_OBJECT_ID } from '@mysten/sui/utils'
|
|
6
6
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* This is the main entry point for the re-architected SDK system
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import type { SuiClient } from '
|
|
7
|
+
import type { SuiClient } from '../suiClient'
|
|
8
8
|
|
|
9
9
|
import type { LPToken, Network } from '../consts'
|
|
10
10
|
import { LPToken as LPTokenEnum } from '../consts'
|