@solana/kora 0.2.0-beta.6 → 0.2.1
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.
- package/dist/src/client.d.ts +7 -201
- package/dist/src/client.js +18 -221
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/kit/executor.js +5 -18
- package/dist/src/kit/index.d.ts +1 -4
- package/dist/src/kit/index.js +2 -10
- package/dist/src/kit/payment.js +2 -3
- package/dist/src/kit/plugin.d.ts +31 -0
- package/dist/src/{plugin.js → kit/plugin.js} +18 -88
- package/dist/src/types/index.d.ts +79 -170
- package/dist/test/auth-setup.js +4 -4
- package/dist/test/integration.test.js +291 -168
- package/dist/test/kit-client.test.js +0 -18
- package/dist/test/plugin.test.js +71 -126
- package/dist/test/setup.d.ts +15 -8
- package/dist/test/setup.js +152 -52
- package/dist/test/unit.test.js +158 -256
- package/package.json +16 -8
- package/dist/src/plugin.d.ts +0 -85
package/dist/test/setup.js
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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';
|
|
4
5
|
import { config } from 'dotenv';
|
|
5
6
|
import path from 'path';
|
|
6
7
|
import { KoraClient } from '../src/index.js';
|
|
7
8
|
config({ path: path.resolve(process.cwd(), '.env') });
|
|
8
9
|
const DEFAULTS = {
|
|
9
10
|
DECIMALS: 6,
|
|
10
|
-
|
|
11
|
-
DESTINATION_ADDRESS: 'AVmDft8deQEo78bRKcGN5ZMf3hyjeLBK4Rd4xGB46yQM',
|
|
12
|
-
// DO NOT USE THESE KEYPAIRS IN PRODUCTION, TESTING KEYPAIRS ONLY
|
|
13
|
-
KORA_ADDRESS: '7AqpcUvgJ7Kh1VmJZ44rWp2XDow33vswo9VK9VqpPU2d',
|
|
11
|
+
TOKEN_DROP_AMOUNT: 100_000,
|
|
14
12
|
KORA_RPC_URL: 'http://localhost:8080/',
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
SOLANA_RPC_URL: 'http://127.0.0.1:8899',
|
|
14
|
+
SOLANA_WS_URL: 'ws://127.0.0.1:8900',
|
|
15
|
+
COMMITMENT: 'processed',
|
|
18
16
|
SOL_DROP_AMOUNT: 1_000_000_000,
|
|
19
|
-
//
|
|
20
|
-
|
|
21
|
-
|
|
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
|
|
22
23
|
};
|
|
23
24
|
const createKeyPairSignerFromB58Secret = async (b58Secret) => {
|
|
24
25
|
const base58Encoder = getBase58Encoder();
|
|
@@ -50,6 +51,9 @@ export function loadEnvironmentVariables() {
|
|
|
50
51
|
}
|
|
51
52
|
}
|
|
52
53
|
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);
|
|
53
57
|
const tokenDecimals = Number(process.env.TOKEN_DECIMALS || DEFAULTS.DECIMALS);
|
|
54
58
|
const tokenDropAmount = Number(process.env.TOKEN_DROP_AMOUNT || DEFAULTS.TOKEN_DROP_AMOUNT);
|
|
55
59
|
const solDropAmount = BigInt(process.env.SOL_DROP_AMOUNT || DEFAULTS.SOL_DROP_AMOUNT);
|
|
@@ -59,15 +63,18 @@ export function loadEnvironmentVariables() {
|
|
|
59
63
|
assertIsAddress(destinationAddress);
|
|
60
64
|
assertIsAddress(koraAddress);
|
|
61
65
|
return {
|
|
62
|
-
destinationAddress,
|
|
63
|
-
koraAddress,
|
|
64
66
|
koraRpcUrl,
|
|
67
|
+
koraAddress,
|
|
65
68
|
koraSignerType,
|
|
66
|
-
|
|
67
|
-
testUsdcMintSecret,
|
|
68
|
-
testWalletSecret,
|
|
69
|
+
commitment,
|
|
69
70
|
tokenDecimals,
|
|
70
71
|
tokenDropAmount,
|
|
72
|
+
solDropAmount,
|
|
73
|
+
solanaRpcUrl,
|
|
74
|
+
solanaWsUrl,
|
|
75
|
+
testWalletSecret,
|
|
76
|
+
testUsdcMintSecret,
|
|
77
|
+
destinationAddress,
|
|
71
78
|
};
|
|
72
79
|
}
|
|
73
80
|
async function createKeyPairSigners() {
|
|
@@ -75,58 +82,151 @@ async function createKeyPairSigners() {
|
|
|
75
82
|
const testWallet = await createKeyPairSignerFromB58Secret(testWalletSecret);
|
|
76
83
|
const usdcMint = await createKeyPairSignerFromB58Secret(testUsdcMintSecret);
|
|
77
84
|
return {
|
|
78
|
-
destinationAddress,
|
|
79
85
|
testWallet,
|
|
80
86
|
usdcMint,
|
|
87
|
+
destinationAddress,
|
|
81
88
|
};
|
|
82
89
|
}
|
|
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
|
+
}
|
|
83
172
|
async function setupTestSuite() {
|
|
84
|
-
const { koraAddress, koraRpcUrl, tokenDecimals, tokenDropAmount, solDropAmount } = loadEnvironmentVariables();
|
|
173
|
+
const { koraAddress, koraRpcUrl, commitment, tokenDecimals, tokenDropAmount, solDropAmount, solanaRpcUrl, solanaWsUrl, } = await loadEnvironmentVariables();
|
|
174
|
+
// Load auth config from environment if not provided
|
|
85
175
|
const authConfig = process.env.ENABLE_AUTH === 'true'
|
|
86
176
|
? {
|
|
87
177
|
apiKey: process.env.KORA_API_KEY || 'test-api-key-123',
|
|
88
178
|
hmacSecret: process.env.KORA_HMAC_SECRET || 'test-hmac-secret-456',
|
|
89
179
|
}
|
|
90
180
|
: 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
|
|
91
187
|
const { testWallet, usdcMint, destinationAddress } = await createKeyPairSigners();
|
|
92
|
-
const
|
|
93
|
-
// Airdrop SOL
|
|
94
|
-
await
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
mintAuthority
|
|
110
|
-
|
|
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,
|
|
111
210
|
decimals: tokenDecimals,
|
|
112
|
-
|
|
113
|
-
|
|
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
|
-
}
|
|
211
|
+
otherAtaWallets: [testWallet.address, koraAddress, destinationAddress],
|
|
212
|
+
});
|
|
123
213
|
return {
|
|
124
|
-
destinationAddress,
|
|
125
|
-
koraAddress,
|
|
126
214
|
koraClient: new KoraClient({ rpcUrl: koraRpcUrl, ...authConfig }),
|
|
127
215
|
koraRpcUrl,
|
|
128
216
|
testWallet,
|
|
129
217
|
usdcMint: usdcMint.address,
|
|
218
|
+
destinationAddress,
|
|
219
|
+
koraAddress,
|
|
220
|
+
authConfig,
|
|
130
221
|
};
|
|
131
222
|
}
|
|
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
|
+
};
|
|
132
232
|
export default setupTestSuite;
|