@@ -5,14 +5,7 @@ import {Account} from '../src/account';
55import { mockRpc } from './__mocks__/node-fetch' ;
66
77const url = 'http://master.testnet.solana.com:8899' ;
8-
9- // Define DOITLIVE in the environment to test against the real full node
10- // identified by `url` instead of using the mock
11- if ( process . env . DOITLIVE ) {
12- console . log ( `Note: node-fetch mock is disabled, testing live against ${ url } ` ) ;
13- } else {
14- jest . mock ( 'node-fetch' ) ;
15- }
8+ //const url = 'http://localhost:8899';
169
1710const errorMessage = 'Invalid request' ;
1811const errorResponse = {
@@ -22,6 +15,7 @@ const errorResponse = {
2215 result : undefined ,
2316} ;
2417
18+
2519test ( 'get balance' , async ( ) => {
2620 const account = new Account ( ) ;
2721 const connection = new Connection ( url ) ;
@@ -109,7 +103,7 @@ test('get last Id', async () => {
109103 } ,
110104 {
111105 error : null ,
112- result : '1111111111111111111111111111111111111111111111 ' ,
106+ result : '2BjEqiiT43J6XskiHdz7aoocjPeWkCPiKD72SiFQsrA2 ' ,
113107 }
114108 ]
115109 ) ;
@@ -200,35 +194,125 @@ test('request airdrop - error', () => {
200194 . rejects . toThrow ( errorMessage ) ;
201195} ) ;
202196
203- test ( 'send transaction - error' , ( ) => {
204- const secretKey = Buffer . from ( [
205- 153 , 218 , 149 , 89 , 225 , 94 , 145 , 62 , 233 , 171 , 46 , 83 , 227 ,
206- 223 , 173 , 87 , 93 , 163 , 59 , 73 , 190 , 17 , 37 , 187 , 146 , 46 , 51 ,
207- 73 , 79 , 73 , 136 , 40 , 27 , 47 , 73 , 9 , 110 , 62 , 93 , 189 , 15 , 207 ,
208- 169 , 192 , 192 , 205 , 146 , 217 , 171 , 59 , 33 , 84 , 75 , 52 , 213 , 221 ,
209- 74 , 101 , 217 , 139 , 135 , 139 , 153 , 34
210- ] ) ;
211- const account = new Account ( secretKey ) ;
197+ test ( 'transaction' , async ( ) => {
198+ const accountFrom = new Account ( ) ;
199+ const accountTo = new Account ( ) ;
212200 const connection = new Connection ( url ) ;
213201
214202 mockRpc . push ( [
215203 url ,
216204 {
217- method : 'sendTransaction' ,
218- params : [ [
219- 78 , 52 , 48 , 146 , 162 , 213 , 83 , 169 , 128 , 10 , 82 , 26 , 145 , 238 ,
220- 1 , 130 , 16 , 44 , 249 , 99 , 121 , 55 , 217 , 72 , 77 , 41 , 73 , 227 , 5 ,
221- 15 , 125 , 212 , 186 , 157 , 182 , 100 , 232 , 232 , 39 , 84 , 5 , 121 , 172 ,
222- 137 , 177 , 248 , 188 , 224 , 196 , 102 , 204 , 43 , 128 , 243 , 170 , 157 ,
223- 134 , 216 , 209 , 8 , 211 , 209 , 44 , 1
224- ] ] ,
205+ method : 'requestAirdrop' ,
206+ params : [ accountFrom . publicKey , 12 ] ,
225207 } ,
226- errorResponse ,
208+ {
209+ error : null ,
210+ result : true ,
211+ }
227212 ] ) ;
213+ mockRpc . push ( [
214+ url ,
215+ {
216+ method : 'getBalance' ,
217+ params : [ accountFrom . publicKey ] ,
218+ } ,
219+ {
220+ error : null ,
221+ result : 12 ,
222+ }
223+ ] ) ;
224+ await connection . requestAirdrop ( accountFrom . publicKey , 12 ) ;
225+ expect ( await connection . getBalance ( accountFrom . publicKey ) ) . toBe ( 12 ) ;
228226
227+ mockRpc . push ( [
228+ url ,
229+ {
230+ method : 'requestAirdrop' ,
231+ params : [ accountTo . publicKey , 21 ] ,
232+ } ,
233+ {
234+ error : null ,
235+ result : true ,
236+ }
237+ ] ) ;
238+ mockRpc . push ( [
239+ url ,
240+ {
241+ method : 'getBalance' ,
242+ params : [ accountTo . publicKey ] ,
243+ } ,
244+ {
245+ error : null ,
246+ result : 21 ,
247+ }
248+ ] ) ;
249+ await connection . requestAirdrop ( accountTo . publicKey , 21 ) ;
250+ expect ( await connection . getBalance ( accountTo . publicKey ) ) . toBe ( 21 ) ;
229251
230- expect ( connection . sendTokens ( account , account . publicKey , 123 ) )
231- . rejects . toThrow ( errorMessage ) ;
232- } ) ;
252+ mockRpc . push ( [
253+ url ,
254+ {
255+ method : 'getLastId' ,
256+ params : [ ] ,
257+ } ,
258+ {
259+ error : null ,
260+ result : '2BjEqiiT43J6XskiHdz7aoocjPeWkCPiKD72SiFQsrA2' ,
261+ }
262+ ]
263+ ) ;
264+ mockRpc . push ( [
265+ url ,
266+ {
267+ method : 'sendTransaction' ,
268+ } ,
269+ {
270+ error : null ,
271+ result : '3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk' ,
272+ }
273+ ]
274+ ) ;
275+ const signature = await connection . sendTokens ( accountFrom , accountTo . publicKey , 10 ) ;
276+
277+ mockRpc . push ( [
278+ url ,
279+ {
280+ method : 'confirmTransaction' ,
281+ params : [
282+ '3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk'
283+ ] ,
284+ } ,
285+ {
286+ error : null ,
287+ result : true ,
288+ }
289+ ]
290+ ) ;
291+ expect ( connection . confirmTransaction ( signature ) ) . resolves . toBe ( true ) ;
233292
293+ mockRpc . push ( [
294+ url ,
295+ {
296+ method : 'getBalance' ,
297+ params : [ accountFrom . publicKey ] ,
298+ } ,
299+ {
300+ error : null ,
301+ result : 2 ,
302+ }
303+ ] ) ;
304+ expect ( await connection . getBalance ( accountFrom . publicKey ) ) . toBe ( 2 ) ;
234305
306+ mockRpc . push ( [
307+ url ,
308+ {
309+ method : 'getBalance' ,
310+ params : [ accountTo . publicKey ] ,
311+ } ,
312+ {
313+ error : null ,
314+ result : 31 ,
315+ }
316+ ] ) ;
317+ expect ( await connection . getBalance ( accountTo . publicKey ) ) . toBe ( 31 ) ;
318+ } ) ;
0 commit comments