-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolana-signer.spec.ts
328 lines (288 loc) · 12.5 KB
/
solana-signer.spec.ts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import { afterEach, beforeAll, describe, expect, it } from '@jest/globals';
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
import {
balanceAccountNonce,
createHolderAccountTransaction,
createScheduledNeonEvmMultipleTransaction,
createScheduledNeonEvmTransaction,
delay,
FaucetDropper,
GasToken,
getGasToken,
getProxyState,
holderAddressWithSeed,
log,
logJson,
MultipleTransactions,
NeonChainId,
NeonClientApi,
NeonProgramStatus,
NeonProxyRpcApi,
NO_CHILD_INDEX,
ScheduledTransaction,
sendSolanaTransaction,
solanaAirdrop,
SolanaNeonAccount
} from '@neonevm/solana-sign';
import { BaseContract } from '@neonevm/solana-contracts';
import { JsonRpcProvider } from 'ethers';
import { config } from 'dotenv';
import bs58 from 'bs58';
config({ path: '.env' });
const NEON_API_RPC_URL = `${process.env.NEON_CORE_API_RPC_URL!}/sol`;
const NEON_CLIENT_API_URL = process.env.NEON_CORE_API_URL!;
const SOLANA_DEVNET_URL = process.env.SOLANA_URL!;
const NEON_FAUCET_URL = process.env.NEON_FAUCET_URL!;
const SOLANA_WALLET = process.env.SOLANA_WALLET!;
let connection: Connection;
let neonProxyRpcApi: NeonProxyRpcApi;
let neonClientApi: NeonClientApi;
let provider: JsonRpcProvider;
let neonEvmProgram: PublicKey;
let proxyStatus: NeonProgramStatus;
let chainId: number;
let chainTokenMint: PublicKey;
let gasToken: GasToken;
let faucet: FaucetDropper;
let solanaUser: SolanaNeonAccount;
let baseContract: BaseContract;
let skipPreflight = false;
let globalNonce: number = 0;
beforeAll(async () => {
const result = await getProxyState(NEON_API_RPC_URL);
const token = getGasToken(result.tokensList, NeonChainId.testnetSol);
const keypair = Keypair.fromSecretKey(bs58.decode(SOLANA_WALLET));
connection = new Connection(SOLANA_DEVNET_URL, 'confirmed');
provider = new JsonRpcProvider(NEON_API_RPC_URL!);
neonClientApi = new NeonClientApi(NEON_CLIENT_API_URL);
neonProxyRpcApi = result.proxyApi;
neonEvmProgram = result.evmProgramAddress;
proxyStatus = result.proxyStatus;
chainId = Number(token.gasToken.tokenChainId);
chainTokenMint = new PublicKey(token.gasToken.tokenMint);
gasToken = token.gasToken;
faucet = new FaucetDropper(NEON_FAUCET_URL);
solanaUser = SolanaNeonAccount.fromKeypair(keypair, neonEvmProgram, chainTokenMint, chainId);
baseContract = new BaseContract(chainId);
log(`Solana wallet: ${solanaUser.publicKey.toBase58()}; ${bs58.encode(solanaUser.keypair.secretKey)}`);
log(`Neon wallet: ${solanaUser.neonWallet}; Balance Account: ${solanaUser.balanceAddress.toBase58()}`);
await solanaAirdrop(connection, solanaUser.publicKey, 100e9);
await solanaUser.balanceAccountCreate(connection);
});
afterEach(async () => {
const nonce = Number(await neonProxyRpcApi.getTransactionCount(solanaUser.neonWallet));
if (nonce > globalNonce) {
globalNonce = nonce;
} else {
await delay(9e3);
}
});
describe('Check Solana signer instructions', () => {
it(`Create ScheduledTransaction and sign with Solana`, async () => {
const neonBalanceAccountNonce = await balanceAccountNonce(connection, solanaUser.neonWallet, neonEvmProgram, chainId);
log('Balance account nonce', neonBalanceAccountNonce);
const nonce = Number(await neonProxyRpcApi.getTransactionCount(solanaUser.neonWallet));
const maxFeePerGas = 0x77359400;
log(`Neon wallet ${solanaUser.neonWallet} nonce: ${nonce}`);
const scheduledTransaction = new ScheduledTransaction({
nonce: nonce,
payer: solanaUser.neonWallet,
target: baseContract.address,
callData: baseContract.transactionData(solanaUser.publicKey),
maxFeePerGas: maxFeePerGas,
chainId
});
log(`Scheduled transaction`, scheduledTransaction.serialize(), scheduledTransaction.hash());
const createScheduledTransaction = await createScheduledNeonEvmTransaction({
chainId,
signerAddress: solanaUser.publicKey,
tokenMintAddress: solanaUser.tokenMint,
neonEvmProgram,
neonWallet: solanaUser.neonWallet,
neonWalletNonce: nonce,
neonTransaction: scheduledTransaction.serialize()
});
// for this test, we check that the pool account has tokens on test stand
const treasuryPool = createScheduledTransaction.instructions[0].keys[2].pubkey;
await solanaAirdrop(connection, treasuryPool, 20e9);
await sendSolanaTransaction(connection, createScheduledTransaction, [solanaUser.signer!], true, {
skipPreflight,
preflightCommitment: 'confirmed'
}, 'scheduled');
const transactions = await neonClientApi.waitTransactionTreeExecution({
address: solanaUser.neonWallet,
chain_id: chainId
}, nonce, 2e3);
log(`Scheduled transactions result`, transactions);
for (const { transaction_hash, status } of transactions) {
const { result } = await neonProxyRpcApi.getTransactionReceipt(`0x${transaction_hash}`);
logJson(result);
expect(status).toBe('Success');
}
});
it(`Send raw ScheduledTransaction and sign with Solana`, async () => {
const nonce = Number(await neonProxyRpcApi.getTransactionCount(solanaUser.neonWallet));
const maxFeePerGas = 0x77359400;
log(`Neon wallet ${solanaUser.neonWallet} nonce: ${nonce}`);
const transaction = new ScheduledTransaction({
nonce: nonce,
payer: solanaUser.neonWallet,
index: 0,
target: baseContract.address,
callData: baseContract.transactionData(solanaUser.publicKey),
maxFeePerGas: maxFeePerGas,
chainId
});
log(`Scheduled transaction`, transaction.serialize(), transaction.hash());
const multiple = new MultipleTransactions(nonce, maxFeePerGas);
multiple.addTransaction(transaction, NO_CHILD_INDEX, 0);
const createScheduledTransaction = await createScheduledNeonEvmMultipleTransaction({
chainId,
neonEvmProgram,
neonTransaction: multiple.data,
signerAddress: solanaUser.publicKey,
tokenMintAddress: solanaUser.tokenMint,
neonWallet: solanaUser.neonWallet,
neonWalletNonce: nonce
});
// for this test, we check that the pool account has tokens on test stand
const treasuryPool = createScheduledTransaction.instructions[0].keys[2].pubkey;
await solanaAirdrop(connection, treasuryPool, 20e9);
await sendSolanaTransaction(connection, createScheduledTransaction, [solanaUser.signer!], true, { skipPreflight }, 'scheduled');
await delay(2e3);
const { result } = await neonProxyRpcApi.sendRawScheduledTransaction(`0x${transaction.serialize()}`);
log(result);
const transactions = await neonClientApi.waitTransactionTreeExecution({
address: solanaUser.neonWallet,
chain_id: chainId
}, nonce, 5e3);
log(`Scheduled transactions result`, transactions);
for (const { transaction_hash, status } of transactions) {
const { result } = await neonProxyRpcApi.getTransactionReceipt(`0x${transaction_hash}`);
logJson(result);
expect(status).toBe('Success');
}
});
it(`Send two depended ScheduledTransactions and sign with Solana`, async () => {
const nonce = Number(await neonProxyRpcApi.getTransactionCount(solanaUser.neonWallet));
const maxFeePerGas = 0x77359400;
log(`Neon wallet ${solanaUser.neonWallet} nonce: ${nonce}`);
const trx0 = new ScheduledTransaction({
nonce: nonce,
payer: solanaUser.neonWallet,
index: 0,
target: baseContract.address,
callData: baseContract.transactionData(solanaUser.publicKey),
maxFeePerGas: maxFeePerGas,
chainId
});
const trx1 = new ScheduledTransaction({
nonce: nonce,
payer: solanaUser.neonWallet,
index: 1,
target: baseContract.address,
callData: baseContract.transactionData(solanaUser.publicKey),
maxFeePerGas: maxFeePerGas,
chainId
});
log(`Scheduled transaction 0`, trx0.serialize(), trx0.hash());
log(`Scheduled transaction 1`, trx1.serialize(), trx1.hash());
const multiple = new MultipleTransactions(nonce, maxFeePerGas);
multiple.addTransaction(trx0, 1, 0);
multiple.addTransaction(trx1, NO_CHILD_INDEX, 1);
const createScheduledTransaction = await createScheduledNeonEvmMultipleTransaction({
chainId,
neonEvmProgram,
neonTransaction: multiple.data,
signerAddress: solanaUser.publicKey,
tokenMintAddress: solanaUser.tokenMint,
neonWallet: solanaUser.neonWallet,
neonWalletNonce: nonce
});
// for this test, we check that the pool account has tokens on test stand
const treasuryPool = createScheduledTransaction.instructions[0].keys[2].pubkey;
await solanaAirdrop(connection, treasuryPool, 20e9);
await sendSolanaTransaction(connection, createScheduledTransaction, [solanaUser.signer!], true, { skipPreflight }, 'scheduled');
await delay(2e3);
const transaction1 = await neonProxyRpcApi.sendRawScheduledTransaction(`0x${trx0.serialize()}`);
const transaction2 = await neonProxyRpcApi.sendRawScheduledTransaction(`0x${trx1.serialize()}`);
log(transaction1.result, transaction2.result);
const transactions = await neonClientApi.waitTransactionTreeExecution({
address: solanaUser.neonWallet,
chain_id: chainId
}, nonce, 5e3);
log(`Scheduled transactions result`, transactions);
for (const { transaction_hash, status } of transactions) {
const { result } = await neonProxyRpcApi.getTransactionReceipt(`0x${transaction_hash}`);
logJson(result);
expect(status).toBe('Success');
}
});
it(`Send tree parallel ScheduledTransactions and sign with Solana`, async () => {
const nonce = Number(await neonProxyRpcApi.getTransactionCount(solanaUser.neonWallet));
const maxFeePerGas = 0x77359400;
log(`Neon wallet ${solanaUser.neonWallet} nonce: ${nonce}`);
const trxs = [];
for (let i = 0; i < 4; i++) {
const trx = new ScheduledTransaction({
nonce: nonce,
payer: solanaUser.neonWallet,
index: i,
target: baseContract.address,
callData: baseContract.transactionData(solanaUser.publicKey),
maxFeePerGas: maxFeePerGas,
chainId
});
trxs.push(trx);
log(`Scheduled transaction ${i}`, trx.serialize(), trx.hash());
}
const multiple = new MultipleTransactions(nonce, maxFeePerGas);
multiple.addTransaction(trxs[0], 3, 0);
multiple.addTransaction(trxs[1], 3, 0);
multiple.addTransaction(trxs[2], 3, 0);
multiple.addTransaction(trxs[3], NO_CHILD_INDEX, 3);
const createScheduledTransaction = await createScheduledNeonEvmMultipleTransaction({
chainId,
neonEvmProgram,
neonTransaction: multiple.data,
signerAddress: solanaUser.publicKey,
tokenMintAddress: solanaUser.tokenMint,
neonWallet: solanaUser.neonWallet,
neonWalletNonce: nonce
});
// for this test, we check that the pool account has tokens on test stand
const treasuryPool = createScheduledTransaction.instructions[0].keys[2].pubkey;
await solanaAirdrop(connection, treasuryPool, 20e9);
await sendSolanaTransaction(connection, createScheduledTransaction, [solanaUser.signer!], true, { skipPreflight }, 'scheduled');
await delay(2e3);
const result = await neonProxyRpcApi.sendRawScheduledTransactions(trxs.map(t => t.serialize()));
logJson(result);
const transactions = await neonClientApi.waitTransactionTreeExecution({
address: solanaUser.neonWallet,
chain_id: chainId
}, nonce, 9e3);
log(`Scheduled transactions result`, transactions);
for (const { transaction_hash, status } of transactions) {
const { result } = await neonProxyRpcApi.getTransactionReceipt(`0x${transaction_hash}`);
logJson(result);
expect(status).toBe('Success');
}
});
it(`Check if we have pending transactions`, async () => {
const response = await neonProxyRpcApi.getPendingTransactions(solanaUser.publicKey);
console.log(response);
});
it.skip(`Create holder account`, async () => {
const solanaUser = SolanaNeonAccount.fromKeypair(Keypair.generate(), neonEvmProgram, chainTokenMint, chainId);
await solanaAirdrop(connection, solanaUser.publicKey, 1e10);
const [holderAccount, holderSeed] = await holderAddressWithSeed(neonEvmProgram, solanaUser.publicKey);
let account = await connection.getAccountInfo(holderAccount);
if (!account) {
const transaction = await createHolderAccountTransaction(neonEvmProgram, solanaUser.publicKey, holderAccount, holderSeed);
await sendSolanaTransaction(connection, transaction, [solanaUser.signer!], false, { skipPreflight });
account = await connection.getAccountInfo(holderAccount);
}
expect(account).not.toBeNull();
log(await neonClientApi.getHolder(holderAccount));
});
});