aptos_network_sdk/
staking.rs

1use crate::{Aptos, types::ContractCall, wallet::Wallet};
2use serde_json::{Value, json};
3use std::sync::Arc;
4
5/// Implementation of the staking function for the aptos system.
6pub struct SystemStaking;
7
8impl SystemStaking {
9    /// stake $apt
10    pub async fn stake(
11        client: Arc<Aptos>,
12        wallet: Arc<Wallet>,
13        amount: u64,
14    ) -> Result<Value, String> {
15        let contract_call = ContractCall {
16            module_address: "0x1".to_string(),
17            module_name: "staking_contract".to_string(),
18            function_name: "stake".to_string(),
19            type_arguments: vec![],
20            arguments: vec![json!(amount.to_string())],
21        };
22        crate::contract::Contract::write(client, wallet, contract_call)
23            .await
24            .map(|result| json!(result))
25    }
26
27    /// unstake
28    pub async fn unstake(
29        client: Arc<Aptos>,
30        wallet: Arc<Wallet>,
31        amount: u64,
32    ) -> Result<Value, String> {
33        let contract_call = ContractCall {
34            module_address: "0x1".to_string(),
35            module_name: "staking_contract".to_string(),
36            function_name: "unstake".to_string(),
37            type_arguments: vec![],
38            arguments: vec![json!(amount.to_string())],
39        };
40        crate::contract::Contract::write(client, wallet, contract_call)
41            .await
42            .map(|result| json!(result))
43    }
44
45    /// claim staking rewards
46    pub async fn claim(client: Arc<Aptos>, wallet: Arc<Wallet>) -> Result<Value, String> {
47        let contract_call = ContractCall {
48            module_address: "0x1".to_string(),
49            module_name: "staking_contract".to_string(),
50            function_name: "claim_rewards".to_string(),
51            type_arguments: vec![],
52            arguments: vec![],
53        };
54        crate::contract::Contract::write(client, wallet, contract_call)
55            .await
56            .map(|result| json!(result))
57    }
58
59    /// get staking info
60    pub async fn get_staking_info(
61        client: Arc<Aptos>,
62        address: &str,
63    ) -> Result<Value, String> {
64        let resource_type = "0x1::staking_contract::StakingInfo";
65        client
66            .get_account_resource(address, resource_type)
67            .await
68            .map(|opt| opt.map(|r| r.data).unwrap_or(Value::Null))
69            .map_err(|e| e.to_string())
70    }
71}