Skip to content

Commit

Permalink
🚧 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
maxgttph committed Dec 20, 2024
1 parent 94907cc commit d62494b
Show file tree
Hide file tree
Showing 24 changed files with 1,919 additions and 52 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [

"crates/bonsai-runner",
"crates/hyrun",
"crates/hyle-loadtest",
]
resolver = "2"

Expand All @@ -33,7 +34,9 @@ hyle-contract-sdk = { path = "./contract-sdk", features = ["tracing"] }
hyle-contracts = { path = "./contracts", package = "hyle-contracts" }
hydentity = { path = "./contracts/hydentity" }
hyllar = { path = "./contracts/hyllar" }
amm = { path = "./contracts/amm" }
staking = { path = "./contracts/staking" }
bonsai-runner = { path = "./crates/bonsai-runner" }
config = "0.15.0"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["json"] }
Expand Down
6 changes: 3 additions & 3 deletions contract-sdk/src/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use bincode::{Decode, Encode};

use crate::{
caller::{CallerCallee, CheckCalleeBlobs},
Blob, BlobData, BlobIndex, ContractName, RunResult, StructuredBlobData,
Blob, BlobData, BlobIndex, ContractAction, ContractName, RunResult, StructuredBlobData,
};

/// Trait representing the ERC-20 token standard interface.
Expand Down Expand Up @@ -106,8 +106,8 @@ pub enum ERC20Action {
},
}

