neo3/neo_contract/famous/
flamingo.rs

1use 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/// Flamingo Finance contract interface for Neo N3
15///
16/// Flamingo Finance is a DeFi platform on Neo N3 offering trading, earning, and borrowing services.
17/// This contract interface provides methods to interact with the Flamingo Finance smart contract.
18#[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	/// The script hash of the Flamingo Finance contract on Neo N3 MainNet
29	pub const CONTRACT_HASH: &'static str = "f970f4cddcd087ab5d8a5697a32b3cfd32c8b465";
30
31	// Method constants
32	/// Method name for swapping tokens
33	pub const SWAP: &'static str = "swap";
34	/// Method name for adding liquidity
35	pub const ADD_LIQUIDITY: &'static str = "addLiquidity";
36	/// Method name for removing liquidity
37	pub const REMOVE_LIQUIDITY: &'static str = "removeLiquidity";
38	/// Method name for staking tokens
39	pub const STAKE: &'static str = "stake";
40	/// Method name for claiming rewards
41	pub const CLAIM_REWARDS: &'static str = "claimRewards";
42
43	/// Creates a new FlamingoContract instance with the default contract hash
44	///
45	/// # Arguments
46	///
47	/// * `provider` - An optional reference to an RPC client
48	///
49	/// # Returns
50	///
51	/// A new FlamingoContract instance
52	pub fn new(provider: Option<&'a RpcClient<P>>) -> Self {
53		Self { script_hash: ScriptHash::from_str(Self::CONTRACT_HASH).unwrap(), provider }
54	}
55
56	/// Creates a new FlamingoContract instance with a custom script hash
57	///
58	/// # Arguments
59	///
60	/// * `script_hash` - The script hash of the Flamingo Finance contract
61	/// * `provider` - An optional reference to an RPC client
62	///
63	/// # Returns
64	///
65	/// A new FlamingoContract instance
66	pub fn with_script_hash(script_hash: ScriptHash, provider: Option<&'a RpcClient<P>>) -> Self {
67		Self { script_hash, provider }
68	}
69
70	/// Swaps tokens on Flamingo Finance
71	///
72	/// # Arguments
73	///
74	/// * `from_token` - The script hash of the token to swap from
75	/// * `to_token` - The script hash of the token to swap to
76	/// * `amount` - The amount of tokens to swap
77	/// * `min_return` - The minimum amount of tokens to receive
78	/// * `account` - The account that will sign the transaction
79	///
80	/// # Returns
81	///
82	/// A transaction builder that can be used to build and sign the transaction
83	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	/// Adds liquidity to a Flamingo Finance liquidity pool
105	///
106	/// # Arguments
107	///
108	/// * `token_a` - The script hash of the first token
109	/// * `token_b` - The script hash of the second token
110	/// * `amount_a` - The amount of the first token to add
111	/// * `amount_b` - The amount of the second token to add
112	/// * `account` - The account that will sign the transaction
113	///
114	/// # Returns
115	///
116	/// A transaction builder that can be used to build and sign the transaction
117	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	/// Removes liquidity from a Flamingo Finance liquidity pool
139	///
140	/// # Arguments
141	///
142	/// * `token_a` - The script hash of the first token
143	/// * `token_b` - The script hash of the second token
144	/// * `liquidity` - The amount of liquidity tokens to remove
145	/// * `account` - The account that will sign the transaction
146	///
147	/// # Returns
148	///
149	/// A transaction builder that can be used to build and sign the transaction
150	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	/// Stakes tokens on Flamingo Finance
166	///
167	/// # Arguments
168	///
169	/// * `token` - The script hash of the token to stake
170	/// * `amount` - The amount of tokens to stake
171	/// * `account` - The account that will sign the transaction
172	///
173	/// # Returns
174	///
175	/// A transaction builder that can be used to build and sign the transaction
176	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	/// Claims rewards from Flamingo Finance
191	///
192	/// # Arguments
193	///
194	/// * `account` - The account that will sign the transaction
195	///
196	/// # Returns
197	///
198	/// A transaction builder that can be used to build and sign the transaction
199	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}