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

refactor init #168

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all 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
2 changes: 1 addition & 1 deletion cosmwasm/contracts/price-feed/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use std::fs::create_dir_all;
use cosmwasm_schema::{export_schema, remove_schemas, schema_for};

use std_reference::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use std_reference::state::{RefData, RefMedianData, ReferenceData,RefDeviationData};
use std_reference::state::{RefData, RefDeviationData, RefMedianData, ReferenceData};

fn main() {
let mut out_dir = current_dir().unwrap();
43 changes: 29 additions & 14 deletions cosmwasm/contracts/price-feed/src/contract.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@ use cosmwasm_std::{
};
use cw2::{get_contract_version, set_contract_version};
use semver::Version;

use crate::errors::ContractError;
use crate::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use crate::state::{
@@ -57,10 +56,11 @@ pub fn execute(
ExecuteMsg::AddRelayers { relayers } => execute_add_relayers(deps, info, relayers),
ExecuteMsg::RemoveRelayers { relayers } => execute_remove_relayers(deps, info, relayers),
ExecuteMsg::Relay {
symbol_rates,
symbols,
rates,
resolve_time,
request_id,
} => execute_relay(deps, info, symbol_rates, resolve_time, request_id),
} => execute_relay(deps, info, symbols, rates, resolve_time, request_id),
ExecuteMsg::ForceRelay {
symbol_rates,
resolve_time,
@@ -167,7 +167,8 @@ fn execute_remove_relayers(
fn execute_relay(
deps: DepsMut,
info: MessageInfo,
symbol_rates: Vec<(String, Uint64)>,
symbols: Vec<String>,
rates: Vec<Uint256>,
resolve_time: Uint64,
request_id: Uint64,
) -> Result<Response, ContractError> {
@@ -179,18 +180,32 @@ fn execute_relay(
});
}

// Saves price data
for (symbol, rate) in symbol_rates {
if let Some(existing_refdata) = REFDATA.may_load(deps.storage, &symbol)? {
if existing_refdata.resolve_time >= resolve_time {
continue;
for i in 0..rates.len() {
let symbol_rates = rates[i].to_le_bytes();
let index=i*4;
for j in index..index+4{
if j >= symbols.len(){
// No more symbols left update
break;
}

let rate= Uint64::from(symbol_rates[j]);
if rate.is_zero(){

}

if let Some(existing_refdata) = REFDATA.may_load(deps.storage, &symbols[i + j])? {
if existing_refdata.resolve_time >= resolve_time {
continue;
}
}

REFDATA.save(
deps.storage,
&symbols[i + j],
&RefData::new(rate, resolve_time, request_id),
)?
}
REFDATA.save(
deps.storage,
&symbol,
&RefData::new(rate, resolve_time, request_id),
)?
}

Ok(Response::default().add_attribute("action", "execute_relay"))
3 changes: 3 additions & 0 deletions cosmwasm/contracts/price-feed/src/errors.rs
Original file line number Diff line number Diff line change
@@ -16,6 +16,9 @@ pub enum ContractError {
#[error("Median is disabled")]
MedianDisabled {},

#[error("Invalid rates")]
InvalidRates {},

#[error("Cannot migrate from different contract type: {previous_contract}")]
CannotMigrate { previous_contract: String },

8 changes: 5 additions & 3 deletions cosmwasm/contracts/price-feed/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::state::{RefData, RefDeviationData, RefMedianData, ReferenceData};
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::Uint64;
use crate::state::{RefData,RefDeviationData,RefMedianData,ReferenceData};
use cosmwasm_std::{Uint256, Uint64};

#[cw_serde]
pub struct InstantiateMsg {}
@@ -37,7 +37,9 @@ pub enum ExecuteMsg {
// e.g.
// BTC = 19,343.34, ETH = 1,348.57
// symbol_rates ≡ <("BTC", 19343340000000), ("ETH", 1348570000000)>
symbol_rates: Vec<(String, Uint64)>,
symbols: Vec<String>,

rates: Vec<Uint256>,
// Resolve time of request on Ojo in Unix timestamp
resolve_time: Uint64,
// Request ID of the results on Ojo