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

cosmwasm-* 1.1.0 migration #24

Merged
merged 9 commits into from Oct 4, 2022
Merged
Show file tree
Hide file tree
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
160 changes: 84 additions & 76 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 8 additions & 9 deletions contracts/cw20-atomic-swap/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cw20-atomic-swap"
version = "0.12.1"
version = "0.13.1"
authors = ["Mauro Lacy <maurolacy@users.noreply.github.com>"]
edition = "2018"
description = "Implementation of Atomic Swaps"
Expand All @@ -18,16 +18,15 @@ backtraces = ["cosmwasm-std/backtraces"]
library = []

[dependencies]
cw-utils = "0.13.2"
cw2 = "0.13.2"
cw20 = "0.13.2"
cosmwasm-std = "1.0.0"
cw-storage-plus = "0.13.2"
schemars = "0.8.10"
serde = { version = "1.0", default-features = false, features = ["derive"] }
cw-utils = "0.13.4"
cw2 = "0.13.4"
cw20 = "0.13.4"
cosmwasm-schema = "1.1.0"
cosmwasm-std = "1.1.0"
cw-storage-plus = "0.13.4"
thiserror = "1.0.31"
hex = "0.3.2"
sha2 = "0.8.2"

[dev-dependencies]
cosmwasm-schema = "1.0.0"

23 changes: 6 additions & 17 deletions contracts/cw20-atomic-swap/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
use std::env::current_dir;
use std::fs::create_dir_all;

use cosmwasm_schema::{export_schema, remove_schemas, schema_for};

use cw20_atomic_swap::msg::DetailsResponse;
use cosmwasm_schema::write_api;
use cw20_atomic_swap::msg::ExecuteMsg;
use cw20_atomic_swap::msg::InstantiateMsg;
use cw20_atomic_swap::msg::ListResponse;
use cw20_atomic_swap::msg::QueryMsg;

fn main() {
let mut out_dir = current_dir().unwrap();
out_dir.push("schema");
create_dir_all(&out_dir).unwrap();
remove_schemas(&out_dir).unwrap();

export_schema(&schema_for!(InstantiateMsg), &out_dir);
export_schema(&schema_for!(ExecuteMsg), &out_dir);
export_schema(&schema_for!(QueryMsg), &out_dir);
export_schema(&schema_for!(ListResponse), &out_dir);
export_schema(&schema_for!(DetailsResponse), &out_dir);
write_api! {
instantiate: InstantiateMsg,
query: QueryMsg,
execute: ExecuteMsg,
}
}
25 changes: 12 additions & 13 deletions contracts/cw20-atomic-swap/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use cosmwasm_schema::{cw_serde, QueryResponses};

