Skip to content

Commit

Permalink
update to 0.27.0 rip
Browse files Browse the repository at this point in the history
  • Loading branch information
Gianmarco Fraccaroli committed Nov 22, 2023
1 parent d6dfa64 commit b42b393
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ rand = "0.8.5"
rand_chacha = "0.3.1"
dotenvy = "0.15.7"
hex = "0.4.3"
namada_sdk = { git = "https://github.com/anoma/namada.git", rev = "v0.27.0", default-features = false, features = ["tendermint-rpc", "std"] }
namada_sdk = { git = "https://github.com/anoma/namada", branch = "fraccaman/sdk-plus-send", default-features = false, features = ["tendermint-rpc", "std", "async-send"] }
borsh-derive = { version = "=1.0.0-alpha.4" }
tendermint-config = "0.34.0"
tendermint-rpc = { version = "0.34.0", features = ["http-client"]}
Expand Down
6 changes: 3 additions & 3 deletions scripts/faucet.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from requests import get, post
from hashlib import sha256

URL = "http://0.0.0.0:5001/api/v1/faucet"
URL = "http://0.0.0.0:5000/api/v1/faucet"

def request_challenge():
return get(URL).json()
Expand Down Expand Up @@ -38,8 +38,8 @@ def compute_pow_solution(challenge, difficulty):
'tag': response['tag'],
'challenge': response['challenge'],
'transfer': {
'target': 'atest1d9khqw36gscrxdenggu5yw2ygsc5zwf3g9p5gde3xgc5z335gs6rjvpnx5erw33s8yuyxwfhu3xktm',
'token': 'atest1v4ehgw36x3prswzxggunzv6pxqmnvdj9xvcyzvpsggeyvs3cg9qnywf589qnwvfsg5erg3fkl09rg5',
'target': 'tnam1qyf2tv9l8w7hfu5glr72chzz5zjknywjugnvh34x',
'token': 'tnam1qyxg9d8zr4su9ks3a368485kjjr3e880kysz076g',
'amount': 100 * 10**6
}
})
Expand Down
4 changes: 3 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::{
net::{Ipv4Addr, SocketAddr},
str::FromStr,
sync::{Arc, RwLock},
sync::{Arc},
time::Duration,
};

use tokio::sync::RwLock;

