Skip to content

Commit daeaab6

Browse files
committed
support dynamic staking system and ability to automatically increase stake
1 parent 82a9b4d commit daeaab6

File tree

11 files changed

+297
-25
lines changed

11 files changed

+297
-25
lines changed

shared/artifacts/abi/compute_registry.json

+38
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,25 @@
510510
],
511511
"stateMutability": "view"
512512
},
513+
{
514+
"type": "function",
515+
"name": "getProviderTotalCompute",
516+
"inputs": [
517+
{
518+
"name": "provider",
519+
"type": "address",
520+
"internalType": "address"
521+
}
522+
],
523+
"outputs": [
524+
{
525+
"name": "",
526+
"type": "uint256",
527+
"internalType": "uint256"
528+
}
529+
],
530+
"stateMutability": "view"
531+
},
513532
{
514533
"type": "function",
515534
"name": "getProviderTotalNodes",
@@ -714,6 +733,25 @@
714733
],
715734
"stateMutability": "view"
716735
},
736+
{
737+
"type": "function",
738+
"name": "providerTotalCompute",
739+
"inputs": [
740+
{
741+
"name": "",
742+
"type": "address",
743+
"internalType": "address"
744+
}
745+
],
746+
"outputs": [
747+
{
748+
"name": "",
749+
"type": "uint256",
750+
"internalType": "uint256"
751+
}
752+
],
753+
"stateMutability": "view"
754+
},
717755
{
718756
"type": "function",
719757
"name": "providers",

shared/artifacts/abi/prime_network.json

+37
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,30 @@
113113
"outputs": [],
114114
"stateMutability": "nonpayable"
115115
},
116+
{
117+
"type": "function",
118+
"name": "calculateMinimumStake",
119+
"inputs": [
120+
{
121+
"name": "provider",
122+
"type": "address",
123+
"internalType": "address"
124+
},
125+
{
126+
"name": "computeUnits",
127+
"type": "uint256",
128+
"internalType": "uint256"
129+
}
130+
],
131+
"outputs": [
132+
{
133+
"name": "",
134+
"type": "uint256",
135+
"internalType": "uint256"
136+
}
137+
],
138+
"stateMutability": "view"
139+
},
116140
{
117141
"type": "function",
118142
"name": "computePool",
@@ -317,6 +341,19 @@
317341
],
318342
"stateMutability": "view"
319343
},
344+
{
345+
"type": "function",
346+
"name": "increaseStake",
347+
"inputs": [
348+
{
349+
"name": "additionalStake",
350+
"type": "uint256",
351+
"internalType": "uint256"
352+
}
353+
],
354+
"outputs": [],
355+
"stateMutability": "nonpayable"
356+
},
320357
{
321358
"type": "function",
322359
"name": "invalidateNode",

shared/src/web3/contracts/core/builder.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ use crate::web3::{
44
contracts::{
55
core::error::ContractError, // Using custom error ContractError
66
implementations::{
7-
ai_token_contract::AIToken, compute_pool_contract::ComputePool,
8-
compute_registry_contract::ComputeRegistryContract,
9-
prime_network_contract::PrimeNetworkContract,
10-
work_validators::synthetic_data_validator::SyntheticDataWorkValidator,
7+
ai_token_contract::AIToken, compute_pool_contract::ComputePool, compute_registry_contract::ComputeRegistryContract, prime_network_contract::PrimeNetworkContract, stake_manager::StakeManagerContract, work_validators::synthetic_data_validator::SyntheticDataWorkValidator
118
},
129
},
1310
wallet::Wallet,
@@ -20,6 +17,7 @@ pub struct Contracts {
2017
pub ai_token: AIToken,
2118
pub prime_network: PrimeNetworkContract,
2219
pub compute_pool: ComputePool,
20+
pub stake_manager: Option<StakeManagerContract>,
2321
pub synthetic_data_validator: Option<SyntheticDataWorkValidator>,
2422
}
2523

@@ -29,6 +27,7 @@ pub struct ContractBuilder<'a> {
2927
ai_token: Option<AIToken>,
3028
prime_network: Option<PrimeNetworkContract>,
3129
compute_pool: Option<ComputePool>,
30+
stake_manager: Option<StakeManagerContract>,
3231
synthetic_data_validator: Option<SyntheticDataWorkValidator>,
3332
}
3433

@@ -40,6 +39,7 @@ impl<'a> ContractBuilder<'a> {
4039
ai_token: None,
4140
prime_network: None,
4241
compute_pool: None,
42+
stake_manager: None,
4343
synthetic_data_validator: None,
4444
}
4545
}
@@ -76,6 +76,11 @@ impl<'a> ContractBuilder<'a> {
7676
self
7777
}
7878

