mirror of
https://github.com/logos-blockchain/lssa.git
synced 2026-08-25 06:41:13 +00:00
refactor: use system faucet and vaults to supply accounts from genesis
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
co-authored by
Copilot
parent
89d1b95738
commit
9075f30f19
@@ -17,5 +17,6 @@ amm_core.workspace = true
|
||||
amm_program.workspace = true
|
||||
ata_core.workspace = true
|
||||
ata_program.workspace = true
|
||||
vault_core.workspace = true
|
||||
risc0-zkvm.workspace = true
|
||||
serde = { workspace = true, default-features = false }
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use authenticated_transfer_core::Instruction;
|
||||
use clock_core::{CLOCK_01_PROGRAM_ACCOUNT_ID, ClockAccountData};
|
||||
use nssa_core::{
|
||||
SYSTEM_FAUCET_ACCOUNT_ID,
|
||||
account::{Account, AccountWithMetadata},
|
||||
program::{
|
||||
AccountPostState, Claim, DEFAULT_PROGRAM_ID, ProgramInput, ProgramOutput, read_nssa_inputs,
|
||||
@@ -27,7 +27,12 @@ fn transfer(
|
||||
balance_to_move: u128,
|
||||
) -> Vec<AccountPostState> {
|
||||
// Continue only if the sender has authorized this operation
|
||||
assert!(sender.is_authorized, "Sender must be authorized");
|
||||
// or it's the system faucet account which is allowed without authorization as it may be used
|
||||
// only by sequencer.
|
||||
assert!(
|
||||
sender.is_authorized || sender.account_id == SYSTEM_FAUCET_ACCOUNT_ID,
|
||||
"Sender must be authorized"
|
||||
);
|
||||
|
||||
// Create accounts post states, with updated balances
|
||||
let sender_post = {
|
||||
@@ -59,39 +64,6 @@ fn transfer(
|
||||
vec![sender_post, recipient_post]
|
||||
}
|
||||
|
||||
/// Mints `balance` into a new account at genesis (`block_id` == 0).
|
||||
///
|
||||
/// Claims the target account and sets its balance in a single operation.
|
||||
fn mint(
|
||||
target: AccountWithMetadata,
|
||||
clock: AccountWithMetadata,
|
||||
balance: u128,
|
||||
) -> Vec<AccountPostState> {
|
||||
assert_eq!(
|
||||
clock.account_id, CLOCK_01_PROGRAM_ACCOUNT_ID,
|
||||
"Second account must be the clock account"
|
||||
);
|
||||
|
||||
let clock_data = ClockAccountData::from_bytes(&clock.account.data.clone().into_inner());
|
||||
assert_eq!(
|
||||
clock_data.block_id, 0,
|
||||
"Mint can only execute at genesis (block_id must be 0)"
|
||||
);
|
||||
|
||||
assert!(
|
||||
target.account == Account::default(),
|
||||
"Target account must be uninitialized"
|
||||
);
|
||||
|
||||
let mut target_post_account = target.account;
|
||||
target_post_account.balance = balance;
|
||||
let target_post = AccountPostState::new_claimed(target_post_account, Claim::Authorized);
|
||||
|
||||
let clock_post = AccountPostState::new(clock.account);
|
||||
|
||||
vec![target_post, clock_post]
|
||||
}
|
||||
|
||||
/// A transfer of balance program.
|
||||
/// To be used both in public and private contexts.
|
||||
fn main() {
|
||||
@@ -119,11 +91,6 @@ fn main() {
|
||||
.expect("Transfer requires exactly 2 accounts");
|
||||
transfer(sender, recipient, balance_to_move)
|
||||
}
|
||||
Instruction::Mint { amount: balance } => {
|
||||
let [target, clock] = <[_; 2]>::try_from(pre_states.clone())
|
||||
.expect("Mint requires exactly 2 accounts: target, clock");
|
||||
mint(target, clock, balance)
|
||||
}
|
||||
};
|
||||
|
||||
ProgramOutput::new(
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
//! Vault program which allows users to create vault accounts and transfer funds to them.
|
||||
//! Funds can later be claimed from the vault accounts by their owners.
|
||||
//!
|
||||
//! The program is designed to be used in conjunction with the authenticated transfer program, which
|
||||
//! performs the actual transfer of funds from the vault accounts.
|
||||
|
||||
use authenticated_transfer_core::Instruction as AuthTransferInstruction;
|
||||
use nssa_core::program::{
|
||||
AccountPostState, ChainedCall, ProgramInput, ProgramOutput, read_nssa_inputs,
|
||||
};
|
||||
use vault_core::Instruction;
|
||||
|
||||
fn unchanged_post_states(
|
||||
pre_states: &[nssa_core::account::AccountWithMetadata],
|
||||
) -> Vec<AccountPostState> {
|
||||
pre_states
|
||||
.iter()
|
||||
.map(|pre_state| AccountPostState::new(pre_state.account.clone()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let (
|
||||
ProgramInput {
|
||||
self_program_id,
|
||||
caller_program_id,
|
||||
pre_states,
|
||||
instruction,
|
||||
},
|
||||
instruction_words,
|
||||
) = read_nssa_inputs::<Instruction>();
|
||||
|
||||
let pre_states_clone = pre_states.clone();
|
||||
let post_states = unchanged_post_states(&pre_states_clone);
|
||||
|
||||
let chained_calls = match instruction {
|
||||
Instruction::Transfer {
|
||||
recipient_id,
|
||||
amount,
|
||||
} => {
|
||||
let [sender, recipient_vault] = pre_states
|
||||
.try_into()
|
||||
.expect("Transfer requires exactly 3 accounts");
|
||||
|
||||
let seed = vault_core::compute_vault_seed(recipient_id);
|
||||
|
||||
let mut recipient_vault_for_callee = recipient_vault;
|
||||
recipient_vault_for_callee.is_authorized = true;
|
||||
|
||||
vec![
|
||||
ChainedCall::new(
|
||||
sender.account.program_owner,
|
||||
vec![sender, recipient_vault_for_callee],
|
||||
&AuthTransferInstruction::Transfer { amount },
|
||||
)
|
||||
.with_pda_seeds(vec![seed]),
|
||||
]
|
||||
}
|
||||
Instruction::Claim { amount } => {
|
||||
let [owner, owner_vault] = pre_states
|
||||
.try_into()
|
||||
.expect("Claim requires exactly 2 accounts");
|
||||
|
||||
assert!(
|
||||
owner.is_authorized,
|
||||
"Owner must be authorized to claim from the vault"
|
||||
);
|
||||
|
||||
let seed = vault_core::compute_vault_seed(owner.account_id);
|
||||
|
||||
let mut owner_vault_for_callee = owner_vault;
|
||||
owner_vault_for_callee.is_authorized = true;
|
||||
|
||||
vec![
|
||||
ChainedCall::new(
|
||||
owner_vault_for_callee.account.program_owner,
|
||||
vec![owner_vault_for_callee, owner],
|
||||
&AuthTransferInstruction::Transfer { amount },
|
||||
)
|
||||
.with_pda_seeds(vec![seed]),
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
ProgramOutput::new(
|
||||
self_program_id,
|
||||
caller_program_id,
|
||||
instruction_words,
|
||||
pre_states_clone,
|
||||
post_states,
|
||||
)
|
||||
.with_chained_calls(chained_calls)
|
||||
.write();
|
||||
}
|
||||
Reference in New Issue
Block a user