-
Notifications
You must be signed in to change notification settings - Fork 3
/
initialize_vault.py
142 lines (123 loc) · 5.09 KB
/
initialize_vault.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
from solders.keypair import Keypair
from solders.pubkey import Pubkey
from solders.sysvar import RENT as SYSVAR_RENT_PUBKEY
from solders.system_program import ID as SYS_PROGRAM_ID
from solana.transaction import Transaction
from anchorpy import Program, Idl, Context
import requests
from anchorpy import Provider, Wallet
from solana.rpc.async_api import AsyncClient
import json
import os
from driftpy.constants.config import configs
from driftpy.drift_client import DriftClient, AccountSubscriptionConfig
from driftpy.accounts import get_user_stats_account_public_key, get_spot_market_account, get_user_account_public_key
from spl.token.constants import TOKEN_PROGRAM_ID
from spl.token.instructions import get_associated_token_address
async def main(keypath,
env,
url,
name,
):
with open(os.path.expanduser(keypath), 'r') as f:
secret = json.load(f)
kp = Keypair.from_bytes(bytes(secret))
print('using public key:', str(kp.pubkey()))
wallet = Wallet(kp)
connection = AsyncClient(url)
provider = Provider(connection, wallet)
url = 'https://raw.githubusercontent.com/drift-labs/drift-vaults/master/ts/sdk/src/idl/drift_vaults.json'
response = requests.get(url)
data = response.text
idl_raw = data
# idl = json.loads(idl_raw)
pid = 'vAuLTsyrvSfZRuRB3XgvkPwNGgYSs9YRYymVebLKoxR'
vault_program = Program(
Idl.from_json(idl_raw),
Pubkey.from_string(pid),
provider,
)
config = configs[env]
# drift_client = DriftClient.from_config(config, provider)
drift_client: DriftClient = DriftClient(provider.connection, provider.wallet, env.split('-')[0], account_subscription=AccountSubscriptionConfig("cached"))
# Initialize an empty list to store the character number array
char_number_array = [0] * 32
# Iterate through each character in the string and get its Unicode code point
for i in range(32):
if i < len(name):
char_number_array[i] = ord(name[i])
# Print the original string and the character number array
vault_pubkey = Pubkey.find_program_address(
[b"vault", bytes(char_number_array)], Pubkey.from_string(pid)
)[0]
params = {
'name': char_number_array,
'spot_market_index': 0, # USDC spot market index
'redeem_period': int(60 * 60 * 24 * 30), # 30 days
'max_tokens': int(1_000_000 * 1e6),
'min_deposit_amount': int(100 * 1e6),
'management_fee': int(.02 * 1e6),
'profit_share': int(.2 * 1e6),
'hurdle_rate': 0, # no supported atm
'permissioned': False,
}
# ch_signer = get_clearing_house_signer_public_key(drift_client.program_id)
spot_market = await get_spot_market_account(
drift_client.program, params['spot_market_index']
)
vault_user = get_user_account_public_key(drift_client.program_id, vault_pubkey)
vault_user_stats = get_user_stats_account_public_key(drift_client.program_id, vault_pubkey)
# vault_ata = get_associated_token_address(drift_client.authority, spot_market.mint)
ata = Pubkey.find_program_address(
[b"vault_token_account", bytes(vault_pubkey)], vault_program.program_id
)[0]
instr_context = Context(
accounts={
'drift_spot_market': spot_market.pubkey,
'drift_spot_market_mint': spot_market.mint,
'drift_user_stats': vault_user_stats,
'drift_user': vault_user,
'drift_state': drift_client.get_state_public_key(),
'vault': vault_pubkey,
'token_account': ata,
'token_program': TOKEN_PROGRAM_ID,
'drift_program': drift_client.program_id,
'manager': drift_client.authority,
'payer': drift_client.authority,
"rent": SYSVAR_RENT_PUBKEY,
"system_program": SYS_PROGRAM_ID,
})
# print(instr_context)
instruction = vault_program.instruction['initialize_vault'](
params,
ctx=instr_context,
)
tx = Transaction()
tx.add(instruction)
txSig = await drift_client.send_ixs([instruction])
print(f"tx sig {txSig}")
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--keypath', type=str, required=False, default=os.environ.get('ANCHOR_WALLET'))
parser.add_argument('--name', type=str, required=True, default='devnet')
parser.add_argument('--env', type=str, default='devnet')
args = parser.parse_args()
if args.keypath is None:
if os.environ['ANCHOR_WALLET'] is None:
raise NotImplementedError("need to provide keypath or set ANCHOR_WALLET")
else:
args.keypath = os.environ['ANCHOR_WALLET']
if args.env == 'devnet':
url = 'https://api.devnet.solana.com'
elif args.env == 'mainnet':
url = 'https://api.mainnet-beta.solana.com'
else:
raise NotImplementedError('only devnet/mainnet env supported')
import asyncio
asyncio.run(main(
args.keypath,
args.env,
url,
args.name,
))