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

refactor: separate contract interfaces from impls #5

Merged
merged 9 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
497 changes: 379 additions & 118 deletions Cargo.lock

Large diffs are not rendered by default.

23 changes: 14 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
[workspace]
resolver = "2"
members = ["account", "contracts", "controller", "intent", "tests"]
default-members = ["contracts"]

[workspace.package]
edition = "2021"

[workspace.dependencies]
defuse-account-contract.path = "./account"
defuse-contracts = { path = "./contracts", default-features = false }
defuse-controller-contract.path = "./controller"
defuse-intent-contract.path = "./intent"

anyhow = "1"
lazy_static = "1.4"
near-contract-standards = "5.1.0"
near-sdk = "5.1"
near-workspaces = "0.10"
serde_json = "1"
thiserror = "1"
tokio = { version = "1.38", default-features = false }

[workspace]
resolver = "2"
members = [
"account",
"controller",
"intent",
"tests"
]

[workspace.lints.clippy]
all = "deny"
nursery = "deny"
Expand All @@ -28,5 +32,6 @@ codegen-units = 1
opt-level = "z"
lto = true
debug = false
strip = "symbols"
panic = "abort"
overflow-checks = true
24 changes: 6 additions & 18 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ default_to_workspace = false
skip_core_tasks = true

