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

Why have any of these checks in the API. Shouldn't they all be in the Smart Cont... #50

Open
github-actions bot opened this issue Nov 3, 2022 · 0 comments
Labels

Comments

@github-actions
Copy link

github-actions bot commented Nov 3, 2022

// TODO: Why have any of these checks in the API. Shouldn't they all be in the Smart Contract Logic.

use anyhow::{anyhow, Result};
use banyan_shared::{eth::EthClient, proofs, proofs::window, types::*};
use log::info;
use rocket::serde::{Deserialize, Serialize};
use serde_json::from_str;
use std::io::Cursor;
use std::sync::Arc;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ChainlinkRequestData {
    pub deal_id: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ResponseData {
    pub deal_id: DealID,
    pub success_count: u64,
    pub num_windows: u64,
    pub status: u16,
    pub result: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ChainlinkResponse {
    pub data: ResponseData,
}
pub struct WebserverState(pub Arc<EthClient>);

/* Function to construct an error response to return to Chainlink */
fn construct_error(deal_id: DealID, reason: String) -> ChainlinkResponse {
    ChainlinkResponse {
        data: ResponseData {
            deal_id,
            success_count: 0,
            num_windows: 0,
            status: 0,
            result: reason,
        },
    }
}

/// this validates the deal based on a deal_id, returns a json response of either the success count and num_windows,
/// or an error message to be turned into Json<ChainlinkResponse> in the caller!
/// TODO fix logging... :|
pub(crate) async fn validate_deal_internal(
    provider: Arc<EthClient>,
    input_data: ChainlinkRequestData,
) -> Result<ChainlinkResponse> {
    let deal_id = from_str(&input_data.deal_id)?;
    let deal_info = provider
        .get_offer(deal_id)
        .await
        .map_err(|e| anyhow!("Error in get_deal: {:?}", e))?;

    // checking that deal is either finished or cancelled
    let current_block_num = provider
        .get_latest_block_num()
        .await
        .map_err(|e| anyhow!("Couldn't get most recent block number: {e}"))?;

    // TODO: Why have any of these checks in the API. Shouldn't they all be in the Smart Contract Logic.

    let deal_over = EthClient::deal_over(current_block_num, deal_info.clone());
    let deal_cancelled = false; // TODO need to figure out how to get this

    // this refuses to do the validation computations unless the deal is done with or cancelled
@github-actions github-actions bot added the todo label Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

0 participants