use axum::{
error_handling::HandleErrorLayer,
http::{HeaderValue, StatusCode},
Expand Down
82 changes: 41 additions & 41 deletions src/handler/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub async fn request_challenge(
Ok(Json(response))
}

// #[debug_handler]
#[debug_handler]
pub async fn request_transfer(
State(mut state): State<FaucetState>,
ValidatedRequest(payload): ValidatedRequest<FaucetRequestDto>,
Expand All @@ -48,7 +48,7 @@ pub async fn request_transfer(
return Err(FaucetError::InvalidAddress.into());
};

if state.faucet_repo.contains(&payload.challenge) {
if state.faucet_repo.contains(&payload.challenge).await {
return Err(FaucetError::DuplicateChallenge.into());
}

Expand All @@ -68,49 +68,49 @@ pub async fn request_transfer(
return Err(FaucetError::InvalidPoW.into());
}

let faucet_key_lock = state.sdk.read().unwrap();
let faucet_key_lock = state.sdk.read().await;
let faucet_key = faucet_key_lock.faucet_sk.clone();
drop(faucet_key_lock);
let mut sdk_lock = state.sdk.write().unwrap();
let mut sdk_lock = state.sdk.write().await;
let sdk = sdk_lock.namada_ctx().await;

// let nam_address = sdk.native_token();
// let faucet_pk = faucet_key.to_public();
// let faucet_address = Address::from(&faucet_pk);

// let mut transfer_tx_builder = sdk.new_transfer(
// TransferSource::Address(faucet_address),
// TransferTarget::Address(target_address),
// token_address.clone(),
// InputAmount::Unvalidated(DenominatedAmount::native(token::Amount::from_u64(
// payload.transfer.amount,
// ))),
// );

// let (mut transfer_tx, signing_data, _epoch) = transfer_tx_builder
// .build(&sdk)
// .await
// .expect("unable to build transfer");
// sdk.sign(&mut transfer_tx, &transfer_tx_builder.tx, signing_data, default_sign)
// .await
// .expect("unable to sign reveal pk tx");
// let process_tx_response = sdk
// .submit(transfer_tx, &transfer_tx_builder.tx)
// .await;

// let transfer_result = if let Ok(response) = process_tx_response {
// match response {
// namada_sdk::tx::ProcessTxResponse::Applied(r) => r.code.eq(&"0"),
// namada_sdk::tx::ProcessTxResponse::Broadcast(r) => r.code.eq(&Code::Ok),
// _ => false,
// }
// } else {
// false
// };

// if transfer_result {
// state.faucet_repo.add(payload.challenge.clone());
// }
let nam_address = sdk.native_token();
let faucet_pk = faucet_key.to_public();
let faucet_address = Address::from(&faucet_pk);

let mut transfer_tx_builder = sdk.new_transfer(
TransferSource::Address(faucet_address),
TransferTarget::Address(target_address),
token_address.clone(),
InputAmount::Unvalidated(DenominatedAmount::native(token::Amount::from_u64(
payload.transfer.amount,
))),
);

let (mut transfer_tx, signing_data, _epoch) = transfer_tx_builder
.build(&sdk)
.await
.expect("unable to build transfer");
sdk.sign(&mut transfer_tx, &transfer_tx_builder.tx, signing_data, default_sign)
.await
.expect("unable to sign reveal pk tx");
let process_tx_response = sdk
.submit(transfer_tx, &transfer_tx_builder.tx)
.await;

let transfer_result = if let Ok(response) = process_tx_response {
match response {
namada_sdk::tx::ProcessTxResponse::Applied(r) => r.code.eq(&"0"),
namada_sdk::tx::ProcessTxResponse::Broadcast(r) => r.code.eq(&Code::Ok),
_ => false,
}
} else {
false
};

if transfer_result {
state.faucet_repo.add(payload.challenge.clone());
}

let response = FaucetResponseStatusDto {
token: payload.transfer.token.clone(),
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::sync::{Arc, RwLock};
use std::sync::{Arc};
use tokio::sync::RwLock;

use anyhow::Context;
use clap::Parser;
Expand Down
15 changes: 8 additions & 7 deletions src/repository/faucet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::sync::{Arc, RwLock};
use std::sync::{Arc};
use tokio::sync::RwLock;

use async_trait::async_trait;

Expand All @@ -12,8 +13,8 @@ pub struct FaucetRepository {
#[async_trait]
pub trait FaucetRepositoryTrait {
fn new(data: &Arc<RwLock<AppState>>) -> Self;
fn add(&mut self, challenge: String);
fn contains(&self, challenge: &str) -> bool;
async fn add(&mut self, challenge: String);
async fn contains(&self, challenge: &str) -> bool;
}

#[async_trait]
Expand All @@ -22,13 +23,13 @@ impl FaucetRepositoryTrait for FaucetRepository {
Self { data: data.clone() }
}

fn add(&mut self, challenge: String) {
let mut state = self.data.write().expect("Should be able to lock appstate.");
async fn add(&mut self, challenge: String) {
let mut state = self.data.write().await;
state.add(challenge)
}

fn contains(&self, challenge: &str) -> bool {
let state = self.data.read().expect("Should be able to lock appstate.");
async fn contains(&self, challenge: &str) -> bool {
let state = self.data.read().await;
state.contains(&challenge.to_string())
}
}
17 changes: 10 additions & 7 deletions src/sdk/namada.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ use namada_sdk::{
wallet::{fs::FsWalletUtils, Wallet, WalletIo},
NamadaImpl,
};
use rand_chacha::rand_core::OsRng;
use tendermint_rpc::{HttpClient, Url};

pub struct Sdk {
pub faucet_sk: SecretKey,
rpc_client: HttpClient,
wallet: Wallet<FsWalletUtils>,
shielded_ctx: ShieldedContext<FsShieldedUtils>,
io: NullIo
io: NullIo,
}

impl Sdk {
Expand All @@ -29,23 +28,27 @@ impl Sdk {
rpc_client: HttpClient,
wallet: Wallet<FsWalletUtils>,
shielded_ctx: ShieldedContext<FsShieldedUtils>,
io: NullIo
io: NullIo,
) -> Self {
Self {
faucet_sk,
rpc_client,
wallet,
shielded_ctx,
io: NullIo
io: NullIo,
}
}

pub async fn namada_ctx(&mut self) -> NamadaImpl<'_, HttpClient, FsWalletUtils, FsShieldedUtils, NullIo> {
pub async fn namada_ctx(
&mut self,
) -> NamadaImpl<'_, HttpClient, FsWalletUtils, FsShieldedUtils, NullIo> {
NamadaImpl::new(
&self.rpc_client,
&mut self.wallet,
&mut self.shielded_ctx,
&self.io,
).await.unwrap()
)
.await
.unwrap()
}
}
}
3 changes: 2 additions & 1 deletion src/services/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use rand::SeedableRng;
use rand_chacha::rand_core::OsRng;
use rand_chacha::ChaCha20Core;

use std::sync::{Arc, RwLock};
use std::sync::{Arc};
use tokio::sync::RwLock;

use crate::{
app_state::AppState, entity::faucet::Faucet, error::api::ApiError,
Expand Down
3 changes: 2 additions & 1 deletion src/state/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use crate::{
repository::faucet::FaucetRepositoryTrait,
services::faucet::FaucetService, sdk::namada::Sdk,
};
use std::sync::{Arc, RwLock, Mutex};
use std::sync::{Arc};
use tokio::sync::RwLock;

#[derive(Clone)]
pub struct FaucetState {
Expand Down

0 comments on commit b42b393

Please sign in to comment.