@zoralabs/protocol-sdk 0.5.3 → 0.5.4-MINT.0
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/.turbo/turbo-build.log +6 -7
- package/CHANGELOG.md +9 -0
- package/dist/apis/chain-constants.d.ts +1 -0
- package/dist/apis/chain-constants.d.ts.map +1 -1
- package/dist/index.cjs +86 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +83 -4
- package/dist/index.js.map +1 -1
- package/dist/mints/mints-queries.d.ts +44 -0
- package/dist/mints/mints-queries.d.ts.map +1 -0
- package/dist/preminter.d.ts +1 -1
- package/dist/preminter.d.ts.map +1 -1
- package/package.json +8 -7
- package/src/apis/chain-constants.ts +10 -0
- package/src/index.ts +2 -0
- package/src/mints/mints-queries.test.ts +78 -0
- package/src/mints/mints-queries.ts +121 -0
- package/src/preminter.ts +1 -1
|
@@ -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
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
MintAccountBalance,
|
|
4
|
+
selectMintsToCollectWithFromQueryResult,
|
|
5
|
+
} from "./mints-queries";
|
|
6
|
+
|
|
7
|
+
describe("MINTs queries", () => {
|
|
8
|
+
describe("getMINTsToCollectWith", () => {
|
|
9
|
+
it("should return the optimum tokenIds and quantities to collect", async () => {
|
|
10
|
+
// account has 3 of token 1, 4 of token 2, 10 of token 3
|
|
11
|
+
// token 1 price is 2, token 2 price is 1, token 3 price is 3
|
|
12
|
+
// we want to collect 10 tokens
|
|
13
|
+
// we should return token 2 with 4 tokens, token 1 with 3 tokens, and token 3 with 3 tokens
|
|
14
|
+
const mintTokenBalances: MintAccountBalance[] = [
|
|
15
|
+
{
|
|
16
|
+
mintToken: {
|
|
17
|
+
id: "1",
|
|
18
|
+
pricePerToken: "2",
|
|
19
|
+
},
|
|
20
|
+
balance: "3",
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
mintToken: {
|
|
24
|
+
id: "2",
|
|
25
|
+
pricePerToken: "1",
|
|
26
|
+
},
|
|
27
|
+
balance: "4",
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
mintToken: {
|
|
31
|
+
id: "3",
|
|
32
|
+
pricePerToken: "3",
|
|
33
|
+
},
|
|
34
|
+
balance: "10",
|
|
35
|
+
},
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
const result = selectMintsToCollectWithFromQueryResult(
|
|
39
|
+
mintTokenBalances,
|
|
40
|
+
10n,
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const expectedResult = {
|
|
44
|
+
tokenIds: [2n, 1n, 3n],
|
|
45
|
+
quantities: [4n, 3n, 3n],
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
expect(result).toEqual(expectedResult);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("should throw an error if not enough tokens to collect with", () => {
|
|
52
|
+
const mintTokenBalances: MintAccountBalance[] = [
|
|
53
|
+
{
|
|
54
|
+
mintToken: {
|
|
55
|
+
id: "1",
|
|
56
|
+
pricePerToken: "2",
|
|
57
|
+
},
|
|
58
|
+
balance: "3",
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
mintToken: {
|
|
62
|
+
id: "2",
|
|
63
|
+
pricePerToken: "1",
|
|
64
|
+
},
|
|
65
|
+
balance: "4",
|
|
66
|
+
},
|
|
67
|
+
];
|
|
68
|
+
const quantityToCollect = 8n;
|
|
69
|
+
|
|
70
|
+
expect(() => {
|
|
71
|
+
selectMintsToCollectWithFromQueryResult(
|
|
72
|
+
mintTokenBalances,
|
|
73
|
+
quantityToCollect,
|
|
74
|
+
);
|
|
75
|
+
}).toThrowError("Not enough MINTs to collect with");
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
});
|
|
@@ -0,0 +1,121 @@
|
|
|
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 getMintsAccountBalanceQuery = (account: Address) => {
|
|
11
|
+
const query = gql`
|
|
12
|
+
query GetMintAccountBalances($account: String!) {
|
|
13
|
+
mintTokenBalances(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 account and quantity of MINTs to use to collect with, queries for MINTs
|
|
92
|
+
* owned by an account, and selects the best MINTs to use to collect with that will satisfy that quantity.
|
|
93
|
+
* @param account Account to query for MINTs
|
|
94
|
+
* @param chainId
|
|
95
|
+
* @param quantityToCollect How many MINTs to use to collect with
|
|
96
|
+
* @returns
|
|
97
|
+
*/
|
|
98
|
+
export const getMINTsToCollectWith = async ({
|
|
99
|
+
account,
|
|
100
|
+
chainId,
|
|
101
|
+
quantityToCollect,
|
|
102
|
+
}: {
|
|
103
|
+
account: Address;
|
|
104
|
+
chainId: number;
|
|
105
|
+
quantityToCollect: bigint;
|
|
106
|
+
}): Promise<CollectQueryResult> => {
|
|
107
|
+
const subgraphUrl = getSubgraphUrl(chainId);
|
|
108
|
+
|
|
109
|
+
const { query, variables } = getMintsAccountBalanceQuery(account);
|
|
110
|
+
|
|
111
|
+
const result = await request<MintAccountBalancesQueryResult>(
|
|
112
|
+
subgraphUrl,
|
|
113
|
+
query,
|
|
114
|
+
variables,
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
return selectMintsToCollectWithFromQueryResult(
|
|
118
|
+
result.mintTokenBalances,
|
|
119
|
+
quantityToCollect,
|
|
120
|
+
);
|
|
121
|
+
};
|