1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use crate::{
    account::SuiAccount,
    client::Client,
    network::Network,
    print_beauty,
    utils::{self, current_timestamp},
};
use serde_json::Value;

const GAS_EXPIRED_MS: u64 = 300_000;

struct UpdateGas {
    gas_object: String,
    expire_at: u64,
}

impl UpdateGas {
    pub fn expired(&self) -> bool {
        print_beauty!("\n{}\n{}\n", self.expire_at, current_timestamp());
        self.expire_at <= current_timestamp()
    }
}

impl Default for UpdateGas {
    fn default() -> Self {
        Self {
            gas_object: String::from(""),
            expire_at: 0,
        }
    }
}

pub struct HookCaller<'a> {
    target: Target,
    account: SuiAccount,
    client: &'a Client<'a>,
    gas: UpdateGas,
}

pub struct Target {
    package: String,
    module: String,
    fun_name: String,
}

impl Target {
    pub fn new(package: String, module: String, fun_name: String) -> Self {
        Self {
            package,
            module,
            fun_name,
        }
    }
}

impl Default for Target {
    fn default() -> Self {
        Self::new(String::from(""), String::from(""), String::from(""))
    }
}

impl<'a> HookCaller<'a> {
    pub fn get_network(&self) -> &Network {
        self.client.network
    }
    pub async fn call(&mut self, type_arguments: Vec<String>, arguments: Vec<Value>) {
        self.update_gas().await;
        print_beauty!("you will call sui network : ");

        let result = self
            .client
            .unsafe_move_call(
                self.account.to_address(),
                self.target.package.to_string(),
                self.target.module.to_string(),
                self.target.fun_name.to_string(),
                type_arguments,
                arguments,
                self.gas.gas_object.to_string(),
                utils::ADVISE_GAS_BUDGET,
            )
            .await
            .unwrap();

        let effet = result
            .result
            .with_signed_execute(&self.client, &self.account)
            .await
            .unwrap();
        print_beauty!(
            "transaction goes : {}",
            self.client.network.transaction_link(&effet.result.digest)
        );
    }

    async fn update_gas(&mut self) {
        if self.gas.expired() {
            print_beauty!("now update gas!!!!");
            match self
                .client
                .get_avaliable_gas(self.account.to_address(), utils::ADVISE_GAS_BUDGET)
                .await
            {
                Err(err) => {
                    print_beauty!(
                        "gas error {} , with address: {}",
                        err,
                        self.account.to_address()
                    );
                    panic!();
                }
                Ok(gas_result) => {
                    self.gas.gas_object = gas_result.coin_object_id;
                    self.gas.expire_at = current_timestamp() + GAS_EXPIRED_MS;
                }
            }
        }
    }

    pub fn new(target: Target, account: SuiAccount, client: &'a Client) -> Self {
        Self {
            target,
            account,
            client,
            gas: UpdateGas::default(),
        }
    }
}