Skip to content

Commit c870217

Browse files
refactor: disbale add/remove liquidity
1 parent fa659e8 commit c870217

File tree

8 files changed

+112
-72
lines changed

8 files changed

+112
-72
lines changed

contracts/solver-registry/src/admin.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ impl Contract {
77
self.assert_owner();
88
self.approved_codehashes.insert(codehash);
99
}
10+
11+
pub fn change_owner(&mut self, new_owner_id: AccountId) {
12+
self.assert_owner();
13+
self.owner_id = new_owner_id;
14+
}
1015
}
1116

1217
impl Contract {

contracts/solver-registry/src/collateral.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ pub fn verify_codehash(raw_tcb_info: String, rtmr3: String) -> String {
5454
"Invalid compose hash"
5555
);
5656
// event with compose hash matches report rtmr3
57-
require!(
58-
replayed_rtmr3 == rtmr3,
59-
format!("Invalid rtmr3: {} v.s. {}", replayed_rtmr3, rtmr3)
60-
);
57+
require!(replayed_rtmr3 == rtmr3, "Invalid rtmr3");
6158

6259
let (_, right) = app_compose.split_once("\\n image:").unwrap();
6360
let (left, _) = right.split_once("\\n").unwrap();

contracts/solver-registry/src/events.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use near_sdk::json_types::U128;
21
use near_sdk::serde::Serialize;
32
use near_sdk::serde_json::json;
43
use near_sdk::{log, AccountId, PublicKey};
@@ -17,24 +16,26 @@ pub const EVENT_STANDARD_VERSION: &str = "1.0.0";
1716
pub enum Event<'a> {
1817
WorkerRegistered {
1918
worker_id: &'a AccountId,
20-
public_key: &'a PublicKey,
2119
pool_id: &'a u32,
20+
public_key: &'a PublicKey,
21+
codehash: &'a String,
22+
checksum: &'a String,
2223
},
2324
CreateLiquidityPool {
2425
pool_id: &'a u32,
2526
token_ids: &'a Vec<AccountId>,
2627
fee: &'a u32,
2728
},
28-
AddLiquidity {
29-
pool_id: &'a u32,
30-
account_id: &'a AccountId,
31-
amounts: &'a Vec<U128>,
32-
},
33-
RemoveLiquidity {
34-
pool_id: &'a u32,
35-
account_id: &'a AccountId,
36-
amounts: &'a Vec<U128>,
37-
},
29+
// AddLiquidity {
30+
// pool_id: &'a u32,
31+
// account_id: &'a AccountId,
32+
// amounts: &'a Vec<U128>,
33+
// },
34+
// RemoveLiquidity {
35+
// pool_id: &'a u32,
36+
// account_id: &'a AccountId,
37+
// amounts: &'a Vec<U128>,
38+
// },
3839
}
3940

4041
impl Event<'_> {

contracts/solver-registry/src/lib.rs

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ use near_sdk::{
55
env::{self, block_timestamp},
66
ext_contract, log, near, require,
77
store::{IterableMap, IterableSet, Vector},
8-
AccountId, NearToken, PanicOnDefault, Promise, PublicKey,
8+
AccountId, Gas, NearToken, PanicOnDefault, Promise, PromiseError, PublicKey,
99
};
1010

11+
use crate::events::*;
1112
use crate::pool::*;
1213
use crate::types::*;
1314

@@ -20,6 +21,8 @@ mod types;
2021
mod upgrade;
2122
mod view;
2223

24+
const GAS_REGISTER_WORKER_CALLBACK: Gas = Gas::from_tgas(10);
25+
2326
#[near(serializers = [json, borsh])]
2427
#[derive(Clone)]
2528
pub struct Worker {
@@ -84,26 +87,48 @@ impl Contract {
8487

8588
// TODO: verify predecessor implicit account is derived from this public key
8689
let public_key = env::signer_account_pk();
87-
88-
let predecessor = env::predecessor_account_id();
89-
self.worker_by_account_id.insert(
90-
predecessor,
91-
Worker {
92-
pool_id,
93-
checksum,
94-
codehash,
95-
},
96-
);
90+
let worker_id = env::predecessor_account_id();
9791

9892
// add the public key to the intents vault
9993
ext_intents_vault::ext(self.get_pool_account_id(pool_id))
10094
.with_attached_deposit(NearToken::from_yoctonear(1))
101-
.add_public_key(self.intents_contract_id.clone(), public_key)
95+
.add_public_key(self.intents_contract_id.clone(), public_key.clone())
96+
.then(
97+
Self::ext(env::current_account_id())
98+
.with_static_gas(GAS_REGISTER_WORKER_CALLBACK)
99+
.on_worker_key_added(worker_id, pool_id, public_key, codehash, checksum),
100+
)
102101
}
103102

104-
pub fn require_approved_codehash(&self) {
105-
let worker = self.get_worker(env::predecessor_account_id());
106-
self.assert_approved_codehash(&worker.codehash);
103+
#[private]
104+
pub fn on_worker_key_added(
105+
&mut self,
106+
worker_id: AccountId,
107+
pool_id: u32,
108+
public_key: PublicKey,
109+
codehash: String,
110+
checksum: String,
111+
#[callback_result] call_result: Result<(), PromiseError>,
112+
) {
113+
if call_result.is_ok() {
114+
self.worker_by_account_id.insert(
115+
worker_id.clone(),
116+
Worker {
117+
pool_id,
118+
checksum: checksum.clone(),
119+
codehash: codehash.clone(),
120+
},
121+
);
122+
123+
Event::WorkerRegistered {
124+
worker_id: &worker_id,
125+
pool_id: &pool_id,
126+
public_key: &public_key,
127+
codehash: &codehash,
128+
checksum: &checksum,
129+
}
130+
.emit();
131+
}
107132
}
108133
}
109134

@@ -114,4 +139,9 @@ impl Contract {
114139
"Invalid code hash"
115140
);
116141
}
142+
143+
// fn require_approved_codehash(&self) {
144+
// let worker = self.get_worker(env::predecessor_account_id());
145+
// self.assert_approved_codehash(&worker.codehash);
146+
// }
117147
}

