@zoralabs/protocol-sdk 0.5.7 → 0.5.8-DEV.2

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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mints-contracts.d.ts","sourceRoot":"","sources":["../../src/mints/mints-contracts.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,0BAA0B,EAC3B,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,6BAA6B,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAC5E,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC5E,OAAO,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EACL,OAAO,EACP,OAAO,EACP,GAAG,EACH,sBAAsB,EACtB,uBAAuB,EACvB,0BAA0B,EAI3B,MAAM,MAAM,CAAC;AAEd,wBAAgB,iBAAiB,CAAC,EAChC,QAAQ,EACR,SAAS,EACT,OAAO,EACP,YAAY,EACZ,OAAO,GACR,EAAE;IACD,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,EAAE,OAAO,CAAC;IACnB,OAAO,EAAE,MAAM,OAAO,mBAAmB,CAAC,OAAO,CAAC;IAClD,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC;CAC5B,GAAG,0BAA0B,CAC5B,OAAO,0BAA0B,CAAC,GAAG,EACrC,aAAa,CACd,CASA;AAQD,eAAO,MAAM,2BAA2B;aAI7B,OAAO;aACP,MAAM,OAAO,oBAAoB,OAAO;MAC/C,uBACF,OAAO,oBAAoB,GAAG,EAC9B,kBAAkB,CAMlB,CAAC;AAEH,eAAO,MAAM,kBAAkB;aAIpB,MAAM,OAAO,oBAAoB,OAAO;aACxC,OAAO;MACd,uBAAuB,OAAO,oBAAoB,GAAG,EAAE,QAAQ,CAKjE,CAAC;AAEH,KAAK,sBAAsB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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;0CAaf,CAAC"}
@@ -0,0 +1,59 @@
1
+ import { Address } from "viem";
2
+ type CollectQueryResult = {
3
+ tokenIds: bigint[];
4
+ quantities: bigint[];
5
+ };
6
+ export declare const getMintsAccountBalanceWithPriceQuery: (account: Address) => {
7
+ query: string;
8
+ variables: {
9
+ account: `0x${string}`;
10
+ };
11
+ };
12
+ export type MintAccountBalance = {
13
+ balance: string;
14
+ mintToken: {
15
+ id: string;
16
+ pricePerToken: string;
17
+ };
18
+ };
19
+ export type MintAccountBalancesQueryResult = {
20
+ mintTokenBalances: MintAccountBalance[];
21
+ };
22
+ /**
23
+ * Given the result of a mint token balances query, selects the best MINTs to use to collect with that will satisfy the quantity,
24
+ * by selecting the lowest priced MINTs first. Throws an error if not enough mints to collect with.
25
+ */
26
+ export declare const selectMintsToCollectWithFromQueryResult: (mintAccountBalances: MintAccountBalance[], quantityToCollect: bigint) => {
27
+ tokenIds: bigint[];
28
+ quantities: bigint[];
29
+ };
30
+ /**
31
+ * Given an array of mint account balances, sums the balances.
32
+ * @param mintAccountBalances
33
+ * @returns Total balance
34
+ */
35
+ export declare const sumBalances: (mintAccountBalances: Pick<MintAccountBalance, "balance">[]) => bigint;
36
+ /***
37
+ * Given an account and quantity of MINTs to use to collect with, queries for MINTs
38
+ * owned by an account, and selects the best MINTs to use to collect with that will satisfy that quantity.
39
+ * @param account Account to query for MINTs
40
+ * @param chainId
41
+ * @param quantityToCollect How many MINTs to use to collect with
42
+ * @returns
43
+ */
44
+ export declare const getMINTsToCollectWith: ({ account, chainId, quantityToCollect, }: {
45
+ account: Address;
46
+ chainId: number;
47
+ quantityToCollect: bigint;
48
+ }) => Promise<CollectQueryResult>;
49
+ /***
50
+ * Given an account, queries for MINTs owned by an account, and sums the balances.
51
+ * @param account Account to query for MINTs
52
+ * @returns Total MINTs balance of account
53
+ */
54
+ export declare const getMINTsBalance: ({ chainId, account, }: {
55
+ account: Address;
56
+ chainId: number;
57
+ }) => Promise<bigint>;
58
+ export {};
59
+ //# sourceMappingURL=mints-queries.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mints-queries.d.ts","sourceRoot":"","sources":["../../src/mints/mints-queries.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAG/B,KAAK,kBAAkB,GAAG;IACxB,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,UAAU,EAAE,MAAM,EAAE,CAAC;CACtB,CAAC;AAEF,eAAO,MAAM,oCAAoC,YAAa,OAAO;;;;;CAiBpE,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE;QACT,EAAE,EAAE,MAAM,CAAC;QACX,aAAa,EAAE,MAAM,CAAC;KACvB,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,8BAA8B,GAAG;IAC3C,iBAAiB,EAAE,kBAAkB,EAAE,CAAC;CACzC,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,uCAAuC,wBAC7B,kBAAkB,EAAE,qBACtB,MAAM;;;CAyC1B,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,WAAW,wBACD,KAAK,kBAAkB,EAAE,SAAS,CAAC,EAAE,WAK3D,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,qBAAqB;aAKvB,OAAO;aACP,MAAM;uBACI,MAAM;MACvB,QAAQ,kBAAkB,CAe7B,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,eAAe;aAIjB,OAAO;aACP,MAAM;qBAahB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zoralabs/protocol-sdk",
3
- "version": "0.5.7",
3
+ "version": "0.5.8-DEV.2",
4
4
  "repository": "https://github.com/ourzora/zora-protocol",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -25,18 +25,19 @@
