@solana/kora 0.2.0 → 0.3.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,25 +1,24 @@
1
- import { getCreateAccountInstruction } from '@solana-program/system';
2
- import { findAssociatedTokenPda, getCreateAssociatedTokenIdempotentInstructionAsync, getInitializeMintInstruction, getMintSize, getMintToInstruction, TOKEN_PROGRAM_ADDRESS, } from '@solana-program/token';
3
- import { airdropFactory, createSolanaRpc, createSolanaRpcSubscriptions, lamports, sendAndConfirmTransactionFactory, pipe, createTransactionMessage, setTransactionMessageLifetimeUsingBlockhash, setTransactionMessageFeePayerSigner, appendTransactionMessageInstructions, signTransactionMessageWithSigners, getSignatureFromTransaction, assertIsAddress, createKeyPairSignerFromBytes, getBase58Encoder, assertIsSendableTransaction, assertIsTransactionWithBlockhashLifetime, } from '@solana/kit';
4
- import { updateOrAppendSetComputeUnitLimitInstruction, updateOrAppendSetComputeUnitPriceInstruction, MAX_COMPUTE_UNIT_LIMIT, } from '@solana-program/compute-budget';
1
+ import { assertIsAddress, createKeyPairSignerFromBytes, getBase58Encoder, lamports, } from '@solana/kit';
2
+ import { createClient } from '@solana/kit-client-litesvm';
3
+ import { tokenProgram, associatedTokenProgram } from '@solana-program/token';
5
4
  import { config } from 'dotenv';
6
5
  import path from 'path';
7
6
  import { KoraClient } from '../src/index.js';
8
7
  config({ path: path.resolve(process.cwd(), '.env') });
9
8
  const DEFAULTS = {
10
9
  DECIMALS: 6,
11
- TOKEN_DROP_AMOUNT: 100_000,
10
+ // Make sure this matches the USDC mint in kora.toml (9BgeTKqmFsPVnfYscfM6NvsgmZxei7XfdciShQ6D3bxJ)
11
+ DESTINATION_ADDRESS: 'AVmDft8deQEo78bRKcGN5ZMf3hyjeLBK4Rd4xGB46yQM',
12
+ // DO NOT USE THESE KEYPAIRS IN PRODUCTION, TESTING KEYPAIRS ONLY
13
+ KORA_ADDRESS: '7AqpcUvgJ7Kh1VmJZ44rWp2XDow33vswo9VK9VqpPU2d',
12
14
  KORA_RPC_URL: 'http://localhost:8080/',
13
- SOLANA_RPC_URL: 'http://127.0.0.1:8899',
14
- SOLANA_WS_URL: 'ws://127.0.0.1:8900',
15
- COMMITMENT: 'processed',
15
+ KORA_SIGNER_TYPE: 'memory',
16
+ // Make sure this matches the kora-rpc signer address on launch (root .env)
17
+ SENDER_SECRET: 'tzgfgSWTE3KUA6qfRoFYLaSfJm59uUeZRDy4ybMrLn1JV2drA1mftiaEcVFvq1Lok6h6EX2C4Y9kSKLvQWyMpS5',
16
18
  SOL_DROP_AMOUNT: 1_000_000_000,
17
- // DO NOT USE THESE KEYPAIRS IN PRODUCTION, TESTING KEYPAIRS ONLY
18
- KORA_ADDRESS: '7AqpcUvgJ7Kh1VmJZ44rWp2XDow33vswo9VK9VqpPU2d', // Make sure this matches the kora-rpc signer address on launch (root .env)
19
- SENDER_SECRET: 'tzgfgSWTE3KUA6qfRoFYLaSfJm59uUeZRDy4ybMrLn1JV2drA1mftiaEcVFvq1Lok6h6EX2C4Y9kSKLvQWyMpS5', // HhA5j2rRiPbMrpF2ZD36r69FyZf3zWmEHRNSZbbNdVjf
20
- TEST_USDC_MINT_SECRET: '59kKmXphL5UJANqpFFjtH17emEq3oRNmYsx6a3P3vSGJRmhMgVdzH77bkNEi9bArRViT45e8L2TsuPxKNFoc3Qfg', // Make sure this matches the USDC mint in kora.toml (9BgeTKqmFsPVnfYscfM6NvsgmZxei7XfdciShQ6D3bxJ)
21
- DESTINATION_ADDRESS: 'AVmDft8deQEo78bRKcGN5ZMf3hyjeLBK4Rd4xGB46yQM',
22
- KORA_SIGNER_TYPE: 'memory', // Default signer type
19
+ // HhA5j2rRiPbMrpF2ZD36r69FyZf3zWmEHRNSZbbNdVjf
20
+ TEST_USDC_MINT_SECRET: '59kKmXphL5UJANqpFFjtH17emEq3oRNmYsx6a3P3vSGJRmhMgVdzH77bkNEi9bArRViT45e8L2TsuPxKNFoc3Qfg',
21
+ TOKEN_DROP_AMOUNT: 100_000, // Default signer type
23
22
  };
