Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIL-227] Automatic allocator #225

Merged
merged 11 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,513 changes: 2,712 additions & 801 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion fplus-database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ serde = { version = "1.0.164", features = ["derive", "std",
"serde_derive", "alloc", "rc"] }
serial_test = "3.0.0"
sha1 = "0.10.6"
serde_json = "1.0.96"
serde_json = "1.0.96"
alloy = { git = "https://github.com/alloy-rs/alloy", version = "0.3.2", features = [
kacperzuk-neti marked this conversation as resolved.
Show resolved Hide resolved
"signers",
] }
sea-orm-newtype = "0.0.1"
11 changes: 11 additions & 0 deletions fplus-database/src/database/applications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,14 @@ pub async fn delete_application(
application.delete(&conn).await?;
Ok(())
}

pub async fn get_applications_by_client_id(
id: &String,
) -> Result<Vec<ApplicationModel>, sea_orm::DbErr> {
let conn = get_database_connection().await?;
let result = Application::find()
.filter(Column::Id.eq(id))
.all(&conn)
.await?;
Ok(result)
}
35 changes: 35 additions & 0 deletions fplus-database/src/database/autoallocations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::get_database_connection;
use crate::models::autoallocations::AddressWrapper;
use crate::models::autoallocations::{ActiveModel, Column, Entity as Autoallocations};
use chrono::{DateTime, FixedOffset, Utc};
use sea_orm::{entity::*, query::*, DbErr};

pub async fn get_last_client_autoallocation(
client_evm_address: impl Into<AddressWrapper>,
) -> Result<Option<DateTime<FixedOffset>>, DbErr> {
let conn = get_database_connection().await?;
let response = Autoallocations::find()
.filter(Column::EvmWalletAddress.contains(client_evm_address.into()))
.one(&conn)
.await?;

Ok(response.map(|allocation| allocation.last_allocation))
}

pub async fn create_or_update_autoallocation(
client_evm_address: impl Into<AddressWrapper> + Clone,
) -> Result<(), sea_orm::DbErr> {
let conn = get_database_connection().await?;
let last_autoallocation = get_last_client_autoallocation(client_evm_address.clone()).await?;

let autoallocation_active_model = ActiveModel {
evm_wallet_address: Set(client_evm_address.into()),
last_allocation: Set(Utc::now().into()),
};
if last_autoallocation.is_some() {
autoallocation_active_model.update(&conn).await?;
} else {
autoallocation_active_model.insert(&conn).await?;
}
Ok(())
}
1 change: 1 addition & 0 deletions fplus-database/src/database/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod allocation_amounts;
pub mod allocators;
pub mod applications;
pub mod autoallocations;
41 changes: 41 additions & 0 deletions fplus-database/src/models/autoallocations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use alloy::primitives::{Address, AddressError};
use chrono::{DateTime, FixedOffset};
use sea_orm::entity::prelude::*;
use sea_orm_newtype::DeriveNewType;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "autoallocations")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub evm_wallet_address: AddressWrapper,
pub last_allocation: DateTime<FixedOffset>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

#[derive(Clone, Debug, PartialEq, DeriveNewType, Eq, Serialize, Deserialize)]
#[sea_orm_newtype(try_from_into = "String", primary_key)]
pub struct AddressWrapper(pub Address);

impl TryFrom<String> for AddressWrapper {
type Error = AddressError;
fn try_from(value: String) -> Result<Self, Self::Error> {
Ok(AddressWrapper(Address::parse_checksummed(value, None)?))
}
}

impl From<AddressWrapper> for String {
fn from(value: AddressWrapper) -> Self {
value.0.to_checksum(None)
}
}

