@zoralabs/protocol-sdk 0.5.6 → 0.5.7-MINT.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.
@@ -98,3 +98,13 @@ export const networkConfigByChain: Record<number, NetworkConfig> = {
98
98
  subgraphUrl: getSubgraph("zora-create-zora-testnet", "stable"),
99
99
  },
100
100
  };
101
+
102
+ export const getSubgraphUrl = (chainId: number): string => {
103
+ const networkConfig = networkConfigByChain[chainId];
104
+
105
+ if (!networkConfig) {
106
+ throw new Error(`Network not configured for chain id ${chainId}`);
107
+ }
108
+
109
+ return networkConfig.subgraphUrl;
110
+ };
package/src/index.ts CHANGED
@@ -13,3 +13,5 @@ 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";
@@ -0,0 +1,105 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import {
3
+ MintAccountBalance,
4
+ selectMintsToCollectWithFromQueryResult,
5
+ sumBalances,
6
+ } from "./mints-queries";
7
+
8
+ describe("MINTs queries", () => {
9
+ describe("selectMintsToCollectWithFromQueryResult", () => {
10
+ it("should return the optimum tokenIds and quantities to collect", async () => {
11
+ // account has 3 of token 1, 4 of token 2, 10 of token 3
12
+ // token 1 price is 2, token 2 price is 1, token 3 price is 3
13
+ // we want to collect 10 tokens
14
+ // we should return token 2 with 4 tokens, token 1 with 3 tokens, and token 3 with 3 tokens
15
+ const mintTokenBalances: MintAccountBalance[] = [
16
+ {
17
+ mintToken: {
18
+ id: "1",
19
+ pricePerToken: "2",
20
+ },
21
+ balance: "3",
22
+ },
23
+ {
24
+ mintToken: {
25
+ id: "2",
26
+ pricePerToken: "1",
27
+ },
28
+ balance: "4",
29
+ },
30
+ {
31
+ mintToken: {
32
+ id: "3",
33
+ pricePerToken: "3",
34
+ },
35
+ balance: "10",
36
+ },
37
+ ];
38
+
39
+ const result = selectMintsToCollectWithFromQueryResult(
40
+ mintTokenBalances,
41
+ 10n,
42
+ );
43
+
44
+ const expectedResult = {
45
+ tokenIds: [2n, 1n, 3n],
46
+ quantities: [4n, 3n, 3n],
47
+ };
48
+
49
+ expect(result).toEqual(expectedResult);
50
+ });
51
+
52
+ it("should throw an error if not enough tokens to collect with", () => {
53
+ const mintTokenBalances: MintAccountBalance[] = [
54
+ {
55
+ mintToken: {
56
+ id: "1",
57
+ pricePerToken: "2",
58
+ },
59
+ balance: "3",
60
+ },
61
+ {
62
+ mintToken: {
63
+ id: "2",
64
+ pricePerToken: "1",
65
+ },
66
+ balance: "4",
67
+ },
68
+ ];
69
+ const quantityToCollect = 8n;
70
+
71
+ expect(() => {
72
+ selectMintsToCollectWithFromQueryResult(
73
+ mintTokenBalances,
74
+ quantityToCollect,
75
+ );
76
+ }).toThrowError("Not enough MINTs to collect with");
77
+ });
78
+ });
79
+
80
+ describe("sumBalances", () => {
81
+ it("should return the sum of the balances", async () => {
82
+ // account has 3 of token 1, 4 of token 2, 10 of token 3
83
+ // token 1 price is 2, token 2 price is 1, token 3 price is 3
84
+ // we want to collect 10 tokens
85
+ // we should return token 2 with 4 tokens, token 1 with 3 tokens, and token 3 with 3 tokens
86
+ const mintTokenBalances: Pick<MintAccountBalance, "balance">[] = [
87
+ {
88
+ balance: "3",
89
+ },
90
+ {
91
+ balance: "4",
92
+ },
93
+ {
94
+ balance: "10",
95
+ },
96
+ ];
97
+
98
+ const result = sumBalances(mintTokenBalances);
99
+
100
+ const expectedResult = 3n + 4n + 10n;
101
+
102
+ expect(result).toEqual(expectedResult);
103
+ });
104
+ });
105
+ });
@@ -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
+ };