@zoralabs/protocol-sdk 0.5.7-MINT.3 → 0.5.7
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 +16 -0
- package/CHANGELOG.md +5 -23
- package/README.md +2 -2
- package/dist/apis/chain-constants.d.ts +0 -1
- package/dist/apis/chain-constants.d.ts.map +1 -1
- package/dist/apis/generated/premint-api-types.d.ts +1 -1
- package/dist/apis/generated/premint-api-types.d.ts.map +1 -1
- package/dist/index.cjs +14 -132
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17 -132
- package/dist/index.js.map +1 -1
- package/dist/premint/premint-client.d.ts +1 -1
- package/package.json +8 -9
- package/src/apis/chain-constants.ts +12 -38
- package/src/apis/generated/premint-api-types.ts +3 -1
- package/src/create/1155-create-helper.test.ts +104 -0
- package/src/index.ts +0 -2
- package/src/mint/mint-client.test.ts +114 -0
- package/src/premint/premint-client.test.ts +239 -0
- package/src/premint/premint-client.ts +1 -1
- package/src/premint/preminter.test.ts +615 -0
- package/test-integration/premint-client.test.ts +146 -0
- package/tsconfig.build.json +10 -0
- package/tsup.config.ts +12 -0
- package/dist/mints/mints-queries.d.ts +0 -59
- package/dist/mints/mints-queries.d.ts.map +0 -1
- package/src/mints/mints-queries.ts +0 -159
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { zoraSepolia, zoraTestnet } from "viem/chains";
|
|
2
|
+
import { describe } from "vitest";
|
|
3
|
+
|
|
4
|
+
import { createPremintClient } from "src/premint/premint-client";
|
|
5
|
+
import { forkUrls, makeAnvilTest } from "src/anvil";
|
|
6
|
+
import { PremintConfigVersion } from "src/premint/contract-types";
|
|
7
|
+
|
|
8
|
+
const zoraGoerliTest = makeAnvilTest({
|
|
9
|
+
forkBlockNumber: 2107926,
|
|
10
|
+
forkUrl: forkUrls.zoraGoerli,
|
|
11
|
+
anvilChainId: zoraTestnet.id,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const zoraSepoliaTest = makeAnvilTest({
|
|
15
|
+
forkBlockNumber: 3118200,
|
|
16
|
+
forkUrl: forkUrls.zoraSepolia,
|
|
17
|
+
anvilChainId: zoraSepolia.id,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const tests = [
|
|
21
|
+
{
|
|
22
|
+
anvilTest: zoraGoerliTest,
|
|
23
|
+
chain: zoraTestnet,
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
anvilTest: zoraSepoliaTest,
|
|
27
|
+
chain: zoraSepolia,
|
|
28
|
+
},
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
tests.forEach(({ anvilTest, chain }) => {
|
|
32
|
+
describe(chain.name, () => {
|
|
33
|
+
describe("ZoraCreator1155Premint", () => {
|
|
34
|
+
describe("v2 signatures", () => {
|
|
35
|
+
anvilTest(
|
|
36
|
+
"can sign and execute on the forked premint contract",
|
|
37
|
+
async ({
|
|
38
|
+
viemClients: { walletClient, publicClient, testClient },
|
|
39
|
+
}) => {
|
|
40
|
+
const [creatorAccount, createReferralAccount, minterAccount] =
|
|
41
|
+
await walletClient.getAddresses();
|
|
42
|
+
const premintClient = createPremintClient({
|
|
43
|
+
chain,
|
|
44
|
+
publicClient,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const { uid, verifyingContract } =
|
|
48
|
+
await premintClient.createPremint({
|
|
49
|
+
walletClient,
|
|
50
|
+
creatorAccount: creatorAccount!,
|
|
51
|
+
checkSignature: true,
|
|
52
|
+
collection: {
|
|
53
|
+
contractAdmin: creatorAccount!,
|
|
54
|
+
contractName: "Testing Contract Premint V2",
|
|
55
|
+
contractURI:
|
|
56
|
+
"ipfs://bafkreiainxen4b4wz4ubylvbhons6rembxdet4a262nf2lziclqvv7au3f",
|
|
57
|
+
},
|
|
58
|
+
premintConfigVersion: PremintConfigVersion.V2,
|
|
59
|
+
tokenCreationConfig: {
|
|
60
|
+
tokenURI:
|
|
61
|
+
"ipfs://bafkreice23maski3x52tsfqgxstx3kbiifnt5jotg3a5ynvve53c4soi2f",
|
|
62
|
+
createReferral: createReferralAccount!,
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const mintParameters = await premintClient.makeMintParameters({
|
|
67
|
+
minterAccount: minterAccount!,
|
|
68
|
+
tokenContract: verifyingContract,
|
|
69
|
+
uid,
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const mintCosts = await premintClient.getMintCosts({
|
|
73
|
+
tokenContract: verifyingContract,
|
|
74
|
+
quantityToMint: 1n,
|
|
75
|
+
pricePerToken: 0n,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
await testClient.setBalance({
|
|
79
|
+
address: minterAccount!,
|
|
80
|
+
value: mintCosts.totalCost,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// if simulation succeeds, mint will succeed
|
|
84
|
+
await publicClient.simulateContract(mintParameters);
|
|
85
|
+
},
|
|
86
|
+
20 * 1000,
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe("v1 signatures", () => {
|
|
91
|
+
anvilTest(
|
|
92
|
+
"can sign and execute on the forked premint contract",
|
|
93
|
+
async ({
|
|
94
|
+
viemClients: { walletClient, publicClient, testClient },
|
|
95
|
+
}) => {
|
|
96
|
+
const [creatorAccount, minterAccount] =
|
|
97
|
+
await walletClient.getAddresses();
|
|
98
|
+
const premintClient = createPremintClient({
|
|
99
|
+
chain,
|
|
100
|
+
publicClient,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const { uid, verifyingContract } =
|
|
104
|
+
await premintClient.createPremint({
|
|
105
|
+
walletClient,
|
|
106
|
+
creatorAccount: creatorAccount!,
|
|
107
|
+
checkSignature: true,
|
|
108
|
+
collection: {
|
|
109
|
+
contractAdmin: creatorAccount!,
|
|
110
|
+
contractName: `Testing Contract Premint V1 ${publicClient.chain?.name}`,
|
|
111
|
+
contractURI:
|
|
112
|
+
"ipfs://bafkreiainxen4b4wz4ubylvbhons6rembxdet4a262nf2lziclqvv7au3fg",
|
|
113
|
+
},
|
|
114
|
+
premintConfigVersion: PremintConfigVersion.V1,
|
|
115
|
+
tokenCreationConfig: {
|
|
116
|
+
tokenURI:
|
|
117
|
+
"ipfs://bafkreice23maski3x52tsfqgxstx3kbiifnt5jotg3a5ynvve53c4soi2u",
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
const mintParameters = await premintClient.makeMintParameters({
|
|
122
|
+
minterAccount: minterAccount!,
|
|
123
|
+
tokenContract: verifyingContract,
|
|
124
|
+
uid,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const mintCosts = await premintClient.getMintCosts({
|
|
128
|
+
tokenContract: verifyingContract,
|
|
129
|
+
quantityToMint: 1n,
|
|
130
|
+
pricePerToken: 0n,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
await testClient.setBalance({
|
|
134
|
+
address: minterAccount!,
|
|
135
|
+
value: mintCosts.totalCost,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// if simulation succeeds, mint will succeed
|
|
139
|
+
await publicClient.simulateContract(mintParameters);
|
|
140
|
+
},
|
|
141
|
+
20 * 1000,
|
|
142
|
+
);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
});
|
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { defineConfig } from "tsup";
|
|
2
|
+
|
|
3
|
+
export default defineConfig({
|
|
4
|
+
entry: ["src/index.ts"],
|
|
5
|
+
sourcemap: true,
|
|
6
|
+
clean: true,
|
|
7
|
+
tsconfig: "tsconfig.build.json",
|
|
8
|
+
dts: false,
|
|
9
|
+
format: ["cjs", "esm"],
|
|
10
|
+
onSuccess:
|
|
11
|
+
"tsc --project tsconfig.build.json --emitDeclarationOnly --declaration --declarationMap",
|
|
12
|
+
});
|
|
@@ -1,59 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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"}
|
|
@@ -1,159 +0,0 @@
|
|
|
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
|
-
};
|