contracts/solver-registry/src/pool.rs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use near_sdk::json_types::U128;
1+
// use near_sdk::json_types::U128;
22
use near_sdk::store::LookupMap;
33
use near_sdk::{near, require, AccountId, Gas, NearToken, PromiseError, PromiseOrValue};
44

@@ -96,30 +96,31 @@ impl Contract {
9696
}
9797
}
9898

99-
#[payable]
100-
pub fn add_liquidity(
101-
&mut self,
102-
pool_id: u32,
103-
token_ids: Vec<AccountId>,
104-
amounts: Vec<Balance>,
105-
) {
106-
require!(token_ids.len() == 2, "Must have exactly 2 tokens");
107-
require!(amounts.len() == 2, "Must have exactly 2 amounts");
108-
require!(amounts[0] > 0, "Amount must be greater than 0");
109-
require!(amounts[1] > 0, "Amount must be greater than 0");
110-
111-
let pool = self.pools.get(pool_id).expect("Pool not found");
112-
let shares_total_supply = pool.shares_total_supply;
113-
}
114-
115-
#[payable]
116-
pub fn remove_liquidity(&mut self, pool_id: u32, shares: U128) {
117-
let shares = shares.0;
118-
require!(shares > 0, "Shares must be greater than 0");
119-
120-
let pool = self.pools.get(pool_id).expect("Pool not found");
121-
// pool.shares_total_supply -= shares;
122-
}
99+
// `add_liquidity` and `remove_liquidity` are not needed for now
100+
// #[payable]
101+
// pub fn add_liquidity(
102+
// &mut self,
103+
// pool_id: u32,
104+
// token_ids: Vec<AccountId>,
105+
// amounts: Vec<Balance>,
106+
// ) {
107+
// require!(token_ids.len() == 2, "Must have exactly 2 tokens");
108+
// require!(amounts.len() == 2, "Must have exactly 2 amounts");
109+
// require!(amounts[0] > 0, "Amount must be greater than 0");
110+
// require!(amounts[1] > 0, "Amount must be greater than 0");
111+
112+
// let pool = self.pools.get(pool_id).expect("Pool not found");
113+
// let shares_total_supply = pool.shares_total_supply;
114+
// }
115+
116+
// #[payable]
117+
// pub fn remove_liquidity(&mut self, pool_id: u32, shares: U128) {
118+
// let shares = shares.0;
119+
// require!(shares > 0, "Shares must be greater than 0");
120+
121+
// let pool = self.pools.get(pool_id).expect("Pool not found");
122+
// // pool.shares_total_supply -= shares;
123+
// }
123124
}
124125

125126
impl Contract {
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use near_sdk::json_types::U128;
2-
use near_sdk::{near, AccountId};
1+
// use near_sdk::json_types::U128;
2+
// use near_sdk::{near, AccountId};
33

4-
use crate::*;
4+
// use crate::*;
55

6-
#[near]
7-
impl Contract {
8-
#[payable]
9-
pub fn ft_on_transfer(&mut self, token_id: AccountId, amount: U128) {
10-
// TODO
11-
}
12-
}
6+
// #[near]
7+
// impl Contract {
8+
// #[payable]
9+
// pub fn ft_on_transfer(&mut self, token_id: AccountId, amount: U128) {
10+
// // TODO
11+
// }
12+
// }

contracts/solver-registry/src/view.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@ use near_sdk::AccountId;
33

44
#[near]
55
impl Contract {
6+
pub fn get_owner_id(&self) -> AccountId {
7+
self.owner_id.clone()
8+
}
9+
10+
pub fn get_pool_len(&self) -> u32 {
11+
self.pools.len()
12+
}
13+
14+
pub fn get_worker_len(&self) -> u32 {
15+
self.worker_by_account_id.len()
16+
}
17+
618
pub fn get_worker(&self, account_id: AccountId) -> Worker {
719
self.worker_by_account_id
820
.get(&account_id)
921
.unwrap()
1022
.to_owned()
1123
}
12-
13-
pub fn get_owner_id(&self) -> AccountId {
14-
self.owner_id.clone()
15-
}
1624
}

contracts/solver-registry/tests/constants.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,3 @@ pub const QUOTE_HEX : &str = "040002008100000000000000939a7233f79c4ca9940a0db395
77
pub const CHECKSUM: &str = "test_checksum";
88
pub const TCB_INFO: &str = include_str!("samples/tcb_info.json");
99
pub const CODE_HASH: &str = "a27359cd0d747ae62300649e959c02707d70a9fb0900a9771d1f6f9311e89c3f";
10-
11-
pub const QUOTE: &str = r#"{"signer_id": "solvers.near", "nonce": "3awkvkIUsqRsSJhdlNhGzBafs5ITIUZYb1T3AUOB7bA=", "verifying_contract": "intents.near", "deadline": "2025-12-31T11:59:59.000Z", "intents": [{"intent": "token_diff", "diff": {"nep141:wrap.near": "-500000000000000000000000", "nep141:eth-0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.omft.near": "-1277075"}}]}"#;

0 commit comments

Comments
 (0)