Skip to content

Commit

Permalink
feat: add intent for swapping nep141
Browse files Browse the repository at this point in the history
  • Loading branch information
gonchar1987 committed May 23, 2024
1 parent 3c75762 commit 1b16ba4
Show file tree
Hide file tree
Showing 26 changed files with 724 additions and 188 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: CI

on:
push:
branches:
- main
- develop
pull_request:

jobs:
fmt:
name: Formatting
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Run rustfmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all --check

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Install cargo-make
uses: actions-rs/cargo@v1
with:
command: install
args: cargo-make
- name: Run clippy
run: cargo make clippy

tests:
name: Tests
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Install cargo-make
uses: actions-rs/cargo@v1
with:
command: install
args: cargo-make
- name: Run tests
run: cargo make tests
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.

5 changes: 5 additions & 0 deletions account/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ edition.workspace = true
[lib]
crate-type = ["cdylib", "rlib"]

[lints.clippy]
all = "deny"
nursery = "deny"
pedantic = "deny"

[dependencies]
near-sdk.workspace = true

Expand Down
1 change: 1 addition & 0 deletions account/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl AsRef<str> for Error {
}
}

#[allow(clippy::module_name_repetitions)]
pub trait LogError<T> {
fn log_error(self) -> T;
}
Expand Down
10 changes: 6 additions & 4 deletions account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub struct AccountContract {
#[near_bindgen]
impl AccountContract {
#[init]
#[must_use]
#[allow(clippy::use_self)]
pub fn new(owner_id: AccountId, mpc_contract_id: AccountId) -> Self {
Self {
owner_id,
Expand All @@ -55,23 +57,23 @@ impl AccountContract {
}

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

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

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

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

Expand Down
24 changes: 12 additions & 12 deletions account/src/types/account_db.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use near_sdk::borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::collections::HashMap;
use near_sdk::store::LookupMap;
use near_sdk::{AccountId, IntoStorageKey};
use std::collections::HashMap;

use crate::error::Error;
use crate::types::Account;
Expand Down Expand Up @@ -40,28 +40,28 @@ impl AccountDb {

pub fn change_owner(
&mut self,
from: AccountId,
from: &AccountId,
to: AccountId,
derivation_path: String,
) -> Result<(), Error> {
let account = self
.0
.get_mut(&from)
.get_mut(from)
.ok_or(Error::EmptyAccounts)
.and_then(|accounts| accounts.remove(&derivation_path).ok_or(Error::NoAccount))?;

self.add_account(to, derivation_path, account)
}

pub fn get_accounts(&self, account_id: &AccountId) -> Result<Vec<(String, Account)>, Error> {
if let Some(accounts) = self.0.get(account_id) {
Ok(accounts
.iter()
.map(|(d, a)| (d.clone(), a.clone()))
.collect())
} else {
Err(Error::EmptyAccounts)
}
self.0
.get(account_id)
.map_or(Err(Error::EmptyAccounts), |accounts| {
Ok(accounts
.iter()
.map(|(d, a)| (d.clone(), a.clone()))
.collect())
})
}

#[allow(dead_code)]
Expand Down Expand Up @@ -105,7 +105,7 @@ fn test_account_db_change_owner() {
let new_owner: AccountId = "owner.near".parse().unwrap();

assert!(db
.change_owner(account_id.clone(), new_owner.clone(), path.clone())
.change_owner(&account_id, new_owner.clone(), path.clone())
.is_ok());

assert!(matches!(
Expand Down
5 changes: 5 additions & 0 deletions controller/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ edition.workspace = true
[lib]
crate-type = ["cdylib", "rlib"]

[lints.clippy]
all = "deny"
nursery = "deny"
pedantic = "deny"

[dependencies]
near-sdk.workspace = true
4 changes: 3 additions & 1 deletion controller/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ pub struct ControllerContract {
#[near_bindgen]
impl ControllerContract {
#[init]
pub fn new(owner_id: AccountId) -> Self {
#[must_use]
#[allow(clippy::use_self)]
pub const fn new(owner_id: AccountId) -> Self {
Self { owner_id }
}
}
5 changes: 5 additions & 0 deletions intent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ edition.workspace = true
[lib]
crate-type = ["cdylib", "rlib"]

[lints.clippy]
all = "deny"
nursery = "deny"
pedantic = "deny"

[dependencies]
near-sdk.workspace = true
19 changes: 19 additions & 0 deletions intent/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[allow(clippy::module_name_repetitions)]
#[derive(Debug)]
pub enum ContractError {
BorshSerializeError,
BorshDeserializeError,
Base64EncodeError,
Base64DecodeError,
}

impl AsRef<str> for ContractError {
fn as_ref(&self) -> &str {
match self {
Self::BorshSerializeError => "BORSH_SERIALIZE_ERROR",
Self::BorshDeserializeError => "BORSH_DESERIALIZE_ERROR",
Self::Base64EncodeError => "BASE64_ENCODE_ERROR",
Self::Base64DecodeError => "BASE64_DECODE_ERROR",
}
}
}
Loading

0 comments on commit 1b16ba4

Please sign in to comment.