neo3/neo_contract/famous/
flamingo.rs1use async_trait::async_trait;
2use primitive_types::H160;
3use serde::{Deserialize, Serialize};
4use std::str::FromStr;
5
6use crate::{
7 builder::{AccountSigner, TransactionBuilder},
8 neo_clients::{APITrait, JsonRpcProvider, RpcClient},
9 neo_contract::{ContractError, SmartContractTrait},
10 neo_protocol::Account,
11};
12use neo3::prelude::*;
13
14#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct FlamingoContract<'a, P: JsonRpcProvider> {
20 #[serde(deserialize_with = "deserialize_script_hash")]
21 #[serde(serialize_with = "serialize_script_hash")]
22 script_hash: ScriptHash,
23 #[serde(skip)]
24 provider: Option<&'a RpcClient<P>>,
25}
26
27impl<'a, P: JsonRpcProvider + 'static> FlamingoContract<'a, P> {
28 pub const CONTRACT_HASH: &'static str = "f970f4cddcd087ab5d8a5697a32b3cfd32c8b465";
30
31 pub const SWAP: &'static str = "swap";
34 pub const ADD_LIQUIDITY: &'static str = "addLiquidity";
36 pub const REMOVE_LIQUIDITY: &'static str = "removeLiquidity";
38 pub const STAKE: &'static str = "stake";
40 pub const CLAIM_REWARDS: &'static str = "claimRewards";
42
43 pub fn new(provider: Option<&'a RpcClient<P>>) -> Self {
53 Self { script_hash: ScriptHash::from_str(Self::CONTRACT_HASH).unwrap(), provider }
54 }
55
56 pub fn with_script_hash(script_hash: ScriptHash, provider: Option<&'a RpcClient<P>>) -> Self {
67 Self { script_hash, provider }
68 }
69
70 pub async fn swap(
84 &self,
85 from_token: &ScriptHash,
86 to_token: &ScriptHash,
87 amount: i64,
88 min_return: i64,
89 account: &Account,
90 ) -> Result<TransactionBuilder<P>, ContractError> {
91 let params = vec![
92 from_token.into(),
93 to_token.into(),
94 ContractParameter::integer(amount),
95 ContractParameter::integer(min_return),
96 ];
97
98 let mut builder = self.invoke_function(Self::SWAP, params).await?;
99 builder.set_signers(vec![AccountSigner::called_by_entry(account).unwrap().into()]);
100
101 Ok(builder)
102 }
103
104 pub async fn add_liquidity(
118 &self,
119 token_a: &ScriptHash,
120 token_b: &ScriptHash,
121 amount_a: i64,
122 amount_b: i64,
123 account: &Account,
124 ) -> Result<TransactionBuilder<P>, ContractError> {
125 let params = vec![
126 token_a.into(),
127 token_b.into(),
128 ContractParameter::integer(amount_a),
129 ContractParameter::integer(amount_b),
130 ];
131
132 let mut builder = self.invoke_function(Self::ADD_LIQUIDITY, params).await?;
133 builder.set_signers(vec![AccountSigner::called_by_entry(account).unwrap().into()]);
134
135 Ok(builder)
136 }
137
138 pub async fn remove_liquidity(
151 &self,
152 token_a: &ScriptHash,
153 token_b: &ScriptHash,
154 liquidity: i64,
155 account: &Account,
156 ) -> Result<TransactionBuilder<P>, ContractError> {
157 let params = vec![token_a.into(), token_b.into(), ContractParameter::integer(liquidity)];
158
159 let mut builder = self.invoke_function(Self::REMOVE_LIQUIDITY, params).await?;
160 builder.set_signers(vec![AccountSigner::called_by_entry(account).unwrap().into()]);
161
162 Ok(builder)
163 }
164
165 pub async fn stake(
177 &self,
178 token: &ScriptHash,
179 amount: i64,
180 account: &Account,
181 ) -> Result<TransactionBuilder<P>, ContractError> {
182 let params = vec![token.into(), ContractParameter::integer(amount)];
183
184 let mut builder = self.invoke_function(Self::STAKE, params).await?;
185 builder.set_signers(vec![AccountSigner::called_by_entry(account).unwrap().into()]);
186
187 Ok(builder)
188 }
189
190 pub async fn claim_rewards(
200 &self,
201 account: &Account,
202 ) -> Result<TransactionBuilder<P>, ContractError> {
203 let params = vec![];
204
205 let mut builder = self.invoke_function(Self::CLAIM_REWARDS, params).await?;
206 builder.set_signers(vec![AccountSigner::called_by_entry(account).unwrap().into()]);
207
208 Ok(builder)
209 }
210}
211
212#[async_trait]
213impl<'a, P: JsonRpcProvider> SmartContractTrait<'a> for FlamingoContract<'a, P> {
214 type P = P;
215
216 fn script_hash(&self) -> H160 {
217 self.script_hash
218 }
219
220 fn set_script_hash(&mut self, script_hash: H160) {
221 self.script_hash = script_hash;
222 }
223
224 fn provider(&self) -> Option<&RpcClient<P>> {
225 self.provider
226 }
227}