-
Notifications
You must be signed in to change notification settings - Fork 0
/
vanilla_instructions.py
95 lines (77 loc) · 3.5 KB
/
vanilla_instructions.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
from solana.publickey import PublicKey
from solana.keypair import Keypair
from solana.system_program import create_account, CreateAccountParams
from spl.token.constants import MINT_LEN, TOKEN_PROGRAM_ID
from spl.token.instructions import initialize_mint, InitializeMintParams, \
create_associated_token_account, get_associated_token_address, \
mint_to, MintToParams, approve, ApproveParams
from utils import get_minimum_balance_rent_exemption, get_config_space
from constants import CANDY_MACHINE_PROGRAM_ID
async def get_create_new_config_account_instructions(async_client, new_account, from_account, num_nfts):
space_needed = get_config_space(num_nfts)
min_lamports = await get_minimum_balance_rent_exemption(async_client, space_needed)
print("space needed for %s nfts: %s bytes" % (num_nfts, space_needed))
print("minimum lamports for rent exemption for %s bytes : %s" % (space_needed, min_lamports))
instruction = create_account(
CreateAccountParams(
from_pubkey=from_account.public_key,
new_account_pubkey=new_account.public_key,
lamports=min_lamports, space=space_needed, program_id=PublicKey(CANDY_MACHINE_PROGRAM_ID))
)
return ([instruction], [from_account, new_account])
async def get_user_account_mint_prep_instructions(async_client, user_account):
mint_account = Keypair()
instruction_list = []
min_lamports = await get_minimum_balance_rent_exemption(async_client, MINT_LEN)
create_account_instruction = create_account(
CreateAccountParams(
from_pubkey=user_account.public_key,
new_account_pubkey=mint_account.public_key,
lamports=min_lamports,
space=MINT_LEN,
program_id=TOKEN_PROGRAM_ID
)
)
instruction_list.append(create_account_instruction)
# no freeze rn bleh
initialize_mint_instruction = initialize_mint(
InitializeMintParams(
decimals=0,
program_id=TOKEN_PROGRAM_ID,
mint=mint_account.public_key,
mint_authority=user_account.public_key
)
)
instruction_list.append(initialize_mint_instruction)
create_assoc_instruction = create_associated_token_account(
payer=user_account.public_key,
owner=user_account.public_key,
mint=mint_account.public_key
)
instruction_list.append(create_assoc_instruction)
assoc_token_account = get_associated_token_address(user_account.public_key, mint_account.public_key)
mint_to_instruction = mint_to(
params=MintToParams(
program_id=TOKEN_PROGRAM_ID,
mint=mint_account.public_key,
dest=assoc_token_account,
mint_authority=user_account.public_key,
amount=1
)
)
instruction_list.append(mint_to_instruction)
return (mint_account, instruction_list, [user_account, mint_account])
def get_approval_instruction(user_account, purchase_token, amount):
transfer_authority_keypair = Keypair()
assoc_token_account_public_key = get_associated_token_address(user_account.public_key,
PublicKey(purchase_token))
approval_instruction = approve(
ApproveParams(
program_id=TOKEN_PROGRAM_ID,
source=assoc_token_account_public_key,
delegate=transfer_authority_keypair.public_key,
owner=user_account.public_key,
amount=amount
)
)
return (approval_instruction, transfer_authority_keypair)