@tradeport/sui-trading-sdk 0.4.2 → 0.4.4

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tradeport/sui-trading-sdk",
3
3
  "license": "MIT",
4
- "version": "0.4.2",
4
+ "version": "0.4.4",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
@@ -21,3 +21,25 @@ export const fetchNftsByKioskId = gql`
21
21
  }
22
22
  }
23
23
  `;
24
+
25
+ export const fetchBulkNftsByKioskId = gql`
26
+ query fetchNftsByKioskId($where: nfts_bool_exp) {
27
+ nfts(where: $where) {
28
+ id
29
+ name
30
+ token_id
31
+ properties
32
+ chain_state
33
+ collection_id
34
+ claimable_reason
35
+ owner
36
+ contract {
37
+ properties
38
+ }
39
+ collection {
40
+ id
41
+ chain_state
42
+ }
43
+ }
44
+ }
45
+ `;
@@ -8,7 +8,7 @@ type Args = {
8
8
  export const destroyZeroCoin = ({ tx, coin }: Args) => {
9
9
  tx.moveCall({
10
10
  target: '0x2::coin::destroy_zero',
11
- arguments: [tx.object(coin)],
11
+ arguments: [tx.object(coin as any)],
12
12
  typeArguments: ['0x2::sui::SUI'],
13
13
  });
14
14
  };
@@ -114,10 +114,7 @@ export const kioskTxWrapper = async ({
114
114
  version: '',
115
115
  },
116
116
  });