impl From<Address> for AddressWrapper {
fn from(value: Address) -> Self {
AddressWrapper(value)
}
}
1 change: 1 addition & 0 deletions fplus-database/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pub mod prelude;
pub mod allocation_amounts;
pub mod allocators;
pub mod applications;
pub mod autoallocations;
3 changes: 0 additions & 3 deletions fplus-http-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,3 @@ async-trait = "0.1.73"
uuidv4 = "1.0.0"
log = "0.4.20"
cron = "0.12.1"
alloy = { git = "https://github.com/alloy-rs/alloy", version = "0.1.0", features = [
"signers",
] }
2 changes: 2 additions & 0 deletions fplus-http-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ async fn main() -> std::io::Result<()> {
.service(router::allocator::delete)
.service(router::allocator::create_allocator_from_json)
.service(router::allocator::update_allocator_force)
.service(router::autoallocator::last_client_allocation)
.service(router::autoallocator::trigger_autoallocation)
// .service(router::allocator::get_installation_ids)
})
.bind(("0.0.0.0", 8080))?
Expand Down
24 changes: 24 additions & 0 deletions fplus-http-server/src/router/autoallocator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use actix_web::{get, post, web, HttpResponse, Responder};
use fplus_database::database::autoallocations as autoallocations_db;
use fplus_lib::core::autoallocator;
use fplus_lib::core::{LastAutoallocationQueryParams, TriggerAutoallocationInfo};

#[get("/autoallocator/last_client_allocation")]
pub async fn last_client_allocation(
query: web::Query<LastAutoallocationQueryParams>,
) -> impl Responder {
match autoallocations_db::get_last_client_autoallocation(query.evm_wallet_address).await {
Ok(last_client_allocation) => {
HttpResponse::Ok().body(serde_json::to_string_pretty(&last_client_allocation).unwrap())
}
Err(e) => HttpResponse::BadRequest().body(e.to_string()),
}
}

#[post("autoallocator/trigger_autoallocation")]
pub async fn trigger_autoallocation(info: web::Json<TriggerAutoallocationInfo>) -> impl Responder {
match autoallocator::trigger_autoallocation(&info.into_inner()).await {
Ok(()) => HttpResponse::Ok().body(serde_json::to_string_pretty("Success").unwrap()),
Err(e) => HttpResponse::BadRequest().body(e.to_string()),
}
}
1 change: 1 addition & 0 deletions fplus-http-server/src/router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use actix_web::{get, HttpResponse, Responder};

pub mod allocator;
pub mod application;
pub mod autoallocator;
pub mod blockchain;
pub mod verifier;

Expand Down
14 changes: 2 additions & 12 deletions fplus-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,10 @@ fplus-database = { path = "../fplus-database", version = "1.8.4"}
pem = "1.0"
anyhow = "1.0"
regex = "1.0"
alloy = { git = "https://github.com/alloy-rs/alloy", version = "0.1.0", features = [
"signers",
"providers",
"node-bindings",
"sol-types",
"json",
"network",
"rpc-types-eth",
"provider-http",
"dyn-abi",
"eip712",
] }
tempfile = "3.10.1"
size = "0.5.0-preview2"
alloy = { version = "0.3.2", features = ["full"] }
fvm = "4.4.0"

[dev-dependencies]
actix-rt = "2.9.0"
Expand Down
11 changes: 11 additions & 0 deletions fplus-lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ pub fn default_env_vars() -> &'static HashMap<&'static str, &'static str> {
m.insert("RPC_URL", "https://mainnet.optimism.io");
m.insert("DMOB_API_URL", "https://api.datacapstats.io/public/api");
m.insert("DMOB_API_KEY", "5c993a17-7b18-4ead-a8a8-89dad981d87e");
m.insert("DAYS_TO_NEXT_AUTOALLOCATION", "14");
m.insert("FILECOIN_RPC_URL", "http://127.0.0.1:8545");
kacperzuk-neti marked this conversation as resolved.
Show resolved Hide resolved
m.insert(
"ALLOCATOR_CONTRACT_ADDRESS",
"0x640bD4be149f40714D95aBcD414338bc7CfF39a3",
);
m.insert(
"PRIVATE_KEY",
kacperzuk-neti marked this conversation as resolved.
Show resolved Hide resolved
"257d553b55d5928c7ec48c2f7d1288931640166e2c1269e05492914accf7e966",
);
m.insert("AMOUNT_TO_AUTOALLOCATION", "68719476736"); // 68719476736 B == 64 GB
kacperzuk-neti marked this conversation as resolved.
Show resolved Hide resolved
m
})
}
Expand Down
37 changes: 35 additions & 2 deletions fplus-lib/src/core/application/gitcoin_interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ use crate::config::get_env_var_or_default;
use crate::error::LDNError;
use anyhow::Result;

