Skip to content

Commit

Permalink
feat: funds distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
gluax committed Dec 19, 2024
1 parent 4d6cd9f commit dcd51b6
Show file tree
Hide file tree
Showing 13 changed files with 295 additions and 123 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ lazy_static = "1.4"
libfuzzer-sys = "0.4"
rand = "0.8"
schemars = { version = "0.8", features = ["semver"] }
seda-common = { git = "https://github.com/sedaprotocol/seda-common-rs.git", tag = "v0.3.0" }
seda-common = { git = "https://github.com/sedaprotocol/seda-common-rs.git", branch = "feat/funds-distribution" }
# leaving this in to make local development easier
# seda-common = { path = "../seda-common-rs/crates/common" }
semver = { version = "1.0", features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ seda-common = { workspace = true, features = ["cosmwasm"] }
semver.workspace = true
serde.workspace = true
serde-big-array.workspace = true
serde_json.workspace = true
sha3.workspace = true
thiserror.workspace = true
vrf-rs.workspace = true
Expand All @@ -39,5 +40,4 @@ vrf-rs.workspace = true
cw-multi-test.workspace = true
k256.workspace = true
seda-common = { workspace = true, features = ["cosmwasm", "test-utils"] }
serde_json.workspace = true
lazy_static.workspace = true
3 changes: 3 additions & 0 deletions contract/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ pub enum ContractError {
#[error(transparent)]
Common(#[from] seda_common::error::Error),

#[error(transparent)]
Overflow(#[from] cosmwasm_std::OverflowError),

#[error("Invalid hash length `{0}` expected 32 bytes")]
InvalidHashLength(usize),
#[error("Invalid public key length `{0}` expected 33 bytes")]
Expand Down
3 changes: 2 additions & 1 deletion contract/src/msgs/data_requests/sudo/expire_data_requests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use cosmwasm_std::{to_json_string, DepsMut, Env, Response};
use seda_common::msgs::data_requests::sudo::expire_data_requests;

use super::{state, ContractError, SudoHandler};
use super::{ContractError, SudoHandler};
use crate::msgs::data_requests::state;

impl SudoHandler for expire_data_requests::Sudo {
/// Expires all data requests that have timed out
Expand Down
26 changes: 5 additions & 21 deletions contract/src/msgs/data_requests/sudo/mod.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
use super::{
msgs::data_requests::sudo::{self, SudoMsg},
*,
};
pub(in crate::msgs::data_requests) mod expire_data_requests;
pub(in crate::msgs::data_requests) mod remove_request;
pub(in crate::msgs::data_requests) mod remove_requests;

fn remove_request(request: sudo::RemoveDataRequest, deps: &mut DepsMut, env: &Env) -> Result<Event, ContractError> {
// find the data request from the committed pool (if it exists, otherwise error)
let dr_id = Hash::from_hex_str(&request.dr_id)?;
state::load_request(deps.storage, &dr_id)?;

let block_height: u64 = env.block.height;
use cosmwasm_std::{DepsMut, Env, Response};
use seda_common::msgs::data_requests::sudo::SudoMsg;

let event =
Event::new("remove-dr").add_attributes([("dr_id", request.dr_id), ("block_height", block_height.to_string())]);
use super::{ContractError, SudoHandler};

state::remove_request(deps.storage, dr_id)?;

Ok(event)
}
pub(in crate::msgs::data_requests) mod expire_data_requests;
pub(in crate::msgs::data_requests) mod remove_requests;

impl SudoHandler for SudoMsg {
fn sudo(self, deps: DepsMut, env: Env) -> Result<Response, ContractError> {
match self {
SudoMsg::RemoveDataRequest(sudo) => sudo.sudo(deps, env),
SudoMsg::RemoveDataRequests(sudo) => sudo.sudo(deps, env),
SudoMsg::ExpireDataRequests(sudo) => sudo.sudo(deps, env),
}
Expand Down
10 changes: 0 additions & 10 deletions contract/src/msgs/data_requests/sudo/remove_request.rs

This file was deleted.

108 changes: 103 additions & 5 deletions contract/src/msgs/data_requests/sudo/remove_requests.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,112 @@
use super::*;
use cosmwasm_std::{to_json_string, Addr, BankMsg, Coin, DepsMut, Env, Event, Response, Uint128};
use cw_storage_plus::KeyDeserialize;
use seda_common::{
msgs::data_requests::sudo::{remove_requests, DistributionKind, DistributionMessages},
types::Hash,
};
use serde_json::json;

impl SudoHandler for sudo::remove_requests::Sudo {
use super::{ContractError, SudoHandler};
use crate::{
msgs::data_requests::state::{self, DR_ESCROW},
state::TOKEN,
types::FromHexStr,
};

fn amount_to_tokens(amount: Uint128, token: &str) -> Coin {
Coin {
denom: token.to_string(),
amount,
}
}

fn remove_request(
dr_id_str: String,
messages: DistributionMessages,
deps: &mut DepsMut,
env: &Env,
token: &str,
) -> Result<(Event, Vec<BankMsg>), ContractError> {
// find the data request from the committed pool (if it exists, otherwise error)
let dr_id = Hash::from_hex_str(&dr_id_str)?;
state::load_request(deps.storage, &dr_id)?;

let block_height: u64 = env.block.height;

let mut event =
Event::new("remove-dr").add_attributes([("dr_id", dr_id_str), ("block_height", block_height.to_string())]);

let mut dr_escrow = DR_ESCROW.load(deps.storage, &dr_id)?;

// add 1 so we can account for the refund message that may be sent
let mut bank_messages = Vec::with_capacity(messages.messages.len() + 1);
for message in messages.messages {
match &message.kind {
DistributionKind::Burn(distribution_burn) => {
dr_escrow.amount = dr_escrow.amount.checked_sub(distribution_burn.amount)?;
bank_messages.push(BankMsg::Burn {
amount: vec![amount_to_tokens(distribution_burn.amount, token)],
});
event = event.add_attribute(
"burn",
to_json_string(&json!({
"amount": distribution_burn.amount,
"type": to_json_string(&message.type_)?,
"kind": to_json_string(&message.kind)?,
}))?,
);
}
DistributionKind::Send(distribution_send) => {
dr_escrow.amount = dr_escrow.amount.checked_sub(distribution_send.amount)?;
bank_messages.push(BankMsg::Send {
to_address: Addr::from_vec(distribution_send.to.to_vec())?.to_string(),
amount: vec![amount_to_tokens(distribution_send.amount, token)],
});
event = event.add_attribute(
"send",
to_json_string(&json!({
"amount": distribution_send.amount,
"to": distribution_send.to,
"type": to_json_string(&message.type_)?,
"kind": to_json_string(&message.kind)?,
}))?,
);
}
}
}

if !dr_escrow.amount.is_zero() {
bank_messages.push(BankMsg::Send {
to_address: dr_escrow.staker.to_string(),
amount: vec![amount_to_tokens(dr_escrow.amount, token)],
});
event = event.add_attribute(
"refund",
to_json_string(&json!({
"amount": dr_escrow.amount,
"type": to_json_string(&messages.refund_type)?,
}))?,
);
event = event.add_attribute("refund", dr_escrow.amount.to_string());
}

state::remove_request(deps.storage, dr_id)?;
DR_ESCROW.remove(deps.storage, &dr_id);

Ok((event, bank_messages))
}

impl SudoHandler for remove_requests::Sudo {
fn sudo(self, mut deps: DepsMut, env: Env) -> Result<Response, ContractError> {
let token = TOKEN.load(deps.storage)?;
let mut response = Response::new();
for event in self
for removal in self
.requests
.into_iter()
.map(|request| remove_request(request, &mut deps, &env))
.map(|(dr_id, messages)| remove_request(dr_id, messages, &mut deps, &env, &token))
{
response = response.add_event(event?);
let (event, bank_messages) = removal?;
response = response.add_event(event).add_messages(bank_messages);
}
Ok(response)
}
Expand Down
21 changes: 10 additions & 11 deletions contract/src/msgs/data_requests/test_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use msgs::data_requests::sudo::expire_data_requests;
use msgs::data_requests::sudo::{expire_data_requests, DistributionMessages};
use semver::{BuildMetadata, Prerelease, Version};
use sha3::{Digest, Keccak256};

Expand Down Expand Up @@ -190,20 +190,19 @@ impl TestInfo {
}

#[track_caller]
pub fn remove_data_request(&mut self, dr_id: String) -> Result<(), ContractError> {
let msg = sudo::RemoveDataRequest { dr_id }.into();
pub fn remove_data_request(&mut self, dr_id: String, msgs: DistributionMessages) -> Result<(), ContractError> {
let mut requests = HashMap::new();
requests.insert(dr_id, msgs);
let msg = sudo::remove_requests::Sudo { requests }.into();
self.sudo(&msg)
}

#[track_caller]
pub fn remove_data_requests(&mut self, results: Vec<String>) -> Result<(), ContractError> {
let msg = sudo::remove_requests::Sudo {
requests: results
.into_iter()
.map(|dr_id| sudo::RemoveDataRequest { dr_id })
.collect(),
}
.into();
pub fn remove_data_requests(
&mut self,
requests: HashMap<String, DistributionMessages>,
) -> Result<(), ContractError> {
let msg = sudo::remove_requests::Sudo { requests }.into();
self.sudo(&msg)
}

Expand Down
Loading

0 comments on commit dcd51b6

Please sign in to comment.