117
- } else if (shouldConvertToPersonalKiosk || kiosks.length === 0) {
118
- kioskTx = new KioskTransaction({ transactionBlock: tx as any, kioskClient });
119
- kioskTx.createPersonal(true);
120
- } else {
117
+ } else if (kiosks.length > 0 && !shouldConvertToPersonalKiosk) {
121
118
  const kioskCapId = (
122
119
  await gqlChainRequest({
123
120
  chain: 'sui',
@@ -137,9 +134,11 @@ export const kioskTxWrapper = async ({
137
134
  version: '',
138
135
  },
139
136
  });
137
+ } else {
138
+ // creating new personal kiosk
139
+ kioskTx = new KioskTransaction({ transactionBlock: tx as any, kioskClient });
140
+ kioskTx.createPersonal(true);
140
141
  }
141
142
 
142
143
  await runCommands(kioskTx);
143
-
144
- kioskTx.finalize();
145
144
  };
@@ -193,7 +193,7 @@ export const resolveTransferPolicies = async ({
193
193
  packageId: rulePackageId,
194
194
  nftType: policy.isCustom && policy.type ? policy.type : nftType,
195
195
  policyId: policy.data.id,
196
- transferRequest: policy.transferRequest,
196
+ transferRequest: policy.transferRequest as any,
197
197
  });
198
198
  break;
199
199
  default:
@@ -1,5 +1,6 @@
1
1
  import { type SuiClient } from '@mysten/sui/client';
2
2
  import { isValidSuiObjectId } from '@mysten/sui/utils';
3
+ import { addLeadingZerosAfter0x } from '../../utils/addLeadingZerosAfter0x';
3
4
 
4
5
  export const getObjectType = async ({
5
6
  suiClient,
@@ -8,8 +9,11 @@ export const getObjectType = async ({
8
9
  suiClient: SuiClient;
9
10
  objectId: string;
10
11
  }): Promise<string> => {
11
- if (isValidSuiObjectId(objectId)) {
12
- const res = await suiClient.getObject({ id: objectId, options: { showType: true } });
12
+ const normalizedObjectId = objectId.startsWith('0x')
13
+ ? addLeadingZerosAfter0x(objectId)
14
+ : objectId;
15
+ if (isValidSuiObjectId(normalizedObjectId)) {
16
+ const res = await suiClient.getObject({ id: normalizedObjectId, options: { showType: true } });
13
17
  return res.data.type;
14
18
  }
15
19
 
@@ -165,8 +165,10 @@ export const addClaimTransferredNftWithPurchaseCapTx = async ({
165
165
  nftTokenId,
166
166
  nftType,
167
167
  sellerKiosk,
168
+ preloadedTransferPolicyId,
168
169
  }: ClaimTransferredNftTx) => {
169
- const transferPolicyId = (await getKioskTransferPolicies(nftType))?.at(0)?.id;
170
+ const transferPolicyId =
171
+ preloadedTransferPolicyId ?? (await getKioskTransferPolicies(nftType))?.at(0)?.id;
170
172
 
171
173
  const [transferRequest] = tx.moveCall({
172
174
  target:
@@ -174,8 +176,8 @@ export const addClaimTransferredNftWithPurchaseCapTx = async ({
174
176
  arguments: [
175
177
  tx.object(TRADEPORT_KIOSK_TRANSFERS_STORE),
176
178
  tx.object(sellerKiosk),
177
- tx.object(kioskTx.kiosk.value ?? kioskTx.kiosk),
178
- tx.object(kioskTx.kioskCap.value ?? kioskTx.kioskCap),
179
+ kioskTx.kiosk,
180
+ kioskTx.kioskCap,
179
181
  tx.pure.address(nftTokenId),
180
182
  tx.object(transferPolicyId),
181
183
  ],
@@ -44,6 +44,7 @@ export type ClaimTransferredNftTx = {
44
44
  nftType: string;
45
45
  sellerKiosk: string;
46
46
  seller: string;
47
+ preloadedTransferPolicyId?: string;
47
48
  };
48
49
 
49
50
  export type ClaimAcceptedBidNftTx = {
@@ -89,7 +90,9 @@ export const claimNfts = async (
89
90
  });
90
91
  const walletAddressKiosks = kiosksByOwnerRes?.kiosks;
91
92
 
93
+ let globalKioskTx: any;
92
94
  for (const nft of res.nfts) {
95
+ let existingKioskTx;
93
96
  if (DELOREAN_TOKEN_IDS_TO_DISABLE?.includes(nft?.token_id)) {
94
97
  throw new Error(DELOREAN_TOKEN_IDS_TO_DISABLE_MESSAGE);
95
98
  }
@@ -150,9 +153,11 @@ export const claimNfts = async (
150
153
  kioskOwner: txData?.claimer,
151
154
  kioskOwnerKiosks: walletAddressKiosks,
152
155
  kiosk: txData?.sellerKiosk,
156
+ kioskTx: globalKioskTx,
153
157
  shouldConvertToPersonalKiosk: true,
154
158
  shouldAllowNftUnsharedKiosk: true,
155
159
  async runCommands(kioskTx) {
160
+ existingKioskTx = kioskTx;
156
161
  await addBluemoveClaimAcceptedBidNft({
157
162
  ...txData,
158
163
  suiClient: context.suiClient,
@@ -171,9 +176,11 @@ export const claimNfts = async (
171
176
  kioskOwner: txData?.claimer,
172
177
  kioskOwnerKiosks: walletAddressKiosks,
173
178
  kiosk: txData?.sellerKiosk,
179
+ kioskTx: globalKioskTx,
174
180
  shouldConvertToPersonalKiosk: true,
175
181
  shouldAllowNftUnsharedKiosk: true,
176
182
  async runCommands(kioskTx) {
183
+ existingKioskTx = kioskTx;
177
184
  await addClaimAcceptedBidNftTx({ ...txData, kioskTx });
178
185
  },
179
186
  });
@@ -184,9 +191,11 @@ export const claimNfts = async (
184
191
  kioskOwner: txData?.claimer,
185
192
  kioskOwnerKiosks: walletAddressKiosks,
186
193
  kiosk: txData?.sellerKiosk,
194
+ kioskTx: globalKioskTx,
187
195
  shouldConvertToPersonalKiosk: true,
188
196
  shouldAssertNftInSharedKiosk: true,
189
197
  async runCommands(kioskTx) {
198
+ existingKioskTx = kioskTx;
190
199
  await addClaimAcceptedBidNftWithPurchaseCapTx({ ...txData, kioskTx });
191
200
  },
192
201
  });
@@ -216,9 +225,11 @@ export const claimNfts = async (
216
225
  kiosk: txData?.sellerKiosk,
217
226
  kioskOwnerKiosks: walletAddressKiosks,
218
227
  kioskStrategy: 'exclude',
228
+ kioskTx: globalKioskTx,
219
229
  shouldConvertToPersonalKiosk: true,
220
230
  shouldAllowNftUnsharedKiosk: true,
221
231
  async runCommands(kioskTx) {
232
+ existingKioskTx = kioskTx;
222
233
  await addClaimTransferredNftTx({ ...txData, kioskTx });
223
234
  },
224
235
  });
@@ -230,9 +241,11 @@ export const claimNfts = async (
230
241
  kiosk: txData?.sellerKiosk,
231
242
  kioskOwnerKiosks: walletAddressKiosks,
232
243
  kioskStrategy: 'exclude',
244
+ kioskTx: globalKioskTx,
233
245
  shouldConvertToPersonalKiosk: true,
234
246
  shouldAllowNftUnsharedKiosk: true,
235
247
  async runCommands(kioskTx) {
248
+ existingKioskTx = kioskTx;
236
249
  await addClaimTransferredNftWithPurchaseCapTx({ ...txData, kioskTx });
237
250
  },
238
251
  });
@@ -244,6 +257,7 @@ export const claimNfts = async (
244
257
  // claimer: walletAddress,
245
258
  // claimReason: nft?.chain_state?.claimable_reason,
246
259
  // });
260
+ globalKioskTx = existingKioskTx;
247
261
  }
248
262
 
249
263
  // if (process.env.ENABLE_SEGMENT_TRACKING === 'true' && nftsForTracking.length > 0) {
@@ -255,5 +269,6 @@ export const claimNfts = async (
255
269
  // });
256
270
  // }
257
271
 
272
+ globalKioskTx?.finalize();
258
273
  return Transaction.from(tx);
259
274
  };
@@ -2,8 +2,12 @@ import { Transaction } from '@mysten/sui/transactions';
2
2
  import { type RequestContext } from '../../SuiTradingClient';
3
3
  import { gqlChainRequest } from '../../graphql/gqlChainRequest';
4
4
  import { fetchKiosksByOwner } from '../../graphql/queries/fetchKiosksByOwner';
5
- import { fetchNftsByKioskId } from '../../graphql/queries/fetchNftsByKioskId';
5
+ import {
6
+ fetchBulkNftsByKioskId,
7
+ fetchNftsByKioskId,
8
+ } from '../../graphql/queries/fetchNftsByKioskId';
6
9
  import { getNftType } from '../../helpers/getNftType';
10
+ import { getKioskTransferPolicies } from '../../helpers/kiosk/getKioskTransferPolicies';
7
11
  import { kioskTxWrapper } from '../../helpers/kiosk/kioskTxWrapper';
8
12
  import { addLeadingZerosAfter0x } from '../../utils/addLeadingZerosAfter0x';
9
13
  import { addClaimTransferredNftWithPurchaseCapTx } from '../claimNfts/addClaimNftsTxs';
@@ -15,7 +19,7 @@ export type MigrateNftsFromUnsharedToSharedKiosks = {
15
19
  };
16
20
 
17
21
  export async function migrateNftsFromUnsharedToSharedKiosks(
18
- { walletAddress, max = 5 }: MigrateNftsFromUnsharedToSharedKiosks,
22
+ { walletAddress, max = 50 }: MigrateNftsFromUnsharedToSharedKiosks,
19
23
  context: RequestContext,
20
24
  ): Promise<Transaction> {
21
25
  const tx = new Transaction();
@@ -57,11 +61,6 @@ export async function migrateNftsFromUnsharedToSharedKiosks(
57
61
  continue;
58
62
  }
59
63
 
60
- // if (nft?.collection_id === '67ded9ed-4dcc-4d15-9e6f-04dded46f419') {
61
- // // Novagen, skip until we know what to do with them
62
- // continue;
63
- // }
64
-
65
64
  const nftType = getNftType({
66
65
  collectionId: nft?.collection?.id,
67
66
  collectionChainState: nft?.collection?.chain_state,
@@ -100,24 +99,59 @@ export async function migrateNftsFromUnsharedToSharedKiosks(
100
99
  ?.filter((k: any) => !k.is_shared && !k.is_origin_byte)
101
100
  ?.map((k: any) => k.id);
102
101
 
103
- for (const unsharedNativeKiosk of unsharedNativeKiosks) {
104
- if (currentMigrationCount >= max) {
105
- continue;
102
+ const allNfts = await gqlChainRequest({
103
+ chain: 'sui',
104
+ query: fetchBulkNftsByKioskId,
105
+ variables: {
106
+ where: {
107
+ _or: unsharedNativeKiosks.map((nk: any) => ({
108
+ chain_state: { _contains: { kiosk_id: nk } },
109
+ })),
110
+ },
111
+ },
112
+ });
113
+
114
+ const formatted: any = {};
115
+ allNfts.nfts.forEach((e: any) => {
116
+ if (formatted[e.chain_state.kiosk_id]) {
117
+ formatted[e.chain_state.kiosk_id]?.push(e);
118
+ } else {
119
+ formatted[e.chain_state.kiosk_id] = [e];
106
120
  }
121
+ });
107
122
 
108
- const res = await gqlChainRequest({
109
- chain: 'sui',
110
- query: fetchNftsByKioskId,
111
- variables: { jsonFilter: { kiosk_id: unsharedNativeKiosk } },
123
+ const allNftTypes = allNfts.nfts.reduce((acc: any[], curr: any) => {
124
+ const type = getNftType({
125
+ collectionId: curr?.collection?.id,
126
+ collectionChainState: curr?.collection?.chain_state,
127
+ nft: curr,
112
128
  });
129
+ if (!acc.includes(type)) acc.push(type);
130
+ return acc;
131
+ }, []);
113
132
 
114
- const nfts = res?.nfts;
133
+ const transferPolicies = await Promise.all(
134
+ allNftTypes.map(async (type: string) => getKioskTransferPolicies(type)),
135
+ );
115
136
 
137
+ const mappedTrasferPolicies: Record<string, any> = {};
138
+ transferPolicies.forEach((tp: any) => {
139
+ mappedTrasferPolicies[tp.transfer_policies_by_type?.[0]?.type] = tp.transfer_policies_by_type;
140
+ });
141
+
142
+ let globalKioskTx: any;
143
+ for (const unsharedNativeKiosk of unsharedNativeKiosks) {
144
+ if (currentMigrationCount >= max) {
145
+ continue;
146
+ }
147
+
148
+ const nfts = formatted[unsharedNativeKiosk] ?? [];
116
149
  if (nfts?.length > 0) {
117
150
  currentMigrationCount++;
118
151
  }
119
152
 
120
153
  for (const nft of nfts) {
154
+ let existingKioskTx;
121
155
  if (nft?.claimable_reason === 'offer-transfer') {
122
156
  // native kiosk kiosk_transfers transfer hold. Transfer needs to be cancelled first in order to be migrated
123
157
  continue;
@@ -128,9 +162,7 @@ export async function migrateNftsFromUnsharedToSharedKiosks(
128
162
  collectionChainState: nft?.collection?.chain_state,
129
163
  nft,
130
164
  });
131
-
132
165
  const nftTokenId = nft?.token_id;
133
-
134
166
  await kioskTxWrapper({
135
167
  tx,
136
168
  kioskClient: context?.kioskClient,
@@ -147,18 +179,20 @@ export async function migrateNftsFromUnsharedToSharedKiosks(
147
179
  recipientAddress: walletAddress,
148
180
  nftTokenId,
149
181
  nftType,
182
+ transferPolicy: mappedTrasferPolicies[nftType]?.[0],
150
183
  });
151
184
  },
152
185
  });
153
-
154
186
  await kioskTxWrapper({
155
187
  tx,
156
188
  kioskClient: context.kioskClient,
157
189
  kioskOwner: walletAddress,
158
190
  kiosk: unsharedNativeKiosk,
159
191
  kioskStrategy: 'exclude',
192
+ kioskTx: globalKioskTx,
160
193
  shouldConvertToPersonalKiosk: true,
161
194
  async runCommands(kioskTx) {
195
+ existingKioskTx = kioskTx;
162
196
  await addClaimTransferredNftWithPurchaseCapTx({
163
197
  tx,
164
198
  kioskTx,
@@ -170,11 +204,15 @@ export async function migrateNftsFromUnsharedToSharedKiosks(
170
204
  nftType,
171
205
  sellerKiosk: unsharedNativeKiosk,
172
206
  seller: nft?.owner,
207
+ preloadedTransferPolicyId: mappedTrasferPolicies[nftType]?.[0]?.id,
173
208
  });
174
209
  },
175
210
  });
211
+
212
+ globalKioskTx = existingKioskTx;
176
213
  }
177
214
  }
178
215
 
216
+ globalKioskTx?.finalize();
179
217
  return Transaction.from(tx);
180
218
  }
@@ -1,3 +1,4 @@
1
+ import { type KioskClient } from '@mysten/kiosk';
1
2
  import { type Transaction } from '@mysten/sui/dist/cjs/transactions';
2
3
  import { TRADEPORT_KIOSK_TRANSFERS_STORE } from '../../constants';
3
4
  import { destroyZeroCoin } from '../../helpers/destroyZeroCoin';
@@ -8,7 +9,6 @@ import { kioskTxWrapper } from '../../helpers/kiosk/kioskTxWrapper';
8
9
  import { getOBKiosk } from '../../helpers/originByte/getOBKiosk';
9
10
  import { addLeadingZerosAfter0x } from '../../utils/addLeadingZerosAfter0x';
10
11
  import { type TransferNftTx } from './transferNfts';
11
- import { type KioskClient } from '@mysten/kiosk';
12
12
 
13
13
  type CollectionChainState = {
14
14
  transfer_policies: TransferPolicy[];
@@ -61,9 +61,10 @@ export async function addTradeportKioskTransferTx({
61
61
  nftTokenId,
62
62
  nftType,
63
63
  recipientAddress,
64
+ transferPolicy,
64
65
  }: TransferNftTx) {
65
- const transferPolicy = (await getKioskTransferPolicies(nftType))?.at(0);
66
- const hasFloorPriceRule = transferPolicy?.rules?.some(
66
+ const existingTransferPolicy = transferPolicy ?? (await getKioskTransferPolicies(nftType))?.at(0);
67
+ const hasFloorPriceRule = existingTransferPolicy?.rules?.some(
67
68
  (rule: any) => rule.type === 'floor_price_rule',
68
69
  );
69
70
  if (hasFloorPriceRule) {
@@ -30,6 +30,7 @@ export type TransferNftTx = {
30
30
  recipientAddress: string;
31
31
  sharedObjects?: any;
32
32
  kioskTx?: any;
33
+ transferPolicy?: any;
33
34
  };
34
35
 
35
36
  export type TransferNfts = {