Browse Source

Add direct calling to the System program for creating accounts

Dadepo Aderemi 3 years ago
parent
commit
6178ad1ce6
2 changed files with 25 additions and 1 deletions
  1. 6 0
      basics/create-account/README.md
  2. 19 1
      basics/create-account/native/tests/test.ts

+ 6 - 0
basics/create-account/README.md

@@ -4,6 +4,12 @@
    
 This account is going to be a **system account** - meaning it will be owned by the System Program. In short, this means only the System Program will be allowed to modify it's data.   
 
+In the test, we use two methods for creating the accounts. One of the methods uses Cross program invocation and the other calls the System Program directly. 
+
+Cross program invocation means that we send the transaction to create the account first to our deployed Solana Program, which then calls the System Program. See [here](https://github.com/solana-developers/program-examples/tree/main/basics/cross-program-invocation) for more Cross Program Invocation examples. 
+
+Calling the System Program directly means that the client sends the transaction to create the account directly to the Solana Program
+   
 In this example, this account will simply hold some SOL.
 
 ### Links:

+ 19 - 1
basics/create-account/native/tests/test.ts

@@ -1,6 +1,7 @@
 import {
     Connection,
     Keypair,
+    LAMPORTS_PER_SOL,
     PublicKey,
     sendAndConfirmTransaction,
     SystemProgram,
@@ -25,7 +26,7 @@ describe("Create a system account", async () => {
         "Au21huMZuDQrbzu2Ec5ohpW5CKRqhcGV6qLawfydStGs"
     );
   
-    it("Create the account", async () => {
+    it("Create the account via a cross program invocation", async () => {
 
         const newKeypair = Keypair.generate();
 
@@ -45,5 +46,22 @@ describe("Create a system account", async () => {
             [payer, newKeypair]
         );
     });
+
+    it("Create the account via direct call to system program", async () => {
+
+        const newKeypair = Keypair.generate();
+
+        const ix = SystemProgram.createAccount({
+            fromPubkey: payer.publicKey,
+            newAccountPubkey: newKeypair.publicKey,
+            lamports: LAMPORTS_PER_SOL,
+            space: 0,
+            programId: SystemProgram.programId
+        })
+
+        await sendAndConfirmTransaction(connection,
+            new Transaction().add(ix),
+            [payer, newKeypair]);
+    });
   });