@tradeport/sui-trading-sdk 0.4.39 → 0.4.40

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.
Files changed (34) hide show
  1. package/dist/index.d.mts +19 -2
  2. package/dist/index.d.ts +19 -2
  3. package/dist/index.js +310 -160
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +270 -130
  6. package/dist/index.mjs.map +1 -1
  7. package/package.json +7 -4
  8. package/src/SuiTradingClient.ts +18 -4
  9. package/src/helpers/kiosk/getKioskIdFromKioskTx.ts +1 -1
  10. package/src/helpers/kiosk/kioskTxWrapper.ts +3 -3
  11. package/src/helpers/kiosk/lockNftInsideKioskIfNotLocked.ts +1 -1
  12. package/src/helpers/kiosk/resolveRoyaltyRule.ts +5 -0
  13. package/src/helpers/kiosk/resolveTransferPolicies.ts +3 -0
  14. package/src/helpers/kiosk/takeLockedNftFromKiosk.ts +12 -4
  15. package/src/helpers/swap/swap.ts +147 -0
  16. package/src/methods/acceptCollectionBid/acceptCollectionBid.ts +1 -1
  17. package/src/methods/acceptNftBids/acceptNftBids.ts +1 -1
  18. package/src/methods/buyListings/addBuyListingTxs.ts +5 -0
  19. package/src/methods/buyListings/buyListings.ts +20 -7
  20. package/src/methods/cancelNftTransfers/cancelNftTransfers.ts +1 -1
  21. package/src/methods/claimNfts/claimNfts.ts +1 -1
  22. package/src/methods/createMultiBid/createMultiBid.ts +6 -7
  23. package/src/methods/listNfts/listNfts.ts +1 -1
  24. package/src/methods/migrateNftsFromUnsharedToSharedKiosks/migrateNftsFromUnsharedToSharedKiosks.ts +1 -1
  25. package/src/methods/placeCollectionBids/placeCollectionBids.ts +28 -8
  26. package/src/methods/placeNftBids/addPlaceNftBidTxs.ts +8 -7
  27. package/src/methods/placeNftBids/placeNftBids.ts +27 -8
  28. package/src/methods/removeCollectionBids/removeCollectionBids.ts +1 -1
  29. package/src/methods/removeNftBids/removeNftBids.ts +1 -1
  30. package/src/methods/transferNfts/transferNfts.ts +1 -1
  31. package/src/methods/unlistListings/unlistListings.ts +1 -2
  32. package/src/methods/updateMultiBid/updateMultiBid.ts +35 -4
  33. package/src/methods/withdrawProfitsFromKiosks/withdrawProfitsFromKiosks.ts +3 -3
  34. package/src/helpers/extractSwapResultCoin.ts +0 -17
@@ -1,4 +1,5 @@
1
1
  import { type SuiClient } from '@mysten/sui/client';
2
+ import { type Transaction } from '@mysten/sui/transactions';
2
3
  import { fromHex, normalizeSuiObjectId } from '@mysten/sui/utils';