pub trait ExpirableSolStruct: SolStruct {
fn get_expires_at(&self) -> String;
fn get_issued_at(&self) -> String;
kacperzuk-neti marked this conversation as resolved.
Show resolved Hide resolved
}

sol! {
#[allow(missing_docs)]
function getScore(address user) view returns (uint256);
Expand All @@ -28,6 +33,34 @@ sol! {
string issued_at;
string expires_at;
}

#[derive(Deserialize)]
struct KycAutoallocationApproval {
string message;
string client_fil_address;
string issued_at;
string expires_at;
}
}

impl ExpirableSolStruct for KycApproval {
fn get_expires_at(&self) -> String {
self.expires_at.clone()
}

fn get_issued_at(&self) -> String {
self.issued_at.clone()
}
}

impl ExpirableSolStruct for KycAutoallocationApproval {
fn get_expires_at(&self) -> String {
self.expires_at.clone()
}

fn get_issued_at(&self) -> String {
self.issued_at.clone()
}
}

pub async fn verify_on_gitcoin(address_from_signature: &Address) -> Result<f64, LDNError> {
Expand Down Expand Up @@ -73,8 +106,8 @@ fn calculate_score(response: Bytes) -> f64 {
score as f64 / 10000.0
}

pub fn get_address_from_signature(
message: &KycApproval,
pub fn get_address_from_signature<T: SolStruct>(
message: &T,
signature: &str,
) -> Result<Address, LDNError> {
let domain = eip712_domain! {
Expand Down
69 changes: 69 additions & 0 deletions fplus-lib/src/core/autoallocator/metaallocator_interaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::str::FromStr;

use crate::config::get_env_var_or_default;
use crate::error::LDNError;
use alloy::{
network::{EthereumWallet, TransactionBuilder},
primitives::{Address, Bytes, U256},
providers::{Provider, ProviderBuilder},
rpc::types::eth::TransactionRequest,
signers::local::PrivateKeySigner,
sol,
sol_types::SolCall,
};
use anyhow::Result;
use fvm::kernel::prelude::Address as FilecoinAddress;
sol! {
#[allow(missing_docs)]
function addVerifiedClient(bytes calldata clientAddress, uint256 amount);
}

pub async fn add_verified_client(address: &String, amount: &u64) -> Result<(), LDNError> {
let private_key = get_env_var_or_default("PRIVATE_KEY");
let signer: PrivateKeySigner = private_key.parse().expect("Should parse private key");
let wallet = EthereumWallet::from(signer);
let rpc_url = get_env_var_or_default("FILECOIN_RPC_URL")
.parse()
.map_err(|e| LDNError::New(format!("Failed to pase string to URL /// {}", e)))?;
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(wallet)
.on_http(rpc_url);
kacperzuk-neti marked this conversation as resolved.
Show resolved Hide resolved
let fil_address_bytes = get_filecoin_address_from_string_to_bytes(address)?;
kacperzuk-neti marked this conversation as resolved.
Show resolved Hide resolved
let amount = U256::try_from(amount.clone())
.map_err(|e| LDNError::New(format!("Failed to prase amount to U256 /// {}", e)))?;
let call = addVerifiedClientCall {
clientAddress: fil_address_bytes.into(),
amount,
}
.abi_encode();
let allocator_contract =
Address::parse_checksummed(get_env_var_or_default("ALLOCATOR_CONTRACT_ADDRESS"), None)
.map_err(|e| {
LDNError::New(format!(
"Parse ALLOCATOR_CONTRACT_ADDRESS to Address failed: {}",
e
))
})?;
let input = Bytes::from(call);

let tx = TransactionRequest::default()
.with_to(allocator_contract)
.with_input(input)
.with_gas_limit(45_000_000);

provider
.send_transaction(tx)
.await
.map_err(|e| LDNError::New(format!("RPC error: {}", e)))?
.watch()
.await
.map_err(|e| LDNError::New(format!("Transaction failed: {}", e)))?;
Ok(())
}

fn get_filecoin_address_from_string_to_bytes(address: &str) -> Result<Vec<u8>, LDNError> {
let fil_address = FilecoinAddress::from_str(address)
.map_err(|e| LDNError::New(format!("Failed to prase address from string /// {}", e)))?;
Ok(fil_address.to_bytes())
}
Loading