|
1 |
| -from solana.publickey import PublicKey |
2 |
| -from solana.keypair import Keypair |
| 1 | +from solders.pubkey import Pubkey |
| 2 | +from solders.keypair import Keypair |
| 3 | +import solders.system_program as sys |
| 4 | +from solana.constants import SYSTEM_PROGRAM_ID |
3 | 5 | from solana.rpc.async_api import AsyncClient
|
4 | 6 | from solana.rpc.commitment import Confirmed
|
5 | 7 | from solana.rpc.types import TxOpts
|
6 |
| -from solana.sysvar import SYSVAR_CLOCK_PUBKEY, SYSVAR_STAKE_HISTORY_PUBKEY |
| 8 | +from solders.sysvar import CLOCK, STAKE_HISTORY |
7 | 9 | from solana.transaction import Transaction
|
8 |
| -import solana.system_program as sys |
9 | 10 |
|
10 | 11 | from stake.constants import STAKE_LEN, STAKE_PROGRAM_ID, SYSVAR_STAKE_CONFIG_ID
|
11 | 12 | from stake.state import Authorized, Lockup, StakeAuthorize
|
12 | 13 | import stake.instructions as st
|
13 | 14 |
|
14 | 15 |
|
15 |
| -async def create_stake(client: AsyncClient, payer: Keypair, stake: Keypair, authority: PublicKey, lamports: int): |
16 |
| - print(f"Creating stake {stake.public_key}") |
| 16 | +OPTS = TxOpts(skip_confirmation=False, preflight_commitment=Confirmed) |
| 17 | + |
| 18 | + |
| 19 | +async def create_stake(client: AsyncClient, payer: Keypair, stake: Keypair, authority: Pubkey, lamports: int): |
| 20 | + print(f"Creating stake {stake.pubkey()}") |
17 | 21 | resp = await client.get_minimum_balance_for_rent_exemption(STAKE_LEN)
|
18 |
| - txn = Transaction() |
| 22 | + txn = Transaction(fee_payer=payer.pubkey()) |
19 | 23 | txn.add(
|
20 | 24 | sys.create_account(
|
21 | 25 | sys.CreateAccountParams(
|
22 |
| - from_pubkey=payer.public_key, |
23 |
| - new_account_pubkey=stake.public_key, |
24 |
| - lamports=resp['result'] + lamports, |
| 26 | + from_pubkey=payer.pubkey(), |
| 27 | + to_pubkey=stake.pubkey(), |
| 28 | + lamports=resp.value + lamports, |
25 | 29 | space=STAKE_LEN,
|
26 |
| - program_id=STAKE_PROGRAM_ID, |
| 30 | + owner=STAKE_PROGRAM_ID, |
27 | 31 | )
|
28 | 32 | )
|
29 | 33 | )
|
30 | 34 | txn.add(
|
31 | 35 | st.initialize(
|
32 | 36 | st.InitializeParams(
|
33 |
| - stake=stake.public_key, |
| 37 | + stake=stake.pubkey(), |
34 | 38 | authorized=Authorized(
|
35 | 39 | staker=authority,
|
36 | 40 | withdrawer=authority,
|
37 | 41 | ),
|
38 | 42 | lockup=Lockup(
|
39 | 43 | unix_timestamp=0,
|
40 | 44 | epoch=0,
|
41 |
| - custodian=sys.SYS_PROGRAM_ID, |
| 45 | + custodian=SYSTEM_PROGRAM_ID, |
42 | 46 | )
|
43 | 47 | )
|
44 | 48 | )
|
45 | 49 | )
|
46 |
| - await client.send_transaction( |
47 |
| - txn, payer, stake, opts=TxOpts(skip_confirmation=False, preflight_commitment=Confirmed)) |
| 50 | + recent_blockhash = (await client.get_latest_blockhash()).value.blockhash |
| 51 | + await client.send_transaction(txn, payer, stake, recent_blockhash=recent_blockhash, opts=OPTS) |
48 | 52 |
|
49 | 53 |
|
50 |
| -async def delegate_stake(client: AsyncClient, payer: Keypair, staker: Keypair, stake: PublicKey, vote: PublicKey): |
51 |
| - txn = Transaction() |
| 54 | +async def delegate_stake(client: AsyncClient, payer: Keypair, staker: Keypair, stake: Pubkey, vote: Pubkey): |
| 55 | + txn = Transaction(fee_payer=payer.pubkey()) |
52 | 56 | txn.add(
|
53 | 57 | st.delegate_stake(
|
54 | 58 | st.DelegateStakeParams(
|
55 | 59 | stake=stake,
|
56 | 60 | vote=vote,
|
57 |
| - clock_sysvar=SYSVAR_CLOCK_PUBKEY, |
58 |
| - stake_history_sysvar=SYSVAR_STAKE_HISTORY_PUBKEY, |
| 61 | + clock_sysvar=CLOCK, |
| 62 | + stake_history_sysvar=STAKE_HISTORY, |
59 | 63 | stake_config_id=SYSVAR_STAKE_CONFIG_ID,
|
60 |
| - staker=staker.public_key, |
| 64 | + staker=staker.pubkey(), |
61 | 65 | )
|
62 | 66 | )
|
63 | 67 | )
|
64 |
| - signers = [payer, staker] if payer != staker else [payer] |
65 |
| - await client.send_transaction( |
66 |
| - txn, *signers, opts=TxOpts(skip_confirmation=False, preflight_commitment=Confirmed)) |
| 68 | + signers = [payer, staker] if payer.pubkey() != staker.pubkey() else [payer] |
| 69 | + recent_blockhash = (await client.get_latest_blockhash()).value.blockhash |
| 70 | + await client.send_transaction(txn, *signers, recent_blockhash=recent_blockhash, opts=OPTS) |
67 | 71 |
|
68 | 72 |
|
69 | 73 | async def authorize(
|
70 |
| - client: AsyncClient, payer: Keypair, authority: Keypair, stake: PublicKey, |
71 |
| - new_authority: PublicKey, stake_authorize: StakeAuthorize |
| 74 | + client: AsyncClient, payer: Keypair, authority: Keypair, stake: Pubkey, |
| 75 | + new_authority: Pubkey, stake_authorize: StakeAuthorize |
72 | 76 | ):
|
73 |
| - txn = Transaction() |
| 77 | + txn = Transaction(fee_payer=payer.pubkey()) |
74 | 78 | txn.add(
|
75 | 79 | st.authorize(
|
76 | 80 | st.AuthorizeParams(
|
77 | 81 | stake=stake,
|
78 |
| - clock_sysvar=SYSVAR_CLOCK_PUBKEY, |
79 |
| - authority=authority.public_key, |
| 82 | + clock_sysvar=CLOCK, |
| 83 | + authority=authority.pubkey(), |
80 | 84 | new_authority=new_authority,
|
81 | 85 | stake_authorize=stake_authorize,
|
82 | 86 | )
|
83 | 87 | )
|
84 | 88 | )
|
85 |
| - signers = [payer, authority] if payer != authority else [payer] |
86 |
| - await client.send_transaction( |
87 |
| - txn, *signers, opts=TxOpts(skip_confirmation=False, preflight_commitment=Confirmed)) |
| 89 | + signers = [payer, authority] if payer.pubkey() != authority.pubkey() else [payer] |
| 90 | + recent_blockhash = (await client.get_latest_blockhash()).value.blockhash |
| 91 | + await client.send_transaction(txn, *signers, recent_blockhash=recent_blockhash, opts=OPTS) |
0 commit comments