use async_trait::async_trait;
use hex_literal::hex;
use primitive_types::H160;
use serde::{Deserialize, Serialize};
use crate::{
builder::{AccountSigner, TransactionBuilder},
neo_clients::{JsonRpcProvider, RpcClient},
neo_contract::{ContractError, SmartContractTrait},
neo_protocol::Account,
};
use neo3::prelude::*;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NeoCompoundContract<'a, P: JsonRpcProvider> {
#[serde(deserialize_with = "deserialize_script_hash")]
#[serde(serialize_with = "serialize_script_hash")]
script_hash: ScriptHash,
#[serde(skip)]
provider: Option<&'a RpcClient<P>>,
}
impl<'a, P: JsonRpcProvider + 'static> NeoCompoundContract<'a, P> {
pub const CONTRACT_HASH: &'static str = "f0151f528127558851b39c2cd8aa47da7418ab28";
pub const DEPOSIT: &'static str = "deposit";
pub const WITHDRAW: &'static str = "withdraw";
pub const COMPOUND: &'static str = "compound";
pub const GET_APY: &'static str = "getAPY";
pub fn new(provider: Option<&'a RpcClient<P>>) -> Self {
Self {
script_hash: ScriptHash::from(hex!("f0151f528127558851b39c2cd8aa47da7418ab28")),
provider,
}
}
pub fn with_script_hash(script_hash: ScriptHash, provider: Option<&'a RpcClient<P>>) -> Self {
Self { script_hash, provider }
}
pub async fn deposit(
&self,
token: &ScriptHash,
amount: i64,
account: &Account,
) -> Result<TransactionBuilder<'_, P>, ContractError> {
let params = vec![token.into(), ContractParameter::integer(amount)];
let mut builder = self.invoke_function(Self::DEPOSIT, params).await?;
let signer = AccountSigner::called_by_entry(account)
.map_err(|err| ContractError::RuntimeError(err.to_string()))?;
builder
.set_signers(vec![signer.into()])
.map_err(|err| ContractError::RuntimeError(err.to_string()))?;
Ok(builder)
}
pub async fn withdraw(
&self,
token: &ScriptHash,
amount: i64,
account: &Account,
) -> Result<TransactionBuilder<'_, P>, ContractError> {
let params = vec![token.into(), ContractParameter::integer(amount)];
let mut builder = self.invoke_function(Self::WITHDRAW, params).await?;
let signer = AccountSigner::called_by_entry(account)
.map_err(|err| ContractError::RuntimeError(err.to_string()))?;
builder
.set_signers(vec![signer.into()])
.map_err(|err| ContractError::RuntimeError(err.to_string()))?;
Ok(builder)
}
pub async fn compound(
&self,
token: &ScriptHash,
account: &Account,
) -> Result<TransactionBuilder<'_, P>, ContractError> {
let params = vec![token.into()];
let mut builder = self.invoke_function(Self::COMPOUND, params).await?;
let signer = AccountSigner::called_by_entry(account)
.map_err(|err| ContractError::RuntimeError(err.to_string()))?;
builder
.set_signers(vec![signer.into()])
.map_err(|err| ContractError::RuntimeError(err.to_string()))?;
Ok(builder)
}
pub async fn get_apy(&self, token: &ScriptHash) -> Result<f64, ContractError> {
let result = self.call_function_returning_int(Self::GET_APY, vec![token.into()]).await?;
Ok(result as f64 / 100.0) }
}
#[async_trait]
impl<'a, P: JsonRpcProvider> SmartContractTrait<'a> for NeoCompoundContract<'a, P> {
type P = P;
fn script_hash(&self) -> H160 {
self.script_hash
}
fn set_script_hash(&mut self, script_hash: H160) {
self.script_hash = script_hash;
}
fn provider(&self) -> Option<&RpcClient<P>> {
self.provider
}
}