Cleanup (#7176)
* Drop serde error instances These were no longer needed after ToPrimitive instances were added. * Rename last of _api crates
This commit is contained in:
1
programs/librapay/.gitignore
vendored
Normal file
1
programs/librapay/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target/
|
4102
programs/librapay/Cargo.lock
generated
Normal file
4102
programs/librapay/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
23
programs/librapay/Cargo.toml
Normal file
23
programs/librapay/Cargo.toml
Normal file
@@ -0,0 +1,23 @@
|
||||
[package]
|
||||
name = "solana-librapay"
|
||||
version = "0.22.0"
|
||||
description = "Solana Libra Payment"
|
||||
authors = ["Solana Maintainers <maintainers@solana.com>"]
|
||||
repository = "https://github.com/solana-labs/solana"
|
||||
license = "Apache-2.0"
|
||||
homepage = "https://solana.com/"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
bincode = "1.2.0"
|
||||
log = "0.4.8"
|
||||
solana-logger = { path = "../../logger", version = "0.22.0" }
|
||||
solana-move-loader-program = { path = "../move_loader", version = "0.22.0" }
|
||||
solana-runtime = { path = "../../runtime", version = "0.22.0" }
|
||||
solana-sdk = { path = "../../sdk", version = "0.22.0" }
|
||||
language_e2e_tests = { version = "0.0.1-sol4", package = "solana_libra_language_e2e_tests" }
|
||||
types = { version = "0.0.1-sol4", package = "solana_libra_types" }
|
||||
|
||||
[lib]
|
||||
crate-type = ["lib", "cdylib"]
|
||||
name = "solana_librapay"
|
90
programs/librapay/src/lib.rs
Normal file
90
programs/librapay/src/lib.rs
Normal file
@@ -0,0 +1,90 @@
|
||||
solana_sdk::declare_id!(
|
||||
"LibraPay11111111111111111111111111111111111"
|
||||
);
|
||||
|
||||
pub mod librapay_instruction;
|
||||
pub mod librapay_transaction;
|
||||
|
||||
extern crate solana_move_loader_program;
|
||||
|
||||
use solana_move_loader_program::account_state::LibraAccountState;
|
||||
use solana_runtime::loader_utils::load_program;
|
||||
use solana_sdk::account::KeyedAccount;
|
||||
use solana_sdk::client::Client;
|
||||
use solana_sdk::instruction::InstructionError;
|
||||
use solana_sdk::message::Message;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::system_instruction;
|
||||
|
||||
use types::account_config;
|
||||
|
||||
pub fn create_genesis<T: Client>(from: &Keypair, client: &T, amount: u64) -> Keypair {
|
||||
let genesis = Keypair::new();
|
||||
|
||||
let instruction = system_instruction::create_account(
|
||||
&from.pubkey(),
|
||||
&genesis.pubkey(),
|
||||
1,
|
||||
bincode::serialized_size(&LibraAccountState::create_genesis(amount).unwrap()).unwrap()
|
||||
as u64,
|
||||
&solana_sdk::move_loader::id(),
|
||||
);
|
||||
|
||||
client
|
||||
.send_message(&[&from, &genesis], Message::new(vec![instruction]))
|
||||
.unwrap();
|
||||
|
||||
let instruction = librapay_instruction::genesis(&genesis.pubkey(), amount);
|
||||
let message = Message::new_with_payer(vec![instruction], Some(&from.pubkey()));
|
||||
client.send_message(&[from, &genesis], message).unwrap();
|
||||
|
||||
genesis
|
||||
}
|
||||
|
||||
pub fn publish_module<T: Client>(from: &Keypair, client: &T, code: &str) -> Pubkey {
|
||||
let address = account_config::association_address();
|
||||
let account_state = LibraAccountState::create_module(&address, code, vec![]);
|
||||
let bytes = bincode::serialize(&account_state).unwrap();
|
||||
|
||||
load_program(client, &from, &solana_sdk::move_loader::id(), bytes)
|
||||
}
|
||||
|
||||
pub fn upload_script<T: Client>(from: &Keypair, client: &T, code: &str) -> Pubkey {
|
||||
let address = account_config::association_address();
|
||||
let account_state = LibraAccountState::create_script(&address, code, vec![]);
|
||||
let bytes = bincode::serialize(&account_state).unwrap();
|
||||
|
||||
load_program(client, &from, &solana_sdk::move_loader::id(), bytes)
|
||||
}
|
||||
|
||||
pub fn upload_mint_script<T: Client>(from: &Keypair, client: &T) -> Pubkey {
|
||||
let code = "
|
||||
import 0x0.LibraAccount;
|
||||
import 0x0.LibraCoin;
|
||||
main(payee: address, amount: u64) {
|
||||
LibraAccount.mint_to_address(move(payee), move(amount));
|
||||
return;
|
||||
}";
|
||||
upload_script(from, client, code)
|
||||
}
|
||||
pub fn upload_payment_script<T: Client>(from: &Keypair, client: &T) -> Pubkey {
|
||||
let code = "
|
||||
import 0x0.LibraAccount;
|
||||
import 0x0.LibraCoin;
|
||||
main(payee: address, amount: u64) {
|
||||
LibraAccount.pay_from_sender(move(payee), move(amount));
|
||||
return;
|
||||
}
|
||||
";
|
||||
|
||||
upload_script(from, client, code)
|
||||
}
|
||||
|
||||
pub fn process_instruction(
|
||||
program_id: &Pubkey,
|
||||
keyed_accounts: &mut [KeyedAccount],
|
||||
data: &[u8],
|
||||
) -> Result<(), InstructionError> {
|
||||
solana_move_loader_program::processor::process_instruction(program_id, keyed_accounts, data)
|
||||
}
|
96
programs/librapay/src/librapay_instruction.rs
Normal file
96
programs/librapay/src/librapay_instruction.rs
Normal file
@@ -0,0 +1,96 @@
|
||||
use bincode;
|
||||
use solana_move_loader_program::account_state::pubkey_to_address;
|
||||
use solana_move_loader_program::processor::InvokeCommand;
|
||||
use solana_sdk::instruction::{AccountMeta, Instruction};
|
||||
use solana_sdk::loader_instruction::LoaderInstruction;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use types::account_config;
|
||||
use types::transaction::TransactionArgument;
|
||||
|
||||
pub fn genesis(genesis_pubkey: &Pubkey, microlibras: u64) -> Instruction {
|
||||
let data = bincode::serialize(&InvokeCommand::CreateGenesis(microlibras)).unwrap();
|
||||
let ix_data = LoaderInstruction::InvokeMain { data };
|
||||
let accounts = vec![AccountMeta::new(*genesis_pubkey, true)];
|
||||
Instruction::new(solana_sdk::move_loader::id(), &ix_data, accounts)
|
||||
}
|
||||
|
||||
pub fn mint(
|
||||
script_pubkey: &Pubkey,
|
||||
genesis_pubkey: &Pubkey,
|
||||
to_pubkey: &Pubkey,
|
||||
microlibras: u64,
|
||||
) -> Instruction {
|
||||
let args = vec![
|
||||
TransactionArgument::Address(pubkey_to_address(to_pubkey)),
|
||||
TransactionArgument::U64(microlibras),
|
||||
];
|
||||
|
||||
let data = bincode::serialize(&InvokeCommand::RunScript {
|
||||
sender_address: account_config::association_address(),
|
||||
function_name: "main".to_string(),
|
||||
args,
|
||||
})
|
||||
.unwrap();
|
||||
let ix_data = LoaderInstruction::InvokeMain { data };
|
||||
|
||||
let accounts = vec![
|
||||
AccountMeta::new_readonly(*script_pubkey, false),
|
||||
AccountMeta::new(*genesis_pubkey, true),
|
||||
AccountMeta::new(*to_pubkey, false),
|
||||
];
|
||||
|
||||
Instruction::new(solana_sdk::move_loader::id(), &ix_data, accounts)
|
||||
}
|
||||
|
||||
pub fn transfer(
|
||||
script_pubkey: &Pubkey,
|
||||
genesis_pubkey: &Pubkey,
|
||||
from_pubkey: &Pubkey,
|
||||
to_pubkey: &Pubkey,
|
||||
microlibras: u64,
|
||||
) -> Instruction {
|
||||
let args = vec![
|
||||
TransactionArgument::Address(pubkey_to_address(to_pubkey)),
|
||||
TransactionArgument::U64(microlibras),
|
||||
];
|
||||
|
||||
let data = bincode::serialize(&InvokeCommand::RunScript {
|
||||
sender_address: pubkey_to_address(from_pubkey),
|
||||
function_name: "main".to_string(),
|
||||
args,
|
||||
})
|
||||
.unwrap();
|
||||
let ix_data = LoaderInstruction::InvokeMain { data };
|
||||
|
||||
let accounts = vec![
|
||||
AccountMeta::new_readonly(*script_pubkey, false),
|
||||
AccountMeta::new_readonly(*genesis_pubkey, false),
|
||||
AccountMeta::new(*from_pubkey, true),
|
||||
AccountMeta::new(*to_pubkey, false),
|
||||
];
|
||||
|
||||
Instruction::new(solana_sdk::move_loader::id(), &ix_data, accounts)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_pay() {
|
||||
let from_pubkey = Pubkey::new_rand();
|
||||
let to_pubkey = Pubkey::new_rand();
|
||||
let program_id = Pubkey::new_rand();
|
||||
let mint_id = Pubkey::new_rand();
|
||||
transfer(&program_id, &mint_id, &from_pubkey, &to_pubkey, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mint() {
|
||||
let program_id = Pubkey::new_rand();
|
||||
let from_pubkey = Pubkey::new_rand();
|
||||
let to_pubkey = Pubkey::new_rand();
|
||||
|
||||
mint(&program_id, &from_pubkey, &to_pubkey, 1);
|
||||
}
|
||||
}
|
236
programs/librapay/src/librapay_transaction.rs
Normal file
236
programs/librapay/src/librapay_transaction.rs
Normal file
@@ -0,0 +1,236 @@
|
||||
use crate::librapay_instruction;
|
||||
use log::*;
|
||||
use solana_move_loader_program::account_state::{pubkey_to_address, LibraAccountState};
|
||||
use solana_move_loader_program::data_store::DataStore;
|
||||
use solana_sdk::client::Client;
|
||||
use solana_sdk::commitment_config::CommitmentConfig;
|
||||
use solana_sdk::hash::Hash;
|
||||
use solana_sdk::pubkey::Pubkey;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use solana_sdk::system_instruction;
|
||||
use solana_sdk::transaction::Transaction;
|
||||
use std::boxed::Box;
|
||||
use std::error;
|
||||
use std::fmt;
|
||||
|
||||
pub fn create_genesis(keypair: &Keypair, microlibras: u64, recent_blockhash: Hash) -> Transaction {
|
||||
let ix = librapay_instruction::genesis(&keypair.pubkey(), microlibras);
|
||||
Transaction::new_signed_with_payer(
|
||||
vec![ix],
|
||||
Some(&keypair.pubkey()),
|
||||
&[keypair],
|
||||
recent_blockhash,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn mint_tokens(
|
||||
script_pubkey: &Pubkey,
|
||||
payer_keypair: &Keypair,
|
||||
genesis_keypair: &Keypair,
|
||||
to_pubkey: &Pubkey,
|
||||
microlibras: u64,
|
||||
recent_blockhash: Hash,
|
||||
) -> Transaction {
|
||||
let ix = librapay_instruction::mint(
|
||||
script_pubkey,
|
||||
&genesis_keypair.pubkey(),
|
||||
to_pubkey,
|
||||
microlibras,
|
||||
);
|
||||
Transaction::new_signed_with_payer(
|
||||
vec![ix],
|
||||
Some(&payer_keypair.pubkey()),
|
||||
&[payer_keypair, genesis_keypair],
|
||||
recent_blockhash,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn transfer(
|
||||
script_pubkey: &Pubkey,
|
||||
genesis_pubkey: &Pubkey,
|
||||
payer_keypair: &Keypair,
|
||||
from_keypair: &Keypair,
|
||||
to_pubkey: &Pubkey,
|
||||
microlibras: u64,
|
||||
recent_blockhash: Hash,
|
||||
) -> Transaction {
|
||||
let ix = librapay_instruction::transfer(
|
||||
script_pubkey,
|
||||
genesis_pubkey,
|
||||
&from_keypair.pubkey(),
|
||||
to_pubkey,
|
||||
microlibras,
|
||||
);
|
||||
Transaction::new_signed_with_payer(
|
||||
vec![ix],
|
||||
Some(&payer_keypair.pubkey()),
|
||||
&[payer_keypair, from_keypair],
|
||||
recent_blockhash,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn create_accounts(
|
||||
from_keypair: &Keypair,
|
||||
to_keypair: &[&Keypair],
|
||||
lamports: u64,
|
||||
recent_blockhash: Hash,
|
||||
) -> Transaction {
|
||||
let instructions = to_keypair
|
||||
.iter()
|
||||
.map(|keypair| {
|
||||
system_instruction::create_account(
|
||||
&from_keypair.pubkey(),
|
||||
&keypair.pubkey(),
|
||||
lamports,
|
||||
400,
|
||||
&solana_sdk::move_loader::id(),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut from_signers = vec![from_keypair];
|
||||
from_signers.extend_from_slice(to_keypair);
|
||||
Transaction::new_signed_instructions(&from_signers, instructions, recent_blockhash)
|
||||
}
|
||||
|
||||
pub fn create_account(
|
||||
from_keypair: &Keypair,
|
||||
to_keypair: &Keypair,
|
||||
lamports: u64,
|
||||
recent_blockhash: Hash,
|
||||
) -> Transaction {
|
||||
create_accounts(from_keypair, &[to_keypair], lamports, recent_blockhash)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum LibrapayError {
|
||||
UnknownAccountState,
|
||||
}
|
||||
|
||||
impl fmt::Display for LibrapayError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for LibrapayError {}
|
||||
|
||||
pub fn get_libra_balance<T: Client>(
|
||||
client: &T,
|
||||
pubkey: &Pubkey,
|
||||
) -> Result<u64, Box<dyn error::Error>> {
|
||||
if let Some(account) =
|
||||
client.get_account_with_commitment(&pubkey, CommitmentConfig::recent())?
|
||||
{
|
||||
let mut data_store = DataStore::default();
|
||||
match bincode::deserialize(&account.data)? {
|
||||
LibraAccountState::User(_, write_set) => {
|
||||
data_store.apply_write_set(&write_set);
|
||||
}
|
||||
LibraAccountState::Unallocated => {
|
||||
return Ok(0);
|
||||
}
|
||||
state => {
|
||||
info!("Unknown account state: {:?}", state);
|
||||
return Err(LibrapayError::UnknownAccountState.into());
|
||||
}
|
||||
}
|
||||
let resource = data_store
|
||||
.read_account_resource(&pubkey_to_address(pubkey))
|
||||
.unwrap();
|
||||
let res = resource.balance();
|
||||
Ok(res)
|
||||
} else {
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{create_genesis, upload_mint_script, upload_payment_script};
|
||||
use solana_runtime::bank::Bank;
|
||||
use solana_runtime::bank_client::BankClient;
|
||||
use solana_sdk::genesis_config::create_genesis_config;
|
||||
use solana_sdk::signature::{Keypair, KeypairUtil};
|
||||
use std::sync::Arc;
|
||||
|
||||
fn create_bank(lamports: u64) -> (Arc<Bank>, Keypair, Keypair, Pubkey, Pubkey) {
|
||||
let (genesis_config, mint) = create_genesis_config(lamports);
|
||||
let mut bank = Bank::new(&genesis_config);
|
||||
bank.add_instruction_processor(
|
||||
solana_sdk::move_loader::id(),
|
||||
solana_move_loader_program::processor::process_instruction,
|
||||
);
|
||||
let shared_bank = Arc::new(bank);
|
||||
let bank_client = BankClient::new_shared(&shared_bank);
|
||||
let genesis_pubkey = create_genesis(&mint, &bank_client, 1_000_000);
|
||||
let mint_script_pubkey = upload_mint_script(&mint, &bank_client);
|
||||
let script_pubkey = upload_payment_script(&mint, &bank_client);
|
||||
(
|
||||
shared_bank,
|
||||
mint,
|
||||
genesis_pubkey,
|
||||
mint_script_pubkey,
|
||||
script_pubkey,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_transfer() {
|
||||
solana_logger::setup();
|
||||
|
||||
let (bank, mint, genesis_keypair, mint_script_pubkey, payment_script_pubkey) =
|
||||
create_bank(10_000);
|
||||
let from_keypair = Keypair::new();
|
||||
let to_keypair = Keypair::new();
|
||||
|
||||
let tx = create_accounts(
|
||||
&mint,
|
||||
&[&from_keypair, &to_keypair],
|
||||
1,
|
||||
bank.last_blockhash(),
|
||||
);
|
||||
bank.process_transaction(&tx).unwrap();
|
||||
|
||||
info!(
|
||||
"created accounts: mint: {} genesis_pubkey: {}",
|
||||
mint.pubkey(),
|
||||
genesis_keypair.pubkey()
|
||||
);
|
||||
info!(
|
||||
" from: {} to: {}",
|
||||
from_keypair.pubkey(),
|
||||
to_keypair.pubkey()
|
||||
);
|
||||
|
||||
let tx = mint_tokens(
|
||||
&mint_script_pubkey,
|
||||
&mint,
|
||||
&genesis_keypair,
|
||||
&from_keypair.pubkey(),
|
||||
1,
|
||||
bank.last_blockhash(),
|
||||
);
|
||||
bank.process_transaction(&tx).unwrap();
|
||||
let client = BankClient::new_shared(&bank);
|
||||
assert_eq!(
|
||||
1,
|
||||
get_libra_balance(&client, &from_keypair.pubkey()).unwrap()
|
||||
);
|
||||
|
||||
info!("passed mint... doing another transfer..");
|
||||
|
||||
let tx = transfer(
|
||||
&payment_script_pubkey,
|
||||
&genesis_keypair.pubkey(),
|
||||
&mint,
|
||||
&from_keypair,
|
||||
&to_keypair.pubkey(),
|
||||
1,
|
||||
bank.last_blockhash(),
|
||||
);
|
||||
bank.process_transaction(&tx).unwrap();
|
||||
assert_eq!(1, get_libra_balance(&client, &to_keypair.pubkey()).unwrap());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user