-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
273 lines (230 loc) · 14.5 KB
/
main.py
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import os
import asyncio
import argparse
import math
import time
import json
from solana.keypair import Keypair
from solana.rpc.async_api import AsyncClient
from solana.transaction import Transaction
from anchor_commands.initialize_candy_machine import initialize_candy_machine
from anchor_commands.mint_nft import mint_one_nft
from anchor_commands.update_candy_machine import update_candy_machine
from pda import get_candy_machine_pda_nonce
from vanilla_instructions import get_create_new_config_account_instructions, get_user_account_mint_prep_instructions, \
get_approval_instruction
from anchor_commands.add_config_lines import add_nfts_to_machine
from anchor_commands.initialize_config import initialize_candymachine_for_config_account
from anchor_commands.mint_prepped_nft import mint_prepped_nft
from constants import DEVNET, TESTNET, MAINNET
from utils import get_keypair, get_default_creator_array, get_creator_array, get_nft_rows, get_keypair_from_byte_list
if os.name == 'nt':
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
async def run_instructions(async_client, instruction_list, signature_list):
tx = Transaction()
for i in instruction_list:
tx = tx.add(i)
response = await async_client.send_transaction(tx, *signature_list)
return response
async def main():
parser = argparse.ArgumentParser(description='candy machine configurations')
parser.add_argument('--usenet', action="store", choices=["devnet", "testnet", "mainnet"],
help='path to the keypair used for payments')
parser.add_argument('--customnet', action="store",
help='custom rpc endpoint to hit')
subparsers = parser.add_subparsers(dest="subcommand_name", help='candy machine config help')
parser_config_creator = subparsers.add_parser('create_config_account', help='create account to store NFTs')
parser_config_creator.add_argument('payment_key', action="store", help='path to the keypair used for payments')
parser_config_creator.add_argument('num_nfts', action="store",
help='number of nfts this config will hold. determines space')
parser_init_config = subparsers.add_parser('initialize_config_account',
help='initialize the config account in candymachine. allocate space')
parser_init_config.add_argument('payment_key', action="store", help='path to the keypair used for payments')
parser_init_config.add_argument('nft_symbol', action="store", help='NFT symbol')
parser_init_config.add_argument('num_nfts', action="store", help='number of nfts')
parser_init_config.add_argument('config_pub_key', action="store", help='public key of config account')
parser_init_config.add_argument('seller_basis_points', action="store", help='basis points / royalty')
parser_init_config.add_argument('--is_immutable', action="store_true", help='mutability')
parser_init_config.add_argument('--creator_json_file', action="store",
help='optional json list of creators. Users the payment_key by default')
parser_init_addlines = subparsers.add_parser('add_config_lines',
help='initialize the config account in candymachine. allocate space')
parser_init_addlines.add_argument('payment_key', action="store", help='path to the keypair used for payments')
parser_init_addlines.add_argument('config_pub_key', action="store", help='public key of config account')
parser_init_addlines.add_argument('name_uri_json', action="store", help='file containing names mapped to metadata '
'uris')
parser_create_machine = subparsers.add_parser('initialize_candy_machine',
help='create the main candy machine account')
parser_create_machine.add_argument('payment_key', action="store", help='path to the keypair used for payments')
parser_create_machine.add_argument('price', action="store", help='price per NFT in sol')
parser_create_machine.add_argument('livedate', action="store", help='date when the mint should be live epoch time')
parser_create_machine.add_argument('itemcount', action="store", help='number of nfts for the candymachine')
parser_create_machine.add_argument('config_pub_key', action="store", help='public key of config account')
parser_create_machine.add_argument('treasury_pub_key', action="store", help='public key of treasury account')
parser_update_machine = subparsers.add_parser('update_candy_machine',
help='update the price and date in the candy machine')
parser_update_machine.add_argument('payment_key', action="store", help='path to the keypair used for payments')
parser_update_machine.add_argument('price', action="store", help='price per NFT in sol')
parser_update_machine.add_argument('livedate', action="store", help='date when the mint should be live epoch time')
parser_update_machine.add_argument('config_pub_key', action="store", help='public key of config account')
parser_mint = subparsers.add_parser('mint',
help='mint from a machine')
parser_mint.add_argument('payment_key', action="store", help='path to the keypair used for payments')
parser_mint.add_argument('config_pub_key', action="store", help='public key of config account')
parser_mint.add_argument('treasury_pub_key', action="store", help='wallet pub key')
parser_mint.add_argument('mint_auth', action="store", help='wallet pub key')
parser_mint_pre = subparsers.add_parser('gen_pre_mint',
help='generate pre mint configuration')
parser_mint_pre.add_argument('payment_key', action="store", help='path to the keypair used for payments')
parser_mint_pre.add_argument('num_accounts', action="store", help='num accounts to generate')
parser_mint_pre.add_argument('mint_file', action="store", help='file to store mint keys in')
parser_mint_prepped_nft = subparsers.add_parser('mint_prepped_nft',
help='generate pre mint configuration')
parser_mint_prepped_nft.add_argument('payment_key', action="store", help='path to the keypair used for payments')
parser_mint_prepped_nft.add_argument('mint_file', action="store", help='file containing mint keys in')
parser_mint_prepped_nft.add_argument('purchase_token_address', action="store", help='purchase_token_address (spl token mint addr)')
parser_mint_prepped_nft.add_argument('config_pub_key', action="store", help='public key of config account')
parser_mint_prepped_nft.add_argument('treasury_pub_key', action="store", help='wallet pub key')
parser_mint_prepped_nft.add_argument('mint_auth', action="store", help='wallet pub key')
args = parser.parse_args()
use_network = DEVNET
if args.usenet == "testnet":
use_network = TESTNET
elif args.usenet == "mainnet":
use_network = MAINNET
payment_keypair = get_keypair(args.payment_key)
# vanilla
if args.subcommand_name == "create_config_account":
new_account_keypair = Keypair()
num_nfts = int(args.num_nfts)
async_client = AsyncClient(endpoint=use_network)
from_account_keypair = payment_keypair
(instruction_list, signature_list) = await get_create_new_config_account_instructions(async_client,
new_account_keypair,
from_account_keypair,
num_nfts)
response = await run_instructions(async_client, instruction_list, signature_list)
print(response)
print("new config account generated with candymachine as owner: %s" % new_account_keypair.public_key)
# anchor
elif args.subcommand_name == "initialize_config_account":
num_nfts = int(args.num_nfts)
config_pub_key = args.config_pub_key
nft_symbol = args.nft_symbol
seller_basis_points = int(args.seller_basis_points)
is_mut = True
if args.is_immutable:
is_mut = False
if not args.creator_json_file:
creator_array = get_default_creator_array(payment_keypair)
else:
creator_array = get_creator_array(args.creator_json_file)
async_client = AsyncClient(endpoint=use_network)
response = await initialize_candymachine_for_config_account(async_client,
payment_keypair,
config_pub_key,
num_nfts,
nft_symbol,
seller_basis_points,
is_mut,
num_nfts,
creator_array)
print(response)
# anchor
elif args.subcommand_name == "add_config_lines":
config_pub_key_str = args.config_pub_key
nft_rows_json_file = args.name_uri_json
nft_rows = get_nft_rows(nft_rows_json_file)
async_client = AsyncClient(endpoint=use_network)
response = await add_nfts_to_machine(async_client, payment_keypair, config_pub_key_str, nft_rows)
print(response)
# anchor
elif args.subcommand_name == "initialize_candy_machine":
async_client = AsyncClient(endpoint=use_network)
price = math.ceil(float(args.price) * 1000000000)
config_pub_key_str = args.config_pub_key
itemcount = int(args.itemcount)
treasury_pub_key_str = args.treasury_pub_key
if args.livedate == "now":
livedate = int(time.time())
else:
livedate = int(args.livedate)
candy_machine_pda, bump = get_candy_machine_pda_nonce(config_pub_key_str)
print("created candy machine with address: %s" % (candy_machine_pda.to_base58().decode()))
response = await initialize_candy_machine(async_client,
payment_keypair,
config_pub_key_str,
treasury_pub_key_str,
candy_machine_pda,
bump,
livedate,
price,
itemcount
)
print(response)
# anchor
elif args.subcommand_name == "update_candy_machine":
async_client = AsyncClient(endpoint=use_network)
price = math.ceil(float(args.price) * 1000000000)
config_pub_key_str = args.config_pub_key
candy_machine_pda, _ = get_candy_machine_pda_nonce(config_pub_key_str)
if args.livedate == "now":
livedate = int(time.time())
else:
livedate = int(args.livedate)
result = await update_candy_machine(async_client,
payment_keypair,
candy_machine_pda,
price,
livedate)
print(result)
# anchor with vanilla
elif args.subcommand_name == "mint":
async_client = AsyncClient(endpoint=use_network)
config_pub_key_str = args.config_pub_key
treasury_pub_key_str = args.treasury_pub_key
mint_account, account_create_instructions, signers = await get_user_account_mint_prep_instructions(async_client,
payment_keypair)
approval_instruction, signers = get_approval_instruction(payment_keypair,
mint_account.public_key, 1)
result = await mint_one_nft(async_client, payment_keypair, config_pub_key_str,
mint_account, treasury_pub_key_str, account_create_instructions + approval_instruction)
print(result)
# vanilla
elif args.subcommand_name == "gen_pre_mint":
async_client = AsyncClient(endpoint=use_network)
num_accounts = int(args.num_accounts)
mint_key_file = args.mint_file
mint_account_list = []
for i in range(0,num_accounts):
mint_account, account_create_instructions, signers = await get_user_account_mint_prep_instructions(async_client,
payment_keypair)
mint_account_list.append(mint_account)
# approval_instruction, signers = get_approval_instruction(payment_keypair,
# mint_account.public_key, 1)
response = await run_instructions(async_client, account_create_instructions, signers)
print(response)
with open(mint_key_file,"w") as f:
for m in mint_account_list:
f.write(json.dumps(list(m.secret_key)))
f.write("\n")
# anchor
elif args.subcommand_name == "mint_prepped_nft":
async_client = AsyncClient(endpoint=use_network)
mint_file = args.mint_file
mint_keys = []
with open(mint_file) as f:
lines = f.read().split("\n")
for l in lines:
if len(l) > 0:
mint_keys.append(get_keypair_from_byte_list(json.loads(l)))
config_pub_key_str = args.config_pub_key
treasury_pub_key_str = args.treasury_pub_key
purchase_token_address = args.purchase_token_address
for m in mint_keys:
approval_instruction, transfer_authority_keypair = get_approval_instruction(payment_keypair,
purchase_token_address, 1)
result = await mint_prepped_nft(async_client, payment_keypair, config_pub_key_str,
m, treasury_pub_key_str, approval_instruction,transfer_authority_keypair)
print(result)
asyncio.run(main())