Skip to content

Commit 6113f28

Browse files
authored
clients-js: Bump kit deps (#577)
* clients-js: Bump kit deps #### Problem The kit deps on the js package are old. #### Summary of changes Bump the kit deps to the newest. While doing this, I ran into some changed types, and I tried my hand at `pipe`, but I had to do some ugly things to make it work. * Address feedback
1 parent ff68732 commit 6113f28

6 files changed

Lines changed: 368 additions & 244 deletions

File tree

clients/js-legacy/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
"@solana/web3.js": "^1.98.4",
4646
"@solana/addresses": "6.5.0",
4747
"@solana/prettier-config-solana": "^0.0.6",
48+
"@solana/rpc-api": "6.5.0",
49+
"@solana/rpc-spec": "6.5.0",
4850
"@solana/spl-single-pool": "workspace:*",
4951
"eslint-config-prettier": "^10.1.8",
5052
"eslint-plugin-prettier": "^5.5.5",

clients/js-legacy/src/internal.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
1+
import { Address } from '@solana/addresses';
2+
import {
3+
GetAccountInfoApi,
4+
GetMinimumBalanceForRentExemptionApi,
5+
GetStakeMinimumDelegationApi,
6+
} from '@solana/rpc-api';
7+
import { Rpc } from '@solana/rpc-spec';
18
import { Connection, Transaction, TransactionInstruction, PublicKey } from '@solana/web3.js';
29
import { Buffer } from 'buffer';
310

4-
export function rpc(connection: Connection) {
11+
export function rpc(
12+
connection: Connection,
13+
): Rpc<GetAccountInfoApi & GetMinimumBalanceForRentExemptionApi & GetStakeMinimumDelegationApi> {
514
return {
6-
getAccountInfo(address: string) {
15+
getAccountInfo(address: Address): any {
716
return {
817
async send() {
918
const pubkey = new PublicKey(address);
1019
return await connection.getAccountInfo(pubkey);
1120
},
1221
};
1322
},
14-
getMinimumBalanceForRentExemption(size: bigint) {
23+
getMinimumBalanceForRentExemption(size: bigint): any {
1524
return {
1625
async send() {
1726
return BigInt(await connection.getMinimumBalanceForRentExemption(Number(size)));
1827
},
1928
};
2029
},
21-
getStakeMinimumDelegation() {
30+
getStakeMinimumDelegation(): any {
2231
return {
2332
async send() {
2433
const minimumDelegation = await connection.getStakeMinimumDelegation();

clients/js/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@
3636
},
3737
"dependencies": {
3838
"@solana/addresses": "6.5.0",
39-
"@solana/instructions": "2.3.0",
40-
"@solana/transaction-messages": "2.2.1"
39+
"@solana/functional": "6.5.0",
40+
"@solana/instructions": "6.5.0",
41+
"@solana/instruction-plans": "6.5.0",
42+
"@solana/rpc-api": "6.5.0",
43+
"@solana/rpc-spec": "6.5.0",
44+
"@solana/transaction-messages": "6.5.0"
4145
}
4246
}

clients/js/src/instructions.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { getAddressCodec, Address } from '@solana/addresses';
22
import {
33
ReadonlySignerAccount,
44
ReadonlyAccount,
5-
IInstructionWithAccounts,
6-
IInstructionWithData,
5+
InstructionWithAccounts,
6+
InstructionWithData,
77
WritableAccount,
88
WritableSignerAccount,
9-
IInstruction,
9+
Instruction,
1010
AccountRole,
1111
} from '@solana/instructions';
1212

@@ -42,8 +42,8 @@ import {
4242
u64,
4343
} from './quarantine.js';
4444

45-
type InitializePoolInstruction = IInstruction<typeof SINGLE_POOL_PROGRAM_ID> &
46-
IInstructionWithAccounts<
45+
type InitializePoolInstruction = Instruction<typeof SINGLE_POOL_PROGRAM_ID> &
46+
InstructionWithAccounts<
4747
[
4848
ReadonlyAccount<VoteAccountAddress>,
4949
WritableAccount<PoolAddress>,
@@ -60,10 +60,10 @@ type InitializePoolInstruction = IInstruction<typeof SINGLE_POOL_PROGRAM_ID> &
6060
ReadonlyAccount<typeof STAKE_PROGRAM_ID>,
6161
]
6262
> &
63-
IInstructionWithData<Uint8Array>;
63+
InstructionWithData<Uint8Array>;
6464

65-
type ReplenishPoolInstruction = IInstruction<typeof SINGLE_POOL_PROGRAM_ID> &
66-
IInstructionWithAccounts<
65+
type ReplenishPoolInstruction = Instruction<typeof SINGLE_POOL_PROGRAM_ID> &
66+
InstructionWithAccounts<
6767
[
6868
ReadonlyAccount<VoteAccountAddress>,
6969
ReadonlyAccount<PoolAddress>,
@@ -76,10 +76,10 @@ type ReplenishPoolInstruction = IInstruction<typeof SINGLE_POOL_PROGRAM_ID> &
7676
ReadonlyAccount<typeof STAKE_PROGRAM_ID>,
7777
]
7878
> &
79-
IInstructionWithData<Uint8Array>;
79+
InstructionWithData<Uint8Array>;
8080

81-
type DepositStakeInstruction = IInstruction<typeof SINGLE_POOL_PROGRAM_ID> &
82-
IInstructionWithAccounts<
81+
type DepositStakeInstruction = Instruction<typeof SINGLE_POOL_PROGRAM_ID> &
82+
InstructionWithAccounts<
8383
[
8484
ReadonlyAccount<PoolAddress>,
8585
WritableAccount<PoolStakeAddress>,
@@ -96,10 +96,10 @@ type DepositStakeInstruction = IInstruction<typeof SINGLE_POOL_PROGRAM_ID> &
9696
ReadonlyAccount<typeof STAKE_PROGRAM_ID>,
9797
]
9898
> &
99-
IInstructionWithData<Uint8Array>;
99+
InstructionWithData<Uint8Array>;
100100

101-
type WithdrawStakeInstruction = IInstruction<typeof SINGLE_POOL_PROGRAM_ID> &
102-
IInstructionWithAccounts<
101+
type WithdrawStakeInstruction = Instruction<typeof SINGLE_POOL_PROGRAM_ID> &
102+
InstructionWithAccounts<
103103
[
104104
ReadonlyAccount<PoolAddress>,
105105
WritableAccount<PoolStakeAddress>,
@@ -114,10 +114,10 @@ type WithdrawStakeInstruction = IInstruction<typeof SINGLE_POOL_PROGRAM_ID> &
114114
ReadonlyAccount<typeof STAKE_PROGRAM_ID>,
115115
]
116116
> &
117-
IInstructionWithData<Uint8Array>;
117+
InstructionWithData<Uint8Array>;
118118

119-
type CreateTokenMetadataInstruction = IInstruction<typeof SINGLE_POOL_PROGRAM_ID> &
120-
IInstructionWithAccounts<
119+
type CreateTokenMetadataInstruction = Instruction<typeof SINGLE_POOL_PROGRAM_ID> &
120+
InstructionWithAccounts<
121121
[
122122
ReadonlyAccount<PoolAddress>,
123123
ReadonlyAccount<PoolMintAddress>,
@@ -129,10 +129,10 @@ type CreateTokenMetadataInstruction = IInstruction<typeof SINGLE_POOL_PROGRAM_ID
129129
ReadonlyAccount<typeof SYSTEM_PROGRAM_ID>,
130130
]
131131
> &
132-
IInstructionWithData<Uint8Array>;
132+
InstructionWithData<Uint8Array>;
133133

134-
type UpdateTokenMetadataInstruction = IInstruction<typeof SINGLE_POOL_PROGRAM_ID> &
135-
IInstructionWithAccounts<
134+
type UpdateTokenMetadataInstruction = Instruction<typeof SINGLE_POOL_PROGRAM_ID> &
135+
InstructionWithAccounts<
136136
[
137137
ReadonlyAccount<VoteAccountAddress>,
138138
ReadonlyAccount<PoolAddress>,
@@ -142,10 +142,10 @@ type UpdateTokenMetadataInstruction = IInstruction<typeof SINGLE_POOL_PROGRAM_ID
142142
ReadonlyAccount<typeof MPL_METADATA_PROGRAM_ID>,
143143
]
144144
> &
145-
IInstructionWithData<Uint8Array>;
145+
InstructionWithData<Uint8Array>;
146146

147-
type InitializeOnRampInstruction = IInstruction<typeof SINGLE_POOL_PROGRAM_ID> &
148-
IInstructionWithAccounts<
147+
type InitializeOnRampInstruction = Instruction<typeof SINGLE_POOL_PROGRAM_ID> &
148+
InstructionWithAccounts<
149149
[
150150
ReadonlyAccount<PoolAddress>,
151151
WritableAccount<PoolOnRampAddress>,
@@ -155,7 +155,7 @@ type InitializeOnRampInstruction = IInstruction<typeof SINGLE_POOL_PROGRAM_ID> &
155155
ReadonlyAccount<typeof STAKE_PROGRAM_ID>,
156156
]
157157
> &
158-
IInstructionWithData<Uint8Array>;
158+
InstructionWithData<Uint8Array>;
159159

160160
const enum SinglePoolInstructionType {
161161
InitializePool = 0,

clients/js/src/transactions.ts

Lines changed: 76 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
import { Address } from '@solana/addresses';
2+
import { pipe } from '@solana/functional';
3+
import {
4+
InstructionPlan,
5+
assertIsSingleTransactionPlan,
6+
createTransactionPlanner,
7+
parallelInstructionPlan,
8+
sequentialInstructionPlan,
9+
} from '@solana/instruction-plans';
10+
import {
11+
GetAccountInfoApi,
12+
GetMinimumBalanceForRentExemptionApi,
13+
GetStakeMinimumDelegationApi,
14+
} from '@solana/rpc-api';
15+
import { Rpc } from '@solana/rpc-spec';
216
import {
317
appendTransactionMessageInstruction,
418
createTransactionMessage,
19+
setTransactionMessageFeePayer,
520
TransactionVersion,
621
TransactionMessage,
722
} from '@solana/transaction-messages';
@@ -40,7 +55,7 @@ import {
4055
} from './quarantine.js';
4156

4257
interface DepositParams {
43-
rpc: any; // XXX Rpc<???>
58+
rpc: Rpc<GetAccountInfoApi & GetMinimumBalanceForRentExemptionApi & GetStakeMinimumDelegationApi>;
4459
pool: PoolAddress;
4560
userWallet: Address;
4661
userStakeAccount?: Address;
@@ -52,7 +67,7 @@ interface DepositParams {
5267
}
5368

5469
interface WithdrawParams {
55-
rpc: any; // XXX Rpc<???>
70+
rpc: Rpc<GetMinimumBalanceForRentExemptionApi & GetStakeMinimumDelegationApi>;
5671
pool: PoolAddress;
5772
userWallet: Address;
5873
userStakeAccount: Address;
@@ -78,83 +93,72 @@ export const SinglePoolProgram = {
7893
createAndDelegateUserStake: createAndDelegateUserStakeTransaction,
7994
};
8095

81-
export async function initializeTransaction(
82-
rpc: any, // XXX not exported: Rpc<???>,
96+
async function getInitializeInstructionPlan(
97+
rpc: Rpc<GetMinimumBalanceForRentExemptionApi & GetStakeMinimumDelegationApi>,
8398
voteAccount: VoteAccountAddress,
8499
payer: Address,
85100
skipMetadata = false,
86-
): Promise<TransactionMessage> {
87-
let transaction = createTransactionMessage({ version: 0 });
88-
101+
): Promise<InstructionPlan> {
89102
const pool = await findPoolAddress(SINGLE_POOL_PROGRAM_ID, voteAccount);
90-
const [stake, mint, onramp, poolRent, stakeRent, mintRent, minimumDelegationObj] =
91-
await Promise.all([
92-
findPoolStakeAddress(SINGLE_POOL_PROGRAM_ID, pool),
93-
findPoolMintAddress(SINGLE_POOL_PROGRAM_ID, pool),
94-
findPoolOnRampAddress(SINGLE_POOL_PROGRAM_ID, pool),
95-
rpc.getMinimumBalanceForRentExemption(SINGLE_POOL_ACCOUNT_SIZE).send(),
96-
rpc.getMinimumBalanceForRentExemption(STAKE_ACCOUNT_SIZE).send(),
97-
rpc.getMinimumBalanceForRentExemption(MINT_SIZE).send(),
98-
rpc.getStakeMinimumDelegation().send(),
99-
]);
103+
const [
104+
stake,
105+
mint,
106+
onramp,
107+
poolRent,
108+
stakeRent,
109+
mintRent,
110+
minimumDelegationObj,
111+
initializePool,
112+
initializeOnRamp,
113+
] = await Promise.all([
114+
findPoolStakeAddress(SINGLE_POOL_PROGRAM_ID, pool),
115+
findPoolMintAddress(SINGLE_POOL_PROGRAM_ID, pool),
116+
findPoolOnRampAddress(SINGLE_POOL_PROGRAM_ID, pool),
117+
rpc.getMinimumBalanceForRentExemption(SINGLE_POOL_ACCOUNT_SIZE).send(),
118+
rpc.getMinimumBalanceForRentExemption(STAKE_ACCOUNT_SIZE).send(),
119+
rpc.getMinimumBalanceForRentExemption(MINT_SIZE).send(),
120+
rpc.getStakeMinimumDelegation().send(),
121+
initializePoolInstruction(voteAccount),
122+
initializeOnRampInstruction(pool),
123+
]);
100124
const lamportsPerSol = 1_000_000_000n;
101125
const minimumPoolBalance =
102126
minimumDelegationObj.value > lamportsPerSol ? minimumDelegationObj.value : lamportsPerSol;
103127

104-
transaction = appendTransactionMessageInstruction(
105-
SystemInstruction.transfer({
106-
from: payer,
107-
to: pool,
108-
lamports: poolRent,
109-
}),
110-
transaction,
111-
);
112-
113-
transaction = appendTransactionMessageInstruction(
114-
SystemInstruction.transfer({
115-
from: payer,
116-
to: stake,
117-
lamports: stakeRent + minimumPoolBalance,
118-
}),
119-
transaction,
120-
);
121-
122-
transaction = appendTransactionMessageInstruction(
123-
SystemInstruction.transfer({
124-
from: payer,
125-
to: onramp,
126-
lamports: stakeRent,
127-
}),
128-
transaction,
129-
);
130-
131-
transaction = appendTransactionMessageInstruction(
132-
SystemInstruction.transfer({
133-
from: payer,
134-
to: mint,
135-
lamports: mintRent,
136-
}),
137-
transaction,
138-
);
139-
140-
transaction = appendTransactionMessageInstruction(
141-
await initializePoolInstruction(voteAccount),
142-
transaction,
143-
);
144-
145-
transaction = appendTransactionMessageInstruction(
146-
await initializeOnRampInstruction(pool),
147-
transaction,
148-
);
149-
150-
if (!skipMetadata) {
151-
transaction = appendTransactionMessageInstruction(
152-
await createTokenMetadataInstruction(pool, payer),
153-
transaction,
154-
);
155-
}
128+
return sequentialInstructionPlan([
129+
parallelInstructionPlan([
130+
SystemInstruction.transfer({ from: payer, to: pool, lamports: poolRent }),
131+
SystemInstruction.transfer({
132+
from: payer,
133+
to: stake,
134+
lamports: stakeRent + minimumPoolBalance,
135+
}),
136+
SystemInstruction.transfer({ from: payer, to: onramp, lamports: stakeRent }),
137+
SystemInstruction.transfer({ from: payer, to: mint, lamports: mintRent }),
138+
]),
139+
initializePool,
140+
initializeOnRamp,
141+
...(skipMetadata ? [] : [await createTokenMetadataInstruction(pool, payer)]),
142+
]);
143+
}
156144

157-
return transaction;
145+
export async function initializeTransaction(
146+
rpc: Rpc<GetMinimumBalanceForRentExemptionApi & GetStakeMinimumDelegationApi>,
147+
voteAccount: VoteAccountAddress,
148+
payer: Address,
149+
skipMetadata = false,
150+
): Promise<TransactionMessage> {
151+
const transactionPlanner = createTransactionPlanner({
152+
createTransactionMessage: () =>
153+
pipe(createTransactionMessage({ version: 0 }), (m) =>
154+
setTransactionMessageFeePayer(payer, m),
155+
),
156+
});
157+
158+
const instructionPlan = await getInitializeInstructionPlan(rpc, voteAccount, payer, skipMetadata);
159+
const transactionPlan = await transactionPlanner(instructionPlan);
160+
assertIsSingleTransactionPlan(transactionPlan);
161+
return transactionPlan.message;
158162
}
159163

160164
export async function replenishPoolTransaction(
@@ -321,7 +325,7 @@ export async function updateTokenMetadataTransaction(
321325
}
322326

323327
export async function initializeOnRampTransaction(
324-
rpc: any, // XXX not exported: Rpc<???>,
328+
rpc: Rpc<GetMinimumBalanceForRentExemptionApi & GetStakeMinimumDelegationApi>,
325329
pool: PoolAddress,
326330
payer: Address,
327331
): Promise<TransactionMessage> {
@@ -351,7 +355,7 @@ export async function initializeOnRampTransaction(
351355

352356
/** @deprecated */
353357
export async function createAndDelegateUserStakeTransaction(
354-
rpc: any, // XXX not exported: Rpc<???>,
358+
rpc: Rpc<GetMinimumBalanceForRentExemptionApi & GetStakeMinimumDelegationApi>,
355359
voteAccount: VoteAccountAddress,
356360
userWallet: Address,
357361
stakeAmount: bigint,

0 commit comments

Comments
 (0)