79+
pub fn with_stake_manager(mut self) -> Self {
80+
self.stake_manager = Some(StakeManagerContract::new(self.wallet, "stake_manager.json"));
81+
self
82+
}
83+
7984
// TODO: This is not ideal yet - now you have to init all contracts all the time
8085
pub fn build(self) -> Result<Contracts, ContractError> {
8186
// Using custom error ContractError
@@ -101,6 +106,7 @@ impl<'a> ContractBuilder<'a> {
101106
None => return Err(ContractError::Other("PrimeNetwork not initialized".into())), // Custom error handling
102107
},
103108
synthetic_data_validator: self.synthetic_data_validator,
109+
stake_manager: self.stake_manager,
104110
})
105111
}
106112
}

shared/src/web3/contracts/core/contract.rs

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ impl Contract {
4545
"synthetic_data_work_validator.json" => {
4646
include_abi!("../../../../artifacts/abi/synthetic_data_work_validator.json")
4747
}
48+
"stake_manager.json" => include_abi!("../../../../artifacts/abi/stake_manager.json"),
4849
_ => panic!("Unknown ABI file: {}", path),
4950
};
5051

shared/src/web3/contracts/implementations/compute_registry_contract.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::{
55
use crate::web3::contracts::helpers::utils::get_selector;
66
use crate::web3::wallet::Wallet;
77
use alloy::dyn_abi::DynSolValue;
8-
use alloy::primitives::Address;
8+
use alloy::primitives::{Address, U256};
99

1010
pub struct ComputeRegistryContract {
1111
instance: Contract,
@@ -41,6 +41,20 @@ impl ComputeRegistryContract {
4141
Ok(provider)
4242
}
4343

44+
pub async fn get_provider_total_compute(
45+
&self,
46+
address: Address,
47+
) -> Result<U256, Box<dyn std::error::Error>> {
48+
let provider_response = self
49+
.instance
50+
.instance()
51+
.function("getProviderTotalCompute", &[address.into()])?
52+
.call()
53+
.await?;
54+
55+
Ok(U256::from(provider_response.first().unwrap().as_uint().unwrap().0))
56+
}
57+
4458
pub async fn get_node(
4559
&self,
4660
#[allow(unused_variables)] provider_address: Address,

shared/src/web3/contracts/implementations/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ pub mod compute_pool_contract;
33
pub mod compute_registry_contract;
44
pub mod prime_network_contract;
55
pub mod work_validators;
6+
pub mod stake_manager;

shared/src/web3/contracts/implementations/prime_network_contract.rs

+16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ impl PrimeNetworkContract {
3232
Ok(register_tx)
3333
}
3434

35+
pub async fn stake(
36+
&self,
37+
additional_stake: U256,
38+
) -> Result<FixedBytes<32>, Box<dyn std::error::Error>> {
39+
let stake_tx = self
40+
.instance
41+
.instance()
42+
.function("increaseStake", &[additional_stake.into()])?
43+
.send()
44+
.await?
45+
.watch()
46+
.await?;
47+
48+
Ok(stake_tx)
49+
}
50+
3551
pub async fn add_compute_node(
3652
&self,
3753
node_address: Address,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
use crate::web3::contracts::constants::addresses::STAKE_MANAGER_ADDRESS;
2+
use crate::web3::contracts::core::contract::Contract;
3+
use crate::web3::wallet::Wallet;
4+
use alloy::primitives::{Address, U256};
5+
6+
pub struct StakeManagerContract {
7+
instance: Contract,
8+
}
9+
10+
impl StakeManagerContract {
11+
pub fn new(wallet: &Wallet, abi_file_path: &str) -> Self {
12+
let instance = Contract::new(STAKE_MANAGER_ADDRESS, wallet, abi_file_path);
13+
Self { instance }
14+
}
15+
16+
pub async fn get_stake_minimum(&self) -> Result<U256, Box<dyn std::error::Error>> {
17+
let result = self
18+
.instance
19+
.instance()
20+
.function("getStakeMinimum", &[])?
21+
.call()
22+
.await?;
23+
24+
let minimum: U256 = result
25+
.into_iter()
26+
.next()
27+
.map(|value| value.as_uint().unwrap_or_default())
28+
.unwrap_or_default()
29+
.0;
30+
Ok(minimum)
31+
}
32+
pub async fn get_stake(&self, staker: Address) -> Result<U256, Box<dyn std::error::Error>> {
33+
let result = self
34+
.instance
35+
.instance()
36+
.function("getStake", &[staker.into()])?
37+
.call()
38+
.await?;
39+
println!("Result: {:?}", result);
40+
41+
Ok(result[0].as_uint().unwrap_or_default().0)
42+
}
43+
44+
pub async fn calculate_stake(
45+
&self,
46+
compute_units: U256,
47+
provider_total_compute: U256,
48+
) -> Result<U256, Box<dyn std::error::Error>> {
49+
let min_stake_per_unit = self.get_stake_minimum().await?;
50+
let total_compute = provider_total_compute + compute_units;
51+
let required_stake = total_compute * min_stake_per_unit;
52+
Ok(required_stake)
53+
}
54+
}

0 commit comments

Comments
 (0)