Skip to content

Commit

Permalink
Update contract to use correct L1Deposit type
Browse files Browse the repository at this point in the history
  • Loading branch information
Avi-D-coder committed Jun 24, 2024
1 parent 2dd3518 commit 5d0b197
Show file tree
Hide file tree
Showing 13 changed files with 428 additions and 35 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions kairos-contracts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub const RUNTIME_ARG_INITIAL_TRIE_ROOT: &str = "initial_trie_root";
pub const RUNTIME_ARG_TEMP_PURSE: &str = "temp_purse";
pub const RUNTIME_ARG_AMOUNT: &str = "amount";
pub const RUNTIME_ARG_RECEIPT: &str = "risc0_receipt";
pub const RUNTIME_ARG_RECIPIENT: &str = "risc0_recipient";

pub const EP_INIT_NAME: &str = "init";
pub const EP_GET_PURSE_NAME: &str = "get_purse";
Expand Down
9 changes: 0 additions & 9 deletions kairos-contracts/demo-contract/contract-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
#![no_std]
pub mod constants;

use casper_event_standard::casper_types::Key;
use casper_event_standard::Event;

extern crate alloc;

#[derive(Event)]
pub struct Deposit {
pub depositor: Key,
pub amount: u64,
}
2 changes: 1 addition & 1 deletion kairos-contracts/demo-contract/contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition.workspace = true
license.workspace = true

[dependencies]
kairos-circuit-logic = {path="../../../kairos-prover/kairos-circuit-logic", default-features=false, features=["serde"]}
kairos-circuit-logic = {path="../../../kairos-prover/kairos-circuit-logic", default-features=false, features=["serde", "casper-event-standard"]}
kairos-verifier-risc0-lib = {path="../../../kairos-prover/kairos-verifier-risc0-lib", default-features=false, features=["verifier", "disable-dev-mode"]}
serde = {version="1", default-features=false, features=["derive"]}
serde-json-wasm = { version="1", default-features=false }
Expand Down
3 changes: 2 additions & 1 deletion kairos-contracts/demo-contract/contract/src/entry_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloc::vec;
use casper_types::{CLType, EntryPoint, EntryPointAccess, EntryPointType, Parameter};
use contract_utils::constants::{
EP_DEPOSIT_NAME, EP_GET_PURSE_NAME, EP_INIT_NAME, EP_SUBMIT_NAME, RUNTIME_ARG_AMOUNT,
RUNTIME_ARG_RECEIPT, RUNTIME_ARG_TEMP_PURSE,
RUNTIME_ARG_RECEIPT, RUNTIME_ARG_RECIPIENT, RUNTIME_ARG_TEMP_PURSE,
};

pub fn init() -> EntryPoint {
Expand All @@ -29,6 +29,7 @@ pub fn deposit() -> EntryPoint {
EntryPoint::new(
EP_DEPOSIT_NAME,
vec![
Parameter::new(RUNTIME_ARG_RECIPIENT, CLType::PublicKey),
Parameter::new(RUNTIME_ARG_AMOUNT, CLType::U512),
Parameter::new(RUNTIME_ARG_TEMP_PURSE, CLType::URef),
],
Expand Down
22 changes: 13 additions & 9 deletions kairos-contracts/demo-contract/contract/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ use casper_contract::{
unwrap_or_revert::UnwrapOrRevert,
};
use casper_event_standard::Schemas;
use casper_types::bytesrepr::Bytes;
use casper_types::bytesrepr::{Bytes, ToBytes};
use casper_types::{
contracts::NamedKeys, runtime_args, AccessRights, ApiError, CLValue, EntryPoints, Key,
RuntimeArgs, URef, U512,
};
use contract_utils::constants::{
KAIROS_CONTRACT_HASH, KAIROS_CONTRACT_PACKAGE_HASH, KAIROS_CONTRACT_UREF, KAIROS_DEPOSIT_PURSE,
KAIROS_LAST_PROCESSED_DEPOSIT_COUNTER, KAIROS_TRIE_ROOT, RUNTIME_ARG_AMOUNT,
RUNTIME_ARG_INITIAL_TRIE_ROOT, RUNTIME_ARG_RECEIPT, RUNTIME_ARG_TEMP_PURSE,
RUNTIME_ARG_INITIAL_TRIE_ROOT, RUNTIME_ARG_RECEIPT, RUNTIME_ARG_RECIPIENT,
RUNTIME_ARG_TEMP_PURSE,
};
use contract_utils::Deposit;
mod entry_points;
mod utils;
use kairos_verifier_risc0_lib::verifier::Receipt;
Expand All @@ -29,7 +29,7 @@ use utils::get_immediate_caller;
#[allow(unused)]
use casper_contract_no_std_helpers;

use kairos_circuit_logic::ProofOutputs;
use kairos_circuit_logic::{transactions::L1Deposit, ProofOutputs};

// This entry point is called once when the contract is installed.
// The contract purse will be created in contract context so that it is "owned" by the contract
Expand All @@ -41,7 +41,7 @@ pub extern "C" fn init() {
}

// initialize event schema
let schemas = Schemas::new().with::<Deposit>();
let schemas = Schemas::new().with::<L1Deposit>();
casper_event_standard::init(schemas);

let new_deposit_purse: URef = system::create_purse();
Expand Down Expand Up @@ -69,6 +69,7 @@ pub extern "C" fn get_purse() {
#[no_mangle]
pub extern "C" fn deposit() {
let temp_purse: URef = runtime::get_named_arg(RUNTIME_ARG_TEMP_PURSE);
let recipient: casper_types::PublicKey = runtime::get_named_arg(RUNTIME_ARG_RECIPIENT);
let amount: U512 = runtime::get_named_arg(RUNTIME_ARG_AMOUNT);
let deposit_purse_uref: URef = runtime::get_key(KAIROS_DEPOSIT_PURSE)
.unwrap_or_revert_with(DepositError::MissingKeyDepositPurse)
Expand All @@ -81,10 +82,13 @@ pub extern "C" fn deposit() {
let amount =
u64::try_from(amount).unwrap_or_else(|_| runtime::revert(ApiError::InvalidArgument));

let new_deposit_record: Deposit = Deposit {
depositor: get_immediate_caller().unwrap_or_revert(),
amount,
};
// FIXME: verify that the caller's account hash matches a depositor public key argument.
// We have to ensure we know who the depositor is for regulatory reasons.
// We could check that the recipient of the funds is the caller or off chain get another signature from the public key.
let _account_hash = get_immediate_caller().unwrap_or_revert();

let recipient = recipient.into_bytes().unwrap_or_revert();
let new_deposit_record: L1Deposit = L1Deposit { recipient, amount };
// this increases a counter automatically - we don't need to create one ourselves
casper_event_standard::emit(new_deposit_record);
}
Expand Down
Loading

0 comments on commit 5d0b197

Please sign in to comment.