From 72ec30c9a96be7e4d1ac917677d034916a13c2df Mon Sep 17 00:00:00 2001 From: Dadepo Aderemi Date: Sat, 10 Sep 2022 16:53:55 +0200 Subject: [PATCH 1/7] Add direct calling to the System program for creating accounts --- basics/create-account/README.md | 6 ++++++ basics/create-account/native/cicd.sh | 0 basics/create-account/native/tests/test.ts | 22 ++++++++++++++++++++-- 3 files changed, 26 insertions(+), 2 deletions(-) mode change 100644 => 100755 basics/create-account/native/cicd.sh diff --git a/basics/create-account/README.md b/basics/create-account/README.md index d1b260d24..7ab5f5811 100644 --- a/basics/create-account/README.md +++ b/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. diff --git a/basics/create-account/native/cicd.sh b/basics/create-account/native/cicd.sh old mode 100644 new mode 100755 diff --git a/basics/create-account/native/tests/test.ts b/basics/create-account/native/tests/test.ts index 947cd143a..ffcc3ad69 100644 --- a/basics/create-account/native/tests/test.ts +++ b/basics/create-account/native/tests/test.ts @@ -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]); + }); }); \ No newline at end of file From d5e30f34357ea8be845f848956116d9691b3dab4 Mon Sep 17 00:00:00 2001 From: Dadepo Aderemi Date: Sat, 10 Sep 2022 21:19:50 +0200 Subject: [PATCH 2/7] remove executable permission --- basics/create-account/native/cicd.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 basics/create-account/native/cicd.sh diff --git a/basics/create-account/native/cicd.sh b/basics/create-account/native/cicd.sh old mode 100755 new mode 100644 From dcc646132c6976b9e404bada9dd9c44650eb2c1e Mon Sep 17 00:00:00 2001 From: Dadepo Aderemi Date: Thu, 15 Sep 2022 08:43:44 +0200 Subject: [PATCH 3/7] remove changes to main --- basics/create-account/README.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/basics/create-account/README.md b/basics/create-account/README.md index 7ab5f5811..9ef39a852 100644 --- a/basics/create-account/README.md +++ b/basics/create-account/README.md @@ -4,12 +4,6 @@ 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: From 6178ad1ce6648b3c1620b2388edb88ebb0372ef8 Mon Sep 17 00:00:00 2001 From: Dadepo Aderemi Date: Thu, 15 Sep 2022 09:05:53 +0200 Subject: [PATCH 4/7] Add direct calling to the System program for creating accounts --- basics/create-account/README.md | 6 ++++++ basics/create-account/native/tests/test.ts | 20 +++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/basics/create-account/README.md b/basics/create-account/README.md index 9ef39a852..7ab5f5811 100644 --- a/basics/create-account/README.md +++ b/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: diff --git a/basics/create-account/native/tests/test.ts b/basics/create-account/native/tests/test.ts index a2b94fa85..25f3c1896 100644 --- a/basics/create-account/native/tests/test.ts +++ b/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]); + }); }); \ No newline at end of file From 0e3ffefe47bb85f28aa0571de970e15f253a2e02 Mon Sep 17 00:00:00 2001 From: Dadepo Aderemi Date: Wed, 26 Oct 2022 22:22:16 +0400 Subject: [PATCH 5/7] Update not to use hard coded public key for program id --- basics/create-account/native/tests/test.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/basics/create-account/native/tests/test.ts b/basics/create-account/native/tests/test.ts index 25f3c1896..6d9551318 100644 --- a/basics/create-account/native/tests/test.ts +++ b/basics/create-account/native/tests/test.ts @@ -21,10 +21,9 @@ describe("Create a system account", async () => { const connection = new Connection(`http://localhost:8899`, 'confirmed'); const payer = createKeypairFromFile(require('os').homedir() + '/.config/solana/id.json'); - - const PROGRAM_ID: PublicKey = new PublicKey( - "Au21huMZuDQrbzu2Ec5ohpW5CKRqhcGV6qLawfydStGs" - ); + const program = createKeypairFromFile('./program/target/so/program-keypair.json') + + const PROGRAM_ID: PublicKey = program.publicKey; it("Create the account via a cross program invocation", async () => { From e1623de4e8a65324def0f5c80dc9e18d9fb96eac Mon Sep 17 00:00:00 2001 From: Dadepo Aderemi Date: Wed, 26 Oct 2022 22:29:13 +0400 Subject: [PATCH 6/7] Log the public key of created account via direct call to system program --- basics/create-account/native/tests/test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/basics/create-account/native/tests/test.ts b/basics/create-account/native/tests/test.ts index 6d9551318..3dbc0293c 100644 --- a/basics/create-account/native/tests/test.ts +++ b/basics/create-account/native/tests/test.ts @@ -61,6 +61,8 @@ describe("Create a system account", async () => { await sendAndConfirmTransaction(connection, new Transaction().add(ix), [payer, newKeypair]); + + console.log(`Account with public key ${newKeypair.publicKey} successfully created`); }); }); \ No newline at end of file From 996ed027e4a3198509a27e00c2f9c024af984bb6 Mon Sep 17 00:00:00 2001 From: Dadepo Aderemi Date: Thu, 27 Oct 2022 01:57:21 +0400 Subject: [PATCH 7/7] moved in changes from updates-to-cross-program-invocation branch --- basics/cross-program-invocation/README.md | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/basics/cross-program-invocation/README.md b/basics/cross-program-invocation/README.md index e4b059d34..0721147a6 100644 --- a/basics/cross-program-invocation/README.md +++ b/basics/cross-program-invocation/README.md @@ -20,17 +20,19 @@ Let's say we decided it was essential to have our mint (operation 1) and our "mi With the `native` implementation, you have to do a little bit of lifting to import one crate into another within your Cargo workspace. -Add the `no-entrypoint` feature to Cargo.toml: +This is because a Solana Program needs to have a single entry point. This means a Solana Program that depends on +other Solana Programs needs a way to disable the other entry points. This is done using `[features]` in Cargo. + +Add the `no-entrypoint` feature to Cargo.toml of the `lever` crate: ```toml [features] no-entrypoint = [] -cpi = ["no-entrypoint"] ``` -Then use the import just like we did in the `anchor` example: +Then, in the `hand` crate, use the import just like we did in the `anchor` example: ```toml [dependencies] ... -lever = { path = "../lever", features = [ "cpi" ] } +lever = { path = "../lever", features = [ "no-entrypoint" ] } ``` Lastly, add this annotation over the `entrypoint!` macro that you wish to disable on import (the child program): ```rust @@ -38,6 +40,15 @@ Lastly, add this annotation over the `entrypoint!` macro that you wish to disabl entrypoint!(process_instruction); ``` +The above configuration defines `no-entrypoint` as a _feature_ in the `lever` crate. This controls whether the line +`entrypoint!(process_instruction)` gets compiled or not depending on how the `lever` crate is included as a dependency. + +When adding `lever` as a dependency in the Cargo.tml of `hand` crate, we configure it with `features = [ "no-entrypoint" ]` +this makes sure that the `entrypoint!(process_instruction)` line is not part of the compilation. This ensures that only +the `entrypoint!(process_instruction)` from the `hand` crate is part of the compilation. + +For more about how `[features]` see [Features chapter in the Rust Book](https://doc.rust-lang.org/cargo/reference/features.html) + ### Let's switch the power on and off using a CPI! lever