impl ERC20Action {
pub fn as_blob(
impl ContractAction for ERC20Action {
fn as_blob(
&self,
contract_name: ContractName,
caller: Option<BlobIndex>,
Expand Down
13 changes: 9 additions & 4 deletions contract-sdk/src/identity_provider.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::{format, string::String};
use alloc::{format, string::String, vec::Vec};
use bincode::{Decode, Encode};

use crate::{Blob, BlobData, ContractName, RunResult};
use crate::{Blob, BlobData, BlobIndex, ContractAction, ContractName, RunResult};

/// Trait representing an identity verification contract.
pub trait IdentityVerification {
Expand Down Expand Up @@ -56,8 +56,13 @@ pub enum IdentityAction {
GetIdentityInfo { account: String },
}

impl IdentityAction {
pub fn as_blob(self, contract_name: ContractName) -> Blob {
impl ContractAction for IdentityAction {
fn as_blob(
&self,
contract_name: ContractName,
_caller: Option<BlobIndex>,
_callees: Option<Vec<BlobIndex>>,
) -> Blob {
Blob {
contract_name,
data: BlobData(
Expand Down
9 changes: 9 additions & 0 deletions contract-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ impl<Parameters: Decode> TryFrom<Blob> for StructuredBlob<Parameters> {
}
}

pub trait ContractAction: Send {
fn as_blob(
&self,
contract_name: ContractName,
caller: Option<BlobIndex>,
callees: Option<Vec<BlobIndex>>,
) -> Blob;
}

pub fn flatten_blobs(blobs: &[Blob]) -> Vec<u8> {
blobs
.iter()
Expand Down
35 changes: 22 additions & 13 deletions contracts/amm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bincode::{Decode, Encode};
use sdk::caller::{CalleeBlobs, CallerCallee, CheckCalleeBlobs, ExecutionContext, MutCalleeBlobs};
use sdk::erc20::{ERC20BlobChecker, ERC20};
use sdk::{erc20::ERC20Action, Identity};
use sdk::{Blob, BlobIndex, Digestable, RunResult};
use sdk::{Blob, BlobIndex, ContractAction, Digestable, RunResult};
use sdk::{BlobData, ContractName, StructuredBlobData};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -90,6 +90,14 @@ impl AmmState {
}
None
}

pub fn create_new_pair(&mut self, pair: UnorderedTokenPair, amounts: TokenPairAmount) {
self.pairs.insert(pair, amounts);
}

pub fn update_pair(&mut self, pair: UnorderedTokenPair, amounts: TokenPairAmount) {
self.pairs.insert(pair, amounts);
}
}

impl AmmContract {
Expand Down Expand Up @@ -144,7 +152,7 @@ impl AmmContract {

let program_outputs = format!("Pair {:?} created", normalized_pair);

self.state.pairs.insert(normalized_pair, amounts);
self.state.create_new_pair(normalized_pair, amounts);

Ok(program_outputs)
}
Expand Down Expand Up @@ -173,20 +181,21 @@ impl AmmContract {
// Compute x,y and check swap is legit (x*y=k)
let normalized_pair = UnorderedTokenPair::new(pair.0.clone(), pair.1.clone());
let is_normalized_order = pair.0 <= pair.1;
let Some((prev_x, prev_y)) = self.state.pairs.get_mut(&normalized_pair) else {
let Some((prev_x, prev_y)) = self.state.pairs.get(&normalized_pair) else {
return Err(format!("Pair {:?} not found in AMM state", pair));
};
let expected_to_amount = if is_normalized_order {
let (expected_to_amount, new_x, new_y) = if is_normalized_order {
let amount = *prev_y - (*prev_x * *prev_y / (*prev_x + from_amount));
*prev_x += from_amount;
*prev_y -= amount; // we need to remove the full amount to avoid slipping
amount
let new_x = prev_x + from_amount;
let new_y = prev_y - amount; // we need to remove the full amount to avoid slipping
(amount, new_x, new_y)
} else {
let amount = *prev_x - (*prev_y * *prev_x / (*prev_y + from_amount));
*prev_y += from_amount;
*prev_x -= amount; // we need to remove the full amount to avoid slipping
amount
let new_y = prev_y + from_amount;
let new_x = prev_x - amount; // we need to remove the full amount to avoid slipping
(amount, new_x, new_y)
};
self.state.update_pair(normalized_pair, (new_x, new_y));

// Assert that we transferred less than that, within 2%
if to_amount > expected_to_amount || to_amount < expected_to_amount * 98 / 100 {
Expand Down Expand Up @@ -250,9 +259,9 @@ pub enum AmmAction {
},
}

impl AmmAction {
pub fn as_blob(
self,
impl ContractAction for AmmAction {
fn as_blob(
&self,
contract_name: ContractName,
caller: Option<BlobIndex>,
callees: Option<Vec<BlobIndex>>,
Expand Down
12 changes: 12 additions & 0 deletions contracts/hyllar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ pub struct HyllarTokenContract {
}

impl HyllarToken {
pub fn init(
total_supply: u128,
balances: BTreeMap<String, u128>,
allowances: BTreeMap<(String, String), u128>,
) -> Self {
HyllarToken {
total_supply,
balances,
allowances,
}
}

/// Creates a new Hyllar token with the specified initial supply.
///
/// # Arguments
Expand Down
5 changes: 4 additions & 1 deletion contracts/hyllar/tests/hyllar_r0.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use core::str;

use hyllar::HyllarToken;
use sdk::{erc20::ERC20Action, BlobData, BlobIndex, ContractInput, ContractName, HyleOutput};
use sdk::{
erc20::ERC20Action, BlobData, BlobIndex, ContractAction, ContractInput, ContractName,
HyleOutput,
};

fn execute(inputs: ContractInput<HyllarToken>) -> HyleOutput {
let env = risc0_zkvm::ExecutorEnv::builder()
Expand Down
21 changes: 21 additions & 0 deletions crates/hyle-loadtest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "hyle-loadtest"
version.workspace = true
edition.workspace = true
homepage.workspace = true
repository.workspace = true


[dependencies]
serde_json = "1.0.133"
tokio = "^1.12"
hyle = { path = "../../" }
hyllar = { path = "../../contracts/hyllar" }
amm = { path = "../../contracts/amm" }
hydentity = { path = "../../contracts/hydentity" }
hyle-contracts = { path = "../../contracts", package = "hyle-contracts" }
hyle-contract-sdk = { path = "../../contract-sdk", features = ["tracing"] }
reqwest = "0.12.9"
anyhow = "1.0.94"
bincode = { version = "2.0.0-rc.3" }
clap = "4.5.23"
Loading

0 comments on commit d62494b

Please sign in to comment.