diff --git a/src/billing_job.rs b/src/billing_job.rs index bb560bb..7c12e1f 100644 --- a/src/billing_job.rs +++ b/src/billing_job.rs @@ -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}; @@ -19,9 +19,9 @@ pub async fn billing_scheduler(appstate: Data,) -> Result() - .context("Unable to parse contract address")?, + appstate.billing_contract.as_str() + .parse::
() + .context("Unable to parse contract address")?, appstate.abi.to_owned(), Arc::new(client)); @@ -50,13 +50,13 @@ pub async fn billing_scheduler(appstate: Data,) -> Result( "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 diff --git a/src/handler.rs b/src/handler.rs index 3fce1e3..1ab93e3 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -67,6 +67,7 @@ pub async fn serverless( workerd_runtime_path, &appstate.rpc, &appstate.contract, + &appstate.billing_contract, &appstate.abi, ) .await @@ -279,6 +280,6 @@ pub async fn serverless( *amount += execution_cost; appstate.billing_hasher.lock().await.update(&hash); - + return response; } diff --git a/src/main.rs b/src/main.rs index 6da8791..ec2bddb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,6 +39,9 @@ struct Args { )] contract: String, + #[clap(long, value_parser)] + billing_contract: String, + #[clap(long, value_parser)] operator_wallet_key: String, @@ -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, diff --git a/src/model.rs b/src/model.rs index 374607b..3198e99 100644 --- a/src/model.rs +++ b/src/model.rs @@ -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, diff --git a/src/tests.rs b/src/tests.rs index 390a0d0..0231aa9 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -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 diff --git a/src/workerd.rs b/src/workerd.rs index 45ce61c..014d0e7 100644 --- a/src/workerd.rs +++ b/src/workerd.rs @@ -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 { let provider = Provider::::try_from(rpc)? .interval(Duration::from_millis(1000)); let contract = Contract::new( - contract_add.parse::
()?, + billing_contract_add.parse::
()?, 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)?; @@ -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), @@ -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..])?;