This repository has been archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integration of native dynamic programs
- Loading branch information
Showing
12 changed files
with
495 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[package] | ||
name = "move_funds" | ||
version = "0.1.0" | ||
authors = [ | ||
"Anatoly Yakovenko <anatoly@solana.com>", | ||
"Greg Fitzgerald <greg@solana.com>", | ||
"Stephen Akridge <stephen@solana.com>", | ||
"Michael Vines <mvines@solana.com>", | ||
"Rob Walker <rob@solana.com>", | ||
"Pankaj Garg <pankaj@solana.com>", | ||
"Tyera Eulberg <tyera@solana.com>", | ||
"Jack May <jack@solana.com>", | ||
] | ||
|
||
[dependencies] | ||
bincode = "1.0.0" | ||
generic-array = { version = "0.12.0", default-features = false, features = ["serde"] } | ||
libloading = "0.5.0" | ||
solana = { path = "../.." } | ||
|
||
[lib] | ||
name = "move_funds" | ||
crate-type = ["dylib"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
extern crate bincode; | ||
extern crate solana; | ||
|
||
use bincode::deserialize; | ||
use solana::dynamic_program::KeyedAccount; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn process(infos: &mut Vec<KeyedAccount>, data: &[u8]) { | ||
let tokens: i64 = deserialize(data).unwrap(); | ||
if infos[0].account.tokens >= tokens { | ||
infos[0].account.tokens -= tokens; | ||
infos[1].account.tokens += tokens; | ||
} else { | ||
println!( | ||
"Insufficient funds, asked {}, only had {}", | ||
tokens, infos[0].account.tokens | ||
); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use bincode::serialize; | ||
use solana::bank::Account; | ||
use solana::signature::Pubkey; | ||
|
||
#[test] | ||
fn test_move_funds() { | ||
let tokens: i64 = 100; | ||
let data: Vec<u8> = serialize(&tokens).unwrap(); | ||
let keys = vec![Pubkey::default(); 2]; | ||
let mut accounts = vec![Account::default(), Account::default()]; | ||
accounts[0].tokens = 100; | ||
accounts[1].tokens = 1; | ||
|
||
{ | ||
let mut infos: Vec<KeyedAccount> = Vec::new(); | ||
for (key, account) in keys.iter().zip(&mut accounts).collect::<Vec<_>>() { | ||
infos.push(KeyedAccount { key, account }); | ||
} | ||
|
||
process(&mut infos, &data); | ||
} | ||
assert_eq!(0, accounts[0].tokens); | ||
assert_eq!(101, accounts[1].tokens); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "noop" | ||
version = "0.1.0" | ||
authors = [ | ||
"Anatoly Yakovenko <anatoly@solana.com>", | ||
"Greg Fitzgerald <greg@solana.com>", | ||
"Stephen Akridge <stephen@solana.com>", | ||
"Michael Vines <mvines@solana.com>", | ||
"Rob Walker <rob@solana.com>", | ||
"Pankaj Garg <pankaj@solana.com>", | ||
"Tyera Eulberg <tyera@solana.com>", | ||
"Jack May <jack@solana.com>", | ||
] | ||
|
||
[dependencies] | ||
libloading = "0.5.0" | ||
solana = { path = "../.." } | ||
|
||
[lib] | ||
name = "noop" | ||
crate-type = ["dylib"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
extern crate solana; | ||
|
||
use solana::dynamic_program::KeyedAccount; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn process(_infos: &mut Vec<KeyedAccount>, _data: &[u8]) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "print" | ||
version = "0.1.0" | ||
authors = [ | ||
"Anatoly Yakovenko <anatoly@solana.com>", | ||
"Greg Fitzgerald <greg@solana.com>", | ||
"Stephen Akridge <stephen@solana.com>", | ||
"Michael Vines <mvines@solana.com>", | ||
"Rob Walker <rob@solana.com>", | ||
"Pankaj Garg <pankaj@solana.com>", | ||
"Tyera Eulberg <tyera@solana.com>", | ||
"Jack May <jack@solana.com>", | ||
] | ||
|
||
[dependencies] | ||
libloading = "0.5.0" | ||
solana = { path = "../.." } | ||
|
||
[lib] | ||
name = "print" | ||
crate-type = ["dylib"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
extern crate solana; | ||
|
||
use solana::dynamic_program::KeyedAccount; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn process(infos: &mut Vec<KeyedAccount>, _data: &[u8]) { | ||
println!("AccountInfos: {:#?}", infos); | ||
//println!("data: {:#?}", data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.