24
23
  const createKeyPairSignerFromB58Secret = async (b58Secret) => {
25
24
  const base58Encoder = getBase58Encoder();
@@ -51,9 +50,6 @@ export function loadEnvironmentVariables() {
51
50
  }
52
51
  }
53
52
  const koraRpcUrl = process.env.KORA_RPC_URL || DEFAULTS.KORA_RPC_URL;
54
- const solanaRpcUrl = process.env.SOLANA_RPC_URL || DEFAULTS.SOLANA_RPC_URL;
55
- const solanaWsUrl = process.env.SOLANA_WS_URL || DEFAULTS.SOLANA_WS_URL;
56
- const commitment = (process.env.COMMITMENT || DEFAULTS.COMMITMENT);
57
53
  const tokenDecimals = Number(process.env.TOKEN_DECIMALS || DEFAULTS.DECIMALS);
58
54
  const tokenDropAmount = Number(process.env.TOKEN_DROP_AMOUNT || DEFAULTS.TOKEN_DROP_AMOUNT);
59
55
  const solDropAmount = BigInt(process.env.SOL_DROP_AMOUNT || DEFAULTS.SOL_DROP_AMOUNT);
@@ -63,18 +59,15 @@ export function loadEnvironmentVariables() {
63
59
  assertIsAddress(destinationAddress);
64
60
  assertIsAddress(koraAddress);
65
61
  return {
66
- koraRpcUrl,
62
+ destinationAddress,
67
63
  koraAddress,
64
+ koraRpcUrl,
68
65
  koraSignerType,
69
- commitment,
70
- tokenDecimals,
71
- tokenDropAmount,
72
66
  solDropAmount,
73
- solanaRpcUrl,
74
- solanaWsUrl,
75
- testWalletSecret,
76
67
  testUsdcMintSecret,
77
- destinationAddress,
68
+ testWalletSecret,
69
+ tokenDecimals,
70
+ tokenDropAmount,
78
71
  };
79
72
  }
