1+ import { describe , test } from 'node:test' ;
2+ import { assert } from 'chai' ;
3+ import { deployProgramShell , parseProgramID } from './deploy' ;
4+
5+ import {
6+ Connection ,
7+ Keypair ,
8+ PublicKey ,
9+ sendAndConfirmTransaction ,
10+ Transaction ,
11+ TransactionInstruction ,
12+ } from '@solana/web3.js' ;
13+
14+ function createKeypairFromFile ( path : string ) : Keypair {
15+ return Keypair . fromSecretKey (
16+ Buffer . from ( JSON . parse ( require ( 'fs' ) . readFileSync ( path , "utf-8" ) ) )
17+ )
18+ } ;
19+
20+ function getInstructionData ( path : string ) : Buffer {
21+ // instruction_data (See: sui/external-crates/move/solana/move-mv-llvm-compiler/docs/Entrypoint.md)
22+ let j = JSON . parse ( require ( 'fs' ) . readFileSync ( path , "utf-8" ) ) ;
23+ return j [ 'instruction_data' ] ;
24+ }
25+
26+ async function deployProgram ( programPath : string ) : Promise < string > {
27+ const programIdLog = await deployProgramShell ( programPath ) ;
28+ const programId = await parseProgramID ( programIdLog ) ;
29+ if ( programId ) {
30+ console . log ( 'Program deployed with' , programId ) ;
31+ return programId ;
32+ }
33+ console . log ( 'Program could not be deployed' ) ;
34+ return null ;
35+ }
36+
37+ describe ( "hello-solana" , async ( ) => {
38+ // Loading these from local files for development
39+ const connection = new Connection ( `http://localhost:8899` , 'confirmed' ) ;
40+ const payer = createKeypairFromFile ( require ( 'os' ) . homedir ( ) + '/.config/solana/id.json' ) ;
41+ // PublicKey of the deployed program.
42+ const programPath = 'bin/hello_solana_move_program.so' ;
43+ const programIdStr = await deployProgram ( programPath ) ;
44+ const programId = new PublicKey ( programIdStr ) ;
45+ const instructionData = getInstructionData ( 'bin/input.json' ) ;
46+
47+ it ( "Say hello!" , async ( ) => {
48+ // Set up transaction instructions first.
49+ let ix = new TransactionInstruction ( {
50+ keys : [
51+ { pubkey : payer . publicKey , isSigner : true , isWritable : true }
52+ ] ,
53+ programId,
54+ data : instructionData ,
55+ } ) ;
56+
57+ // Send the transaction over RPC
58+ let signature = await sendAndConfirmTransaction (
59+ connection ,
60+ new Transaction ( ) . add ( ix ) , // Add our instruction (you can add more than one)
61+ [ payer ]
62+ ) ;
63+
64+ let transaction = await connection . getTransaction ( signature , { commitment : "confirmed" } ) ;
65+ console . log ( transaction ) ;
66+ assert ( transaction ?. meta ?. logMessages [ 0 ] . startsWith ( `Program ${ programId } ` ) ) ;
67+ // 'Hello Solana' as bytes
68+ assert ( transaction ?. meta ?. logMessages [ 1 ] === 'Program log: 0000000000000000000000000000000000000000000000000000000000000001::string::String { bytes: [72, 101, 108, 108, 111, 32, 83, 111, 108, 97, 110, 97], }' ) ;
69+ assert ( transaction ?. meta ?. logMessages [ 2 ] === `Program ${ programId } consumed 5331 of 200000 compute units` ) ;
70+ assert ( transaction ?. meta ?. logMessages [ 3 ] === `Program ${ programId } success` ) ;
71+ } ) ;
72+ } ) ;
0 commit comments