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

[pull] master from broxus:master #14

Merged
merged 8 commits into from
Apr 15, 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
36 changes: 19 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = [
"Ivan Kalinin <i.kalinin@dexpa.io>",
"Stanislav Eliseev <s.eliseev@dexpa.io>"
]
rust-version = "1.62.0"
rust-version = "1.65.0"
edition = "2021"

[workspace]
Expand All @@ -25,35 +25,34 @@ members = [
anyhow = "1.0"
async-trait = "0.1"
base64 = "0.13"
chacha20poly1305 = "0.10.0-pre.1"
curve25519-dalek-ng = "4.1.1"
chacha20poly1305 = { version = "0.10.1", optional = true }
curve25519-dalek-ng = { version = "4.1.1", optional = true }
downcast-rs = "1.2"
dyn-clone = "1.0"
erased-serde = { version = "0.3.23", optional = true }
futures-util = "0.3"
getrandom = "0.2.4"
getrandom = { version = "0.2.4", optional = true }
hex = "0.4"
hmac = "0.11.0"
hmac = { version = "0.11.0", optional = true }
log = "0.4"
lru = "0.8.0"
num-bigint = "0.4"
once_cell = "1.12.0"
parking_lot = "0.12.0"
pbkdf2 = "0.9.0"
quick_cache = "0.3.0"
rand = { version = "0.8", features = ["getrandom"] }
secstr = { version = "0.5.0", features = ["serde"] }
pbkdf2 = { version = "0.9.0", optional = true }
quick_cache = "0.4.1"
rand = { version = "0.8", features = ["getrandom"] , optional = true }
secstr = { version = "0.5.0", features = ["serde"], optional = true }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
sha2 = "0.9.9"
sha2 = { version = "0.9.9", optional = true }
thiserror = "1.0"
tiny-jsonrpc = { version = "0.6.0", default-features = false, optional = true }
tokio = { version = "1", default-features = false, features = ["sync"] }
zeroize = "1"
zeroize = { version = "1", optional = true }

ed25519-dalek = { git = "https://github.com/broxus/ed25519-dalek.git" }
tiny-bip39 = { git = "https://github.com/broxus/tiny-bip39.git", default-features = false }
tiny-hderive = { git = "https://github.com/broxus/tiny-hderive.git" }
ed25519-dalek = { git = "https://github.com/broxus/ed25519-dalek.git", optional = true }
tiny-bip39 = { git = "https://github.com/broxus/tiny-bip39.git", default-features = false, optional = true }
tiny-hderive = { git = "https://github.com/broxus/tiny-hderive.git", optional = true }

ton_abi = { git = "https://github.com/broxus/ton-labs-abi" }
ton_block = { git = "https://github.com/broxus/ton-labs-block.git" }
Expand All @@ -62,7 +61,7 @@ ton_types = { git = "https://github.com/broxus/ton-labs-types.git" }

nekoton-contracts = { path = "nekoton-contracts" }
nekoton-abi = { path = "nekoton-abi", features = ["derive"] }
nekoton-utils = { path = "nekoton-utils", features = ["encryption"] }
nekoton-utils = { path = "nekoton-utils" }
nekoton-proto = { path = "nekoton-proto", optional = true }

[dev-dependencies]
Expand All @@ -71,20 +70,23 @@ cargo-husky = { version = "1", features = ["default", "run-cargo-fmt", "run-carg
tokio = { version = "1", features = ["rt-multi-thread", "macros"] }

[features]
default = ["gql_transport"]
default = ["gql_transport", "wallet_core"]
integration_test = []
web = [
"nekoton-contracts/web",
"nekoton-abi/web",
"nekoton-utils/web",
"getrandom/wasm-bindgen",
"ton_abi/web",
"wallet_core"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bitcoin

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To whom it may concern please I need a full details

]
gql_transport = ["dep:erased-serde"]
jrpc_transport = ["dep:tiny-jsonrpc"]
proto_transport = ["dep:nekoton-proto"]
extended_models = []
non_threadsafe = []
wallet_core = ["dep:pbkdf2", "dep:chacha20poly1305", "dep:zeroize", "dep:secstr", "dep:hmac", "dep:ed25519-dalek",
"dep:tiny-bip39", "dep:tiny-hderive", "dep:sha2", "dep:getrandom", "dep:rand", "dep:curve25519-dalek-ng", "nekoton-utils/encryption"]

[package.metadata.docs.rs]
all-features = true
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ cargo add nekoton

### Prerequisites

- Rust 1.62+
- Rust 1.65+
- `wasm-pack` 0.9.1+ (to test build for wasm target)
- protoc 3.12.4+ (to generate .rs files from .proto)

Expand Down
2 changes: 1 addition & 1 deletion nekoton-abi/src/tvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub fn call_msg(
.map_err(|_| ExecutionError::FailedToRetrieveActions)?;

let mut msgs = Vec::new();
for (_, action) in actions.iter_mut().enumerate() {
for action in actions.iter_mut() {
if let OutAction::SendMsg { out_msg, .. } = std::mem::replace(action, OutAction::None) {
msgs.push(out_msg);
}
Expand Down
2 changes: 2 additions & 0 deletions nekoton-contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ edition = "2021"
[dependencies]
anyhow = "1.0"
once_cell = "1.12"
serde = { version = "1.0.183", features = ["derive"] }
thiserror = "1.0"

ton_block = { git = "https://github.com/broxus/ton-labs-block.git" }
ton_types = { git = "https://github.com/broxus/ton-labs-types.git" }
ton_abi = { git = "https://github.com/broxus/ton-labs-abi" }

nekoton-abi = { path = "../nekoton-abi", features = ["derive"] }
nekoton-utils = { path = "../nekoton-utils" }

[features]
web = ["ton_abi/web"]
2 changes: 1 addition & 1 deletion nekoton-contracts/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![warn(
missing_copy_implementations,
macro_use_extern_crate,
keyword_idents,
explicit_outlives_requirements,
Expand Down Expand Up @@ -60,6 +59,7 @@ pub mod old_tip3;
pub mod tip1155;
pub mod tip3;
pub mod tip3_1;
pub mod tip3_any;
pub mod tip4_1;
pub mod tip4_2;
pub mod tip4_3;
Expand Down
62 changes: 62 additions & 0 deletions nekoton-contracts/src/tip3_any/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use nekoton_abi::num_bigint::BigUint;
use nekoton_utils::*;
use serde::{Deserialize, Serialize};
use ton_block::MsgAddressInt;

pub use self::root_token_contract::RootTokenContractState;
pub use self::token_wallet_contract::TokenWalletContractState;

mod root_token_contract;
mod token_wallet_contract;

define_string_enum!(
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum TokenWalletVersion {
/// Third iteration of token wallets, but with fixed bugs
/// [implementation](https://github.com/broxus/ton-eth-bridge-token-contracts/tree/74905260499d79cf7cb0d89a6eb572176fc1fcd5)
OldTip3v4,
/// Latest iteration with completely new standard
/// [implementation](https://github.com/broxus/ton-eth-bridge-token-contracts/tree/9168190f218fd05a64269f5f24295c69c4840d94)
Tip3,
}
);

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct RootTokenContractDetails {
/// Token ecosystem version
pub version: TokenWalletVersion,
/// Full currency name
pub name: String,
/// Short currency name
pub symbol: String,
/// Decimals
pub decimals: u8,
/// Root owner contract address. Used as proxy address in Tip3v1
#[serde(with = "serde_address")]
pub owner_address: MsgAddressInt,
#[serde(with = "serde_string")]
pub total_supply: BigUint,
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TokenWalletDetails {
/// Linked root token contract address
#[serde(with = "serde_address")]
pub root_address: MsgAddressInt,

/// Owner wallet address
#[serde(with = "serde_address")]
pub owner_address: MsgAddressInt,

#[serde(with = "serde_string")]
pub balance: BigUint,
}

#[derive(thiserror::Error, Debug)]
pub enum Tip3Error {
#[error("Unknown version")]
UnknownVersion,
#[error("Wallet not deployed")]
WalletNotDeployed,
}
80 changes: 80 additions & 0 deletions nekoton-contracts/src/tip3_any/root_token_contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use nekoton_abi::ExecutionContext;
use ton_block::MsgAddressInt;

use super::{RootTokenContractDetails, Tip3Error, TokenWalletVersion};
use crate::{old_tip3, tip3, tip3_1, tip6};

pub struct RootTokenContractState<'a>(pub ExecutionContext<'a>);

impl RootTokenContractState<'_> {
/// Calculates token wallet address
pub fn get_wallet_address(
&self,
version: TokenWalletVersion,
owner: &MsgAddressInt,
) -> anyhow::Result<MsgAddressInt> {
match version {
TokenWalletVersion::OldTip3v4 => {
old_tip3::RootTokenContract(self.0).get_wallet_address(owner.clone())
}
TokenWalletVersion::Tip3 => tip3_1::RootTokenContract(self.0).wallet_of(owner.clone()),
}
}

/// Tries to guess version and retrieve details
pub fn guess_details(&self) -> anyhow::Result<RootTokenContractDetails> {
if let Ok(true) = tip6::SidContract(self.0).supports_interfaces(&[
tip3::root_token_contract::INTERFACE_ID,
tip3_1::root_token_contract::INTERFACE_ID,
]) {
return self.get_details(TokenWalletVersion::Tip3);
}

let version = match old_tip3::RootTokenContract(self.0).get_version()? {
4 => TokenWalletVersion::OldTip3v4,
_ => anyhow::bail!(Tip3Error::UnknownVersion),
};

self.get_details(version)
}

/// Retrieve details using specified version
pub fn get_details(
&self,
version: TokenWalletVersion,
) -> anyhow::Result<RootTokenContractDetails> {
Ok(match version {
TokenWalletVersion::OldTip3v4 => {
let details = old_tip3::RootTokenContract(self.0).get_details()?;

RootTokenContractDetails {
version,
name: details.name,
symbol: details.symbol,
decimals: details.decimals,
owner_address: details.root_owner_address,
total_supply: details.total_supply,
}
}
TokenWalletVersion::Tip3 => {
let root_contract = tip3::RootTokenContract(self.0);
let name = root_contract.name()?;
let symbol = root_contract.symbol()?;
let decimals = root_contract.decimals()?;
let total_supply = root_contract.total_supply()?;

let root_contract = tip3_1::RootTokenContract(self.0);
let owner_address = root_contract.root_owner()?;

RootTokenContractDetails {
version,
name,
symbol,
decimals,
owner_address,
total_supply,
}
}
})
}
}
71 changes: 71 additions & 0 deletions nekoton-contracts/src/tip3_any/token_wallet_contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use nekoton_abi::num_bigint::BigUint;
use nekoton_abi::ExecutionContext;

use super::{Tip3Error, TokenWalletDetails, TokenWalletVersion};
use crate::{old_tip3, tip3, tip3_1, tip6};

pub struct TokenWalletContractState<'a>(pub ExecutionContext<'a>);

impl<'a> TokenWalletContractState<'a> {
pub fn get_code_hash(&self) -> anyhow::Result<ton_types::UInt256> {
match &self.0.account_stuff.storage.state {
ton_block::AccountState::AccountActive { state_init, .. } => {
let code = state_init
.code
.as_ref()
.ok_or(Tip3Error::WalletNotDeployed)?;
Ok(code.repr_hash())
}
_ => Err(Tip3Error::WalletNotDeployed.into()),
}
}

pub fn get_balance(&self, version: TokenWalletVersion) -> anyhow::Result<BigUint> {
match version {
TokenWalletVersion::OldTip3v4 => old_tip3::TokenWalletContract(self.0).balance(),
TokenWalletVersion::Tip3 => tip3::TokenWalletContract(self.0).balance(),
}
}

pub fn get_details(&self, version: TokenWalletVersion) -> anyhow::Result<TokenWalletDetails> {
Ok(match version {
TokenWalletVersion::OldTip3v4 => {
let details = old_tip3::TokenWalletContract(self.0).get_details()?;

TokenWalletDetails {
root_address: details.root_address,
owner_address: details.owner_address,
balance: details.balance,
}
}
TokenWalletVersion::Tip3 => {
let token_wallet = tip3::TokenWalletContract(self.0);
let root_address = token_wallet.root()?;
let balance = token_wallet.balance()?;

let token_wallet = tip3_1::TokenWalletContract(self.0);
let owner_address = token_wallet.owner()?;

TokenWalletDetails {
root_address,
owner_address,
balance,
}
}
})
}

pub fn get_version(&self) -> anyhow::Result<TokenWalletVersion> {
if let Ok(true) = tip6::SidContract(self.0).supports_interfaces(&[
tip3::token_wallet_contract::INTERFACE_ID,
tip3_1::token_wallet_contract::INTERFACE_ID,
]) {
return Ok(TokenWalletVersion::Tip3);
}

match old_tip3::TokenWalletContract(self.0).get_version()? {
4 => Ok(TokenWalletVersion::OldTip3v4),
_ => Err(Tip3Error::UnknownVersion.into()),
}
}
}
2 changes: 1 addition & 1 deletion nekoton-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0"
base64 = "0.13"
chacha20poly1305 = { version = "0.10.0-pre.1", optional = true }
chacha20poly1305 = { version = "0.10.1", optional = true }
hex = "0.4"
hmac = "0.11.0"
js-sys = { version = "0.3", optional = true }
Expand Down
17 changes: 17 additions & 0 deletions nekoton-utils/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,22 @@ pub fn compute_total_transaction_fees(
.map(|grams| grams.as_u128())
.unwrap_or_default();
};
if let Some(ton_block::TrBouncePhase::Ok(phase)) = &description.bounce {
total_fees += phase.fwd_fees.as_u128();
}
total_fees
}

#[cfg(test)]
mod tests {
use ton_block::Deserializable;

use super::*;

#[test]
fn balance_change_for_bounce_tx() {
let tx = ton_block::Transaction::construct_from_base64("te6ccgECBwEAAXgAA7V7I6v9Bo6UZTcpUTDMPNHomt63V2qkcrrjqlh+9STZH1AAArX2P2tMMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAZfQohAAD5gUWEIAwIBAB8ECQ7kyjgBwDAosIMFFhhAAIJykK7Illr6uxbrw8ubQI665xthjXh4i8gNCYQ1k8rJjaSQrsiWWvq7FuvDy5tAjrrnG2GNeHiLyA0JhDWTysmNpAIB4AYEAQHfBQC5WAFkdX+g0dKMpuUqJhmHmj0TW9bq7VSOV1x1Sw/epJsj6wAmfD7CYutxv9bl0y1a1XmYfSoPdQXCpsr6XdmJS4KcONDuLh8ABgosMAAAVr7H7WmIy+hRCH/////AALFoATPh9hMXW43+ty6ZatarzMPpUHuoLhU2V9LuzEpcFOHHACyOr/QaOlGU3KVEwzDzR6Jret1dqpHK646pYfvUk2R9UO5Mo4AGCiwwAABWvsftaYTL6FEIQA==").unwrap();
let balance_change = compute_balance_change(&tx);
assert_eq!(balance_change, 0);
}
}
Loading
Loading