Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(FI-885) add created at time #27

Merged
merged 2 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cycles-ledger/src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub type NumCycles = Nat;
pub struct DepositArg {
pub to: Account,
pub memo: Option<Memo>,
#[serde(default)]
pub created_at_time: Option<u64>,
}

#[derive(CandidType, Deserialize, Clone, Debug, PartialEq, Eq)]
Expand Down
13 changes: 9 additions & 4 deletions cycles-ledger/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,13 @@ fn deposit(arg: endpoints::DepositArg) -> endpoints::DepositResult {
ic_cdk::trap("deposit amount is insufficient");
}
let memo = validate_memo(arg.memo);
let (txid, balance, _phash) =
storage::record_deposit(&arg.to, amount, memo, ic_cdk::api::time());
let (txid, balance, _phash) = storage::record_deposit(
&arg.to,
amount,
memo,
ic_cdk::api::time(),
arg.created_at_time,
);

// TODO(FI-766): set the certified variable.

Expand Down Expand Up @@ -175,7 +180,7 @@ fn icrc1_transfer(args: TransferArg) -> Result<Nat, TransferError> {
}
}

let (txid, _hash) = storage::transfer(&from, &args.to, amount, memo, now);
let (txid, _hash) = storage::transfer(&from, &args.to, amount, memo, now, args.created_at_time);

Ok(Nat::from(txid))
}
Expand Down Expand Up @@ -289,7 +294,7 @@ async fn send(args: endpoints::SendArg) -> Result<Nat, SendError> {
)
} else {
let now = ic_cdk::api::time();
let (send, _send_hash) = storage::send(&from, amount, memo, now);
let (send, _send_hash) = storage::send(&from, amount, memo, now, args.created_at_time);
Ok(send)
}
}
Expand Down
27 changes: 26 additions & 1 deletion cycles-ledger/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ pub enum Operation {
fee: u128,
#[serde(skip_serializing_if = "Option::is_none")]
memo: Option<Memo>,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "ts")]
created_at_time: Option<u64>,
},
Transfer {
#[serde(with = "compact_account")]
Expand All @@ -51,6 +55,10 @@ pub enum Operation {
fee: u128,
#[serde(skip_serializing_if = "Option::is_none")]
memo: Option<Memo>,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "ts")]
created_at_time: Option<u64>,
},
Burn {
#[serde(with = "compact_account")]
Expand All @@ -60,6 +68,10 @@ pub enum Operation {
fee: u128,
#[serde(skip_serializing_if = "Option::is_none")]
memo: Option<Memo>,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "ts")]
created_at_time: Option<u64>,
NikolasHaimerl marked this conversation as resolved.
Show resolved Hide resolved
},
}

Expand Down Expand Up @@ -197,6 +209,7 @@ pub fn record_deposit(
amount: u128,
memo: Option<Memo>,
now: u64,
created_at_time: Option<u64>,
) -> (u64, u128, Hash) {
assert!(amount >= crate::config::FEE);

Expand All @@ -212,6 +225,7 @@ pub fn record_deposit(
amount,
memo,
fee: crate::config::FEE,
created_at_time,
},
timestamp: now,
phash,
Expand All @@ -226,6 +240,7 @@ pub fn transfer(
amount: u128,
memo: Option<Memo>,
now: u64,
created_at_time: Option<u64>,
) -> (u64, Hash) {
let from_key = to_account_key(from);
let to_key = to_account_key(to);
Expand Down Expand Up @@ -253,6 +268,7 @@ pub fn transfer(
amount,
memo,
fee: crate::config::FEE,
created_at_time,
},
timestamp: now,
phash,
Expand Down Expand Up @@ -281,6 +297,7 @@ pub fn penalize(from: &Account, now: u64) -> (BlockIndex, Hash) {
amount: 0,
memo: None,
fee: crate::config::FEE,
created_at_time: None,
},
timestamp: now,
phash,
Expand All @@ -289,7 +306,13 @@ pub fn penalize(from: &Account, now: u64) -> (BlockIndex, Hash) {
})
}

pub fn send(from: &Account, amount: u128, memo: Option<Memo>, now: u64) -> (BlockIndex, Hash) {
pub fn send(
from: &Account,
amount: u128,
memo: Option<Memo>,
now: u64,
created_at_time: Option<u64>,
) -> (BlockIndex, Hash) {
let from_key = to_account_key(from);

mutate_state(|s| {
Expand All @@ -311,6 +334,7 @@ pub fn send(from: &Account, amount: u128, memo: Option<Memo>, now: u64) -> (Bloc
amount,
memo,
fee: crate::config::FEE,
created_at_time,
},
timestamp: now,
phash,
Expand Down Expand Up @@ -343,6 +367,7 @@ mod tests {
amount: u128::MAX,
fee: 10_000,
memo: Some(Memo::default()),
created_at_time: None,
},
timestamp: 1691065957,
phash: None,
Expand Down
1 change: 1 addition & 0 deletions depositor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ async fn deposit(arg: DepositArg) -> DepositResult {
let arg = cycles_ledger::endpoints::DepositArg {
to: arg.to,
memo: arg.memo,
created_at_time: Some(ic_cdk::api::time()),
};
let (result,): (DepositResult,) = call_with_payment128(ledger_id, "deposit", (arg,), cycles)
.await
Expand Down
Loading