Endpoint for accepting deposit

This commit is contained in:
Bogdan-Ștefan Neacșu
2022-02-28 15:58:35 +02:00
parent 2d55d09f24
commit de450f87de
8 changed files with 57 additions and 5 deletions
@@ -0,0 +1,8 @@
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
// event types
pub const VOUCHER_ACQUIRED_EVENT_TYPE: &str = "coconut-acquired";
// attributes that are used in multiple places
pub const VOUCHER_VALUE: &str = "value";
@@ -1,3 +1,4 @@
pub mod events;
pub mod keys;
pub mod msg;
pub mod payment;
@@ -14,6 +14,7 @@ pub struct InstantiateMsg {}
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
LinkPayment { data: LinkPaymentData },
BuyBandwidth {},
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
+1 -3
View File
@@ -8,11 +8,9 @@ edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]
[dev-dependencies]
config = { path = "../../common/config"}
[dependencies]
bandwidth-claim-contract = { path = "../../common/bandwidth-claim-contract" }
config = { path = "../../common/config"}
cosmwasm-std = "1.0.0-beta3"
cosmwasm-storage = "1.0.0-beta3"
+11
View File
@@ -1,6 +1,8 @@
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use config::defaults::DENOM;
use cosmwasm_std::{StdError, VerificationError};
use thiserror::Error;
@@ -24,4 +26,13 @@ pub enum ContractError {
#[error("The payment is not properly signed")]
BadSignature,
#[error("Received multiple coin types")]
MultipleDenoms,
#[error("No coin was sent for voucher")]
NoCoin,
#[error("Wrong coin denomination, you must send {}", DENOM)]
WrongDenom,
}
+1
View File
@@ -39,6 +39,7 @@ pub fn execute(
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::LinkPayment { data } => transactions::link_payment(deps, env, info, data),
ExecuteMsg::BuyBandwidth {} => transactions::buy_bandwidth(deps, env, info),
}
}
+9
View File
@@ -11,6 +11,7 @@ use bandwidth_claim_contract::payment::Payment;
// buckets
const PREFIX_PAYMENTS: &[u8] = b"payments";
const PREFIX_STATUS: &[u8] = b"status";
const PREFIX_COCONUT: &[u8] = b"coconut";
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize, JsonSchema)]
pub enum Status {
@@ -31,6 +32,14 @@ pub fn status(storage: &mut dyn Storage) -> Bucket<'_, Status> {
bucket(storage, PREFIX_STATUS)
}
pub fn coconut(storage: &mut dyn Storage) -> Bucket<'_, Payment> {
bucket(storage, PREFIX_COCONUT)
}
pub fn coconut_read(storage: &dyn Storage) -> ReadonlyBucket<'_, Payment> {
bucket_read(storage, PREFIX_COCONUT)
}
#[cfg(test)]
mod tests {
use super::*;
+25 -2
View File
@@ -1,11 +1,13 @@
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use cosmwasm_std::{DepsMut, Env, MessageInfo, Response};
use cosmwasm_std::{DepsMut, Env, Event, MessageInfo, Response};
use crate::error::ContractError;
use crate::storage::{payments, status, Status};
use crate::storage::{coconut, payments, status, Status};
use bandwidth_claim_contract::events::{VOUCHER_ACQUIRED_EVENT_TYPE, VOUCHER_VALUE};
use bandwidth_claim_contract::payment::{LinkPaymentData, Payment};
use config::defaults::DENOM;
pub(crate) fn link_payment(
deps: DepsMut<'_>,
@@ -44,6 +46,27 @@ pub(crate) fn link_payment(
Ok(Response::default())
}
pub(crate) fn buy_bandwidth(
_deps: DepsMut<'_>,
_env: Env,
info: MessageInfo,
) -> Result<Response, ContractError> {
if info.funds.is_empty() {
return Err(ContractError::NoCoin);
}
if info.funds.len() > 1 {
return Err(ContractError::MultipleDenoms);
}
if info.funds[0].denom != DENOM {
return Err(ContractError::WrongDenom);
}
let voucher_value = info.funds.last().unwrap();
let event =
Event::new(VOUCHER_ACQUIRED_EVENT_TYPE).add_attribute(VOUCHER_VALUE, voucher_value.amount);
Ok(Response::new().add_event(event))
}
#[cfg(test)]
mod tests {
use super::*;