La transaction Solana la plus basique : déplacer des SOL d'un compte à un autre. C'est un bon premier test pour vérifier que votre configuration litesvm-go fonctionne avant de tester une logique de programme plus complexe.
Exemple complet
package mytestimport ("testing"litesvm "github.com/LiteSVM/litesvm-go"solana "github.com/gagliardetto/solana-go""github.com/gagliardetto/solana-go/programs/system")func TestSolTransfer(t *testing.T) {svm, err := litesvm.New()if err != nil {t.Fatal(err)}defer svm.Close()// Generate payer + recipient.priv, err := solana.NewRandomPrivateKey()if err != nil {t.Fatal(err)}payer := priv.PublicKey()recipient := solana.NewWallet().PublicKey()// Airdrop SOL to the payer.if err := svm.Airdrop(payer, 10_000_000_000); err != nil {t.Fatal(err)}// Check initial balance.senderInitial, _, err := svm.Balance(payer)if err != nil {t.Fatal(err)}t.Logf("sender initial: %.3f SOL", float64(senderInitial)/1e9)// Build, sign, and encode the transfer.blockhash, err := svm.LatestBlockhash()if err != nil {t.Fatal(err)}ix := system.NewTransferInstruction(1_000_000_000, payer, recipient).Build()tx, err := solana.NewTransaction([]solana.Instruction{ix},blockhash,solana.TransactionPayer(payer),)if err != nil {t.Fatal(err)}if _, err := tx.Sign(func(k solana.PublicKey) *solana.PrivateKey {if k.Equals(payer) {return &priv}return nil}); err != nil {t.Fatal(err)}txBytes, err := tx.MarshalBinary()if err != nil {t.Fatal(err)}// Execute the transfer.out, err := svm.SendLegacyTransaction(txBytes)if err != nil {t.Fatal(err)}defer out.Close()if !out.IsOk() {t.Fatalf("tx failed: %s\nlogs: %v", out.Error(), out.Logs())}// Check final balances.senderFinal, _, err := svm.Balance(payer)if err != nil {t.Fatal(err)}recipientFinal, _, err := svm.Balance(recipient)if err != nil {t.Fatal(err)}t.Logf("sender final: %.6f SOL", float64(senderFinal)/1e9)t.Logf("recipient final: %.3f SOL", float64(recipientFinal)/1e9)t.Logf("compute units: %d", out.ComputeUnits())t.Logf("fee: %d lamports", out.Fee())}
Résultat attendu
sender initial: 10.000 SOLsender final: 8.999995 SOLrecipient final: 1.000 SOLcompute units: 150fee: 5000 lamports
Le solde final de l'expéditeur est légèrement inférieur à 9 SOL en raison des frais de transaction (5 000 lamports par signature).
Ceci couvre le type de transaction le plus simple. Pour tester avec des programmes personnalisés, consultez Tests de programmes. Pour la logique dépendante du temps, consultez Tests temporels.
Is this page helpful?