[env]
RUSTFLAGS = "-C link-arg=-s"
TARGET = "wasm32-unknown-unknown"
TARGET_DIR = "${PWD}/res"
ACCOUNT_WASM_FILE = "defuse-account-contract.wasm"
Expand All @@ -22,27 +21,16 @@ dependencies = [

[tasks.clippy]
command = "cargo"
dependencies = [
"build",
]
args = [
"clippy",
"--workspace",
"--all-targets"
]
dependencies = ["build"]
args = ["clippy", "--workspace", "--all-targets"]

[tasks.test]
alias = "tests"

[tasks.tests]
dependencies = [
"build"
]
dependencies = ["build"]
command = "cargo"
args = [
"test",
"--all-targets"
]
args = ["test", "--workspace", "--all-targets"]

[tasks.build-account]
command = "cargo"
Expand All @@ -52,7 +40,7 @@ args = [
"${TARGET}",
"--release",
"--package",
"defuse-account-contract"
"defuse-account-contract",
]

[tasks.build-intent]
Expand All @@ -74,7 +62,7 @@ args = [
"${TARGET}",
"--release",
"-p",
"defuse-controller-contract"
"defuse-controller-contract",
]

[tasks.cp-contracts]
Expand Down
3 changes: 3 additions & 0 deletions account/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ crate-type = ["cdylib", "rlib"]
workspace = true

[dependencies]
defuse-contracts = { workspace = true, features = ["account"] }

near-sdk.workspace = true
thiserror.workspace = true

[dev-dependencies]
near-sdk = { workspace = true, features = ["unit-testing"] }
37 changes: 23 additions & 14 deletions account/src/types/account_db.rs → account/src/account_db.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use near_sdk::store::LookupMap;
use near_sdk::{near, AccountId, IntoStorageKey};
use std::collections::HashMap;

use crate::error::Error;
use crate::types::Account;
use defuse_contracts::account::{Account, AccountError};
use near_sdk::{near, store::LookupMap, AccountId, IntoStorageKey};

// Accounts that belong user. Key here is derivation path.
type UserAccounts = HashMap<String, Account>;
Expand All @@ -21,10 +19,10 @@ impl AccountDb {
account_id: AccountId,
derivation_path: String,
account: Account,
) -> Result<(), Error> {
) -> Result<(), AccountError> {
if let Some(accounts) = self.0.get_mut(&account_id) {
if accounts.contains_key(&derivation_path) {
return Err(Error::AccountExist);
return Err(AccountError::AccountExist);
}

accounts.insert(derivation_path, account);
Expand All @@ -41,20 +39,27 @@ impl AccountDb {
from: &AccountId,
to: AccountId,
derivation_path: String,
) -> Result<(), Error> {
) -> Result<(), AccountError> {
let account = self
.0
.get_mut(from)
.ok_or(Error::EmptyAccounts)
.and_then(|accounts| accounts.remove(&derivation_path).ok_or(Error::NoAccount))?;
.ok_or(AccountError::EmptyAccounts)
.and_then(|accounts| {
accounts
.remove(&derivation_path)
.ok_or(AccountError::NoAccount)
})?;

self.add_account(to, derivation_path, account)
}

pub fn get_accounts(&self, account_id: &AccountId) -> Result<Vec<(String, Account)>, Error> {
pub fn get_accounts(
&self,
account_id: &AccountId,
) -> Result<Vec<(String, Account)>, AccountError> {
self.0
.get(account_id)
.map_or(Err(Error::EmptyAccounts), |accounts| {
.map_or(Err(AccountError::EmptyAccounts), |accounts| {
Ok(accounts
.iter()
.map(|(d, a)| (d.clone(), a.clone()))
Expand All @@ -63,7 +68,11 @@ impl AccountDb {
}

#[allow(dead_code)]
pub fn assert_owner(&self, account_id: &AccountId, derivation_path: &str) -> Result<(), Error> {
pub fn assert_owner(
&self,
account_id: &AccountId,
derivation_path: &str,
) -> Result<(), AccountError> {
self.0
.get(account_id)
.and_then(|accounts| {
Expand All @@ -73,7 +82,7 @@ impl AccountDb {
None
}
})
.ok_or(Error::EmptyAccounts)
.ok_or(AccountError::EmptyAccounts)
}
}

Expand Down Expand Up @@ -108,7 +117,7 @@ fn test_account_db_change_owner() {

assert!(matches!(
db.assert_owner(&account_id, &path),
Err(Error::EmptyAccounts)
Err(AccountError::EmptyAccounts)
));
assert!(matches!(db.assert_owner(&new_owner, &path), Ok(())));
}
36 changes: 0 additions & 36 deletions account/src/error.rs

This file was deleted.

79 changes: 31 additions & 48 deletions account/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
use near_sdk::store::LookupSet;
use near_sdk::{
env, ext_contract, near, AccountId, BorshStorageKey, PanicOnDefault, PromiseOrValue,
};
use defuse_contracts::account::{Account, AccountContract};
use near_sdk::{env, near, store::LookupSet, AccountId, BorshStorageKey, PanicOnDefault};

use crate::error::LogError;
use crate::types::{Account, AccountDb};
use self::account_db::AccountDb;

mod error;
mod types;
mod account_db;

#[derive(BorshStorageKey)]
#[near(serializers=[borsh])]
Expand All @@ -18,7 +14,7 @@ enum Prefix {

#[near(contract_state)]
#[derive(PanicOnDefault)]
pub struct AccountContract {
pub struct AccountContractImpl {
owner_id: AccountId,
/// MPC contract id.
mpc_contract_id: AccountId,
Expand All @@ -30,68 +26,55 @@ pub struct AccountContract {
}

#[near]
impl AccountContract {
#[init]
#[must_use]
#[allow(clippy::use_self)]
pub fn new(owner_id: AccountId, mpc_contract_id: AccountId) -> Self {
Self {
owner_id,
mpc_contract_id,
indexers: LookupSet::new(Prefix::Indexers),
accounts: AccountDb::new(Prefix::Accounts),
}
}

/// Add a new ownership of a new tokens.
pub fn add_account(&mut self, account_id: AccountId, derivation_path: String) {
impl AccountContract for AccountContractImpl {
fn create_account(&mut self, account_id: AccountId, derivation_path: String) {
// Only indexers can call this transaction.
let predecessor_id = env::predecessor_account_id();
self.assert_indexer(&predecessor_id);

self.accounts
.add_account(account_id, derivation_path, Account::default())
.log_error();
.unwrap();
}

/// Change an owner of the account.
pub fn change_owner(&mut self, from: &AccountId, to: AccountId, derivation_path: String) {
fn change_owner(&mut self, from: &AccountId, to: AccountId, derivation_path: String) {
self.accounts
.change_owner(from, to, derivation_path)
.log_error();
.unwrap();
}

/// Return a user's accounts.
pub fn get_accounts(&self, account_id: &AccountId) -> Vec<(String, Account)> {
self.accounts.get_accounts(account_id).log_error()
fn get_accounts(&self, account_id: &AccountId) -> Vec<(String, Account)> {
self.accounts.get_accounts(account_id).unwrap()
}

fn mpc_contract(&self) -> &AccountId {
&self.mpc_contract_id
}
}

#[near]
impl AccountContractImpl {
#[init]
#[must_use]
#[allow(clippy::use_self)]
pub fn new(owner_id: AccountId, mpc_contract_id: AccountId) -> Self {
Self {
owner_id,
mpc_contract_id,
indexers: LookupSet::new(Prefix::Indexers),
accounts: AccountDb::new(Prefix::Accounts),
}
}

#[private]
pub fn set_mpc_contract(&mut self, contract_id: AccountId) {
self.mpc_contract_id = contract_id;
}

pub const fn get_mpc_contract(&self) -> &AccountId {
&self.mpc_contract_id
}

fn assert_indexer(&self, account_id: &AccountId) {
assert!(
self.indexers.contains(account_id),
"Only indexers allow adding an account"
);
}
}

#[ext_contract(ext_mpc)]
pub trait MpcRecovery {
fn sign(
&self,
payload: Vec<u8>,
path: &str,
key_version: u32,
) -> PromiseOrValue<(String, String)>;
}

#[cfg(test)]
mod contract_tests {}
8 changes: 0 additions & 8 deletions account/src/types/account.rs

This file was deleted.

5 changes: 0 additions & 5 deletions account/src/types/mod.rs

This file was deleted.

19 changes: 19 additions & 0 deletions contracts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "defuse-contracts"
version = "0.1.0"
edition.workspace = true

[lints]
workspace = true

[dependencies]
near-sdk.workspace = true
near-contract-standards.workspace = true
thiserror.workspace = true

[features]
default = ["account", "controller", "intent", "mpc"]
account = []
controller = []
intent = []
mpc = []
3 changes: 3 additions & 0 deletions contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Defuse Contracts

This is a collection of interfaces for all Defuse smart-contracts
Loading
Loading