-
Notifications
You must be signed in to change notification settings - Fork 1
/
deployRigs.ts
256 lines (237 loc) · 7.88 KB
/
deployRigs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import {
ethers,
upgrades,
network,
rigsConfig,
rigsDeployment,
mainnet,
} from "hardhat";
import { Wallet, providers, BigNumber, utils } from "ethers";
import {
AllowListEntry,
buildTree,
countList,
getListFromCSVs,
} from "../helpers/allowlist";
import type { TablelandRigs, PaymentSplitter } from "../typechain-types";
import { Database, Statement } from "@tableland/sdk";
import fetch, { Headers, Request, Response } from "node-fetch";
import { getContractURI } from "../helpers/uris";
import assert from "assert";
if (!(globalThis as any).fetch) {
(globalThis as any).fetch = fetch;
(globalThis as any).Headers = Headers;
(globalThis as any).Request = Request;
(globalThis as any).Response = Response;
}
async function main() {
console.log(`\nDeploying rigs to '${network.name}'...`);
// Get owner account
const [account] = await ethers.getSigners();
if (account.provider === undefined) {
throw Error("missing provider");
}
// Ensure we can build URI template
if (rigsDeployment.rigsTable === "") {
throw Error(`missing rigs table entry in deployments`);
}
if (rigsDeployment.attributesTable === "") {
throw Error(`missing attributes table entry in deployments`);
}
if (rigsDeployment.lookupsTable === "") {
throw Error(`missing lookups table entry in deployments`);
}
if (rigsDeployment.dealsTable === "") {
throw Error(`missing deals table entry in deployments`);
}
if (rigsDeployment.pilotSessionsTable === "") {
throw Error(`missing pilot sessions table entry in deployments`);
}
// Don't allow multiple deployments per network
if (rigsDeployment.contractAddress !== "") {
throw Error(`already deployed to '${network.name}'`);
}
// Build merkle trees for allowlist
const allowlist = await getListFromCSVs(rigsConfig.allowlistFiles);
const allowlistTree = buildTree(allowlist);
const allowlistAllowances = countList(allowlist);
const waitlist = await getListFromCSVs(rigsConfig.waitlistFiles);
const waitlistTree = buildTree(waitlist);
const waitlistAllowances = countList(waitlist);
console.log(
`Allowlist has ${allowlistTree.getLeafCount()} entries and ${allowlistAllowances} total allowances with root:`,
allowlistTree.getHexRoot()
);
console.log(
`Waitlist has ${waitlistTree.getLeafCount()} entries and ${waitlistAllowances} total allowances with root:`,
waitlistTree.getHexRoot()
);
// Ensure we have correct allowances
assert(
allowlistAllowances === rigsConfig.maxSupply,
`allowlist total allowances does not equal max supply ${rigsConfig.maxSupply}`
);
assert(
waitlistAllowances === rigsConfig.waitlistSize,
`waitlist total allowances does not equal waitlist size ${rigsConfig.waitlistSize}`
);
// Connect to tableland
let tbl: Database | undefined;
if (
rigsDeployment.contractTable === "" ||
rigsDeployment.allowlistTable === ""
) {
const privateKey = mainnet
? rigsConfig.tables.mainnet.tablelandPrivateKey
: rigsConfig.tables.testnet.tablelandPrivateKey;
if (!privateKey) {
throw Error("missing Tableland private key");
}
const providerUrl = mainnet
? rigsConfig.tables.mainnet.tablelandProviderUrl
: rigsConfig.tables.testnet.tablelandProviderUrl;
if (!providerUrl) {
throw Error("missing Tableland Provider URL");
}
const wallet = new Wallet(privateKey);
const provider = new providers.JsonRpcProvider(providerUrl);
tbl = new Database({ signer: wallet.connect(provider), autoWait: true });
}
// Create allow list table
let allowlistTable: string;
if (tbl && rigsDeployment.allowlistTable === "") {
const { meta } = await tbl
.prepare(
"create table rigs_allowlist (address text, freeAllowance int, paidAllowance int, waitlist int)"
)
.run();
if (!meta.txn) {
throw new Error("no txn found in metadata");
}
allowlistTable = meta.txn.name;
console.log("Allowlist table created as:", allowlistTable);
// Insert allow list entries
async function insertEntries(
tbl: Database,
list: [string, AllowListEntry][],
waitlist: boolean
) {
const stmt = tbl.prepare(
`insert into ${allowlistTable} values (?, ?, ?, ?);`
);
while (list.length > 0) {
const entries = list.splice(0, 50);
const runStatements: Statement[] = [];
for (const [address, entry] of entries) {
runStatements.push(
stmt.bind(
address,
entry.freeAllowance,
entry.paidAllowance,
waitlist ? 1 : 0
)
);
}
const [res] = await tbl.batch(runStatements);
console.log(
`Inserted ${entries.length} allowlist entries with txn '${res.meta.txn?.transactionHash}'`
);
}
}
await insertEntries(tbl, Object.entries(allowlist), false);
await insertEntries(tbl, Object.entries(waitlist), true);
} else {
allowlistTable = rigsDeployment.allowlistTable;
}
// Deploy a PaymentSplitter for rewards
const SplitterFactory = await ethers.getContractFactory("PaymentSplitter");
const splitter = (await SplitterFactory.deploy(
rigsConfig.royaltyReceivers,
rigsConfig.royaltyReceiverShares
)) as PaymentSplitter;
await splitter.deployed();
console.log("Deployed PaymentSplitter:", splitter.address);
// Create contract table
let contractTable: string;
if (tbl && rigsDeployment.contractTable === "") {
const { meta } = await tbl
.prepare(
"create table rigs_contract (name text, description text, image text, external_link text, seller_fee_basis_points int, fee_recipient text)"
)
.run();
if (!meta.txn) {
throw new Error("no txn found on metadata");
}
contractTable = meta.txn.name;
console.log("Contract table created as:", contractTable);
// Insert contract info
const { meta: insertMeta } = await tbl
.prepare(`insert into ${contractTable} values (?, ?, ?, ?, ?, ?);`)
.bind(
rigsConfig.name,
rigsConfig.description,
rigsConfig.image,
rigsConfig.externalLink,
rigsConfig.sellerFeeBasisPoints,
splitter.address
)
.run();
console.log(
`Inserted contract info with txn '${insertMeta.txn?.transactionHash}': `,
rigsConfig.name,
rigsConfig.description,
rigsConfig.image,
rigsConfig.externalLink,
rigsConfig.sellerFeeBasisPoints,
splitter.address
);
} else {
contractTable = rigsDeployment.contractTable;
}
// Deploy Rigs
const RigsFactory = await ethers.getContractFactory("TablelandRigs");
const rigs = await (
(await upgrades.deployProxy(
RigsFactory,
[
BigNumber.from(rigsConfig.maxSupply),
utils.parseEther(rigsConfig.etherPrice),
rigsConfig.feeRecipient,
splitter.address,
allowlistTree.getHexRoot(),
waitlistTree.getHexRoot(),
],
{
kind: "uups",
}
)) as TablelandRigs
).deployed();
console.log("Deployed Rigs:", rigs.address);
// Set contract URI
const contractURI = getContractURI(
rigsDeployment.tablelandHost,
contractTable
);
const tx = await rigs.setContractURI(contractURI);
await tx.wait();
console.log("Set contract URI:", contractURI);
// Warn that addresses need to be saved in deployments file
console.warn(
`\nSave 'deployments.${network.name}.contractAddress: "${rigs.address}"' in deployments.ts!`
);
console.warn(
`Save 'deployments.${network.name}.royaltyContractAddress: "${splitter.address}"' in deployments.ts!`
);
console.warn(
`Save 'deployments.${network.name}.contractTable: "${contractTable}"' in deployments.ts!`
);
console.warn(
`Save 'deployments.${network.name}.allowlistTable: "${allowlistTable}"' in deployments.ts!`
);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});