diff --git a/bridge-contract/package-lock.json b/bridge-contract/package-lock.json index 38ad63b..ab80eb1 100644 --- a/bridge-contract/package-lock.json +++ b/bridge-contract/package-lock.json @@ -1,16 +1,16 @@ { "name": "nacho-bridge-contract", - "version": "0.1.3", + "version": "0.1.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "nacho-bridge-contract", - "version": "0.1.3", + "version": "0.1.4", "dependencies": { "nacho-common-o1js": "^0.1.8", - "nacho-proof-generator": "^0.1.5", - "nacho-rollup-contract": "^0.1.4", + "nacho-proof-generator": "^0.1.7", + "nacho-rollup-contract": "^0.1.5", "nacho-token-contract": "^0.1.1", "o1js": "^0.17.0" }, @@ -64,21 +64,21 @@ } }, "node_modules/nacho-proof-generator": { - "version": "0.1.5", - "resolved": "https://registry.npmjs.org/nacho-proof-generator/-/nacho-proof-generator-0.1.5.tgz", - "integrity": "sha512-YfnX7RIr27qjdMMziPzM87IUBJyQXka7IZ9nfo6HNmNj9PM4Gy8UmhWA6R9juTdXdQjtTz31mZ174xNKSoDomQ==", + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/nacho-proof-generator/-/nacho-proof-generator-0.1.7.tgz", + "integrity": "sha512-UOQWZMmJPxb2x3bzsUp6vyDaDtyukToVvw1VBs59izFSxlUwUxPm0T21zHVGr65238A8ShrxQrKbh7EJBomSDA==", "dependencies": { - "nacho-common-o1js": "^0.1.7", + "nacho-common-o1js": "^0.1.8", "o1js": "^0.17.0" } }, "node_modules/nacho-rollup-contract": { - "version": "0.1.4", - "resolved": "https://registry.npmjs.org/nacho-rollup-contract/-/nacho-rollup-contract-0.1.4.tgz", - "integrity": "sha512-8SWloEWIMWXiKfo4kCvft7ECdFoFcK8st1Y02llK4zBG25S8D6LVEANWXe8WzRGKbFOyKT7PvIfE5f3rSVsy3g==", + "version": "0.1.5", + "resolved": "https://registry.npmjs.org/nacho-rollup-contract/-/nacho-rollup-contract-0.1.5.tgz", + "integrity": "sha512-GT7LwJ/WkRifCJrEcS8hknTMNh1fvCVR/77NsUqS5tKhLDNJNBq8cgon0CzmWw9okl4qm2nhJNfxRp9EPx7tCQ==", "dependencies": { "nacho-common-o1js": "^0.1.8", - "nacho-proof-generator": "^0.1.4", + "nacho-proof-generator": "^0.1.7", "o1js": "^0.17.0" } }, diff --git a/bridge-contract/package.json b/bridge-contract/package.json index 084bd3b..f507939 100644 --- a/bridge-contract/package.json +++ b/bridge-contract/package.json @@ -1,6 +1,6 @@ { "name": "nacho-bridge-contract", - "version": "0.1.3", + "version": "0.1.4", "type": "module", "main": "build/src/index.js", "types": "build/src/index.d.ts", @@ -13,8 +13,8 @@ }, "dependencies": { "nacho-common-o1js": "^0.1.8", - "nacho-proof-generator": "^0.1.5", - "nacho-rollup-contract": "^0.1.4", + "nacho-proof-generator": "^0.1.7", + "nacho-rollup-contract": "^0.1.5", "nacho-token-contract": "^0.1.1", "o1js": "^0.17.0" }, diff --git a/bridge-contract/tests/bridge-contract.test.ts b/bridge-contract/tests/bridge-contract.test.ts index 1cb5acf..90b62c1 100644 --- a/bridge-contract/tests/bridge-contract.test.ts +++ b/bridge-contract/tests/bridge-contract.test.ts @@ -227,7 +227,6 @@ describe("bridge contract", async () => { depositsList[0].tokenId, depositsList[0].tokenAmount, UInt64.from(0), - Bool(true), ) stateUtil.setBalance( @@ -256,7 +255,6 @@ describe("bridge contract", async () => { depositsList[1].tokenId, depositsList[1].tokenAmount, UInt64.from(0), - Bool(true), ) stateUtil.setBalance( diff --git a/data-structures/src/lib.rs b/data-structures/src/lib.rs index 595d397..8d53298 100644 --- a/data-structures/src/lib.rs +++ b/data-structures/src/lib.rs @@ -35,8 +35,15 @@ pub use sibling::Sibling; pub use signature::Signature; pub use single_merkle_witness::SingleMerkleWitness; pub use state_roots::StateRoots; -pub use stateful_transaction::StatefulTransaction; -pub use transaction::Transaction; +pub use stateful_transaction::{ + BurnTokensTransactionState, BuyTokensTransactionState, CreatePoolTransactionState, + DepositTokensTransactionState, ProvideLiquidityTransactionState, + RemoveLiquidityTransactionState, SellTokensTransactionState, StatefulTransaction, +}; +pub use transaction::{ + BurnTokensTransaction, BuyTokensTransaction, CreatePoolTransaction, DepositTokensTransaction, + ProvideLiquidityTransaction, RemoveLiquidityTransaction, SellTokensTransaction, Transaction, +}; pub use tx_status::TxStatus; pub use u256::U256; pub use withdrawal::Withdrawal; diff --git a/data-structures/src/prover_method.rs b/data-structures/src/prover_method.rs index 7c0ce37..3eeb81a 100644 --- a/data-structures/src/prover_method.rs +++ b/data-structures/src/prover_method.rs @@ -20,7 +20,6 @@ pub enum ProverMethod { token_id: U256, user_deposit_token_amount: u64, user_balance_token_amount: u64, - is_users_first_deposit: bool, }, BurnTokens { state_roots: StateRoots, @@ -66,7 +65,6 @@ pub enum ProverMethod { pool_total_liquidity_points: U256, user_base_token_amount_to_provide: u64, user_quote_token_amount_limit_to_provide: u64, - is_first_providing: bool, user_signature: Signature, }, RemoveLiquidity { @@ -144,7 +142,6 @@ impl ByteConversion<3291> for ProverMethod { token_id, user_deposit_token_amount, user_balance_token_amount, - is_users_first_deposit, } => { buf[0] = 1; buf[1..129].copy_from_slice(&state_roots.to_bytes()); @@ -156,7 +153,6 @@ impl ByteConversion<3291> for ProverMethod { buf[982..1014].copy_from_slice(&token_id.to_bytes()); buf[1014..1022].copy_from_slice(&user_deposit_token_amount.to_bytes()); buf[1022..1030].copy_from_slice(&user_balance_token_amount.to_bytes()); - buf[1030..1031].copy_from_slice(&is_users_first_deposit.to_bytes()); } ProverMethod::BurnTokens { state_roots, @@ -229,7 +225,6 @@ impl ByteConversion<3291> for ProverMethod { pool_total_liquidity_points, user_base_token_amount_to_provide, user_quote_token_amount_limit_to_provide, - is_first_providing, user_signature, } => { buf[0] = 4; @@ -250,8 +245,7 @@ impl ByteConversion<3291> for ProverMethod { buf[3179..3187].copy_from_slice(&user_base_token_amount_to_provide.to_bytes()); buf[3187..3195] .copy_from_slice(&user_quote_token_amount_limit_to_provide.to_bytes()); - buf[3195..3196].copy_from_slice(&is_first_providing.to_bytes()); - buf[3196..3260].copy_from_slice(&user_signature.to_bytes()); + buf[3195..3259].copy_from_slice(&user_signature.to_bytes()); } ProverMethod::RemoveLiquidity { state_roots, diff --git a/data-structures/src/stateful_transaction.rs b/data-structures/src/stateful_transaction.rs index a959626..ac5e0be 100644 --- a/data-structures/src/stateful_transaction.rs +++ b/data-structures/src/stateful_transaction.rs @@ -4,7 +4,7 @@ use crate::{ DepositTokensTransaction, ProvideLiquidityTransaction, RemoveLiquidityTransaction, SellTokensTransaction, }, - U256, + ByteConversion, U256, }; /// The enum that represents stateful transaction types. @@ -14,43 +14,42 @@ use crate::{ #[derive(Clone, Debug)] pub enum StatefulTransaction { CreateGenesis { - state: (), transaction: (), + state: (), }, DepositTokens { - state: DepositTokensTransactionState, transaction: DepositTokensTransaction, + state: DepositTokensTransactionState, }, BurnTokens { - state: BurnTokensTransactionState, transaction: BurnTokensTransaction, + state: BurnTokensTransactionState, }, CreatePool { - state: CreatePoolTransactionState, transaction: CreatePoolTransaction, + state: CreatePoolTransactionState, }, ProvideLiquidity { - state: ProvideLiquidityTransactionState, transaction: ProvideLiquidityTransaction, + state: ProvideLiquidityTransactionState, }, RemoveLiquidity { - state: RemoveLiquidityTransactionState, transaction: RemoveLiquidityTransaction, + state: RemoveLiquidityTransactionState, }, BuyTokens { - state: BuyTokensTransactionState, transaction: BuyTokensTransaction, + state: BuyTokensTransactionState, }, SellTokens { - state: SellTokensTransactionState, transaction: SellTokensTransaction, + state: SellTokensTransactionState, }, } #[derive(Clone, Debug)] pub struct DepositTokensTransactionState { pub user_token_balance: u64, - pub is_first_deposit_of_token: bool, } #[derive(Clone, Debug)] @@ -73,7 +72,6 @@ pub struct ProvideLiquidityTransactionState { pub pool_base_token_amount: u64, pub pool_quote_token_amount: u64, pub pool_total_liquidity_points: U256, - pub is_first_providing: bool, } #[derive(Clone, Debug)] @@ -103,3 +101,238 @@ pub struct SellTokensTransactionState { pub pool_quote_token_amount: u64, pub pool_total_liquidity_points: U256, } + +impl ByteConversion<328> for StatefulTransaction { + fn to_bytes(&self) -> [u8; 328] { + let mut buf = [0u8; 328]; + match self { + StatefulTransaction::CreateGenesis { + transaction: _, + state: _, + } => { + buf[0] = 0; + } + StatefulTransaction::DepositTokens { transaction, state } => { + buf[0] = 1; + buf[1..96].copy_from_slice(&transaction.to_bytes()); + buf[96..104].copy_from_slice(&state.to_bytes()); + } + StatefulTransaction::BurnTokens { transaction, state } => { + buf[0] = 2; + buf[1..160].copy_from_slice(&transaction.to_bytes()); + buf[160..176].copy_from_slice(&state.to_bytes()); + } + StatefulTransaction::CreatePool { transaction, state } => { + buf[0] = 3; + buf[1..200].copy_from_slice(&transaction.to_bytes()); + buf[200..216].copy_from_slice(&state.to_bytes()); + } + StatefulTransaction::ProvideLiquidity { transaction, state } => { + buf[0] = 4; + buf[1..200].copy_from_slice(&transaction.to_bytes()); + buf[200..296].copy_from_slice(&state.to_bytes()); + } + StatefulTransaction::RemoveLiquidity { transaction, state } => { + buf[0] = 5; + buf[1..232].copy_from_slice(&transaction.to_bytes()); + buf[232..328].copy_from_slice(&state.to_bytes()); + } + StatefulTransaction::BuyTokens { transaction, state } => { + buf[0] = 6; + buf[1..200].copy_from_slice(&transaction.to_bytes()); + buf[200..264].copy_from_slice(&state.to_bytes()); + } + StatefulTransaction::SellTokens { transaction, state } => { + buf[0] = 7; + buf[1..200].copy_from_slice(&transaction.to_bytes()); + buf[200..264].copy_from_slice(&state.to_bytes()); + } + } + buf + } + + fn from_bytes(bytes: &[u8; 328]) -> Self { + match bytes[0] { + 0 => StatefulTransaction::CreateGenesis { + transaction: (), + state: (), + }, + 1 => StatefulTransaction::DepositTokens { + transaction: DepositTokensTransaction::from_bytes(bytes[1..96].try_into().unwrap()), + state: DepositTokensTransactionState::from_bytes( + bytes[96..104].try_into().unwrap(), + ), + }, + 2 => StatefulTransaction::BurnTokens { + transaction: BurnTokensTransaction::from_bytes(bytes[1..160].try_into().unwrap()), + state: BurnTokensTransactionState::from_bytes(bytes[160..176].try_into().unwrap()), + }, + 3 => StatefulTransaction::CreatePool { + transaction: CreatePoolTransaction::from_bytes(bytes[1..200].try_into().unwrap()), + state: CreatePoolTransactionState::from_bytes(bytes[200..216].try_into().unwrap()), + }, + 4 => StatefulTransaction::ProvideLiquidity { + transaction: ProvideLiquidityTransaction::from_bytes( + bytes[1..200].try_into().unwrap(), + ), + state: ProvideLiquidityTransactionState::from_bytes( + bytes[200..296].try_into().unwrap(), + ), + }, + 5 => StatefulTransaction::RemoveLiquidity { + transaction: RemoveLiquidityTransaction::from_bytes( + bytes[1..232].try_into().unwrap(), + ), + state: RemoveLiquidityTransactionState::from_bytes( + bytes[232..328].try_into().unwrap(), + ), + }, + 6 => StatefulTransaction::BuyTokens { + transaction: BuyTokensTransaction::from_bytes(bytes[1..200].try_into().unwrap()), + state: BuyTokensTransactionState::from_bytes(bytes[200..264].try_into().unwrap()), + }, + _ => StatefulTransaction::SellTokens { + transaction: SellTokensTransaction::from_bytes(bytes[1..200].try_into().unwrap()), + state: SellTokensTransactionState::from_bytes(bytes[200..264].try_into().unwrap()), + }, + } + } +} + +impl ByteConversion<8> for DepositTokensTransactionState { + fn to_bytes(&self) -> [u8; 8] { + let mut buf = [0u8; 8]; + buf[0..8].copy_from_slice(&self.user_token_balance.to_bytes()); + buf + } + + fn from_bytes(bytes: &[u8; 8]) -> Self { + DepositTokensTransactionState { + user_token_balance: u64::from_bytes(bytes[0..8].try_into().unwrap()), + } + } +} + +impl ByteConversion<16> for BurnTokensTransactionState { + fn to_bytes(&self) -> [u8; 16] { + let mut buf = [0u8; 16]; + buf[0..8].copy_from_slice(&self.user_burn_token_amount.to_bytes()); + buf[8..16].copy_from_slice(&self.user_balance_token_amount.to_bytes()); + buf + } + + fn from_bytes(bytes: &[u8; 16]) -> Self { + BurnTokensTransactionState { + user_burn_token_amount: u64::from_bytes(bytes[0..8].try_into().unwrap()), + user_balance_token_amount: u64::from_bytes(bytes[8..16].try_into().unwrap()), + } + } +} + +impl ByteConversion<16> for CreatePoolTransactionState { + fn to_bytes(&self) -> [u8; 16] { + let mut buf = [0u8; 16]; + buf[0..8].copy_from_slice(&self.user_balance_base_token_amount.to_bytes()); + buf[8..16].copy_from_slice(&self.user_balance_quote_token_amount.to_bytes()); + buf + } + + fn from_bytes(bytes: &[u8; 16]) -> Self { + CreatePoolTransactionState { + user_balance_base_token_amount: u64::from_bytes(bytes[0..8].try_into().unwrap()), + user_balance_quote_token_amount: u64::from_bytes(bytes[8..16].try_into().unwrap()), + } + } +} + +impl ByteConversion<96> for ProvideLiquidityTransactionState { + fn to_bytes(&self) -> [u8; 96] { + let mut buf = [0u8; 96]; + buf[0..32].copy_from_slice(&self.user_liquidity_points.to_bytes()); + buf[32..40].copy_from_slice(&self.user_balance_base_token_amount.to_bytes()); + buf[40..48].copy_from_slice(&self.user_balance_quote_token_amount.to_bytes()); + buf[48..56].copy_from_slice(&self.pool_base_token_amount.to_bytes()); + buf[56..64].copy_from_slice(&self.pool_quote_token_amount.to_bytes()); + buf[64..96].copy_from_slice(&self.pool_total_liquidity_points.to_bytes()); + buf + } + + fn from_bytes(bytes: &[u8; 96]) -> Self { + ProvideLiquidityTransactionState { + user_liquidity_points: U256::from_bytes(bytes[0..32].try_into().unwrap()), + user_balance_base_token_amount: u64::from_bytes(bytes[32..40].try_into().unwrap()), + user_balance_quote_token_amount: u64::from_bytes(bytes[40..48].try_into().unwrap()), + pool_base_token_amount: u64::from_bytes(bytes[48..56].try_into().unwrap()), + pool_quote_token_amount: u64::from_bytes(bytes[56..64].try_into().unwrap()), + pool_total_liquidity_points: U256::from_bytes(bytes[64..96].try_into().unwrap()), + } + } +} + +impl ByteConversion<96> for RemoveLiquidityTransactionState { + fn to_bytes(&self) -> [u8; 96] { + let mut buf = [0u8; 96]; + buf[0..32].copy_from_slice(&self.user_liquidity_points.to_bytes()); + buf[32..40].copy_from_slice(&self.user_balance_base_token_amount.to_bytes()); + buf[40..48].copy_from_slice(&self.user_balance_quote_token_amount.to_bytes()); + buf[48..56].copy_from_slice(&self.pool_base_token_amount.to_bytes()); + buf[56..64].copy_from_slice(&self.pool_quote_token_amount.to_bytes()); + buf[64..96].copy_from_slice(&self.pool_total_liquidity_points.to_bytes()); + buf + } + + fn from_bytes(bytes: &[u8; 96]) -> Self { + RemoveLiquidityTransactionState { + user_liquidity_points: U256::from_bytes(bytes[0..32].try_into().unwrap()), + user_balance_base_token_amount: u64::from_bytes(bytes[32..40].try_into().unwrap()), + user_balance_quote_token_amount: u64::from_bytes(bytes[40..48].try_into().unwrap()), + pool_base_token_amount: u64::from_bytes(bytes[48..56].try_into().unwrap()), + pool_quote_token_amount: u64::from_bytes(bytes[56..64].try_into().unwrap()), + pool_total_liquidity_points: U256::from_bytes(bytes[64..96].try_into().unwrap()), + } + } +} + +impl ByteConversion<64> for BuyTokensTransactionState { + fn to_bytes(&self) -> [u8; 64] { + let mut buf = [0u8; 64]; + buf[0..8].copy_from_slice(&self.user_balance_base_token_amount.to_bytes()); + buf[8..16].copy_from_slice(&self.user_balance_quote_token_amount.to_bytes()); + buf[16..24].copy_from_slice(&self.pool_base_token_amount.to_bytes()); + buf[24..32].copy_from_slice(&self.pool_quote_token_amount.to_bytes()); + buf[32..64].copy_from_slice(&self.pool_total_liquidity_points.to_bytes()); + buf + } + + fn from_bytes(bytes: &[u8; 64]) -> Self { + BuyTokensTransactionState { + user_balance_base_token_amount: u64::from_bytes(bytes[0..8].try_into().unwrap()), + user_balance_quote_token_amount: u64::from_bytes(bytes[8..16].try_into().unwrap()), + pool_base_token_amount: u64::from_bytes(bytes[16..24].try_into().unwrap()), + pool_quote_token_amount: u64::from_bytes(bytes[24..32].try_into().unwrap()), + pool_total_liquidity_points: U256::from_bytes(bytes[32..64].try_into().unwrap()), + } + } +} + +impl ByteConversion<64> for SellTokensTransactionState { + fn to_bytes(&self) -> [u8; 64] { + let mut buf = [0u8; 64]; + buf[0..8].copy_from_slice(&self.user_balance_base_token_amount.to_bytes()); + buf[8..16].copy_from_slice(&self.user_balance_quote_token_amount.to_bytes()); + buf[16..24].copy_from_slice(&self.pool_base_token_amount.to_bytes()); + buf[24..32].copy_from_slice(&self.pool_quote_token_amount.to_bytes()); + buf[32..64].copy_from_slice(&self.pool_total_liquidity_points.to_bytes()); + buf + } + + fn from_bytes(bytes: &[u8; 64]) -> Self { + SellTokensTransactionState { + user_balance_base_token_amount: u64::from_bytes(bytes[0..8].try_into().unwrap()), + user_balance_quote_token_amount: u64::from_bytes(bytes[8..16].try_into().unwrap()), + pool_base_token_amount: u64::from_bytes(bytes[16..24].try_into().unwrap()), + pool_quote_token_amount: u64::from_bytes(bytes[24..32].try_into().unwrap()), + pool_total_liquidity_points: U256::from_bytes(bytes[32..64].try_into().unwrap()), + } + } +} diff --git a/data-structures/src/transaction.rs b/data-structures/src/transaction.rs index acb6a87..def42f0 100644 --- a/data-structures/src/transaction.rs +++ b/data-structures/src/transaction.rs @@ -89,109 +89,33 @@ impl ByteConversion<232> for Transaction { Transaction::CreateGenesis {} => { buf[0] = 0; } - Transaction::DepositTokens(DepositTokensTransaction { - user_address, - token_id, - token_amount, - }) => { + Transaction::DepositTokens(deposit_tokens_transaction) => { buf[0] = 1; - buf[1..56].copy_from_slice(&user_address.to_bytes()); - buf[56..88].copy_from_slice(&token_id.to_bytes()); - buf[88..96].copy_from_slice(&token_amount.to_bytes()); + buf[1..96].copy_from_slice(&deposit_tokens_transaction.to_bytes()); } - Transaction::BurnTokens(BurnTokensTransaction { - address, - signature, - token_id, - token_amount, - }) => { + Transaction::BurnTokens(burn_tokens_transaction) => { buf[0] = 2; - buf[1..56].copy_from_slice(&address.to_bytes()); - buf[56..120].copy_from_slice(&signature.to_bytes()); - buf[120..152].copy_from_slice(&token_id.to_bytes()); - buf[152..160].copy_from_slice(&token_amount.to_bytes()); + buf[1..160].copy_from_slice(&burn_tokens_transaction.to_bytes()); } - Transaction::CreatePool(CreatePoolTransaction { - address, - signature, - base_token_id, - quote_token_id, - base_token_amount, - quote_token_amount, - }) => { + Transaction::CreatePool(create_pool_transaction) => { buf[0] = 3; - buf[1..56].copy_from_slice(&address.to_bytes()); - buf[56..120].copy_from_slice(&signature.to_bytes()); - buf[120..152].copy_from_slice(&base_token_id.to_bytes()); - buf[152..184].copy_from_slice("e_token_id.to_bytes()); - buf[184..192].copy_from_slice(&base_token_amount.to_bytes()); - buf[192..200].copy_from_slice("e_token_amount.to_bytes()); + buf[1..200].copy_from_slice(&create_pool_transaction.to_bytes()); } - Transaction::ProvideLiquidity(ProvideLiquidityTransaction { - address, - signature, - base_token_id, - quote_token_id, - base_token_amount, - quote_token_amount_limit, - }) => { + Transaction::ProvideLiquidity(provide_liquidity_transaction) => { buf[0] = 4; - buf[1..56].copy_from_slice(&address.to_bytes()); - buf[56..120].copy_from_slice(&signature.to_bytes()); - buf[120..152].copy_from_slice(&base_token_id.to_bytes()); - buf[152..184].copy_from_slice("e_token_id.to_bytes()); - buf[184..192].copy_from_slice(&base_token_amount.to_bytes()); - buf[192..200].copy_from_slice("e_token_amount_limit.to_bytes()); + buf[1..200].copy_from_slice(&provide_liquidity_transaction.to_bytes()); } - Transaction::RemoveLiquidity(RemoveLiquidityTransaction { - address, - signature, - base_token_id, - quote_token_id, - base_token_amount_limit, - quote_token_amount_limit, - points: liquidity_point_amount, - }) => { + Transaction::RemoveLiquidity(remove_liquidity_transaction) => { buf[0] = 5; - buf[1..56].copy_from_slice(&address.to_bytes()); - buf[56..120].copy_from_slice(&signature.to_bytes()); - buf[120..152].copy_from_slice(&base_token_id.to_bytes()); - buf[152..184].copy_from_slice("e_token_id.to_bytes()); - buf[184..192].copy_from_slice(&base_token_amount_limit.to_bytes()); - buf[192..200].copy_from_slice("e_token_amount_limit.to_bytes()); - buf[200..232].copy_from_slice(&liquidity_point_amount.to_bytes()); + buf[1..232].copy_from_slice(&remove_liquidity_transaction.to_bytes()); } - Transaction::BuyTokens(BuyTokensTransaction { - address, - signature, - base_token_id, - quote_token_id, - base_token_amount, - quote_token_amount_limit, - }) => { + Transaction::BuyTokens(buy_tokens_transaction) => { buf[0] = 6; - buf[1..56].copy_from_slice(&address.to_bytes()); - buf[56..120].copy_from_slice(&signature.to_bytes()); - buf[120..152].copy_from_slice(&base_token_id.to_bytes()); - buf[152..184].copy_from_slice("e_token_id.to_bytes()); - buf[184..192].copy_from_slice(&base_token_amount.to_bytes()); - buf[192..200].copy_from_slice("e_token_amount_limit.to_bytes()); + buf[1..200].copy_from_slice(&buy_tokens_transaction.to_bytes()); } - Transaction::SellTokens(SellTokensTransaction { - address, - signature, - base_token_id, - quote_token_id, - base_token_amount_limit, - quote_token_amount, - }) => { + Transaction::SellTokens(sell_tokens_transaction) => { buf[0] = 7; - buf[1..56].copy_from_slice(&address.to_bytes()); - buf[56..120].copy_from_slice(&signature.to_bytes()); - buf[120..152].copy_from_slice(&base_token_id.to_bytes()); - buf[152..184].copy_from_slice("e_token_id.to_bytes()); - buf[184..192].copy_from_slice(&base_token_amount_limit.to_bytes()); - buf[192..200].copy_from_slice("e_token_amount.to_bytes()); + buf[1..200].copy_from_slice(&sell_tokens_transaction.to_bytes()); } } @@ -201,58 +125,187 @@ impl ByteConversion<232> for Transaction { fn from_bytes(bytes: &[u8; 232]) -> Self { match bytes[0] { 0 => Self::CreateGenesis(), - 1 => Self::DepositTokens(DepositTokensTransaction { - user_address: Address::from_bytes(bytes[1..56].try_into().unwrap()), - token_id: U256::from_bytes(bytes[56..88].try_into().unwrap()), - token_amount: u64::from_le_bytes(bytes[88..96].try_into().unwrap()), - }), - 2 => Self::BurnTokens(BurnTokensTransaction { - address: Address::from_bytes(bytes[1..56].try_into().unwrap()), - signature: Signature::from_bytes(bytes[56..120].try_into().unwrap()), - token_id: U256::from_bytes(bytes[120..152].try_into().unwrap()), - token_amount: u64::from_le_bytes(bytes[152..160].try_into().unwrap()), - }), - 3 => Self::CreatePool(CreatePoolTransaction { - address: Address::from_bytes(bytes[1..56].try_into().unwrap()), - signature: Signature::from_bytes(bytes[56..120].try_into().unwrap()), - base_token_id: U256::from_bytes(bytes[120..152].try_into().unwrap()), - quote_token_id: U256::from_bytes(bytes[152..184].try_into().unwrap()), - base_token_amount: u64::from_le_bytes(bytes[184..192].try_into().unwrap()), - quote_token_amount: u64::from_le_bytes(bytes[192..200].try_into().unwrap()), - }), - 4 => Self::ProvideLiquidity(ProvideLiquidityTransaction { - address: Address::from_bytes(bytes[1..56].try_into().unwrap()), - signature: Signature::from_bytes(bytes[56..120].try_into().unwrap()), - base_token_id: U256::from_bytes(bytes[120..152].try_into().unwrap()), - quote_token_id: U256::from_bytes(bytes[152..184].try_into().unwrap()), - base_token_amount: u64::from_le_bytes(bytes[184..192].try_into().unwrap()), - quote_token_amount_limit: u64::from_le_bytes(bytes[192..200].try_into().unwrap()), - }), - 5 => Self::RemoveLiquidity(RemoveLiquidityTransaction { - address: Address::from_bytes(bytes[1..56].try_into().unwrap()), - signature: Signature::from_bytes(bytes[56..120].try_into().unwrap()), - base_token_id: U256::from_bytes(bytes[120..152].try_into().unwrap()), - quote_token_id: U256::from_bytes(bytes[152..184].try_into().unwrap()), - base_token_amount_limit: u64::from_le_bytes(bytes[184..192].try_into().unwrap()), - quote_token_amount_limit: u64::from_le_bytes(bytes[192..200].try_into().unwrap()), - points: U256::from_bytes(bytes[200..232].try_into().unwrap()), - }), - 6 => Self::BuyTokens(BuyTokensTransaction { - address: Address::from_bytes(bytes[1..56].try_into().unwrap()), - signature: Signature::from_bytes(bytes[56..120].try_into().unwrap()), - base_token_id: U256::from_bytes(bytes[120..152].try_into().unwrap()), - quote_token_id: U256::from_bytes(bytes[152..184].try_into().unwrap()), - base_token_amount: u64::from_le_bytes(bytes[184..192].try_into().unwrap()), - quote_token_amount_limit: u64::from_le_bytes(bytes[192..200].try_into().unwrap()), - }), - _ => Self::SellTokens(SellTokensTransaction { - address: Address::from_bytes(bytes[1..56].try_into().unwrap()), - signature: Signature::from_bytes(bytes[56..120].try_into().unwrap()), - base_token_id: U256::from_bytes(bytes[120..152].try_into().unwrap()), - quote_token_id: U256::from_bytes(bytes[152..184].try_into().unwrap()), - base_token_amount_limit: u64::from_le_bytes(bytes[184..192].try_into().unwrap()), - quote_token_amount: u64::from_le_bytes(bytes[192..200].try_into().unwrap()), - }), + 1 => Self::DepositTokens(DepositTokensTransaction::from_bytes( + bytes[1..96].try_into().unwrap(), + )), + 2 => Self::BurnTokens(BurnTokensTransaction::from_bytes( + bytes[1..160].try_into().unwrap(), + )), + 3 => Self::CreatePool(CreatePoolTransaction::from_bytes( + bytes[1..200].try_into().unwrap(), + )), + 4 => Self::ProvideLiquidity(ProvideLiquidityTransaction::from_bytes( + bytes[1..200].try_into().unwrap(), + )), + 5 => Self::RemoveLiquidity(RemoveLiquidityTransaction::from_bytes( + bytes[1..232].try_into().unwrap(), + )), + 6 => Self::BuyTokens(BuyTokensTransaction::from_bytes( + bytes[1..200].try_into().unwrap(), + )), + _ => Self::SellTokens(SellTokensTransaction::from_bytes( + bytes[1..200].try_into().unwrap(), + )), + } + } +} + +impl ByteConversion<95> for DepositTokensTransaction { + fn to_bytes(&self) -> [u8; 95] { + let mut buf = [0u8; 95]; + buf[0..55].copy_from_slice(&self.user_address.to_bytes()); + buf[55..87].copy_from_slice(&self.token_id.to_bytes()); + buf[87..95].copy_from_slice(&self.token_amount.to_bytes()); + buf + } + + fn from_bytes(bytes: &[u8; 95]) -> Self { + DepositTokensTransaction { + user_address: Address::from_bytes(bytes[0..55].try_into().unwrap()), + token_id: U256::from_bytes(bytes[55..87].try_into().unwrap()), + token_amount: u64::from_bytes(bytes[87..95].try_into().unwrap()), + } + } +} + +impl ByteConversion<159> for BurnTokensTransaction { + fn to_bytes(&self) -> [u8; 159] { + let mut buf = [0u8; 159]; + buf[0..55].copy_from_slice(&self.address.to_bytes()); + buf[55..119].copy_from_slice(&self.signature.to_bytes()); + buf[119..151].copy_from_slice(&self.token_id.to_bytes()); + buf[151..159].copy_from_slice(&self.token_amount.to_bytes()); + buf + } + + fn from_bytes(bytes: &[u8; 159]) -> Self { + BurnTokensTransaction { + address: Address::from_bytes(bytes[0..55].try_into().unwrap()), + signature: Signature::from_bytes(bytes[55..119].try_into().unwrap()), + token_id: U256::from_bytes(bytes[119..151].try_into().unwrap()), + token_amount: u64::from_bytes(bytes[151..159].try_into().unwrap()), + } + } +} + +impl ByteConversion<199> for CreatePoolTransaction { + fn to_bytes(&self) -> [u8; 199] { + let mut buf = [0u8; 199]; + buf[0..55].copy_from_slice(&self.address.to_bytes()); + buf[55..119].copy_from_slice(&self.signature.to_bytes()); + buf[119..151].copy_from_slice(&self.base_token_id.to_bytes()); + buf[151..183].copy_from_slice(&self.quote_token_id.to_bytes()); + buf[183..191].copy_from_slice(&self.base_token_amount.to_bytes()); + buf[191..199].copy_from_slice(&self.quote_token_amount.to_bytes()); + buf + } + + fn from_bytes(bytes: &[u8; 199]) -> Self { + CreatePoolTransaction { + address: Address::from_bytes(bytes[0..55].try_into().unwrap()), + signature: Signature::from_bytes(bytes[55..119].try_into().unwrap()), + base_token_id: U256::from_bytes(bytes[119..151].try_into().unwrap()), + quote_token_id: U256::from_bytes(bytes[151..183].try_into().unwrap()), + base_token_amount: u64::from_bytes(bytes[183..191].try_into().unwrap()), + quote_token_amount: u64::from_bytes(bytes[191..199].try_into().unwrap()), + } + } +} + +impl ByteConversion<199> for ProvideLiquidityTransaction { + fn to_bytes(&self) -> [u8; 199] { + let mut buf = [0u8; 199]; + buf[0..55].copy_from_slice(&self.address.to_bytes()); + buf[55..119].copy_from_slice(&self.signature.to_bytes()); + buf[119..151].copy_from_slice(&self.base_token_id.to_bytes()); + buf[151..183].copy_from_slice(&self.quote_token_id.to_bytes()); + buf[183..191].copy_from_slice(&self.base_token_amount.to_bytes()); + buf[191..199].copy_from_slice(&self.quote_token_amount_limit.to_bytes()); + buf + } + + fn from_bytes(bytes: &[u8; 199]) -> Self { + ProvideLiquidityTransaction { + address: Address::from_bytes(bytes[0..55].try_into().unwrap()), + signature: Signature::from_bytes(bytes[55..119].try_into().unwrap()), + base_token_id: U256::from_bytes(bytes[119..151].try_into().unwrap()), + quote_token_id: U256::from_bytes(bytes[151..183].try_into().unwrap()), + base_token_amount: u64::from_bytes(bytes[183..191].try_into().unwrap()), + quote_token_amount_limit: u64::from_bytes(bytes[191..199].try_into().unwrap()), + } + } +} + +impl ByteConversion<231> for RemoveLiquidityTransaction { + fn to_bytes(&self) -> [u8; 231] { + let mut buf = [0u8; 231]; + buf[0..55].copy_from_slice(&self.address.to_bytes()); + buf[55..119].copy_from_slice(&self.signature.to_bytes()); + buf[119..151].copy_from_slice(&self.base_token_id.to_bytes()); + buf[151..183].copy_from_slice(&self.quote_token_id.to_bytes()); + buf[183..191].copy_from_slice(&self.base_token_amount_limit.to_bytes()); + buf[191..199].copy_from_slice(&self.quote_token_amount_limit.to_bytes()); + buf[199..231].copy_from_slice(&self.points.to_bytes()); + buf + } + + fn from_bytes(bytes: &[u8; 231]) -> Self { + RemoveLiquidityTransaction { + address: Address::from_bytes(bytes[0..55].try_into().unwrap()), + signature: Signature::from_bytes(bytes[55..119].try_into().unwrap()), + base_token_id: U256::from_bytes(bytes[119..151].try_into().unwrap()), + quote_token_id: U256::from_bytes(bytes[151..183].try_into().unwrap()), + base_token_amount_limit: u64::from_bytes(bytes[183..191].try_into().unwrap()), + quote_token_amount_limit: u64::from_bytes(bytes[191..199].try_into().unwrap()), + points: U256::from_bytes(bytes[199..131].try_into().unwrap()), + } + } +} + +impl ByteConversion<199> for BuyTokensTransaction { + fn to_bytes(&self) -> [u8; 199] { + let mut buf = [0u8; 199]; + buf[1..55].copy_from_slice(&self.address.to_bytes()); + buf[55..119].copy_from_slice(&self.signature.to_bytes()); + buf[119..151].copy_from_slice(&self.base_token_id.to_bytes()); + buf[151..183].copy_from_slice(&self.quote_token_id.to_bytes()); + buf[183..191].copy_from_slice(&self.base_token_amount.to_bytes()); + buf[191..199].copy_from_slice(&self.quote_token_amount_limit.to_bytes()); + buf + } + + fn from_bytes(bytes: &[u8; 199]) -> Self { + BuyTokensTransaction { + address: Address::from_bytes(bytes[0..55].try_into().unwrap()), + signature: Signature::from_bytes(bytes[55..119].try_into().unwrap()), + base_token_id: U256::from_bytes(bytes[119..151].try_into().unwrap()), + quote_token_id: U256::from_bytes(bytes[151..183].try_into().unwrap()), + base_token_amount: u64::from_bytes(bytes[183..191].try_into().unwrap()), + quote_token_amount_limit: u64::from_bytes(bytes[191..199].try_into().unwrap()), + } + } +} + +impl ByteConversion<199> for SellTokensTransaction { + fn to_bytes(&self) -> [u8; 199] { + let mut buf = [0u8; 199]; + buf[0..55].copy_from_slice(&self.address.to_bytes()); + buf[55..119].copy_from_slice(&self.signature.to_bytes()); + buf[119..151].copy_from_slice(&self.base_token_id.to_bytes()); + buf[151..193].copy_from_slice(&self.quote_token_id.to_bytes()); + buf[183..191].copy_from_slice(&self.base_token_amount_limit.to_bytes()); + buf[191..199].copy_from_slice(&self.quote_token_amount.to_bytes()); + buf + } + + fn from_bytes(bytes: &[u8; 199]) -> Self { + SellTokensTransaction { + address: Address::from_bytes(bytes[0..55].try_into().unwrap()), + signature: Signature::from_bytes(bytes[55..199].try_into().unwrap()), + base_token_id: U256::from_bytes(bytes[119..151].try_into().unwrap()), + quote_token_id: U256::from_bytes(bytes[151..183].try_into().unwrap()), + base_token_amount_limit: u64::from_bytes(bytes[183..191].try_into().unwrap()), + quote_token_amount: u64::from_bytes(bytes[191..199].try_into().unwrap()), } } } diff --git a/generator-script/package.json b/generator-script/package.json deleted file mode 100644 index d208d24..0000000 --- a/generator-script/package.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "name": "nacho-generator-script", - "version": "0.1.0", - "private": true, - "type": "module", - "files": [ - "build" - ], - "scripts": { - "build": "rm -rf build/ && npx esbuild --bundle src/index.ts --outfile=build/index.mjs --platform=node --format=esm --packages=external" - }, - "dependencies": { - "nacho-common-o1js": "^0.1.8", - "nacho-proof-generator": "^0.1.5", - "o1js": "^0.17.0" - }, - "devDependencies": { - "@types/node": "^20.11.24", - "esbuild": "^0.20.2", - "typescript": "^5.3.3" - } -} \ No newline at end of file diff --git a/merger-script/package-lock.json b/merger-script/package-lock.json deleted file mode 100644 index 22f774b..0000000 --- a/merger-script/package-lock.json +++ /dev/null @@ -1,572 +0,0 @@ -{ - "name": "nacho-merger-script", - "version": "0.1.0", - "lockfileVersion": 3, - "requires": true, - "packages": { - "": { - "name": "nacho-merger-script", - "version": "0.1.0", - "dependencies": { - "nacho-common-o1js": "^0.1.7", - "nacho-proof-generator": "^0.1.5", - "o1js": "^0.17.0" - }, - "devDependencies": { - "@types/node": "^20.11.24", - "esbuild": "^0.20.2", - "typescript": "^5.3.3" - } - }, - "node_modules/@esbuild/aix-ppc64": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.20.2.tgz", - "integrity": "sha512-D+EBOJHXdNZcLJRBkhENNG8Wji2kgc9AZ9KiPr1JuZjsNtyHzrsfLRrY0tk2H2aoFu6RANO1y1iPPUCDYWkb5g==", - "cpu": [ - "ppc64" - ], - "dev": true, - "optional": true, - "os": [ - "aix" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/android-arm": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.20.2.tgz", - "integrity": "sha512-t98Ra6pw2VaDhqNWO2Oph2LXbz/EJcnLmKLGBJwEwXX/JAN83Fym1rU8l0JUWK6HkIbWONCSSatf4sf2NBRx/w==", - "cpu": [ - "arm" - ], - "dev": true, - "optional": true, - "os": [ - "android" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/android-arm64": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.20.2.tgz", - "integrity": "sha512-mRzjLacRtl/tWU0SvD8lUEwb61yP9cqQo6noDZP/O8VkwafSYwZ4yWy24kan8jE/IMERpYncRt2dw438LP3Xmg==", - "cpu": [ - "arm64" - ], - "dev": true, - "optional": true, - "os": [ - "android" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/android-x64": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.20.2.tgz", - "integrity": "sha512-btzExgV+/lMGDDa194CcUQm53ncxzeBrWJcncOBxuC6ndBkKxnHdFJn86mCIgTELsooUmwUm9FkhSp5HYu00Rg==", - "cpu": [ - "x64" - ], - "dev": true, - "optional": true, - "os": [ - "android" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/darwin-arm64": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.20.2.tgz", - "integrity": "sha512-4J6IRT+10J3aJH3l1yzEg9y3wkTDgDk7TSDFX+wKFiWjqWp/iCfLIYzGyasx9l0SAFPT1HwSCR+0w/h1ES/MjA==", - "cpu": [ - "arm64" - ], - "dev": true, - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/darwin-x64": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.20.2.tgz", - "integrity": "sha512-tBcXp9KNphnNH0dfhv8KYkZhjc+H3XBkF5DKtswJblV7KlT9EI2+jeA8DgBjp908WEuYll6pF+UStUCfEpdysA==", - "cpu": [ - "x64" - ], - "dev": true, - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/freebsd-arm64": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.20.2.tgz", - "integrity": "sha512-d3qI41G4SuLiCGCFGUrKsSeTXyWG6yem1KcGZVS+3FYlYhtNoNgYrWcvkOoaqMhwXSMrZRl69ArHsGJ9mYdbbw==", - "cpu": [ - "arm64" - ], - "dev": true, - "optional": true, - "os": [ - "freebsd" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/freebsd-x64": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.20.2.tgz", - "integrity": "sha512-d+DipyvHRuqEeM5zDivKV1KuXn9WeRX6vqSqIDgwIfPQtwMP4jaDsQsDncjTDDsExT4lR/91OLjRo8bmC1e+Cw==", - "cpu": [ - "x64" - ], - "dev": true, - "optional": true, - "os": [ - "freebsd" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/linux-arm": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.20.2.tgz", - "integrity": "sha512-VhLPeR8HTMPccbuWWcEUD1Az68TqaTYyj6nfE4QByZIQEQVWBB8vup8PpR7y1QHL3CpcF6xd5WVBU/+SBEvGTg==", - "cpu": [ - "arm" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/linux-arm64": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.20.2.tgz", - "integrity": "sha512-9pb6rBjGvTFNira2FLIWqDk/uaf42sSyLE8j1rnUpuzsODBq7FvpwHYZxQ/It/8b+QOS1RYfqgGFNLRI+qlq2A==", - "cpu": [ - "arm64" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/linux-ia32": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.20.2.tgz", - "integrity": "sha512-o10utieEkNPFDZFQm9CoP7Tvb33UutoJqg3qKf1PWVeeJhJw0Q347PxMvBgVVFgouYLGIhFYG0UGdBumROyiig==", - "cpu": [ - "ia32" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/linux-loong64": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.20.2.tgz", - "integrity": "sha512-PR7sp6R/UC4CFVomVINKJ80pMFlfDfMQMYynX7t1tNTeivQ6XdX5r2XovMmha/VjR1YN/HgHWsVcTRIMkymrgQ==", - "cpu": [ - "loong64" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/linux-mips64el": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.20.2.tgz", - "integrity": "sha512-4BlTqeutE/KnOiTG5Y6Sb/Hw6hsBOZapOVF6njAESHInhlQAghVVZL1ZpIctBOoTFbQyGW+LsVYZ8lSSB3wkjA==", - "cpu": [ - "mips64el" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/linux-ppc64": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.20.2.tgz", - "integrity": "sha512-rD3KsaDprDcfajSKdn25ooz5J5/fWBylaaXkuotBDGnMnDP1Uv5DLAN/45qfnf3JDYyJv/ytGHQaziHUdyzaAg==", - "cpu": [ - "ppc64" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/linux-riscv64": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.20.2.tgz", - "integrity": "sha512-snwmBKacKmwTMmhLlz/3aH1Q9T8v45bKYGE3j26TsaOVtjIag4wLfWSiZykXzXuE1kbCE+zJRmwp+ZbIHinnVg==", - "cpu": [ - "riscv64" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/linux-s390x": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.20.2.tgz", - "integrity": "sha512-wcWISOobRWNm3cezm5HOZcYz1sKoHLd8VL1dl309DiixxVFoFe/o8HnwuIwn6sXre88Nwj+VwZUvJf4AFxkyrQ==", - "cpu": [ - "s390x" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/linux-x64": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.20.2.tgz", - "integrity": "sha512-1MdwI6OOTsfQfek8sLwgyjOXAu+wKhLEoaOLTjbijk6E2WONYpH9ZU2mNtR+lZ2B4uwr+usqGuVfFT9tMtGvGw==", - "cpu": [ - "x64" - ], - "dev": true, - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/netbsd-x64": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.20.2.tgz", - "integrity": "sha512-K8/DhBxcVQkzYc43yJXDSyjlFeHQJBiowJ0uVL6Tor3jGQfSGHNNJcWxNbOI8v5k82prYqzPuwkzHt3J1T1iZQ==", - "cpu": [ - "x64" - ], - "dev": true, - "optional": true, - "os": [ - "netbsd" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/openbsd-x64": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.20.2.tgz", - "integrity": "sha512-eMpKlV0SThJmmJgiVyN9jTPJ2VBPquf6Kt/nAoo6DgHAoN57K15ZghiHaMvqjCye/uU4X5u3YSMgVBI1h3vKrQ==", - "cpu": [ - "x64" - ], - "dev": true, - "optional": true, - "os": [ - "openbsd" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/sunos-x64": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.20.2.tgz", - "integrity": "sha512-2UyFtRC6cXLyejf/YEld4Hajo7UHILetzE1vsRcGL3earZEW77JxrFjH4Ez2qaTiEfMgAXxfAZCm1fvM/G/o8w==", - "cpu": [ - "x64" - ], - "dev": true, - "optional": true, - "os": [ - "sunos" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/win32-arm64": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.20.2.tgz", - "integrity": "sha512-GRibxoawM9ZCnDxnP3usoUDO9vUkpAxIIZ6GQI+IlVmr5kP3zUq+l17xELTHMWTWzjxa2guPNyrpq1GWmPvcGQ==", - "cpu": [ - "arm64" - ], - "dev": true, - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/win32-ia32": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.20.2.tgz", - "integrity": "sha512-HfLOfn9YWmkSKRQqovpnITazdtquEW8/SoHW7pWpuEeguaZI4QnCRW6b+oZTztdBnZOS2hqJ6im/D5cPzBTTlQ==", - "cpu": [ - "ia32" - ], - "dev": true, - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@esbuild/win32-x64": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.20.2.tgz", - "integrity": "sha512-N49X4lJX27+l9jbLKSqZ6bKNjzQvHaT8IIFUy+YIqmXQdjYCToGWwOItDrfby14c78aDd5NHQl29xingXfCdLQ==", - "cpu": [ - "x64" - ], - "dev": true, - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": ">=12" - } - }, - "node_modules/@types/node": { - "version": "20.12.2", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.2.tgz", - "integrity": "sha512-zQ0NYO87hyN6Xrclcqp7f8ZbXNbRfoGWNcMvHTPQp9UUrwI0mI7XBz+cu7/W6/VClYo2g63B0cjull/srU7LgQ==", - "dev": true, - "dependencies": { - "undici-types": "~5.26.4" - } - }, - "node_modules/blakejs": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/blakejs/-/blakejs-1.2.1.tgz", - "integrity": "sha512-QXUSXI3QVc/gJME0dBpXrag1kbzOqCjCX8/b54ntNyW6sjtoqxqRk3LTmXzaJoh71zMsDCjM+47jS7XiwN/+fQ==" - }, - "node_modules/cachedir": { - "version": "2.4.0", - "resolved": "https://registry.npmjs.org/cachedir/-/cachedir-2.4.0.tgz", - "integrity": "sha512-9EtFOZR8g22CL7BWjJ9BUx1+A/djkofnyW3aOXZORNW2kxoUpx2h+uN2cOqwPmFhnpVmxg+KW2OjOSgChTEvsQ==", - "engines": { - "node": ">=6" - } - }, - "node_modules/esbuild": { - "version": "0.20.2", - "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.20.2.tgz", - "integrity": "sha512-WdOOppmUNU+IbZ0PaDiTst80zjnrOkyJNHoKupIcVyU8Lvla3Ugx94VzkQ32Ijqd7UhHJy75gNWDMUekcrSJ6g==", - "dev": true, - "hasInstallScript": true, - "bin": { - "esbuild": "bin/esbuild" - }, - "engines": { - "node": ">=12" - }, - "optionalDependencies": { - "@esbuild/aix-ppc64": "0.20.2", - "@esbuild/android-arm": "0.20.2", - "@esbuild/android-arm64": "0.20.2", - "@esbuild/android-x64": "0.20.2", - "@esbuild/darwin-arm64": "0.20.2", - "@esbuild/darwin-x64": "0.20.2", - "@esbuild/freebsd-arm64": "0.20.2", - "@esbuild/freebsd-x64": "0.20.2", - "@esbuild/linux-arm": "0.20.2", - "@esbuild/linux-arm64": "0.20.2", - "@esbuild/linux-ia32": "0.20.2", - "@esbuild/linux-loong64": "0.20.2", - "@esbuild/linux-mips64el": "0.20.2", - "@esbuild/linux-ppc64": "0.20.2", - "@esbuild/linux-riscv64": "0.20.2", - "@esbuild/linux-s390x": "0.20.2", - "@esbuild/linux-x64": "0.20.2", - "@esbuild/netbsd-x64": "0.20.2", - "@esbuild/openbsd-x64": "0.20.2", - "@esbuild/sunos-x64": "0.20.2", - "@esbuild/win32-arm64": "0.20.2", - "@esbuild/win32-ia32": "0.20.2", - "@esbuild/win32-x64": "0.20.2" - } - }, - "node_modules/isomorphic-fetch": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/isomorphic-fetch/-/isomorphic-fetch-3.0.0.tgz", - "integrity": "sha512-qvUtwJ3j6qwsF3jLxkZ72qCgjMysPzDfeV240JHiGZsANBYd+EEuu35v7dfrJ9Up0Ak07D7GGSkGhCHTqg/5wA==", - "dependencies": { - "node-fetch": "^2.6.1", - "whatwg-fetch": "^3.4.1" - } - }, - "node_modules/js-sha256": { - "version": "0.9.0", - "resolved": "https://registry.npmjs.org/js-sha256/-/js-sha256-0.9.0.tgz", - "integrity": "sha512-sga3MHh9sgQN2+pJ9VYZ+1LPwXOxuBJBA5nrR5/ofPfuiJBE2hnjsaN8se8JznOmGLN2p49Pe5U/ttafcs/apA==" - }, - "node_modules/nacho-common-o1js": { - "version": "0.1.7", - "resolved": "https://registry.npmjs.org/nacho-common-o1js/-/nacho-common-o1js-0.1.7.tgz", - "integrity": "sha512-3SVyx+CdmzzuYqW6eKtLcHs9Qvt4Q0+J/9sL7AIP9jr6chhVrtyN6LDVh5LHERSYsgIr+QeaZT1m50JMFs6/6g==", - "dependencies": { - "o1js": "^0.17.0" - } - }, - "node_modules/nacho-proof-generator": { - "version": "0.1.5", - "resolved": "https://registry.npmjs.org/nacho-proof-generator/-/nacho-proof-generator-0.1.5.tgz", - "integrity": "sha512-YfnX7RIr27qjdMMziPzM87IUBJyQXka7IZ9nfo6HNmNj9PM4Gy8UmhWA6R9juTdXdQjtTz31mZ174xNKSoDomQ==", - "dependencies": { - "nacho-common-o1js": "^0.1.7", - "o1js": "^0.17.0" - } - }, - "node_modules/node-fetch": { - "version": "2.7.0", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", - "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", - "dependencies": { - "whatwg-url": "^5.0.0" - }, - "engines": { - "node": "4.x || >=6.0.0" - }, - "peerDependencies": { - "encoding": "^0.1.0" - }, - "peerDependenciesMeta": { - "encoding": { - "optional": true - } - } - }, - "node_modules/o1js": { - "version": "0.17.0", - "resolved": "https://registry.npmjs.org/o1js/-/o1js-0.17.0.tgz", - "integrity": "sha512-OPQc3jBG2X5z7yI2mcovzRVbd91Q2D/npep14+g0wBdGXpyyAFdaz59YjVlEsPcY1Gmh2L+LCFhVA2fQ+1ogIg==", - "dependencies": { - "blakejs": "1.2.1", - "cachedir": "^2.4.0", - "isomorphic-fetch": "^3.0.0", - "js-sha256": "^0.9.0", - "reflect-metadata": "^0.1.13", - "tslib": "^2.3.0" - }, - "bin": { - "snarky-run": "src/build/run.js" - }, - "engines": { - "node": ">=16.4.0" - } - }, - "node_modules/reflect-metadata": { - "version": "0.1.14", - "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.14.tgz", - "integrity": "sha512-ZhYeb6nRaXCfhnndflDK8qI6ZQ/YcWZCISRAWICW9XYqMUwjZM9Z0DveWX/ABN01oxSHwVxKQmxeYZSsm0jh5A==" - }, - "node_modules/tr46": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" - }, - "node_modules/tslib": { - "version": "2.6.2", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz", - "integrity": "sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q==" - }, - "node_modules/typescript": { - "version": "5.4.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.3.tgz", - "integrity": "sha512-KrPd3PKaCLr78MalgiwJnA25Nm8HAmdwN3mYUYZgG/wizIo9EainNVQI9/yDavtVFRN2h3k8uf3GLHuhDMgEHg==", - "dev": true, - "bin": { - "tsc": "bin/tsc", - "tsserver": "bin/tsserver" - }, - "engines": { - "node": ">=14.17" - } - }, - "node_modules/undici-types": { - "version": "5.26.5", - "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", - "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", - "dev": true - }, - "node_modules/webidl-conversions": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", - "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" - }, - "node_modules/whatwg-fetch": { - "version": "3.6.20", - "resolved": "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.6.20.tgz", - "integrity": "sha512-EqhiFU6daOA8kpjOWTL0olhVOF3i7OrFzSYiGsEMB8GcXS+RrzauAERX65xMeNWVqxA6HXH2m69Z9LaKKdisfg==" - }, - "node_modules/whatwg-url": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", - "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", - "dependencies": { - "tr46": "~0.0.3", - "webidl-conversions": "^3.0.0" - } - } - } -} diff --git a/merger-script/src/index.ts b/merger-script/src/index.ts deleted file mode 100644 index 5868777..0000000 --- a/merger-script/src/index.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { stdin, stdout } from "node:process" -import { parseInput } from "./parse.js" -import { mergeProofs } from "./prove.js" -import { saveMergedProofToDisk } from "./utils.js" -import { proofGenerator } from "nacho-proof-generator" - -const main = async () => { - const proofDbPath = process.argv.at(2) - - if (proofDbPath === undefined) { - process.exit(1) - } - - await proofGenerator.compile() - stdout.write(new Uint8Array(new ArrayBuffer(1))) - - stdin.on("data", async (chunk) => { - try { - const input = parseInput(chunk) - - const proof = - input.kind === "MergeProofs" ? await mergeProofs(input, proofDbPath) : null - - if (input.kind === "MergeProofs") { - await saveMergedProofToDisk(proofDbPath, proof!) - } - - const buffer = new ArrayBuffer(1) - const array = new Uint8Array(buffer) - - const isSuccess = proof === null ? 0 : 1 - array[0] = isSuccess - - stdout.write(array) - } catch (error) { - const buffer = new ArrayBuffer(1) - const array = new Uint8Array(buffer) - - const isSuccess = 0 // false - array[0] = isSuccess - - stdout.write(array) - } - }) -} - -main() diff --git a/merger-script/src/input.ts b/merger-script/src/input.ts deleted file mode 100644 index e10acbb..0000000 --- a/merger-script/src/input.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { StateRoots } from "./types.js" - -export type Input = MergeProofs | MistakenInput - -export type MergeProofs = { - kind: "MergeProofs" - state_roots: StateRoots - first_proof_index: bigint - second_proof_index: bigint -} - -export type MistakenInput = { - kind: "MistakenInput" -} diff --git a/merger-script/src/parse.ts b/merger-script/src/parse.ts deleted file mode 100644 index abe77f3..0000000 --- a/merger-script/src/parse.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { Input, MergeProofs, MistakenInput } from "./input.js" -import { StateRoots } from "./types.js" - -export const parseInput = (buffer: Buffer): Input => { - const array = new Uint8Array(buffer) - - if (array.length !== 145) { - return { - kind: "MistakenInput", - } - } - - switch (array[0]) { - case 0: { - return { - kind: "MergeProofs", - state_roots: parseStateRoots(buffer.subarray(1, 129)), - first_proof_index: parseUint64(buffer.subarray(129, 137)), - second_proof_index: parseUint64(buffer.subarray(137, 145)), - } satisfies MergeProofs - } - default: { - return { - kind: "MistakenInput", - } satisfies MistakenInput - } - } -} - -const parseUint64 = (array: Uint8Array) => { - let result = 0n - for (let i = 0; i < 8; i++) { - result |= BigInt(array[i]) << BigInt(i * 8) - } - - return result -} - -const parseUint256 = (array: Uint8Array) => { - let result = 0n - for (let i = 0; i < 32; i++) { - result |= BigInt(array[i]) << BigInt(i * 8) - } - - return result -} - -const parseStateRoots = (array: Uint8Array) => { - const stateRoots = { - balances: parseUint256(array.subarray(0, 32)), - liquidities: parseUint256(array.subarray(32, 64)), - pools: parseUint256(array.subarray(64, 96)), - burns: parseUint256(array.subarray(96, 128)), - } satisfies StateRoots - - return stateRoots -} diff --git a/merger-script/src/prove.ts b/merger-script/src/prove.ts deleted file mode 100644 index fe54dd7..0000000 --- a/merger-script/src/prove.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { StateRoots } from "nacho-common-o1js" -import { proofGenerator } from "nacho-proof-generator" -import { Field } from "o1js" -import { MergeProofs } from "./input.js" -import { readProofFromDisk } from "./utils.js" - -export const mergeProofs = async (params: MergeProofs, proofDbPath: string) => { - const proof = await proofGenerator.mergeProofs( - new StateRoots({ - balances: Field(params.state_roots.balances), - liquidities: Field(params.state_roots.liquidities), - pools: Field(params.state_roots.pools), - burns: Field(params.state_roots.burns), - }), - await readProofFromDisk(proofDbPath, params.first_proof_index), - await readProofFromDisk(proofDbPath, params.second_proof_index), - ) - - return proof -} diff --git a/merger-script/src/types.ts b/merger-script/src/types.ts deleted file mode 100644 index 9b11760..0000000 --- a/merger-script/src/types.ts +++ /dev/null @@ -1,6 +0,0 @@ -export type StateRoots = { - balances: bigint - liquidities: bigint - pools: bigint - burns: bigint -} diff --git a/merger-script/src/utils.ts b/merger-script/src/utils.ts deleted file mode 100644 index 4dde06b..0000000 --- a/merger-script/src/utils.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { ZkProof } from "nacho-proof-generator" -import { readFile, writeFile } from "node:fs/promises" - -export const readProofFromDisk = async (proofDbPath: string, index: bigint): Promise => { - const filePath = `${proofDbPath}/${index}` - const file = await readFile(filePath, { encoding: "ascii" }) - const jsonProof = ZkProof.fromJSON(JSON.parse(file)) - return jsonProof -} - -export const saveMergedProofToDisk = async (proofDbPath: string, proof: ZkProof): Promise => { - const filePath = `${proofDbPath}/merged` - await writeFile(filePath, JSON.stringify(proof.toJSON()), { encoding: "ascii" }) -} diff --git a/merger-script/tsconfig.json b/merger-script/tsconfig.json deleted file mode 100644 index 891ea65..0000000 --- a/merger-script/tsconfig.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "compilerOptions": { - "target": "ESNext", - "experimentalDecorators": true, - "emitDecoratorMetadata": true, - "module": "ESNext", - "moduleResolution": "Node", - "declaration": true, - "esModuleInterop": true, - "forceConsistentCasingInFileNames": true, - "strict": true, - "skipLibCheck": true, - "useDefineForClassFields": false, - "outDir": "build" - }, - "include": ["src"] -} diff --git a/processes/src/executor/process.rs b/processes/src/executor/process.rs index a5b7662..d7fcc45 100644 --- a/processes/src/executor/process.rs +++ b/processes/src/executor/process.rs @@ -1,6 +1,12 @@ use super::{Processor, Request}; use crate::{authenticator, balances, burns, liquidities, mempool, pools, proofpool}; -use nacho_data_structures::Transaction; +use nacho_data_structures::{ + BurnTokensTransaction, BurnTokensTransactionState, BuyTokensTransaction, + BuyTokensTransactionState, CreatePoolTransaction, CreatePoolTransactionState, + DepositTokensTransaction, DepositTokensTransactionState, ProvideLiquidityTransaction, + ProvideLiquidityTransactionState, RemoveLiquidityTransaction, RemoveLiquidityTransactionState, + SellTokensTransaction, SellTokensTransactionState, StatefulTransaction, Transaction, +}; use tokio::sync::mpsc; pub fn process( @@ -58,20 +64,48 @@ pub async fn execute_tx( } match tx.clone() { - Transaction::BurnTokens { + Transaction::CreateGenesis {} => {} + Transaction::DepositTokens(DepositTokensTransaction { + user_address, + token_id, + token_amount, + }) => { + let mut maybe_balance = balances + .get_balance(user_address.clone(), token_id.clone()) + .await; + + let amount_to_deposit = token_amount; + + let result = nacho_executor::deposit_tokens( + maybe_balance.as_mut(), + amount_to_deposit, + token_id, + user_address, + ) + .map_err(|_| ())?; + + match (result, maybe_balance) { + (Some(balance), None) => { + balances.push_balance(balance).await.ok_or(())?; + } + (None, Some(balance)) => { + balances.update_balance(balance).await.ok_or(())?; + } + _ => return Err(()), + } + } + Transaction::BurnTokens(BurnTokensTransaction { address, signature: _, token_id, token_amount, - } => { + }) => { let mut balance = balances .get_balance(address.clone(), token_id.clone()) .await .ok_or(())?; - let mut maybe_burn = burns - .get_burn_token_amount(address.clone(), token_id.clone()) - .await; + let mut maybe_burn = burns.get_burn(address.clone(), token_id.clone()).await; let amount_to_burn = token_amount; @@ -91,14 +125,14 @@ pub async fn execute_tx( _ => return Err(()), } } - Transaction::CreatePool { + Transaction::CreatePool(CreatePoolTransaction { address, signature: _, base_token_id, quote_token_id, base_token_amount, quote_token_amount, - } => { + }) => { let is_pool_exists = pools .get_pool(base_token_id.clone(), quote_token_id.clone()) .await @@ -139,14 +173,14 @@ pub async fn execute_tx( liquidities.push_liquidity(liquidity).await.ok_or(())?; pools.push_pool(pool).await.ok_or(())?; } - Transaction::ProvideLiquidity { + Transaction::ProvideLiquidity(ProvideLiquidityTransaction { address, signature: _, base_token_id, quote_token_id, base_token_amount, quote_token_amount_limit, - } => { + }) => { let mut base_token_balance = balances .get_balance(address.clone(), base_token_id.clone()) .await @@ -202,7 +236,7 @@ pub async fn execute_tx( _ => return Err(()), } } - Transaction::RemoveLiquidity { + Transaction::RemoveLiquidity(RemoveLiquidityTransaction { address, signature: _, base_token_id, @@ -210,7 +244,7 @@ pub async fn execute_tx( base_token_amount_limit, quote_token_amount_limit, points, - } => { + }) => { let mut base_token_balance = balances .get_balance(address.clone(), base_token_id.clone()) .await @@ -260,14 +294,14 @@ pub async fn execute_tx( liquidities.update_liquidity(liquidity).await.ok_or(())?; } - Transaction::BuyTokens { + Transaction::BuyTokens(BuyTokensTransaction { address, signature: _, base_token_id, quote_token_id, base_token_amount, quote_token_amount_limit, - } => { + }) => { let mut maybe_base_token_balance = balances .get_balance(address.clone(), base_token_id.clone()) .await; @@ -312,14 +346,14 @@ pub async fn execute_tx( _ => return Err(()), } } - Transaction::SellTokens { + Transaction::SellTokens(SellTokensTransaction { address, signature: _, base_token_id, quote_token_id, base_token_amount_limit, quote_token_amount, - } => { + }) => { let mut base_token_balance = balances .get_balance(address.clone(), base_token_id.clone()) .await @@ -366,7 +400,248 @@ pub async fn execute_tx( } } - proofpool.push(tx).await.ok_or(())?; + proofpool + .push(match tx { + Transaction::CreateGenesis() => StatefulTransaction::CreateGenesis { + transaction: (), + state: (), + }, + Transaction::DepositTokens(deposit_token_tx) => { + let maybe_balance = balances + .get_balance( + deposit_token_tx.user_address.clone(), + deposit_token_tx.token_id.clone(), + ) + .await; + + StatefulTransaction::DepositTokens { + transaction: deposit_token_tx, + state: DepositTokensTransactionState { + user_token_balance: maybe_balance + .map(|balance| balance.token_amount) + .unwrap_or(0), + }, + } + } + Transaction::BurnTokens(burn_tokens_tx) => { + let balance = balances + .get_balance( + burn_tokens_tx.address.clone(), + burn_tokens_tx.token_id.clone(), + ) + .await + .ok_or(())?; + + let maybe_burn = burns + .get_burn( + burn_tokens_tx.address.clone(), + burn_tokens_tx.token_id.clone(), + ) + .await; + + StatefulTransaction::BurnTokens { + transaction: burn_tokens_tx, + state: BurnTokensTransactionState { + user_balance_token_amount: balance.token_amount, + user_burn_token_amount: maybe_burn + .map(|burn| burn.token_amount) + .unwrap_or(0), + }, + } + } + Transaction::CreatePool(create_pool_tx) => { + let base_token_balance = balances + .get_balance( + create_pool_tx.address.clone(), + create_pool_tx.base_token_id.clone(), + ) + .await + .ok_or(())?; + + let quote_token_balance = balances + .get_balance( + create_pool_tx.address.clone(), + create_pool_tx.quote_token_id.clone(), + ) + .await + .ok_or(())?; + + StatefulTransaction::CreatePool { + transaction: create_pool_tx, + state: CreatePoolTransactionState { + user_balance_base_token_amount: base_token_balance.token_amount, + user_balance_quote_token_amount: quote_token_balance.token_amount, + }, + } + } + Transaction::ProvideLiquidity(provide_liquidity_tx) => { + let maybe_liquidity = liquidities + .get_liquidity( + provide_liquidity_tx.address.clone(), + provide_liquidity_tx.base_token_id.clone(), + provide_liquidity_tx.quote_token_id.clone(), + ) + .await; + + let base_token_balance = balances + .get_balance( + provide_liquidity_tx.address.clone(), + provide_liquidity_tx.base_token_id.clone(), + ) + .await + .ok_or(())?; + + let quote_token_balance = balances + .get_balance( + provide_liquidity_tx.address.clone(), + provide_liquidity_tx.base_token_id.clone(), + ) + .await + .ok_or(())?; + + let pool = pools + .get_pool( + provide_liquidity_tx.base_token_id.clone(), + provide_liquidity_tx.quote_token_id.clone(), + ) + .await + .ok_or(())?; + + StatefulTransaction::ProvideLiquidity { + transaction: provide_liquidity_tx, + state: ProvideLiquidityTransactionState { + user_liquidity_points: maybe_liquidity + .map(|liquidity| liquidity.points) + .unwrap_or(0u64.into()), + user_balance_base_token_amount: base_token_balance.token_amount, + user_balance_quote_token_amount: quote_token_balance.token_amount, + pool_base_token_amount: pool.base_token_amount, + pool_quote_token_amount: pool.quote_token_amount, + pool_total_liquidity_points: pool.total_liqudity_points, + }, + } + } + Transaction::RemoveLiquidity(remove_liquidity_tx) => { + let liquidity = liquidities + .get_liquidity( + remove_liquidity_tx.address.clone(), + remove_liquidity_tx.base_token_id.clone(), + remove_liquidity_tx.quote_token_id.clone(), + ) + .await + .ok_or(())?; + + let base_token_balance = balances + .get_balance( + remove_liquidity_tx.address.clone(), + remove_liquidity_tx.base_token_id.clone(), + ) + .await + .ok_or(())?; + + let quote_token_balance = balances + .get_balance( + remove_liquidity_tx.address.clone(), + remove_liquidity_tx.base_token_id.clone(), + ) + .await + .ok_or(())?; + + let pool = pools + .get_pool( + remove_liquidity_tx.base_token_id.clone(), + remove_liquidity_tx.quote_token_id.clone(), + ) + .await + .ok_or(())?; + + StatefulTransaction::RemoveLiquidity { + transaction: remove_liquidity_tx, + state: RemoveLiquidityTransactionState { + user_liquidity_points: liquidity.points, + user_balance_base_token_amount: base_token_balance.token_amount, + user_balance_quote_token_amount: quote_token_balance.token_amount, + pool_base_token_amount: pool.base_token_amount, + pool_quote_token_amount: pool.quote_token_amount, + pool_total_liquidity_points: pool.total_liqudity_points, + }, + } + } + Transaction::BuyTokens(buy_tokens_tx) => { + let base_token_balance = balances + .get_balance( + buy_tokens_tx.address.clone(), + buy_tokens_tx.base_token_id.clone(), + ) + .await + .ok_or(())?; + + let quote_token_balance = balances + .get_balance( + buy_tokens_tx.address.clone(), + buy_tokens_tx.base_token_id.clone(), + ) + .await + .ok_or(())?; + + let pool = pools + .get_pool( + buy_tokens_tx.base_token_id.clone(), + buy_tokens_tx.quote_token_id.clone(), + ) + .await + .ok_or(())?; + + StatefulTransaction::BuyTokens { + transaction: buy_tokens_tx, + state: BuyTokensTransactionState { + user_balance_base_token_amount: base_token_balance.token_amount, + user_balance_quote_token_amount: quote_token_balance.token_amount, + pool_base_token_amount: pool.base_token_amount, + pool_quote_token_amount: pool.quote_token_amount, + pool_total_liquidity_points: pool.total_liqudity_points, + }, + } + } + Transaction::SellTokens(sell_tokens_tx) => { + let base_token_balance = balances + .get_balance( + sell_tokens_tx.address.clone(), + sell_tokens_tx.base_token_id.clone(), + ) + .await + .ok_or(())?; + + let quote_token_balance = balances + .get_balance( + sell_tokens_tx.address.clone(), + sell_tokens_tx.base_token_id.clone(), + ) + .await + .ok_or(())?; + + let pool = pools + .get_pool( + sell_tokens_tx.base_token_id.clone(), + sell_tokens_tx.quote_token_id.clone(), + ) + .await + .ok_or(())?; + + StatefulTransaction::SellTokens { + transaction: sell_tokens_tx, + state: SellTokensTransactionState { + user_balance_base_token_amount: base_token_balance.token_amount, + user_balance_quote_token_amount: quote_token_balance.token_amount, + pool_base_token_amount: pool.base_token_amount, + pool_quote_token_amount: pool.quote_token_amount, + pool_total_liquidity_points: pool.total_liqudity_points, + }, + } + } + }) + .await + .ok_or(())?; Ok(()) } diff --git a/processes/src/proofpool/process.rs b/processes/src/proofpool/process.rs index b76dbba..7501047 100644 --- a/processes/src/proofpool/process.rs +++ b/processes/src/proofpool/process.rs @@ -15,7 +15,7 @@ pub fn process(path: &str) -> Processor { match request { Request::Push { sender, - transaction, + stateful_tx: transaction, } => { let result = proofpool.push(&transaction).await; diff --git a/processes/src/proofpool/processor.rs b/processes/src/proofpool/processor.rs index 0f0101d..94ecfcd 100644 --- a/processes/src/proofpool/processor.rs +++ b/processes/src/proofpool/processor.rs @@ -1,4 +1,4 @@ -use nacho_data_structures::Transaction; +use nacho_data_structures::StatefulTransaction; use tokio::sync::{mpsc, oneshot}; use super::Request; @@ -9,13 +9,13 @@ pub struct Processor { } impl Processor { - pub async fn push(&self, transaction: Transaction) -> Option<()> { + pub async fn push(&self, stateful_tx: StatefulTransaction) -> Option<()> { let (oneshot_sender, oneshot_receiver) = oneshot::channel(); self.sender .send(Request::Push { sender: oneshot_sender, - transaction, + stateful_tx, }) .await .ok()?; @@ -25,7 +25,7 @@ impl Processor { result } - pub async fn pop(&self) -> Option { + pub async fn pop(&self) -> Option { let (oneshot_sender, oneshot_receiver) = oneshot::channel(); self.sender @@ -35,8 +35,8 @@ impl Processor { .await .ok()?; - let maybe_transaction = oneshot_receiver.await.ok()?; + let maybe_stateful_tx = oneshot_receiver.await.ok()?; - maybe_transaction + maybe_stateful_tx } } diff --git a/processes/src/proofpool/request.rs b/processes/src/proofpool/request.rs index c433311..7658825 100644 --- a/processes/src/proofpool/request.rs +++ b/processes/src/proofpool/request.rs @@ -1,12 +1,12 @@ -use nacho_data_structures::Transaction; +use nacho_data_structures::StatefulTransaction; use tokio::sync::oneshot; pub enum Request { Push { sender: oneshot::Sender>, - transaction: Transaction, + stateful_tx: StatefulTransaction, }, Pop { - sender: oneshot::Sender>, + sender: oneshot::Sender>, }, } diff --git a/processes/src/withdrawals/process.rs b/processes/src/withdrawals/process.rs index 3c7fc4e..8da1bf7 100644 --- a/processes/src/withdrawals/process.rs +++ b/processes/src/withdrawals/process.rs @@ -1,4 +1,3 @@ -use nacho_data_structures::FieldConversion; use nacho_withdrawals_db::WithdrawalsDb; use tokio::sync::mpsc; @@ -14,17 +13,27 @@ pub fn process(path: &str) -> Processor { while let Some(request) = receiver.recv().await { match request { - Request::GetWitness { sender, burn_id } => { - let single_witness = withdrawals_db.get_witness(burn_id).await; - - sender.send(single_witness.ok()).unwrap(); - } - Request::SetLeaf { + Request::Set { sender, - burn_id, - value, + index, + withdrawal, } => { - let result = withdrawals_db.set(burn_id, value.to_fields()[0]).await; + let result = withdrawals_db.set(index, &withdrawal).await; + + sender.send(result.ok()).unwrap(); + } + Request::Get { sender, index } => { + let result = withdrawals_db.get(index).await; + + sender.send(result.ok()).unwrap(); + } + Request::GetRoot { sender } => { + let result = withdrawals_db.get_root().await.map(|root| root.into()); + + sender.send(result.ok()).unwrap(); + } + Request::GetWitness { sender, index } => { + let result = withdrawals_db.get_witness(index).await; sender.send(result.ok()).unwrap(); } diff --git a/processes/src/withdrawals/processor.rs b/processes/src/withdrawals/processor.rs index 3814e71..d42d54a 100644 --- a/processes/src/withdrawals/processor.rs +++ b/processes/src/withdrawals/processor.rs @@ -1,4 +1,4 @@ -use nacho_data_structures::U256; +use nacho_data_structures::{Withdrawal, U256}; use nacho_withdrawals_db::SingleWithdrawalWitness; use tokio::sync::{mpsc, oneshot}; @@ -10,30 +10,61 @@ pub struct Processor { } impl Processor { - pub async fn get_witness(&self, burn_id: u64) -> Option { + pub async fn set(&self, index: u64, withdrawal: Withdrawal) -> Option<()> { let (oneshot_sender, oneshot_receiver) = oneshot::channel(); self.sender - .send(Request::GetWitness { + .send(Request::Set { + sender: oneshot_sender, + index, + withdrawal, + }) + .await + .ok()?; + + let result = oneshot_receiver.await.ok()?; + + result + } + + pub async fn get(&self, index: u64) -> Option { + let (oneshot_sender, oneshot_receiver) = oneshot::channel(); + + self.sender + .send(Request::Get { + sender: oneshot_sender, + index, + }) + .await + .ok()?; + + let result = oneshot_receiver.await.ok()?; + + result + } + + pub async fn get_root(&self) -> Option { + let (oneshot_sender, oneshot_receiver) = oneshot::channel(); + + self.sender + .send(Request::GetRoot { sender: oneshot_sender, - burn_id, }) .await .ok()?; - let single_witness = oneshot_receiver.await.ok()?; + let result = oneshot_receiver.await.ok()?; - single_witness + result } - pub async fn set_leaf(&self, burn_id: u64, value: U256) -> Option<()> { + pub async fn get_witness(&self, index: u64) -> Option { let (oneshot_sender, oneshot_receiver) = oneshot::channel(); self.sender - .send(Request::SetLeaf { + .send(Request::GetWitness { sender: oneshot_sender, - burn_id, - value, + index, }) .await .ok()?; diff --git a/processes/src/withdrawals/request.rs b/processes/src/withdrawals/request.rs index ab2b1b0..1997b9b 100644 --- a/processes/src/withdrawals/request.rs +++ b/processes/src/withdrawals/request.rs @@ -1,15 +1,22 @@ -use nacho_data_structures::U256; +use nacho_data_structures::{Withdrawal, U256}; use nacho_withdrawals_db::SingleWithdrawalWitness; use tokio::sync::oneshot; pub enum Request { + Set { + sender: oneshot::Sender>, + index: u64, + withdrawal: Withdrawal, + }, + Get { + sender: oneshot::Sender>, + index: u64, + }, + GetRoot { + sender: oneshot::Sender>, + }, GetWitness { sender: oneshot::Sender>, - burn_id: u64, - }, - SetLeaf { - sender: oneshot::Sender>, - burn_id: u64, - value: U256, + index: u64, }, } diff --git a/generator-script/package-lock.json b/proof-generator-script/package-lock.json similarity index 97% rename from generator-script/package-lock.json rename to proof-generator-script/package-lock.json index 5057b3e..fa444d5 100644 --- a/generator-script/package-lock.json +++ b/proof-generator-script/package-lock.json @@ -8,8 +8,8 @@ "name": "nacho-generator-script", "version": "0.1.0", "dependencies": { - "nacho-common-o1js": "^0.1.7", - "nacho-proof-generator": "^0.1.5", + "nacho-common-o1js": "^0.1.8", + "nacho-proof-generator": "^0.1.7", "o1js": "^0.17.0" }, "devDependencies": { @@ -461,19 +461,19 @@ "integrity": "sha512-sga3MHh9sgQN2+pJ9VYZ+1LPwXOxuBJBA5nrR5/ofPfuiJBE2hnjsaN8se8JznOmGLN2p49Pe5U/ttafcs/apA==" }, "node_modules/nacho-common-o1js": { - "version": "0.1.7", - "resolved": "https://registry.npmjs.org/nacho-common-o1js/-/nacho-common-o1js-0.1.7.tgz", - "integrity": "sha512-3SVyx+CdmzzuYqW6eKtLcHs9Qvt4Q0+J/9sL7AIP9jr6chhVrtyN6LDVh5LHERSYsgIr+QeaZT1m50JMFs6/6g==", + "version": "0.1.8", + "resolved": "https://registry.npmjs.org/nacho-common-o1js/-/nacho-common-o1js-0.1.8.tgz", + "integrity": "sha512-udU84Jbef/oB384VmwyiMXMbscfvkKBaWZzfuMOFgGTavlnEtt16T6rp9obKYHRNAJiuwo43env6NYbkK7EryQ==", "dependencies": { "o1js": "^0.17.0" } }, "node_modules/nacho-proof-generator": { - "version": "0.1.5", - "resolved": "https://registry.npmjs.org/nacho-proof-generator/-/nacho-proof-generator-0.1.5.tgz", - "integrity": "sha512-YfnX7RIr27qjdMMziPzM87IUBJyQXka7IZ9nfo6HNmNj9PM4Gy8UmhWA6R9juTdXdQjtTz31mZ174xNKSoDomQ==", + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/nacho-proof-generator/-/nacho-proof-generator-0.1.7.tgz", + "integrity": "sha512-UOQWZMmJPxb2x3bzsUp6vyDaDtyukToVvw1VBs59izFSxlUwUxPm0T21zHVGr65238A8ShrxQrKbh7EJBomSDA==", "dependencies": { - "nacho-common-o1js": "^0.1.7", + "nacho-common-o1js": "^0.1.8", "o1js": "^0.17.0" } }, diff --git a/merger-script/package.json b/proof-generator-script/package.json similarity index 85% rename from merger-script/package.json rename to proof-generator-script/package.json index 5c09d0f..895162b 100644 --- a/merger-script/package.json +++ b/proof-generator-script/package.json @@ -1,5 +1,5 @@ { - "name": "nacho-merger-script", + "name": "nacho-proof-generator-script", "version": "0.1.0", "private": true, "type": "module", @@ -11,7 +11,7 @@ }, "dependencies": { "nacho-common-o1js": "^0.1.8", - "nacho-proof-generator": "^0.1.5", + "nacho-proof-generator": "^0.1.7", "o1js": "^0.17.0" }, "devDependencies": { diff --git a/generator-script/src/index.ts b/proof-generator-script/src/index.ts similarity index 100% rename from generator-script/src/index.ts rename to proof-generator-script/src/index.ts diff --git a/generator-script/src/input.ts b/proof-generator-script/src/input.ts similarity index 98% rename from generator-script/src/input.ts rename to proof-generator-script/src/input.ts index 73768e0..a6e61a3 100644 --- a/generator-script/src/input.ts +++ b/proof-generator-script/src/input.ts @@ -34,7 +34,6 @@ export type DepositTokens = { token_id: bigint user_deposit_token_amount: bigint user_balance_token_amount: bigint - is_users_first_deposit: boolean } export type BurnTokens = { @@ -86,7 +85,6 @@ export type ProvideLiquidity = { pool_total_liquidity_points: bigint user_base_token_amount_to_provide: bigint user_quote_token_amount_limit_to_provide: bigint - is_first_providing: boolean user_signature: [bigint, bigint] } diff --git a/generator-script/src/parse.ts b/proof-generator-script/src/parse.ts similarity index 98% rename from generator-script/src/parse.ts rename to proof-generator-script/src/parse.ts index b31e1e4..3088903 100644 --- a/generator-script/src/parse.ts +++ b/proof-generator-script/src/parse.ts @@ -40,7 +40,6 @@ export const parseInput = (buffer: Buffer): Input => { token_id: parseUint256(buffer.subarray(982, 1014)), user_deposit_token_amount: parseUint64(buffer.subarray(1014, 1022)), user_balance_token_amount: parseUint64(buffer.subarray(1022, 1030)), - is_users_first_deposit: parseBoolean(buffer.subarray(1030, 1031)), } satisfies DepositTokens } case 2: { @@ -95,8 +94,7 @@ export const parseInput = (buffer: Buffer): Input => { pool_total_liquidity_points: parseUint256(buffer.subarray(3147, 3179)), user_base_token_amount_to_provide: parseUint64(buffer.subarray(3179, 3187)), user_quote_token_amount_limit_to_provide: parseUint64(buffer.subarray(3187, 3195)), - is_first_providing: parseBoolean(buffer.subarray(3195, 3196)), - user_signature: parseSignature(buffer.subarray(3196, 3260)), + user_signature: parseSignature(buffer.subarray(3195, 3259)), } satisfies ProvideLiquidity } case 5: { diff --git a/generator-script/src/prove.ts b/proof-generator-script/src/prove.ts similarity index 99% rename from generator-script/src/prove.ts rename to proof-generator-script/src/prove.ts index 3fcc92a..1171f18 100644 --- a/generator-script/src/prove.ts +++ b/proof-generator-script/src/prove.ts @@ -49,7 +49,6 @@ export const depositTokens = async (params: DepositTokens, proofDbPath: string) Field(params.token_id), UInt64.from(params.user_deposit_token_amount), UInt64.from(params.user_balance_token_amount), - Bool(params.is_users_first_deposit), ) return proof @@ -139,7 +138,6 @@ export const makeProvideLiquidity = async (params: ProvideLiquidity, proofDbPath Field(params.pool_total_liquidity_points), UInt64.from(params.user_base_token_amount_to_provide), UInt64.from(params.user_quote_token_amount_limit_to_provide), - Bool(params.is_first_providing), Signature.fromObject({ r: Field.from(params.user_signature[0]), s: Scalar.from(params.user_signature[1]), diff --git a/generator-script/src/types.ts b/proof-generator-script/src/types.ts similarity index 100% rename from generator-script/src/types.ts rename to proof-generator-script/src/types.ts diff --git a/generator-script/src/utils.ts b/proof-generator-script/src/utils.ts similarity index 100% rename from generator-script/src/utils.ts rename to proof-generator-script/src/utils.ts diff --git a/generator-script/tsconfig.json b/proof-generator-script/tsconfig.json similarity index 100% rename from generator-script/tsconfig.json rename to proof-generator-script/tsconfig.json diff --git a/proof-generator/package-lock.json b/proof-generator/package-lock.json index a889874..45eb953 100644 --- a/proof-generator/package-lock.json +++ b/proof-generator/package-lock.json @@ -1,12 +1,12 @@ { "name": "nacho-proof-generator", - "version": "0.1.6", + "version": "0.1.7", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "nacho-proof-generator", - "version": "0.1.6", + "version": "0.1.7", "dependencies": { "nacho-common-o1js": "^0.1.8", "o1js": "^0.17.0" diff --git a/proof-generator/package.json b/proof-generator/package.json index d6a10fb..1a7e6bd 100644 --- a/proof-generator/package.json +++ b/proof-generator/package.json @@ -1,6 +1,6 @@ { "name": "nacho-proof-generator", - "version": "0.1.6", + "version": "0.1.7", "type": "module", "main": "build/src/index.js", "types": "build/src/index.d.ts", diff --git a/proof-generator/src/methods/deposit-tokens.ts b/proof-generator/src/methods/deposit-tokens.ts index 031f3b3..5966897 100644 --- a/proof-generator/src/methods/deposit-tokens.ts +++ b/proof-generator/src/methods/deposit-tokens.ts @@ -11,7 +11,6 @@ export const depositTokens = ( tokenId: Field, userDepositTokenAmount: UInt64, userBalanceTokenAmount: UInt64, - isUsersFirstDeposit: Bool, ): StateRoots => { stateRoots.assertEquals(earlierProof.publicOutput) earlierProof.verify() @@ -22,11 +21,16 @@ export const depositTokens = ( tokenAmount: userBalanceTokenAmount, }) - stateRoots.balances.assertEquals( - singleBalanceWitness.calculateRoot( - choose(isUsersFirstDeposit, Field(0), Poseidon.hash(userBalance.toFields())), + const balancesRootIfFirstDeposit = singleBalanceWitness.calculateRoot(Field(0)) + + const isUsersFirstDeposit = stateRoots.balances.equals(balancesRootIfFirstDeposit) + + Bool.or( + stateRoots.balances.equals(balancesRootIfFirstDeposit), + stateRoots.balances.equals( + singleBalanceWitness.calculateRoot(Poseidon.hash(userBalance.toFields())), ), - ) + ).assertTrue() const userDeposit = new Deposit({ depositor: userAddress, @@ -39,7 +43,7 @@ export const depositTokens = ( Poseidon.hash([currentDepositsMerkleListHash, Poseidon.hash(userDeposit.toFields())]), ) - choose(isUsersFirstDeposit, Field(0), userBalanceTokenAmount.value).assertEquals(Field(0)) + choose(isUsersFirstDeposit, userBalanceTokenAmount.value, Field(0)).assertEquals(0) userBalance.tokenAmount = userBalanceTokenAmount.add(userDepositTokenAmount) diff --git a/proof-generator/src/methods/make-provide-liquidity.ts b/proof-generator/src/methods/make-provide-liquidity.ts index 74755dc..019b31e 100644 --- a/proof-generator/src/methods/make-provide-liquidity.ts +++ b/proof-generator/src/methods/make-provide-liquidity.ts @@ -28,7 +28,6 @@ export const makeProvideLiquidity = ( poolTotalLiquidityPoints: Field, userBaseTokenAmountToProvide: UInt64, userQuoteTokenAmountLimitToProvide: UInt64, - isFirstProviding: Bool, userSignature: Signature, ): StateRoots => { stateRoots.assertEquals(earlierProof.publicOutput) @@ -76,11 +75,16 @@ export const makeProvideLiquidity = ( doubleBalanceWitness.isCorrect().assertTrue() stateRoots.pools.assertEquals(singlePoolWitness.calculateRoot(Poseidon.hash(pool.toFields()))) - stateRoots.liquidities.assertEquals( - singleLiquidityWitness.calculateRoot( - choose(isFirstProviding, Field(0), Poseidon.hash(userLiquidity.toFields())), + const liquiditiesRootIfFirstProviding = singleLiquidityWitness.calculateRoot(Field(0)) + const isFirstProviding = stateRoots.liquidities.equals(liquiditiesRootIfFirstProviding) + + Bool.or( + stateRoots.liquidities.equals(liquiditiesRootIfFirstProviding), + stateRoots.liquidities.equals( + singleLiquidityWitness.calculateRoot(Poseidon.hash(userLiquidity.toFields())), ), - ) + ).assertTrue() + stateRoots.balances.assertEquals( doubleBalanceWitness.calculateRoot( Poseidon.hash(userBaseTokenBalance.toFields()), diff --git a/proof-generator/src/proof-generator.ts b/proof-generator/src/proof-generator.ts index 9539f8e..714558f 100644 --- a/proof-generator/src/proof-generator.ts +++ b/proof-generator/src/proof-generator.ts @@ -40,7 +40,6 @@ export const proofGenerator = ZkProgram({ Field, UInt64, UInt64, - Bool, ], method: depositTokens, }, @@ -92,7 +91,6 @@ export const proofGenerator = ZkProgram({ Field, UInt64, UInt64, - Bool, Signature, ], method: makeProvideLiquidity, diff --git a/proof-generator/tests/proof-generator.test.ts b/proof-generator/tests/proof-generator.test.ts index a30eb97..2889e79 100644 --- a/proof-generator/tests/proof-generator.test.ts +++ b/proof-generator/tests/proof-generator.test.ts @@ -19,14 +19,11 @@ describe("proof generator", async () => { assert.deepEqual(proof.publicInput, stateUtil.stateRoots) assert.deepEqual(proof.publicOutput, stateUtil.stateRoots) stateUtil.pushProof(proof) - - console.log(JSON.stringify(proof.toJSON()).length) }) it("generates deposit tokens proof", async () => { const tokenAmount = UInt64.from(45_000_000) const currentBalance = UInt64.from(0) - const isUsersFirstDeposit = Bool(true) const proof = await proofGenerator.depositTokens( stateUtil.stateRoots, @@ -38,7 +35,6 @@ describe("proof generator", async () => { minaTokenId, tokenAmount, currentBalance, - isUsersFirstDeposit, ) proof.publicInput.assertEquals(stateUtil.stateRoots) @@ -48,7 +44,6 @@ describe("proof generator", async () => { proof.publicOutput.assertEquals(stateUtil.stateRoots) stateUtil.pushProof(proof) - console.log(JSON.stringify(proof.toJSON()).length) }) it("generates make burn tokens proof", async () => { @@ -77,13 +72,11 @@ describe("proof generator", async () => { proof.publicOutput.assertEquals(stateUtil.stateRoots) stateUtil.pushProof(proof) - console.log(JSON.stringify(proof.toJSON()).length) }) it("generates deposit tokens proof one more time", async () => { const tokenAmount = UInt64.from(100_000_000) const currentBalance = UInt64.from(0) - const isUsersFirstDeposit = Bool(true) const proof = await proofGenerator.depositTokens( stateUtil.stateRoots, @@ -95,7 +88,6 @@ describe("proof generator", async () => { usdcTokenId, tokenAmount, currentBalance, - isUsersFirstDeposit, ) proof.publicInput.assertEquals(stateUtil.stateRoots) @@ -105,7 +97,6 @@ describe("proof generator", async () => { proof.publicOutput.assertEquals(stateUtil.stateRoots) stateUtil.pushProof(proof) - console.log(JSON.stringify(proof.toJSON()).length) }) it("generates make create pool proof", async () => { @@ -180,7 +171,6 @@ describe("proof generator", async () => { const currentLiquidityPoints = currentMinaLiquidity.value.mul(currentUsdcLiquidity.value) const baseTokenAmountToProvide = UInt64.from(5_000_000) const quoteTokenAmountLimitToProvide = UInt64.from(10_000_000) - const isFirstProviding = Bool(false) const userSignature = Signature.create(john.privateKey, [ minaTokenId, usdcTokenId, @@ -205,7 +195,6 @@ describe("proof generator", async () => { currentLiquidityPoints, baseTokenAmountToProvide, quoteTokenAmountLimitToProvide, - isFirstProviding, userSignature, ) diff --git a/proof-merger-script/package-lock.json b/proof-merger-script/package-lock.json index 2872cd8..16f3516 100644 --- a/proof-merger-script/package-lock.json +++ b/proof-merger-script/package-lock.json @@ -9,7 +9,7 @@ "version": "0.1.0", "dependencies": { "nacho-common-o1js": "^0.1.8", - "nacho-proof-generator": "^0.1.5", + "nacho-proof-generator": "^0.1.7", "o1js": "^0.17.0" }, "devDependencies": { @@ -469,11 +469,11 @@ } }, "node_modules/nacho-proof-generator": { - "version": "0.1.5", - "resolved": "https://registry.npmjs.org/nacho-proof-generator/-/nacho-proof-generator-0.1.5.tgz", - "integrity": "sha512-YfnX7RIr27qjdMMziPzM87IUBJyQXka7IZ9nfo6HNmNj9PM4Gy8UmhWA6R9juTdXdQjtTz31mZ174xNKSoDomQ==", + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/nacho-proof-generator/-/nacho-proof-generator-0.1.7.tgz", + "integrity": "sha512-UOQWZMmJPxb2x3bzsUp6vyDaDtyukToVvw1VBs59izFSxlUwUxPm0T21zHVGr65238A8ShrxQrKbh7EJBomSDA==", "dependencies": { - "nacho-common-o1js": "^0.1.7", + "nacho-common-o1js": "^0.1.8", "o1js": "^0.17.0" } }, diff --git a/proof-merger-script/package.json b/proof-merger-script/package.json index 512d704..5416a90 100644 --- a/proof-merger-script/package.json +++ b/proof-merger-script/package.json @@ -11,7 +11,7 @@ }, "dependencies": { "nacho-common-o1js": "^0.1.8", - "nacho-proof-generator": "^0.1.5", + "nacho-proof-generator": "^0.1.7", "o1js": "^0.17.0" }, "devDependencies": { diff --git a/proof-submitter-script/package-lock.json b/proof-submitter-script/package-lock.json index 1f7e5ec..d8caefc 100644 --- a/proof-submitter-script/package-lock.json +++ b/proof-submitter-script/package-lock.json @@ -9,7 +9,7 @@ "version": "0.1.0", "dependencies": { "nacho-common-o1js": "^0.1.8", - "nacho-rollup-contract": "^0.1.4", + "nacho-rollup-contract": "^0.1.5", "o1js": "^0.17.0" }, "devDependencies": { @@ -469,21 +469,21 @@ } }, "node_modules/nacho-proof-generator": { - "version": "0.1.5", - "resolved": "https://registry.npmjs.org/nacho-proof-generator/-/nacho-proof-generator-0.1.5.tgz", - "integrity": "sha512-YfnX7RIr27qjdMMziPzM87IUBJyQXka7IZ9nfo6HNmNj9PM4Gy8UmhWA6R9juTdXdQjtTz31mZ174xNKSoDomQ==", + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/nacho-proof-generator/-/nacho-proof-generator-0.1.7.tgz", + "integrity": "sha512-UOQWZMmJPxb2x3bzsUp6vyDaDtyukToVvw1VBs59izFSxlUwUxPm0T21zHVGr65238A8ShrxQrKbh7EJBomSDA==", "dependencies": { - "nacho-common-o1js": "^0.1.7", + "nacho-common-o1js": "^0.1.8", "o1js": "^0.17.0" } }, "node_modules/nacho-rollup-contract": { - "version": "0.1.4", - "resolved": "https://registry.npmjs.org/nacho-rollup-contract/-/nacho-rollup-contract-0.1.4.tgz", - "integrity": "sha512-8SWloEWIMWXiKfo4kCvft7ECdFoFcK8st1Y02llK4zBG25S8D6LVEANWXe8WzRGKbFOyKT7PvIfE5f3rSVsy3g==", + "version": "0.1.5", + "resolved": "https://registry.npmjs.org/nacho-rollup-contract/-/nacho-rollup-contract-0.1.5.tgz", + "integrity": "sha512-GT7LwJ/WkRifCJrEcS8hknTMNh1fvCVR/77NsUqS5tKhLDNJNBq8cgon0CzmWw9okl4qm2nhJNfxRp9EPx7tCQ==", "dependencies": { "nacho-common-o1js": "^0.1.8", - "nacho-proof-generator": "^0.1.4", + "nacho-proof-generator": "^0.1.7", "o1js": "^0.17.0" } }, diff --git a/proof-submitter-script/package.json b/proof-submitter-script/package.json index c7e114e..cc006c2 100644 --- a/proof-submitter-script/package.json +++ b/proof-submitter-script/package.json @@ -11,7 +11,7 @@ }, "dependencies": { "nacho-common-o1js": "^0.1.8", - "nacho-rollup-contract": "^0.1.4", + "nacho-rollup-contract": "^0.1.5", "o1js": "^0.17.0" }, "devDependencies": { diff --git a/proofpool/src/proofpool.rs b/proofpool/src/proofpool.rs index 8ac550e..79ef3c7 100644 --- a/proofpool/src/proofpool.rs +++ b/proofpool/src/proofpool.rs @@ -1,12 +1,12 @@ use std::path::Path; -use nacho_data_structures::ProverMethod; +use nacho_data_structures::StatefulTransaction; use nacho_dynamic_queue::DynamicQueue; use crate::ProofpoolError; pub struct Proofpool { - queue: DynamicQueue<3291, ProverMethod>, + queue: DynamicQueue<328, StatefulTransaction>, } type Result = std::result::Result; @@ -20,13 +20,13 @@ impl Proofpool { Ok(Self { queue }) } - pub async fn push(&mut self, method: &ProverMethod) -> Result<()> { - self.queue.push(method).await?; + pub async fn push(&mut self, stateful_tx: &StatefulTransaction) -> Result<()> { + self.queue.push(stateful_tx).await?; Ok(()) } - pub async fn pop(&mut self) -> Result> { + pub async fn pop(&mut self) -> Result> { let maybe_method = self.queue.pop().await?; Ok(maybe_method) diff --git a/rollup-contract/package-lock.json b/rollup-contract/package-lock.json index ff6e210..8ed0bf7 100644 --- a/rollup-contract/package-lock.json +++ b/rollup-contract/package-lock.json @@ -1,15 +1,15 @@ { "name": "nacho-rollup-contract", - "version": "0.1.4", + "version": "0.1.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "nacho-rollup-contract", - "version": "0.1.4", + "version": "0.1.5", "dependencies": { "nacho-common-o1js": "^0.1.8", - "nacho-proof-generator": "^0.1.4", + "nacho-proof-generator": "^0.1.7", "o1js": "^0.17.0" }, "devDependencies": { @@ -62,9 +62,9 @@ } }, "node_modules/nacho-proof-generator": { - "version": "0.1.6", - "resolved": "https://registry.npmjs.org/nacho-proof-generator/-/nacho-proof-generator-0.1.6.tgz", - "integrity": "sha512-Z62I692H60M67vMZjPV6z9P89w9km0iulsunRBmNCa2MpvemHxz8ZZPAH5ZWQMImbcfe6LFvjOcjPC73zAXmHQ==", + "version": "0.1.7", + "resolved": "https://registry.npmjs.org/nacho-proof-generator/-/nacho-proof-generator-0.1.7.tgz", + "integrity": "sha512-UOQWZMmJPxb2x3bzsUp6vyDaDtyukToVvw1VBs59izFSxlUwUxPm0T21zHVGr65238A8ShrxQrKbh7EJBomSDA==", "dependencies": { "nacho-common-o1js": "^0.1.8", "o1js": "^0.17.0" diff --git a/rollup-contract/package.json b/rollup-contract/package.json index d0c863f..892528e 100644 --- a/rollup-contract/package.json +++ b/rollup-contract/package.json @@ -1,6 +1,6 @@ { "name": "nacho-rollup-contract", - "version": "0.1.4", + "version": "0.1.5", "type": "module", "main": "build/src/index.js", "types": "build/src/index.d.ts", @@ -13,7 +13,7 @@ }, "dependencies": { "nacho-common-o1js": "^0.1.8", - "nacho-proof-generator": "^0.1.4", + "nacho-proof-generator": "^0.1.7", "o1js": "^0.17.0" }, "devDependencies": { diff --git a/rollup-contract/tests/rollup-contract.test.ts b/rollup-contract/tests/rollup-contract.test.ts index 3c25969..dfa7882 100644 --- a/rollup-contract/tests/rollup-contract.test.ts +++ b/rollup-contract/tests/rollup-contract.test.ts @@ -41,7 +41,6 @@ describe("rollup contract", async () => { const minaTokenId = Field(1) const tokenAmount = UInt64.from(42) const currentBalance = UInt64.zero - const isUsersFirstDeposit = Bool(true) const proof = await proofGenerator.depositTokens( stateUtil.stateRoots, @@ -53,7 +52,6 @@ describe("rollup contract", async () => { minaTokenId, tokenAmount, currentBalance, - isUsersFirstDeposit, ) stateUtil.pushProof(proof)