25
25
  "lint": "prettier --check 'src/**/*.ts' 'test-integration/**/*.ts'"
26
26
  },
27
27
  "dependencies": {
28
- "@zoralabs/protocol-deployments": "*",
29
- "abitype": "^0.10.3",
30
- "vite": "4.5.0"
28
+ "@zoralabs/protocol-deployments": "0.1.5-DEV.4",
29
+ "abitype": "^0.10.3"
31
30
  },
32
31
  "peerDependencies": {
33
- "viem": "^1.19.15"
32
+ "viem": "^1.19.15",
33
+ "graphql-request": "^6.1.0"
34
34
  },
35
35
  "devDependencies": {
36
36
  "@lavamoat/preinstall-always-fail": "2.0.0",
37
- "zoralabs-tsconfig": "*",
37
+ "graphql-prettier": "^1.0.6",
38
38
  "typescript": "^5.2.2",
39
39
  "vite": "^4.5.0",
40
- "vitest": "^0.34.6"
40
+ "vitest": "^0.34.6",
41
+ "zoralabs-tsconfig": "*"
41
42
  }
42
43
  }
@@ -82,3 +82,13 @@ export const networkConfigByChain: Record<number, NetworkConfig> = {
82
82
  subgraphUrl: getSubgraph("zora-create-zora-testnet", "stable"),
83
83
  },
84
84
  };
85
+
86
+ export const getSubgraphUrl = (chainId: number): string => {
87
+ const networkConfig = networkConfigByChain[chainId];
88
+
89
+ if (!networkConfig) {
90
+ throw new Error(`Network not configured for chain id ${chainId}`);
91
+ }
92
+
93
+ return networkConfig.subgraphUrl;
94
+ };
package/src/index.ts CHANGED
@@ -13,3 +13,7 @@ export * from "./mint/mint-api-client";
13
13
  export * from "./mint/mint-client";
14
14
 
15
15
  export * from "./create/1155-create-helper";