use cosmwasm_std::Coin;
use cw20::{Cw20Coin, Cw20ReceiveMsg, Expiration};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[cw_serde]
pub struct InstantiateMsg {}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[cw_serde]
pub enum ExecuteMsg {
Create(CreateMsg),
/// Release sends all tokens to the recipient.
Expand All @@ -26,13 +24,12 @@ pub enum ExecuteMsg {
Receive(Cw20ReceiveMsg),
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[cw_serde]
pub enum ReceiveMsg {
Create(CreateMsg),
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[cw_serde]
pub struct CreateMsg {
/// id is a human-readable name for the swap to use later.
/// 3-20 bytes of utf-8 text
Expand All @@ -54,26 +51,28 @@ pub fn is_valid_name(name: &str) -> bool {
true
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
/// Show all open swaps. Return type is ListResponse.
#[returns(ListResponse)]
List {
start_after: Option<String>,
limit: Option<u32>,
},
/// Returns the details of the named swap, error if not created.
/// Return type: DetailsResponse.
#[returns(DetailsResponse)]
Details { id: String },
}

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
#[cw_serde]
pub struct ListResponse {
/// List all open swap ids
pub swaps: Vec<String>,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
#[cw_serde]
pub struct DetailsResponse {
/// Id of this swap
pub id: String,
Expand All @@ -89,7 +88,7 @@ pub struct DetailsResponse {
pub balance: BalanceHuman,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
#[cw_serde]
pub enum BalanceHuman {
Native(Vec<Coin>),
Cw20(Cw20Coin),
Expand Down
5 changes: 2 additions & 3 deletions contracts/cw20-atomic-swap/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use cosmwasm_schema::cw_serde;

use cosmwasm_std::{Addr, Binary, BlockInfo, Order, StdResult, Storage};
use cw_storage_plus::{Bound, Map};

use cw20::{Balance, Expiration};

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
#[cw_serde]
pub struct AtomicSwap {
/// This is the sha-256 hash of the preimage
pub hash: Binary,
Expand Down
21 changes: 10 additions & 11 deletions contracts/cw20-bonding/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cw20-bonding"
version = "0.12.1"
version = "0.13.1"
authors = ["Ethan Frey <ethanfrey@users.noreply.github.com>"]
edition = "2018"
description = "Implement basic bonding curve to issue cw20 tokens"
Expand All @@ -20,18 +20,17 @@ backtraces = ["cosmwasm-std/backtraces"]
library = []

[dependencies]
cw-utils = "0.13.2"
cw2 = "0.13.2"
cw20 = "0.13.2"
cw20-base = { version = "0.13.2", features = ["library"] }
cw-storage-plus = "0.13.2"
cosmwasm-std = { version = "1.0.0", default-features = false, features = ["staking"] }
schemars = "0.8.1"
serde = { version = "1.0.103", default-features = false, features = ["derive"] }
thiserror = "1.0.23"
cw-utils = "0.13.4"
cw2 = "0.13.4"
cw20 = "0.13.4"
cw20-base ={version = "0.13.4", features = ["library"]}
cw-storage-plus = "0.13.4"
cosmwasm-std = "1.1.0"
cosmwasm-schema = "1.1.0"
thiserror = "1.0.31"
rust_decimal = "1.14.3"
integer-sqrt = "0.1.5"
integer-cbrt = "0.1.2"

[dev-dependencies]
cosmwasm-schema = "1.0.0"

25 changes: 7 additions & 18 deletions contracts/cw20-bonding/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
use std::env::current_dir;
use std::fs::create_dir_all;
use cosmwasm_schema::write_api;

use cosmwasm_schema::{export_schema, remove_schemas, schema_for};

use cw20::{AllowanceResponse, BalanceResponse, TokenInfoResponse};
use cw20_bonding::msg::{CurveInfoResponse, ExecuteMsg, InstantiateMsg, QueryMsg};
use cw20_bonding::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};

fn main() {
let mut out_dir = current_dir().unwrap();
out_dir.push("schema");
create_dir_all(&out_dir).unwrap();
remove_schemas(&out_dir).unwrap();

export_schema(&schema_for!(InstantiateMsg), &out_dir);
export_schema(&schema_for!(ExecuteMsg), &out_dir);
export_schema(&schema_for!(QueryMsg), &out_dir);
export_schema(&schema_for!(AllowanceResponse), &out_dir);
export_schema(&schema_for!(BalanceResponse), &out_dir);
export_schema(&schema_for!(CurveInfoResponse), &out_dir);
export_schema(&schema_for!(TokenInfoResponse), &out_dir);
write_api! {
instantiate: InstantiateMsg,
query: QueryMsg,
execute: ExecuteMsg,
}
}
4 changes: 2 additions & 2 deletions contracts/cw20-bonding/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ pub fn execute_buy(
let payment = must_pay(&info, &state.reserve_denom)?;

// calculate how many tokens can be purchased with this and mint them
let curve = curve_fn(state.decimals);
let curve = curve_fn(state.clone().decimals);
state.reserve += payment;
let new_supply = curve.supply(state.reserve);
let minted = new_supply
Expand Down Expand Up @@ -236,7 +236,7 @@ fn do_sell(

// calculate how many tokens can be purchased with this and mint them
let mut state = CURVE_STATE.load(deps.storage)?;
let curve = curve_fn(state.decimals);
let curve = curve_fn(state.clone().decimals);
state.supply = state
.supply
.checked_sub(amount)
Expand Down
17 changes: 8 additions & 9 deletions contracts/cw20-bonding/src/curves.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use cosmwasm_schema::cw_serde;
use integer_cbrt::IntegerCubeRoot;
use integer_sqrt::IntegerSquareRoot;
use rust_decimal::prelude::ToPrimitive;
use rust_decimal::Decimal;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::str::FromStr;

use cosmwasm_std::{Decimal as StdDecimal, Uint128};
Expand Down Expand Up @@ -85,13 +84,13 @@ impl Curve for Constant {
fn reserve(&self, supply: Uint128) -> Uint128 {
// f(x) = supply * self.value
let reserve = self.normalize.from_supply(supply) * self.value;
self.normalize.to_reserve(reserve)
self.normalize.clone().to_reserve(reserve)
}

fn supply(&self, reserve: Uint128) -> Uint128 {
// f(x) = reserve / self.value
let supply = self.normalize.from_reserve(reserve) / self.value;
self.normalize.to_supply(supply)
self.normalize.clone().to_supply(supply)
}
}

Expand Down Expand Up @@ -120,15 +119,15 @@ impl Curve for Linear {
let square = normalized * normalized;
// Note: multiplying by 0.5 is much faster than dividing by 2
let reserve = square * self.slope * Decimal::new(5, 1);
self.normalize.to_reserve(reserve)
self.normalize.clone().to_reserve(reserve)
}

fn supply(&self, reserve: Uint128) -> Uint128 {
// f(x) = (2 * reserve / self.slope) ^ 0.5
// note: use addition here to optimize 2* operation
let square = self.normalize.from_reserve(reserve + reserve) / self.slope;
let supply = square_root(square);
self.normalize.to_supply(supply)
self.normalize.clone().to_supply(supply)
}
}

Expand Down Expand Up @@ -157,15 +156,15 @@ impl Curve for SquareRoot {
let normalized = self.normalize.from_supply(supply);
let root = square_root(normalized);
let reserve = self.slope * normalized * root / Decimal::new(15, 1);
self.normalize.to_reserve(reserve)
self.normalize.clone().to_reserve(reserve)
}

fn supply(&self, reserve: Uint128) -> Uint128 {
// f(x) = (1.5 * reserve / self.slope) ^ (2/3)
let base = self.normalize.from_reserve(reserve) * Decimal::new(15, 1) / self.slope;
let squared = base * base;
let supply = cube_root(squared);
self.normalize.to_supply(supply)
self.normalize.clone().to_supply(supply)
}
}

Expand Down Expand Up @@ -202,7 +201,7 @@ fn cube_root(cube: Decimal) -> Decimal {
}

/// DecimalPlaces should be passed into curve constructors
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, JsonSchema, Default)]
#[cw_serde]
pub struct DecimalPlaces {
/// Number of decimal places for the supply token (this is what was passed in cw20-base instantiate
pub supply: u32,
Expand Down
25 changes: 14 additions & 11 deletions contracts/cw20-bonding/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use cosmwasm_schema::{cw_serde, QueryResponses};

use crate::curves::{decimal, Constant, Curve, DecimalPlaces, Linear, SquareRoot};
use cosmwasm_std::{Binary, Decimal, Uint128};
use cw20::AllowanceResponse as Cw20AllowanceResponse;
use cw20::BalanceResponse as Cw20BalanceResponse;
use cw20::Expiration;
use cw20::TokenInfoResponse as Cw20TokenInfoResponse;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[cw_serde]
pub struct InstantiateMsg {
/// name of the supply token
pub name: String,
Expand All @@ -30,8 +32,7 @@ pub struct InstantiateMsg {

pub type CurveFn = Box<dyn Fn(DecimalPlaces) -> Box<dyn Curve>>;

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[cw_serde]
pub enum CurveType {
/// Constant always returns `value * 10^-scale` as spot price
Constant { value: Uint128, scale: u32 },
Expand Down Expand Up @@ -66,8 +67,7 @@ impl CurveType {
}
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[cw_serde]
pub enum ExecuteMsg {
/// Buy will attempt to purchase as many supply tokens as possible.
/// You must send only reserve tokens in that message
Expand Down Expand Up @@ -119,22 +119,25 @@ pub enum ExecuteMsg {
BurnFrom { owner: String, amount: Uint128 },
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
/// Returns the reserve and supply quantities, as well as the spot price to buy 1 token
#[returns(CurveInfoResponse)]
CurveInfo {},

/// Implements CW20. Returns the current balance of the given address, 0 if unset.
#[returns(Cw20BalanceResponse)]
Balance { address: String },
/// Implements CW20. Returns metadata on the contract - name, decimals, supply, etc.
#[returns(Cw20TokenInfoResponse)]
TokenInfo {},
/// Implements CW20 "allowance" extension.
/// Returns how much spender can use from owner account, 0 if unset.
#[returns(Cw20AllowanceResponse)]
Allowance { owner: String, spender: String },
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[cw_serde]
pub struct CurveInfoResponse {
// how many reserve tokens have been received
pub reserve: Uint128,
Expand Down
5 changes: 2 additions & 3 deletions contracts/cw20-bonding/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use cosmwasm_schema::cw_serde;

use cosmwasm_std::Uint128;
use cw_storage_plus::Item;
Expand All @@ -8,7 +7,7 @@ use crate::curves::DecimalPlaces;
use crate::msg::CurveType;

/// Supply is dynamic and tracks the current supply of staked and ERC20 tokens.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema, Default)]
#[cw_serde]
pub struct CurveState {
/// reserve is how many native tokens exist bonded to the validator
pub reserve: Uint128,
Expand Down
Loading