80
73
  async function createKeyPairSigners() {
@@ -82,151 +75,58 @@ async function createKeyPairSigners() {
82
75
  const testWallet = await createKeyPairSignerFromB58Secret(testWalletSecret);
83
76
  const usdcMint = await createKeyPairSignerFromB58Secret(testUsdcMintSecret);
84
77
  return {
78
+ destinationAddress,
85
79
  testWallet,
86
80
  usdcMint,
87
- destinationAddress,
88
81
  };
89
82
  }
90
- const createDefaultTransaction = async (client, feePayer, computeLimit = MAX_COMPUTE_UNIT_LIMIT, feeMicroLamports = 1n) => {
91
- const { value: latestBlockhash } = await client.rpc.getLatestBlockhash().send();
92
- return pipe(createTransactionMessage({ version: 0 }), tx => setTransactionMessageFeePayerSigner(feePayer, tx), tx => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx), tx => updateOrAppendSetComputeUnitPriceInstruction(feeMicroLamports, tx), tx => updateOrAppendSetComputeUnitLimitInstruction(computeLimit, tx));
93
- };
94
- const signAndSendTransaction = async (client, transactionMessage, commitment) => {
95
- const signedTransaction = await signTransactionMessageWithSigners(transactionMessage);
96
- const signature = getSignatureFromTransaction(signedTransaction);
97
- assertIsSendableTransaction(signedTransaction);
98
- assertIsTransactionWithBlockhashLifetime(signedTransaction);
99
- await sendAndConfirmTransactionFactory(client)(signedTransaction, { commitment, skipPreflight: true });
100
- return signature;
101
- };
102
- function safeStringify(obj) {
103
- return JSON.stringify(obj, (key, value) => {
104
- if (typeof value === 'bigint') {
105
- return value.toString();
106
- }
107
- return value;
108
- }, 2);
109
- }
110
- async function sendAndConfirmInstructions(client, payer, instructions, description, commitment = loadEnvironmentVariables().commitment) {
111
- try {
112
- const signature = await pipe(await createDefaultTransaction(client, payer, 200_000), tx => appendTransactionMessageInstructions(instructions, tx), tx => signAndSendTransaction(client, tx, commitment));
113
- return signature;
114
- }
115
- catch (error) {
116
- console.error(safeStringify(error));
117
- throw new Error(`Failed to ${description.toLowerCase()}: ${error instanceof Error ? error.message : 'Unknown error'}`);
118
- }
119
- }
120
- async function initializeToken({ client, mintAuthority, payer, owner, mint, dropAmount, decimals, otherAtaWallets, }) {
121
- // Get Owner ATA
122
- const [ata] = await findAssociatedTokenPda({
123
- mint: mint.address,
124
- owner: owner.address,
125
- tokenProgram: TOKEN_PROGRAM_ADDRESS,
126
- });
127
- // Get Mint size & rent
128
- const mintSpace = BigInt(getMintSize());
129
- const mintRent = await client.rpc.getMinimumBalanceForRentExemption(mintSpace).send();
130
- // Create instructions for new token mint
131
- const baseInstructions = [
132
- // Create the Mint Account
133
- getCreateAccountInstruction({
134
- payer,
135
- newAccount: mint,
136
- lamports: mintRent,
137
- space: mintSpace,
138
- programAddress: TOKEN_PROGRAM_ADDRESS,
139
- }),
140
- // Initialize the Mint
141
- getInitializeMintInstruction({
142
- mint: mint.address,
143
- decimals,
144
- mintAuthority: mintAuthority.address,
145
- }),
146
- // Create Associated Token Account
147
- await getCreateAssociatedTokenIdempotentInstructionAsync({
148
- mint: mint.address,
149
- payer,
150
- owner: owner.address,
151
- }),
152
- // Mint To the Destination Associated Token Account
153
- getMintToInstruction({
154
- mint: mint.address,
155
- token: ata,
156
- amount: BigInt(dropAmount * 10 ** decimals),
157
- mintAuthority,
158
- }),
159
- ];
160
- // Generate Create ATA instructions for other token accounts we wish to add
161
- const otherAtaInstructions = otherAtaWallets
162
- ? await Promise.all(otherAtaWallets.map(async (wallet) => await getCreateAssociatedTokenIdempotentInstructionAsync({
163
- mint: mint.address,
164
- payer,
165
- owner: wallet,
166
- })))
167
- : [];
168
- const alreadyExists = await mintExists(client, mint.address);
169
- let instructions = alreadyExists ? [...otherAtaInstructions] : [...baseInstructions, ...otherAtaInstructions];
170
- await sendAndConfirmInstructions(client, payer, instructions, 'Initialize token and ATAs', 'finalized');
171
- }
172
83
  async function setupTestSuite() {
173
- const { koraAddress, koraRpcUrl, commitment, tokenDecimals, tokenDropAmount, solDropAmount, solanaRpcUrl, solanaWsUrl, } = await loadEnvironmentVariables();
174
- // Load auth config from environment if not provided
84
+ const { koraAddress, koraRpcUrl, tokenDecimals, tokenDropAmount, solDropAmount } = loadEnvironmentVariables();
175
85
  const authConfig = process.env.ENABLE_AUTH === 'true'
176
86
  ? {
177
87
  apiKey: process.env.KORA_API_KEY || 'test-api-key-123',
178
88
  hmacSecret: process.env.KORA_HMAC_SECRET || 'test-hmac-secret-456',
179
89
  }
180
90
  : undefined;
181
- // Create Solana client
182
- const rpc = createSolanaRpc(solanaRpcUrl);
183
- const rpcSubscriptions = createSolanaRpcSubscriptions(solanaWsUrl);
184
- const airdrop = airdropFactory({ rpc, rpcSubscriptions });
185
- const client = { rpc, rpcSubscriptions };
186
- // Get or create keypairs
187
91
  const { testWallet, usdcMint, destinationAddress } = await createKeyPairSigners();
188
- const mintAuthority = testWallet; // test wallet can be used as mint authority for the test
189
- // Airdrop SOL to test sender and kora wallets
190
- await Promise.all([
191
- airdrop({
192
- commitment: 'finalized',
193
- lamports: lamports(solDropAmount),
194
- recipientAddress: koraAddress,
195
- }),
196
- airdrop({
197
- commitment: 'finalized',
198
- lamports: lamports(solDropAmount),
199
- recipientAddress: testWallet.address,
200
- }),
201
- ]);
202
- // Initialize token and ATAs
203
- await initializeToken({
204
- client,
205
- mintAuthority,
206
- payer: mintAuthority,
207
- owner: testWallet,
208
- mint: usdcMint,
209
- dropAmount: tokenDropAmount,
92
+ const client = await createClient({ payer: testWallet }).use(tokenProgram()).use(associatedTokenProgram());
93
+ // Airdrop SOL via LiteSVM
94
+ await client.airdrop(koraAddress, lamports(solDropAmount));
95
+ await client.airdrop(testWallet.address, lamports(solDropAmount));
96
+ // Create mint
97
+ await client.token.instructions
98
+ .createMint({
99
+ newMint: usdcMint,
100
+ decimals: tokenDecimals,
101
+ mintAuthority: testWallet.address,
102
+ })
103
+ .sendTransaction();
104
+ // Mint tokens to testWallet's ATA (auto-creates ATA)
105
+ await client.token.instructions
106
+ .mintToATA({
107
+ mint: usdcMint.address,
108
+ owner: testWallet.address,
109
+ mintAuthority: testWallet,
110
+ amount: BigInt(tokenDropAmount * 10 ** tokenDecimals),
210
111
  decimals: tokenDecimals,
211
- otherAtaWallets: [testWallet.address, koraAddress, destinationAddress],
212
- });
112
+ })
113
+ .sendTransaction();
114
+ // Create ATAs for kora and destination wallets
115
+ for (const owner of [koraAddress, destinationAddress]) {
116
+ await client.associatedToken.instructions
117
+ .createAssociatedTokenIdempotent({
118
+ owner,
119
+ mint: usdcMint.address,
120
+ })
121
+ .sendTransaction();
122
+ }
213
123
  return {
124
+ destinationAddress,
125
+ koraAddress,
214
126
  koraClient: new KoraClient({ rpcUrl: koraRpcUrl, ...authConfig }),
215
127
  koraRpcUrl,
216
128
  testWallet,
217
129
  usdcMint: usdcMint.address,
218
- destinationAddress,
219
- koraAddress,
220
- authConfig,
221
130
  };
222
131
  }
223
- const mintExists = async (client, mint) => {
224
- try {
225
- const mintAccount = await client.rpc.getAccountInfo(mint).send();
226
- return mintAccount.value !== null;
227
- }
228
- catch (error) {
229
- return false;
230
- }
231
- };
232
132
  export default setupTestSuite;