浏览代码

Add direct calling to the System program for creating accounts

Dadepo Aderemi 3 年之前
父节点
当前提交
72ec30c9a9
共有 2 个文件被更改,包括 26 次插入2 次删除
  1. 6 0
      basics/create-account/README.md
  2. 20 2
      basics/create-account/native/cicd.sh

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

@@ -3,6 +3,12 @@
 :wrench: We're going to create a Solana account. :wrench:   
    
 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.
 

+ 20 - 2
basics/create-account/native/cicd.sh

@@ -1,6 +1,6 @@
 import {
     Connection,
-    Keypair,
+    Keypair, LAMPORTS_PER_SOL,
     sendAndConfirmTransaction,
     SystemProgram,
     Transaction,
@@ -21,7 +21,7 @@ describe("Create a system account", async () => {
     const payer = createKeypairFromFile(require('os').homedir() + '/.config/solana/id.json');
     const program = createKeypairFromFile('./program/target/so/program-keypair.json');
   
-    it("Create the account", async () => {
+    it("Create the account via a cross program invocation", async () => {
 
         const newKeypair = Keypair.generate();
 
@@ -41,5 +41,23 @@ 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]);
+    });
   });