eigen_client_fireblocks/
contract_call.rs

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use crate::client::Client;
use crate::error::FireBlockError;
use crate::transaction::TransactionRequest;
use crate::transaction::TransactionResponse;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum TransactionOperation {
    ContractCall,
    Transfer,
    Mint,
    Burn,
    TypedMessage,
    Raw,
}

impl TransactionOperation {
    pub fn as_str(&self) -> &'static str {
        match self {
            TransactionOperation::ContractCall => "CONTRACT_CALL",
            TransactionOperation::Transfer => "TRANSFER",
            TransactionOperation::Mint => "MINT",
            TransactionOperation::Burn => "BURN",
            TransactionOperation::TypedMessage => "TYPED_MESSAGE",
            TransactionOperation::Raw => "RAW",
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Account {
    #[serde(rename = "type")]
    type_field: String,
    id: String,
}

impl std::fmt::Display for Account {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Account {{ id: {}, type: {} }}",
            self.id, self.type_field
        )
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub enum ExtraParameters {
    ContractCallData(String),
    RawMessageData(RawMessageData),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RawMessageData {
    pub messages: Vec<UnsignedMessage>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct UnsignedMessage {
    pub content: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct TransferPeerPath {
    #[serde(rename = "type")]
    pub peer_type: Option<PeerType>,
    pub id: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct DestinationTransferPeerPath {
    #[serde(rename = "type")]
    pub peer_type: PeerType,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub one_time_address: Option<OneTimeAddress>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct OneTimeAddress {
    pub address: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tag: Option<String>,
}

#[allow(non_camel_case_types)]
#[derive(Debug, Serialize, Deserialize, Clone)]
#[allow(clippy::upper_case_acronyms)]
pub enum PeerType {
    VAULT_ACCOUNT,
    EXCHANGE_ACCOUNT,
    INTERNAL_WALLET,
    EXTERNAL_WALLET,
    ONE_TIME_ADDRESS,
    NETWORK_CONNECTION,
    FIAT_ACCOUNT,
    COMPOUND,
    END_USER_WALLET,
    CONTRACT,
}

#[allow(async_fn_in_trait)]
pub trait ContractCall {
    async fn contract_call(
        &self,
        transaction_request: TransactionRequest,
    ) -> Result<TransactionResponse, FireBlockError>;
}

impl ContractCall for Client {
    async fn contract_call(
        &self,
        transaction_request: TransactionRequest,
    ) -> Result<TransactionResponse, FireBlockError> {
        let transaction_json =
            serde_json::to_string(&transaction_request).map_err(FireBlockError::SerdeError)?;

        // Call POST request
        let contract_call = self
            .post_request("/v1/transactions/", Some(&transaction_json.to_string()))
            .await?;
        serde_json::from_str(&contract_call).map_err(FireBlockError::SerdeError)
    }
}

#[cfg(test)]
mod tests {

    #[cfg(feature = "fireblock-tests")]
    use super::*;
    #[cfg(feature = "fireblock-tests")]
    use crate::client::AssetID;
    #[cfg(feature = "fireblock-tests")]
    use std::env;

    #[tokio::test]
    #[cfg(feature = "fireblock-tests")]
    async fn test_contract_call() {
        let api_key = env::var("FIREBLOCKS_API_KEY").expect("FIREBLOCKS_API_KEY not set");
        let private_key_path =
            env::var("FIREBLOCKS_PRIVATE_KEY_PATH").expect("FIREBLOCKS_PRIVATE_KEY_PATH not set");
        let api_url = env::var("FIREBLOCKS_API_URL").expect("FIREBLOCKS_API_URL not set");
        let private_key =
            std::fs::read_to_string(private_key_path).expect("Failed to read private key file");

        let client = Client::new(
            api_key.to_string(),
            private_key.to_string(),
            api_url.clone(),
        );
        let source = TransferPeerPath {
            peer_type: Some(PeerType::VAULT_ACCOUNT),
            id: Some("13".to_string()),
        };
        let destination = DestinationTransferPeerPath {
            peer_type: PeerType::ONE_TIME_ADDRESS,
            one_time_address: Some(OneTimeAddress {
                address: "0x43506849D7C04F9138D1A2050bbF3A0c054402dd".to_string(),
                tag: None,
            }),
            id: None,
        };
        let amount = "0";
        let extra_params = ExtraParameters::ContractCallData("0x095ea7b3000000000000000000000000d09971d8ed6c6a5e57581e90d593ee5b94e348d4ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff".to_string());
        let tx_request = TransactionRequest::new(
            TransactionOperation::ContractCall,
            None,
            AssetID::EthTest5.to_string(),
            source,
            Some(destination),
            amount.to_string(),
            Some(extra_params),
            None,
            None,
            None,
            None,
            None,
            None,
        );

        let _ = client.contract_call(tx_request).await.unwrap();
    }
}