16
+
17
+ export * from "./mints/mints-queries";
18
+
19
+ export * from "./mints/mints-contracts";
@@ -0,0 +1,408 @@
1
+ import {
2
+ zoraMints1155Config,
3
+ zoraMintsManagerImplConfig,
4
+ } from "@zoralabs/protocol-deployments";
5
+ import { AbiParametersToPrimitiveTypes, ExtractAbiFunction } from "abitype";
6
+ import { MintArguments, PremintConfigV2 } from "src/premint/contract-types";
7
+ import { ContractCreationConfig } from "src/preminter";
8
+ import {
9
+ Account,
10
+ Address,
11
+ Hex,
12
+ ReadContractParameters,
13
+ SignTypedDataParameters,
14
+ SimulateContractParameters,
15
+ TypedData,
16
+ encodeFunctionData,
17
+ zeroAddress,
18
+ } from "viem";
19
+
20
+ export function mintWithEthParams({
21
+ quantity,
22
+ recipient,
23
+ chainId,
24
+ pricePerMint,
25
+ account,
26
+ }: {
27
+ quantity: bigint;
28
+ recipient: Address;
29
+ chainId: keyof typeof zoraMints1155Config.address;
30
+ pricePerMint: bigint;
31
+ account: Address | Account;
32
+ }): SimulateContractParameters<
33
+ typeof zoraMintsManagerImplConfig.abi,
34
+ "mintWithEth"
35
+ > {
36
+ return {
37
+ abi: zoraMintsManagerImplConfig.abi,
38
+ address: zoraMintsManagerImplConfig.address[chainId],
39
+ functionName: "mintWithEth",
40
+ args: [quantity, recipient],
41
+ value: pricePerMint * quantity,
42
+ account,
43
+ };
44
+ }
45
+
46
+ const getPaidMintValue = (quantities: bigint[], pricePerMint?: bigint) => {
47
+ if (!pricePerMint || pricePerMint === 0n) return;
48
+
49
+ return quantities.reduce((a, b) => a + b, 0n) * pricePerMint;
50
+ };
51
+
52
+ export const mintsBalanceOfAccountParams = ({
53
+ account,
54
+ chainId,
55
+ }: {
56
+ account: Address;
57
+ chainId: keyof typeof zoraMints1155Config.address;
58
+ }): ReadContractParameters<
59
+ typeof zoraMints1155Config.abi,
60
+ "balanceOfAccount"
61
+ > => ({
62
+ abi: zoraMints1155Config.abi,
63
+ address: zoraMints1155Config.address[chainId],
64
+ functionName: "balanceOfAccount",
65
+ args: [account],
66
+ });
67
+
68
+ export const accountNonceParams = ({
69
+ chainId,
70
+ account,
71
+ }: {
72
+ chainId: keyof typeof zoraMints1155Config.address;
73
+ account: Address;
74
+ }): ReadContractParameters<typeof zoraMints1155Config.abi, "nonces"> => ({
75
+ abi: zoraMints1155Config.abi,
76
+ address: zoraMints1155Config.address[chainId],
77
+ functionName: "nonces",
78
+ args: [account],
79
+ });
80
+
81
+ type CollectOnManagerParams = {
82
+ tokenIds: bigint[];
83
+ quantities: bigint[];
84
+ zoraCreator1155Contract: Address;
85
+ zoraCreator1155TokenId: bigint;
86
+ minter: Address;
87
+ mintArguments: MintArguments;
88
+ };
89
+
90
+ export const encodeCollectOnManager = ({
91
+ tokenIds,
92
+ quantities,
93
+ zoraCreator1155Contract,
94
+ minter,
95
+ zoraCreator1155TokenId,
96
+ mintArguments,
97
+ }: CollectOnManagerParams) =>
98
+ encodeFunctionData({
99
+ abi: zoraMintsManagerImplConfig.abi,
100
+ functionName: "collect",
101
+ args: [
102
+ tokenIds,
103
+ quantities,
104
+ zoraCreator1155Contract,
105
+ minter,
106
+ zoraCreator1155TokenId,
107
+ mintArguments,
108
+ ],
109
+ });
110
+
111
+ export function collectWithMintsParams({
112
+ tokenIds,
113
+ quantities,
114
+ chainId,
115
+ pricePerToken,
116
+ account,
117
+ ...rest
118
+ }: {
119
+ paidMintValue?: bigint;
120
+ chainId: keyof typeof zoraMints1155Config.address;
121
+ pricePerToken?: bigint;
122
+ account: Address | Account;
123
+ } & CollectOnManagerParams): SimulateContractParameters<
124
+ typeof zoraMints1155Config.abi,
125
+ "transferBatchToManagerAndCall"
126
+ > {
127
+ const call = encodeCollectOnManager({
128
+ tokenIds,
129
+ quantities,
130
+ ...rest,
131
+ });
132
+
133
+ return {
134
+ abi: zoraMints1155Config.abi,
135
+ address: zoraMints1155Config.address[chainId],
136
+ functionName: "transferBatchToManagerAndCall",
137
+ args: [tokenIds, quantities, "0x", call],
138
+ value: getPaidMintValue(quantities, pricePerToken),
139
+ account,
140
+ };
141
+ }
142
+
143
+ export const collectWithMintsTypedDataDefinition = ({
144
+ tokenIds,
145
+ quantities,
146
+ chainId,
147
+ account,
148
+ nonce,
149
+ deadline,
150
+ ...rest
151
+ }: {
152
+ chainId: keyof typeof zoraMints1155Config.address;
153
+ nonce: bigint;
154
+ deadline: bigint;
155
+ account: Account | Address;
156
+ } & CollectOnManagerParams) => {
157
+ const call = encodeCollectOnManager({
158
+ tokenIds,
159
+ quantities,
160
+ ...rest,
161
+ });
162
+
163
+ return makePermitAndTypeData({
164
+ tokenIds,
165
+ quantities,
166
+ chainId,
167
+ account,
168
+ nonce,
169
+ deadline,
170
+ call,
171
+ });
172
+ };
173
+
174
+ export const collectPremintWithMintsTypedDataDefinition = ({
175
+ tokenIds,
176
+ quantities,
177
+ chainId,
178
+ account,
179
+ nonce,
180
+ deadline,
181
+ ...rest
182
+ }: {
183
+ chainId: keyof typeof zoraMints1155Config.address;
184
+ nonce: bigint;
185
+ deadline: bigint;
186
+ account: Account | Address;
187
+ } & PremintOnManagerParams) => {
188
+ const call = encodePremintOnManager({
189
+ tokenIds,
190
+ quantities,
191
+ ...rest,
192
+ });
193
+
194
+ return makePermitAndTypeData({
195
+ tokenIds,
196
+ quantities,
197
+ chainId,
198
+ account,
199
+ nonce,
200
+ deadline,
201
+ call,
202
+ });
203
+ };
204
+
205
+ function makePermitAndTypeData({
206
+ tokenIds,
207
+ quantities,
208
+ chainId,
209
+ account,
210
+ nonce,
211
+ deadline,
212
+ call,
213
+ }: {
214
+ tokenIds: bigint[];
215
+ quantities: bigint[];
216
+ chainId: keyof typeof zoraMints1155Config.address;
217
+ nonce: bigint;
218
+ deadline: bigint;
219
+ account: Account | Address;
220
+ call: Hex;
221
+ }) {
222
+ const permit: PermitTransferBatchToManager = {
223
+ owner: typeof account === "string" ? account : account.address,
224
+ tokenIds,
225
+ quantities,
226
+ call,
227
+ deadline,
228
+ safeTransferData: "0x",
229
+ };
230
+
231
+ const typedData = permitTypedDataDefinition({
232
+ chainId,
233
+ nonce,
234
+ permit,
235
+ account,
236
+ });
237
+
238
+ return {
239
+ permit,
240
+ typedData,
241
+ };
242
+ }
243
+
244
+ type PremintOnManagerParams = {
245
+ tokenIds: bigint[];
246
+ quantities: bigint[];
247
+ contractCreationConfig: ContractCreationConfig;
248
+ premintConfig: PremintConfigV2;
249
+ premintSignature: Hex;
250
+ mintArguments: MintArguments;
251
+ signerContract?: Address;
252
+ };
253
+
254
+ export const encodePremintOnManager = ({
255
+ tokenIds,
256
+ quantities,
257
+ contractCreationConfig,
258
+ premintConfig,
259
+ premintSignature,
260
+ mintArguments,
261
+ signerContract = zeroAddress,
262
+ }: PremintOnManagerParams) =>
263
+ encodeFunctionData({
264
+ abi: zoraMintsManagerImplConfig.abi,
265
+ functionName: "collectPremintV2",
266
+ args: [
267
+ tokenIds,
268
+ quantities,
269
+ contractCreationConfig,
270
+ premintConfig,
271
+ premintSignature,
272
+ mintArguments,
273
+ signerContract,
274
+ ],
275
+ });
276
+
277
+ export function collectPremintV2WithMintsParams({
278
+ tokenIds,
279
+ quantities,
280
+ pricePerToken,
281
+ account,
282
+ chainId,
283
+ ...rest
284
+ }: {
285
+ pricePerToken?: bigint;
286
+ chainId: keyof typeof zoraMints1155Config.address;
287
+ account: Address | Account;
288
+ } & PremintOnManagerParams): SimulateContractParameters<
289
+ typeof zoraMints1155Config.abi,
290
+ "transferBatchToManagerAndCall"
291
+ > {
292
+ const call = encodePremintOnManager({
293
+ tokenIds,
294
+ quantities,
295
+ ...rest,
296
+ });
297
+
298
+ return {
299
+ abi: zoraMints1155Config.abi,
300
+ address: zoraMints1155Config.address[chainId],
301
+ functionName: "transferBatchToManagerAndCall",
302
+ args: [tokenIds, quantities, "0x", call],
303
+ value: getPaidMintValue(quantities, pricePerToken),
304
+ account,
305
+ };
306
+ }
307
+
308
+ export type PermitTransferBatchToManager = AbiParametersToPrimitiveTypes<
309
+ ExtractAbiFunction<
310
+ typeof zoraMints1155Config.abi,
311
+ "permitTransferBatchToManagerAndCall"
312
+ >["inputs"]
313
+ >[0];
314
+ // export type PermitTransferBatchToManager = ExtractAbiFunction<
315
+
316
+ function makeTypeData<
317
+ const TTypedData extends TypedData | { [key: string]: unknown },
318
+ TPrimaryType extends string,
319
+ >(args: SignTypedDataParameters<TTypedData, TPrimaryType>) {
320
+ return args;
321
+ }
322
+
323
+ export function permitTypedDataDefinition({
324
+ permit,
325
+ chainId,
326
+ nonce,
327
+ account,
328
+ }: {
329
+ permit: PermitTransferBatchToManager;
330
+ chainId: keyof typeof zoraMints1155Config.address;
331
+ nonce: bigint;
332
+ account: Account | Address;
333
+ }) {
334
+ return makeTypeData({
335
+ primaryType: "Permit",
336
+ types: {
337
+ Permit: [
338
+ {
339
+ name: "owner",
340
+ type: "address",
341
+ },
342
+ {
343
+ name: "tokenIds",
344
+ type: "uint256[]",
345
+ },
346
+ {
347
+ name: "quantities",
348
+ type: "uint256[]",
349
+ },
350
+ {
351
+ name: "safeTransferData",
352
+ type: "bytes",
353
+ },
354
+ {
355
+ name: "call",
356
+ type: "bytes",
357
+ },
358
+ {
359
+ name: "nonce",
360
+ type: "uint256",
361
+ },
362
+ {
363
+ name: "deadline",
364
+ type: "uint256",
365
+ },
366
+ ],
367
+ },
368
+ message: {
369
+ owner: permit.owner,
370
+ tokenIds: permit.tokenIds,
371
+ quantities: permit.quantities,
372
+ safeTransferData: permit.safeTransferData,
373
+ call: permit.call,
374
+ nonce,
375
+ deadline: permit.deadline,
376
+ },
377
+ domain: {
378
+ chainId,
379
+ name: "Mints",
380
+ version: "1",
381
+ verifyingContract: zoraMints1155Config.address[chainId],
382
+ },
383
+ // signing account must be permit owner
384
+ account,
385
+ });
386
+ }
387
+
388
+ export const permitTransferBatchToManagerAndCallParams = ({
389
+ permit,
390
+ chainId,
391
+ signature,
392
+ }: {
393
+ permit: PermitTransferBatchToManager;
394
+ chainId: keyof typeof zoraMints1155Config.address;
395
+ signature: Hex;
396
+ }) => {
397
+ const result: SimulateContractParameters<
398
+ typeof zoraMints1155Config.abi,
399
+ "permitTransferBatchToManagerAndCall"
400
+ > = {
401
+ abi: zoraMints1155Config.abi,
402
+ address: zoraMints1155Config.address[chainId],
403
+ functionName: "permitTransferBatchToManagerAndCall",
404
+ args: [permit, signature],
405
+ };
406
+
407
+ return result;
408
+ };
@@ -0,0 +1,159 @@
1
+ import { getSubgraphUrl } from "src/apis/chain-constants";
2
+ import { Address } from "viem";
3
+ import { request, gql } from "graphql-request";
4
+
5
+ type CollectQueryResult = {
6
+ tokenIds: bigint[];
7
+ quantities: bigint[];
8
+ };
9
+
10
+ export const getMintsAccountBalanceWithPriceQuery = (account: Address) => {
11
+ const query = gql`
12
+ query GetMintAccountBalances($account: String!) {
13
+ mintAccountBalances(where: { account: $account }) {
14
+ balance
15
+ mintToken {
16
+ id
17
+ pricePerToken
18
+ }
19
+ }
20
+ }
21
+ `;
22
+
23
+ return {
24
+ query,
25
+ variables: { account },
26
+ };
27
+ };
28
+
29
+ export type MintAccountBalance = {
30
+ balance: string;
31
+ mintToken: {
32
+ id: string;
33
+ pricePerToken: string;
34
+ };
35
+ };
36
+
37
+ export type MintAccountBalancesQueryResult = {
38
+ mintTokenBalances: MintAccountBalance[];
39
+ };
40
+
41
+ /**
42
+ * Given the result of a mint token balances query, selects the best MINTs to use to collect with that will satisfy the quantity,
43
+ * by selecting the lowest priced MINTs first. Throws an error if not enough mints to collect with.
44
+ */
45
+ export const selectMintsToCollectWithFromQueryResult = (
46
+ mintAccountBalances: MintAccountBalance[],
47
+ quantityToCollect: bigint,
48
+ ) => {
49
+ const parsed = mintAccountBalances.map((r) => {
50
+ return {
51
+ tokenId: BigInt(r.mintToken.id),
52
+ quantity: BigInt(r.balance),
53
+ pricePerToken: BigInt(r.mintToken.pricePerToken),
54
+ };
55
+ });
56
+
57
+ // now we want to find the best tokens to collect with, sorted by lowest price ascending
58
+ // given its a bigint, lets not do a straight subtraction but sort based on result of comparison gt/lt:
59
+ const sorted = parsed.sort((a, b) => {
60
+ if (a.pricePerToken < b.pricePerToken) {
61
+ return -1;
62
+ }
63
+ return 1;
64
+ });
65
+
66
+ // we need to get array of tokenIds and quantities to collect with:
67
+ let remainingQuantity = quantityToCollect;
68
+ const tokenIds: bigint[] = [];
69
+ const quantities: bigint[] = [];
70
+
71
+ while (remainingQuantity > 0) {
72
+ const next = sorted.shift();
73
+ if (!next) {
74
+ throw new Error("Not enough MINTs to collect with");
75
+ }
76
+
77
+ const quantityToUse =
78
+ remainingQuantity > next.quantity ? next.quantity : remainingQuantity;
79
+ tokenIds.push(next.tokenId);
80
+ quantities.push(quantityToUse);
81
+ remainingQuantity -= quantityToUse;
82
+ }
83
+
84
+ return {
85
+ tokenIds,
86
+ quantities,
87
+ };
88
+ };
89
+
90
+ /**
91
+ * Given an array of mint account balances, sums the balances.
92
+ * @param mintAccountBalances
93
+ * @returns Total balance
94
+ */
95
+ export const sumBalances = (
96
+ mintAccountBalances: Pick<MintAccountBalance, "balance">[],
97
+ ) => {
98
+ return mintAccountBalances.reduce((acc, curr) => {
99
+ return acc + BigInt(curr.balance);
100
+ }, BigInt(0));
101
+ };
102
+
103
+ /***
104
+ * Given an account and quantity of MINTs to use to collect with, queries for MINTs
105
+ * owned by an account, and selects the best MINTs to use to collect with that will satisfy that quantity.
106
+ * @param account Account to query for MINTs
107
+ * @param chainId
108
+ * @param quantityToCollect How many MINTs to use to collect with
109
+ * @returns
110
+ */
111
+ export const getMINTsToCollectWith = async ({
112
+ account,
113
+ chainId,
114
+ quantityToCollect,
115
+ }: {
116
+ account: Address;
117
+ chainId: number;
118
+ quantityToCollect: bigint;
119
+ }): Promise<CollectQueryResult> => {
120
+ const subgraphUrl = getSubgraphUrl(chainId);
121
+
122
+ const { query, variables } = getMintsAccountBalanceWithPriceQuery(account);
123
+
124
+ const result = await request<MintAccountBalancesQueryResult>(
125
+ subgraphUrl,
126
+ query,
127
+ variables,
128
+ );
129
+
130
+ return selectMintsToCollectWithFromQueryResult(
131
+ result.mintTokenBalances,
132
+ quantityToCollect,
133
+ );
134
+ };
135
+
136
+ /***
137
+ * Given an account, queries for MINTs owned by an account, and sums the balances.
138
+ * @param account Account to query for MINTs
139
+ * @returns Total MINTs balance of account
140
+ */
141
+ export const getMINTsBalance = async ({
142
+ chainId,
143
+ account,
144
+ }: {
145
+ account: Address;
146
+ chainId: number;
147
+ }) => {
148
+ const subgraphUrl = getSubgraphUrl(chainId);
149
+
150
+ const { query, variables } = getMintsAccountBalanceWithPriceQuery(account);
151
+
152
+ const result = await request<MintAccountBalancesQueryResult>(
153
+ subgraphUrl,
154
+ query,
155
+ variables,
156
+ );
157
+
158
+ return sumBalances(result.mintTokenBalances);
159
+ };