Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayush-Yadav committed Jan 29, 2024
1 parent 3208989 commit 838b64f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 18 deletions.
12 changes: 6 additions & 6 deletions src/billing_job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::model::AppState;

use actix_web::web::Data;
use anyhow::{anyhow, Context, Error};
use ethers::prelude::*;
use ethers::{abi::Token, prelude::*};
use k256::elliptic_curve::generic_array::sequence::Lengthen;
use tiny_keccak::{Hasher, Keccak};

Expand All @@ -19,9 +19,9 @@ pub async fn billing_scheduler(appstate: Data<AppState>,) -> Result<String, Erro

let client = SignerMiddleware::new(provider, wallet);
let contract = Contract::new(
appstate.contract.as_str()
.parse::<Address>()
.context("Unable to parse contract address")?,
appstate.billing_contract.as_str()
.parse::<Address>()
.context("Unable to parse contract address")?,
appstate.abi.to_owned(),
Arc::new(client));

Expand Down Expand Up @@ -50,13 +50,13 @@ pub async fn billing_scheduler(appstate: Data<AppState>,) -> Result<String, Erro

let tx_request = contract.method::<_, H256>(
"settle",
(tx_hashes, amounts, signature.as_bytes().to_vec()))
(tx_hashes, amounts, Token::Bytes(signature.into_bytes())))
.context("failed to build transaction request for billing")?;
let pending_tx = tx_request
.send()
.await
.context("Error while sending the billing transaction")?;

let tx_receipt = pending_tx
.confirmations(7)
.await
Expand Down
3 changes: 2 additions & 1 deletion src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub async fn serverless(
workerd_runtime_path,
&appstate.rpc,
&appstate.contract,
&appstate.billing_contract,
&appstate.abi,
)
.await
Expand Down Expand Up @@ -279,6 +280,6 @@ pub async fn serverless(
*amount += execution_cost;

appstate.billing_hasher.lock().await.update(&hash);

return response;
}
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ struct Args {
)]
contract: String,

#[clap(long, value_parser)]
billing_contract: String,

#[clap(long, value_parser)]
operator_wallet_key: String,

Expand Down Expand Up @@ -87,6 +90,7 @@ async fn main() -> anyhow::Result<()> {
runtime_path: cli.runtime_path,
rpc: cli.rpc,
contract: cli.contract,
billing_contract: cli.billing_contract,
signer: signer,
abi: abi,
operator_wallet_key: cli.operator_wallet_key,
Expand Down
1 change: 1 addition & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct AppState {
pub runtime_path: String,
pub rpc: String,
pub contract: String,
pub billing_contract: String,
pub signer: k256::ecdsa::SigningKey,
pub abi: Abi,
pub operator_wallet_key: String,
Expand Down
1 change: 1 addition & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub mod serverlesstest {
runtime_path: "./runtime/".to_owned(),
rpc: "https://goerli-rollup.arbitrum.io/rpc".to_owned(),
contract: "0x30694a76d737211a908d0dd672f47e1d29fbfb02".to_owned(),
billing_contract: String::new(), // TODO: ADD BILLING CONTRACT FOR TESTS
abi: abi,
signer: k256::ecdsa::SigningKey::random(&mut rand::rngs::OsRng),
operator_wallet_key: String::new(), //TODO: ADD A WALLET KEY FOR RUNNING TESTS
Expand Down
24 changes: 13 additions & 11 deletions src/workerd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ pub enum ServerlessError {
async fn get_current_deposit(
tx_hash: &str,
rpc: &str,
contract_add: &str,
billing_contract_add: &str,
abi: &Abi,
) -> Result<U256, Error> {
let provider = Provider::<Http>::try_from(rpc)?
.interval(Duration::from_millis(1000));
let contract = Contract::new(
contract_add.parse::<Address>()?,
billing_contract_add.parse::<Address>()?,
abi.to_owned(),
Arc::new(provider));
Arc::new(provider));

let mut bytes32_tx_hash = [0u8; 32];
hex::decode_to_slice(&tx_hash[2..], &mut bytes32_tx_hash)?;
Expand Down Expand Up @@ -99,16 +99,9 @@ pub async fn create_code_file(
workerd_runtime_path: &str,
rpc: &str,
contract: &str,
billing_contract: &str,
abi: &Abi,
) -> Result<(), ServerlessError> {
let tx_deposit = get_current_deposit(tx_hash, rpc, contract, abi)
.await
.map_err(|_| ServerlessError::TxDepositNotFound)?;
// TODO: FIX THE FIXED MINIMUM VALUE
if tx_deposit <= U256::from(200) {
return Err(ServerlessError::TxDepositNotEnough);
}

// get tx data
let mut tx_data = match get_transaction_data(tx_hash, rpc).await?["result"].take() {
Value::Null => Err(ServerlessError::TxNotFound),
Expand All @@ -135,6 +128,15 @@ pub async fn create_code_file(
_ => Err(ServerlessError::InvalidTxCalldataType),
}?;

// get tx deposit
let tx_deposit = get_current_deposit(tx_hash, rpc, billing_contract, abi)
.await
.map_err(|_| ServerlessError::TxDepositNotFound)?;
// TODO: FIX THE FIXED MINIMUM VALUE
if tx_deposit <= U256::from(200) {
return Err(ServerlessError::TxDepositNotEnough);
}

// hex decode calldata by skipping to the code bytes
let mut calldata = hex::decode(&calldata[138..])?;

Expand Down

0 comments on commit 838b64f

Please sign in to comment.