Skip to content

Commit

Permalink
chore: update packages
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Aug 17, 2024
1 parent ef746a0 commit a32d94e
Show file tree
Hide file tree
Showing 11 changed files with 300 additions and 311 deletions.
416 changes: 242 additions & 174 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ strip = true
opt-level = 's'

[workspace.package]
version = "0.5.2"
version = "0.5.3"
edition = "2021"
repository = "https://github.com/ldclabs/ck-doge"
keywords = ["defi", "dogecoin", "icp", "dapp", "web3"]
Expand All @@ -39,9 +39,10 @@ hex = { package = "hex-conservative", version = "0.2", default-features = false,
] }
async-trait = "0.1"
candid = "0.10"
ic-cdk = "0.14"
ic-cdk-timers = "0.8"
ic-cdk = "0.15"
ic-cdk-timers = "0.9"
ic-stable-structures = "0.6"
icrc-ledger-types = "0.1"
num-traits = "0.2"
once_cell = "1.19"
lazy_static = "1.4"
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# CK-Doge
Chain-key Dogecoin (CK-Doge) offers on-chain integration with the Dogecoin network on [the Internet Computer (ICP)](https://internetcomputer.org/).
🐶 Chain-key Dogecoin (CK-Doge) offers on-chain integration with the Dogecoin network on [the Internet Computer (ICP)](https://internetcomputer.org/).

`ckDOGE` is an ICRC-2-compliant token, supported by the CK-Doge canisters, and is backed 1:1 by DOGE.

## Packages

| Package | Crates.io | Documentation | Description |
| ------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------: | :----------------------------------------------------------------------: |
| Package | Crates.io | Documentation | Description |
| ------------------------------------------------------------------------------------- | :---------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------: | :----------------------------------------------------------------------- |
| [dogecoin](https://github.com/ldclabs/ck-doge/tree/main/src/dogecoin) | [![crates.io](https://img.shields.io/crates/v/dogecoin.svg)](https://crates.io/crates/dogecoin) | [![Documentation](https://docs.rs/dogecoin/badge.svg)](https://docs.rs/dogecoin) | General purpose rust library for using and interoperating with Dogecoin. |
| [CK-Doge Canister](https://github.com/ldclabs/ck-doge/tree/main/src/ck-doge-canister) | - | - | Interact with Dogecoin network from the Internet Computer. |
| [CK-Doge Minter](https://github.com/ldclabs/ck-doge/tree/main/src/ck-doge-minter) | - | - | Mint and burn `ckDOGE` <-> `DOGE` on the Internet Computer. |
| [CK-Doge Canister](https://github.com/ldclabs/ck-doge/tree/main/src/ck-doge-canister) | - | - | Interact with Dogecoin network from the Internet Computer. |
| [CK-Doge Minter](https://github.com/ldclabs/ck-doge/tree/main/src/ck-doge-minter) | - | - | Mint and burn `ckDOGE` <-> `DOGE` on the Internet Computer. |

## Overview

Expand Down
2 changes: 1 addition & 1 deletion src/ck-doge-canister/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ num-traits = { workspace = true }
once_cell = { workspace = true }
ic-cdk-timers = { workspace = true }
ic-stable-structures = { workspace = true }
icrc-ledger-types = { workspace = true }
lazy_static = { workspace = true }
bitcoin = { workspace = true }
dogecoin = { path = "../dogecoin", version = "0.5" }
getrandom = { version = "0.2", features = ["custom"] }
ic-crypto-extended-bip32 = { git = "https://github.com/dfinity/ic/", rev = "d19fa446ab35780b2c6d8b82ea32d808cca558d5" }
icrc-ledger-types = { git = "https://github.com/dfinity/ic/", rev = "d19fa446ab35780b2c6d8b82ea32d808cca558d5" }
9 changes: 5 additions & 4 deletions src/ck-doge-canister/src/api_query.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bitcoin::hashes::sha256d;
use candid::{CandidType, Principal};
use dogecoin::canister::*;
use serde_bytes::ByteArray;
use std::{collections::BTreeSet, str::FromStr};

use crate::{is_authenticated, is_controller_or_manager, store, Account};
Expand Down Expand Up @@ -101,12 +102,12 @@ fn get_utx(id: String) -> Result<UnspentTx, String> {
}

#[ic_cdk::query]
fn get_utx_b(txid: ByteN<32>) -> Option<UnspentTx> {
fn get_utx_b(txid: ByteArray<32>) -> Option<UnspentTx> {
store::get_utx(&txid)
}

#[ic_cdk::query]
fn get_tx_status(txid: ByteN<32>) -> Option<TxStatus> {
fn get_tx_status(txid: ByteArray<32>) -> Option<TxStatus> {
store::get_tx_block_height(&txid).map(|height| {
store::state::with(|s| TxStatus {
height,
Expand All @@ -131,7 +132,7 @@ fn list_utxos(addr: String, take: u16, confirmed: bool) -> Result<UtxosOutput, S
}

#[ic_cdk::query]
fn list_utxos_b(address: ByteN<21>, take: u16, confirmed: bool) -> Result<UtxosOutput, String> {
fn list_utxos_b(address: ByteArray<21>, take: u16, confirmed: bool) -> Result<UtxosOutput, String> {
let utxos = store::list_utxos(&address, take.clamp(10, 10000) as usize, confirmed);
store::state::with(|s| {
Ok(UtxosOutput {
Expand All @@ -150,6 +151,6 @@ fn get_balance(addr: String) -> Result<u64, String> {
}

#[ic_cdk::query]
fn get_balance_b(address: ByteN<21>) -> u64 {
fn get_balance_b(address: ByteArray<21>) -> u64 {
store::get_balance(&address)
}
1 change: 1 addition & 0 deletions src/ck-doge-canister/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use candid::Principal;
use dogecoin::canister;
use dogecoin::canister::*;
use serde_bytes::ByteArray;
use std::collections::BTreeSet;

use crate::api_init::ChainArgs;
Expand Down
48 changes: 24 additions & 24 deletions src/ck-doge-canister/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ic_stable_structures::{
DefaultMemoryImpl, StableBTreeMap, StableCell, Storable,
};
use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;
use serde_bytes::{ByteArray, ByteBuf};
use std::{
borrow::Cow,
cell::RefCell,
Expand Down Expand Up @@ -46,25 +46,25 @@ pub struct State {
pub min_confirmations: u32,

pub tip_height: u64,
pub tip_blockhash: ByteN<32>,
pub tip_blockhash: ByteArray<32>,

pub processed_height: u64,
pub processed_blockhash: ByteN<32>,
pub processed_blockhash: ByteArray<32>,

pub confirmed_height: u64,
pub confirmed_blockhash: ByteN<32>,
pub confirmed_blockhash: ByteArray<32>,

pub start_height: u64,
pub start_blockhash: ByteN<32>,
pub start_blockhash: ByteArray<32>,

pub last_errors: VecDeque<String>,

pub managers: BTreeSet<Principal>,
pub rpc_agents: Vec<RPCAgent>,

pub unconfirmed_utxs: BTreeMap<ByteN<32>, UnspentTxState>,
pub unconfirmed_utxos: BTreeMap<ByteN<21>, (UtxoStates, SpentUtxos)>,
processed_blocks: VecDeque<(u64, ByteN<32>)>,
pub unconfirmed_utxs: BTreeMap<ByteArray<32>, UnspentTxState>,
pub unconfirmed_utxos: BTreeMap<ByteArray<21>, (UtxoStates, SpentUtxos)>,
processed_blocks: VecDeque<(u64, ByteArray<32>)>,
}

impl State {
Expand Down Expand Up @@ -125,7 +125,7 @@ impl Storable for UnspentTxState {

// address -> UnspentOutput
#[derive(Clone, Default, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UtxoState(pub u64, pub ByteN<32>, pub u32, pub u64);
pub struct UtxoState(pub u64, pub ByteArray<32>, pub u32, pub u64);
impl Storable for UtxoState {
const BOUND: Bound = Bound::Bounded {
max_size: 58,
Expand Down Expand Up @@ -498,21 +498,21 @@ pub fn confirm_utxos() -> Result<bool, String> {
})
}

pub fn get_utx(txid: &ByteN<32>) -> Option<UnspentTx> {
pub fn get_utx(txid: &ByteArray<32>) -> Option<UnspentTx> {
state::with(|s| match s.unconfirmed_utxs.get(txid) {
Some(utx) => Some(UnspentTx::from(utx.clone())),
None => UTXS.with(|r| r.borrow().get(txid).map(UnspentTx::from)),
})
}

pub fn get_tx_block_height(txid: &ByteN<32>) -> Option<u64> {
pub fn get_tx_block_height(txid: &ByteArray<32>) -> Option<u64> {
state::with(|s| match s.unconfirmed_utxs.get(txid) {
Some(utx) => Some(utx.0),
None => UTXS.with(|r| r.borrow().get(txid).map(|utx| utx.0)),
})
}

pub fn get_balance(addr: &ByteN<21>) -> u64 {
pub fn get_balance(addr: &ByteArray<21>) -> u64 {
let mut res = UTXOS.with(|r| r.borrow().get(addr).unwrap_or_default()).0;
state::with(|s| {
if let Some((uts, sts)) = s.unconfirmed_utxos.get(addr) {
Expand All @@ -525,7 +525,7 @@ pub fn get_balance(addr: &ByteN<21>) -> u64 {
res.into_iter().map(|v| v.3).sum()
}

pub fn list_utxos(addr: &ByteN<21>, take: usize, confirmed: bool) -> Vec<Utxo> {
pub fn list_utxos(addr: &ByteArray<21>, take: usize, confirmed: bool) -> Vec<Utxo> {
let mut res = UTXOS.with(|r| r.borrow().get(addr).unwrap_or_default()).0;
if !confirmed {
state::with(|s| {
Expand All @@ -542,16 +542,16 @@ pub fn list_utxos(addr: &ByteN<21>, take: usize, confirmed: bool) -> Vec<Utxo> {
}

fn process_spent_tx(
unconfirmed_utxs: &mut BTreeMap<ByteN<32>, UnspentTxState>,
unconfirmed_utxos: &mut BTreeMap<ByteN<21>, (UtxoStates, SpentUtxos)>,
unconfirmed_utxs: &mut BTreeMap<ByteArray<32>, UnspentTxState>,
unconfirmed_utxos: &mut BTreeMap<ByteArray<21>, (UtxoStates, SpentUtxos)>,
utm: &StableBTreeMap<[u8; 32], UnspentTxState, Memory>,
chain: &ChainParams,
tx: &transaction::Transaction,
txid: Txid,
height: u64,
) -> Result<(), String> {
for txin in tx.input.iter() {
let previd: ByteN<32> = (*txin.prevout.txid).into();
let previd: ByteArray<32> = (*txin.prevout.txid).into();
if let std::collections::btree_map::Entry::Vacant(e) = unconfirmed_utxs.entry(previd) {
if let Some(utx) = utm.get(&previd) {
// load unspent tx from stable storage
Expand All @@ -569,7 +569,7 @@ fn process_spent_tx(

// move spent utxo
if let Some(addr) = addr {
let addr: ByteN<21> = addr.0.into();
let addr: ByteArray<21> = addr.0.into();
let utxo = UtxoState(utx.0, previd, txin.prevout.vout, txout.value);
match unconfirmed_utxos.get_mut(&addr) {
Some((uts, sts)) => {
Expand Down Expand Up @@ -599,8 +599,8 @@ fn process_spent_tx(
}

fn add_unspent_txouts(
unconfirmed_utxs: &mut BTreeMap<ByteN<32>, UnspentTxState>,
unconfirmed_utxos: &mut BTreeMap<ByteN<21>, (UtxoStates, SpentUtxos)>,
unconfirmed_utxs: &mut BTreeMap<ByteArray<32>, UnspentTxState>,
unconfirmed_utxos: &mut BTreeMap<ByteArray<21>, (UtxoStates, SpentUtxos)>,
chain: &ChainParams,
tx: &transaction::Transaction,
txid: Txid,
Expand All @@ -622,7 +622,7 @@ fn add_unspent_txouts(
let (_, addr) = script::classify_script(txout.script_pubkey.as_bytes(), chain);

if let Some(addr) = addr {
let addr: ByteN<21> = addr.0.into();
let addr: ByteArray<21> = addr.0.into();
let utxo = UtxoState(height, txid.0, vout as u32, txout.value);
match unconfirmed_utxos.get_mut(&addr) {
Some((uts, _sts)) => {
Expand All @@ -645,13 +645,13 @@ fn add_unspent_txouts(
}

fn flush_confirmed_utxos(
unconfirmed_utxs: &mut BTreeMap<ByteN<32>, UnspentTxState>,
unconfirmed_utxos: &mut BTreeMap<ByteN<21>, (UtxoStates, SpentUtxos)>,
unconfirmed_utxs: &mut BTreeMap<ByteArray<32>, UnspentTxState>,
unconfirmed_utxos: &mut BTreeMap<ByteArray<21>, (UtxoStates, SpentUtxos)>,
utm: &mut StableBTreeMap<[u8; 32], UnspentTxState, Memory>,
xom: &mut StableBTreeMap<[u8; 21], UtxoStates, Memory>,
confirmed_height: u64,
) -> Result<(), String> {
let confirmed_txids: Vec<ByteN<32>> = unconfirmed_utxs
let confirmed_txids: Vec<ByteArray<32>> = unconfirmed_utxs
.iter()
.filter_map(|(txid, utx)| {
// remove the tx if all outputs are spent
Expand Down Expand Up @@ -684,7 +684,7 @@ fn flush_confirmed_utxos(
unconfirmed_utxs.remove(&txid);
}

let empty_addrs: Vec<ByteN<21>> = unconfirmed_utxos
let empty_addrs: Vec<ByteArray<21>> = unconfirmed_utxos
.iter_mut()
.filter_map(|(addr, (uts, sts))| {
let mut confirmed_utxos: BTreeSet<UtxoState> = BTreeSet::new();
Expand Down
2 changes: 1 addition & 1 deletion src/ck-doge-minter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ num-traits = { workspace = true }
once_cell = { workspace = true }
ic-cdk-timers = { workspace = true }
ic-stable-structures = { workspace = true }
icrc-ledger-types = { workspace = true }
lazy_static = { workspace = true }
bitcoin = { workspace = true }
dogecoin = { path = "../dogecoin", version = "0.5" }
getrandom = { version = "0.2", features = ["custom"] }
ic-crypto-extended-bip32 = { git = "https://github.com/dfinity/ic/", rev = "d19fa446ab35780b2c6d8b82ea32d808cca558d5" }
icrc-ledger-types = { git = "https://github.com/dfinity/ic/", rev = "d19fa446ab35780b2c6d8b82ea32d808cca558d5" }
13 changes: 4 additions & 9 deletions src/ck-doge-minter/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use ic_stable_structures::{
};
use icrc_ledger_types::icrc1::transfer::Memo;
use serde::{Deserialize, Serialize};
use serde_bytes::ByteBuf;
use serde_bytes::{ByteArray, ByteBuf};
use std::str::FromStr;
use std::{
borrow::Cow,
Expand Down Expand Up @@ -113,7 +113,7 @@ impl Storable for State {
}

#[derive(Clone, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UtxoState(pub u64, pub canister::ByteN<32>, pub u32, pub u64);
pub struct UtxoState(pub u64, pub ByteArray<32>, pub u32, pub u64);

impl From<UtxoState> for canister::Utxo {
fn from(utxo: UtxoState) -> Self {
Expand Down Expand Up @@ -850,12 +850,7 @@ mod test {

#[test]
fn test_bound_max_size() {
let v = UtxoState(
u64::MAX,
canister::ByteN::from([255u8; 32]),
u32::MAX,
u64::MAX,
);
let v = UtxoState(u64::MAX, ByteArray::from([255u8; 32]), u32::MAX, u64::MAX);
let v = v.to_bytes();
println!(
"UtxoState max_size: {}, {}",
Expand All @@ -864,7 +859,7 @@ mod test {
);
// UtxoState max_size: 58, 841bffffffffffffffff5820ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1affffffff1bffffffffffffffff

let v = UtxoState(0, canister::ByteN::from([0u8; 32]), 0, 0);
let v = UtxoState(0, ByteArray::from([0u8; 32]), 0, 0);
let v = v.to_bytes();
println!(
"UtxoState min_size: {}, {}",
Expand Down
Loading

0 comments on commit a32d94e

Please sign in to comment.