3
4
  import {
4
5
  DYNAMIC_COLLECTION_IDS,
@@ -7,16 +8,13 @@ import {
7
8
  TRADEPORT_MULTI_BID_STORE,
8
9
  } from '../../constants';
9
10
  import { gqlChainRequest } from '../../graphql/gqlChainRequest';
10
- import {
11
- fetchMultibidChainIdAndExpiredSingleBidsById,
12
- fetchMultibidChainIdById,
13
- } from '../../graphql/queries/fetchMultibidById';
11
+ import { fetchMultibidChainIdAndExpiredSingleBidsById } from '../../graphql/queries/fetchMultibidById';
14
12
  import { getNativeKioskTransferPolicies } from '../../helpers/kiosk/getNativeKioskTransferPolicies';
15
13
  import { isOriginByteCollection } from '../../helpers/originByte/isOriginByteCollection';
14
+ import { mergeSwapCoinWithSuiBalance } from '../../helpers/swap/swap';
16
15
  import { normalizedNftType } from '../../utils/normalizeNftType';
17
- import { type PlaceNftBidTx } from './placeNftBids';
18
16
  import { addCancelSingleBidForMultibidChainId } from '../removeNftBids/addRemoveNftBidTxs';
19
- import { type Transaction } from '@mysten/sui/transactions';
17
+ import { type PlaceNftBidTx } from './placeNftBids';
20
18
 
21
19
  export async function addTradePortPlaceNftBidTxHandler(txData: PlaceNftBidTx) {
22
20
  if (
@@ -36,6 +34,7 @@ export async function addTradePortPlaceNftBidTxHandler(txData: PlaceNftBidTx) {
36
34
  bcsHex,
37
35
  expireAt,
38
36
  coinToSplit,
37
+ amountToSplitFromSuiBalance,
39
38
  } = txData;
40
39
  let bcs: Uint8Array | undefined;
41
40
  if (nftTokenId && isDynamicCollection(collectionId)) {
@@ -104,8 +103,10 @@ export async function addTradePortPlaceNftBidTxHandler(txData: PlaceNftBidTx) {
104
103
  txWithState.hasCancelBidTransactions.set(multiBidChainId, true);
105
104
  }
106
105
 
106
+ mergeSwapCoinWithSuiBalance(tx, coinToSplit, amountToSplitFromSuiBalance);
107
+
107
108
  const [coin] = tx.splitCoins(coinToSplit ? coinToSplit : tx.gas, [
108
- multiBidChainId ? 0n : price + fee + royalty,
109
+ multiBidChainId ? 0n : price + fee + royalty,
109
110
  ]);
110
111
  tx.moveCall({
111
112
  target: `${TRADEPORT_MULTI_BID_PACKAGE}::tradeport_biddings::create_bid_with${transferPolicy ? '' : 'out'}_transfer_policy`,
@@ -8,8 +8,12 @@ import {
8
8
  import { gqlChainRequest } from '../../graphql/gqlChainRequest';
9
9
  import { fetchNftsById } from '../../graphql/queries/fetchNftsById';
10
10
  import { deserializeOrCreateTxBlock } from '../../helpers/deserializeOrCreateTxBlock';
11
- import { extractSwapResultCoinFromTxBlock } from '../../helpers/extractSwapResultCoin';
12
11
  import { getNftType } from '../../helpers/getNftType';
12
+ import {
13
+ handleSwapCleanup,
14
+ handleSwapSetup,
15
+ type SwapAndPayOptions,
16
+ } from '../../helpers/swap/swap';
13
17
  import { addTradePortPlaceNftBidTxHandler } from './addPlaceNftBidTxs';
14
18
 
15
19
  export type PlaceNftBidTx = {
@@ -27,6 +31,7 @@ export type PlaceNftBidTx = {
27
31
  transferPolicies: any;
28
32
  bcsHex?: string;
29
33
  coinToSplit?: any;
34
+ amountToSplitFromSuiBalance?: bigint;
30
35
  };
31
36
 
32
37
  export type PlaceNftBids = {
@@ -39,14 +44,29 @@ export type PlaceNftBids = {
39
44
  multiBidId?: string;
40
45
  multiBidChainId?: any;
41
46
  tx?: Transaction | string;
47
+ swapAndPayOptions?: SwapAndPayOptions;
42
48
  };
43
49
 
44
50
  export const placeNftBids = async (
45
- { bids, walletAddress, multiBidId, multiBidChainId, tx: existingTx }: PlaceNftBids,
51
+ {
52
+ bids,
53
+ walletAddress,
54
+ multiBidId,
55
+ multiBidChainId,
56
+ tx: existingTx,
57
+ swapAndPayOptions,
58
+ }: PlaceNftBids,
46
59
  context: RequestContext,
47
60
  ): Promise<Transaction> => {
48
- const tx = deserializeOrCreateTxBlock({ existingTx });
49
- const swapResultCoin = extractSwapResultCoinFromTxBlock(tx);
61
+ let tx = deserializeOrCreateTxBlock({ existingTx });
62
+
63
+ const { transaction: updatedTx, swapResultCoin } = await handleSwapSetup(
64
+ context.suiClient,
65
+ walletAddress,
66
+ tx,
67
+ swapAndPayOptions,
68
+ );
69
+ tx = updatedTx;
50
70
 
51
71
  const res = await gqlChainRequest({
52
72
  chain: 'sui',
@@ -88,6 +108,7 @@ export const placeNftBids = async (
88
108
  multiBidChainId,
89
109
  expireAt: bid?.expireAt,
90
110
  coinToSplit: swapResultCoin,
111
+ amountToSplitFromSuiBalance: BigInt(swapAndPayOptions?.amountToSplitFromSuiBalance ?? 0n),
91
112
  };
92
113
 
93
114
  await addTradePortPlaceNftBidTxHandler(txData);
@@ -100,9 +121,7 @@ export const placeNftBids = async (
100
121
  });
101
122
  }
102
123
 
103
- if (swapResultCoin) {
104
- tx.transferObjects([swapResultCoin], walletAddress);
105
- }
124
+ handleSwapCleanup(tx, swapResultCoin, walletAddress);
106
125
 
107
126
  // if (process.env.ENABLE_SEGMENT_TRACKING === 'true' && nftsForTracking.length > 0) {
108
127
  // trackMethodCall({
@@ -113,5 +132,5 @@ export const placeNftBids = async (
113
132
  // });
114
133
  // }
115
134
 
116
- return Transaction.from(tx);
135
+ return tx instanceof Transaction ? tx : Transaction.from(tx);
117
136
  };
@@ -113,5 +113,5 @@ export const removeCollectionBids = async (
113
113
  // });
114
114
  // }
115
115
 
116
- return Transaction.from(tx);
116
+ return tx instanceof Transaction ? tx : Transaction.from(tx);
117
117
  };
@@ -112,5 +112,5 @@ export const removeNftBids = async (
112
112
  // });
113
113
  // }
114
114
 
115
- return Transaction.from(tx);
115
+ return tx instanceof Transaction ? tx : Transaction.from(tx);
116
116
  };
@@ -207,5 +207,5 @@ export const transferNfts = async (
207
207
  // });
208
208
  // }
209
209
 
210
- return Transaction.from(tx);
210
+ return tx instanceof Transaction ? tx : Transaction.from(tx);
211
211
  };
@@ -20,7 +20,6 @@ import {
20
20
  addTocenUnlistTxHandler,
21
21
  addTradePortUnlistTxHandler,
22
22
  } from './addUnlistListingTxs';
23
- import { normalizeSuiAddress } from '@mysten/sui/utils';
24
23
 
25
24
  export type UnlistListingTx = {
26
25
  tx: Transaction;
@@ -158,5 +157,5 @@ export const unlistListings = async (
158
157
  // });
159
158
  // }
160
159
 
161
- return Transaction.from(tx);
160
+ return tx instanceof Transaction ? tx : Transaction.from(tx);
162
161
  };
@@ -1,10 +1,15 @@
1
+ import { type SuiClient } from '@mysten/sui/client';
1
2
  import { type Transaction } from '@mysten/sui/transactions';
2
3
  import { TRADEPORT_MULTI_BID_PACKAGE, TRADEPORT_MULTI_BID_STORE } from '../../constants';
3
4
  import { gqlChainRequest } from '../../graphql/gqlChainRequest';
4
5
  import { fetchMultibidChainIdById } from '../../graphql/queries/fetchMultibidById';
5
6
  import { deserializeOrCreateTxBlock } from '../../helpers/deserializeOrCreateTxBlock';
6
- import { extractSwapResultCoinFromTxBlock } from '../../helpers/extractSwapResultCoin';
7
-
7
+ import {
8
+ handleSwapCleanup,
9
+ handleSwapSetup,
10
+ mergeSwapCoinWithSuiBalance,
11
+ type SwapAndPayOptions,
12
+ } from '../../helpers/swap/swap';
8
13
  export interface UpdateMultiBid {
9
14
  walletAddress: string;
10
15
  multiBidId?: string;
@@ -13,6 +18,8 @@ export interface UpdateMultiBid {
13
18
  amountToWithdraw?: bigint;
14
19
  tx?: Transaction | string;
15
20
  multiBidChainId?: any;
21
+ swapAndPayOptions?: SwapAndPayOptions;
22
+ suiClient?: SuiClient;
16
23
  }
17
24
 
18
25
  export async function updateMultiBid({
@@ -22,9 +29,23 @@ export async function updateMultiBid({
22
29
  amountToWithdraw,
23
30
  tx: existingTx,
24
31
  multiBidChainId,
32
+ swapAndPayOptions,
33
+ suiClient,
34
+ walletAddress,
25
35
  }: UpdateMultiBid): Promise<Transaction> {
26
- const tx = deserializeOrCreateTxBlock({ existingTx });
27
- const swapResultCoin = extractSwapResultCoinFromTxBlock(tx);
36
+ let tx = deserializeOrCreateTxBlock({ existingTx });
37
+
38
+ let swapResultCoin;
39
+ if (suiClient) {
40
+ const { transaction: updatedTx, swapResultCoin: coin } = await handleSwapSetup(
41
+ suiClient,
42
+ walletAddress,
43
+ tx,
44
+ swapAndPayOptions,
45
+ );
46
+ tx = updatedTx;
47
+ swapResultCoin = coin;
48
+ }
28
49
 
29
50
  if (!multiBidChainId) {
30
51
  const { chain_id: chainId, cancelled_at } =
@@ -46,6 +67,12 @@ export async function updateMultiBid({
46
67
  multiBidChainId = chainId;
47
68
  }
48
69
 
70
+ mergeSwapCoinWithSuiBalance(
71
+ tx,
72
+ swapResultCoin,
73
+ BigInt(swapAndPayOptions?.amountToSplitFromSuiBalance ?? 0n),
74
+ );
75
+
49
76
  const [coin] = tx.splitCoins(swapResultCoin ? swapResultCoin : tx.gas, [amount ?? 0n]);
50
77
  tx.moveCall({
51
78
  target: `${TRADEPORT_MULTI_BID_PACKAGE}::tradeport_biddings::update_multi_bid`,
@@ -58,5 +85,9 @@ export async function updateMultiBid({
58
85
  ],
59
86
  });
60
87
 
88
+ if (suiClient) {
89
+ handleSwapCleanup(tx, swapResultCoin, walletAddress);
90
+ }
91
+
61
92
  return tx;
62
93
  }
@@ -55,7 +55,7 @@ export async function withdrawProfitsFromKiosks(
55
55
  ).personalCap?.id;
56
56
 
57
57
  kioskTx = new KioskTransaction({
58
- transactionBlock: tx as any,
58
+ transaction: tx as any,
59
59
  kioskClient: context.kioskClient,
60
60
  cap: {
61
61
  isPersonal: true,
@@ -75,7 +75,7 @@ export async function withdrawProfitsFromKiosks(
75
75
  ).ownerCap?.id;
76
76
 
77
77
  kioskTx = new KioskTransaction({
78
- transactionBlock: tx as any,
78
+ transaction: tx as any,
79
79
  kioskClient: context.kioskClient,
80
80
  cap: {
81
81
  isPersonal: false,
@@ -96,5 +96,5 @@ export async function withdrawProfitsFromKiosks(
96
96
  console.log('err', err);
97
97
  }
98
98
 
99
- return Transaction.from(tx);
99
+ return tx instanceof Transaction ? tx : Transaction.from(tx);
100
100
  }
@@ -1,17 +0,0 @@
1
- import { type Transaction } from '@mysten/sui/transactions';
2
-
3
- export const extractSwapResultCoinFromTxBlock = (txBlock: Transaction | string) => {
4
- if (!txBlock) return undefined;
5
-
6
- const tx = typeof txBlock === 'string' ? JSON.parse(txBlock) : txBlock?.getData();
7
- const commands = tx.commands ?? [];
8
-
9
- const index = commands.findIndex(
10
- (cmd: any) =>
11
- cmd?.MoveCall?.module === 'universal_router' && cmd?.MoveCall?.function === 'settle',
12
- );
13
-
14
- if (index === -1) return undefined;
15
-
16
- return { $kind: 'Result', Result: index };
17
- };