Skip to content

Commit

Permalink
replace custom binding with anybuf stargate message
Browse files Browse the repository at this point in the history
  • Loading branch information
jhernandezb committed Feb 7, 2025
1 parent 8b56fcb commit 8aee903
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 76 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

110 changes: 55 additions & 55 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,67 +12,67 @@ members = [
resolver = "2"

[workspace.package]
version = "3.15.0"
version = "3.15.0"
rust-version = "1.73.0"
edition = "2021"
homepage = "https://stargaze.zone"
repository = "https://github.com/public-awesome/launchpad"
license = "Apache-2.0"
edition = "2021"
homepage = "https://stargaze.zone"
repository = "https://github.com/public-awesome/launchpad"
license = "Apache-2.0"

[workspace.dependencies]
base-factory = { version = "3.14.0", path = "contracts/factories/base-factory" }
base-minter = { version = "3.14.0", path = "contracts/minters/base-minter" }
cosmwasm-schema = "1.2.1"
cosmwasm-std = "1.2.1"
cw-controllers = "1.1.0"
cw2 = "1.1.0"
cw4 = "1.1.0"
cw4-group = "1.1.0"
cw721 = "0.18.0"
cw721-base = "0.18.0"
cw-multi-test = "0.16.2"
cw-storage-plus = "1.1.0"
cw-utils = "1.0.1"
schemars = "0.8.11"
serde = { version = "1.0.145", default-features = false, features = ["derive"] }
sg1 = { version = "3.14.0", path = "packages/sg1" }
sg2 = { version = "3.14.0", path = "packages/sg2" }
sg4 = { version = "3.14.0", path = "packages/sg4" }
sg721 = { version = "3.14.0", path = "packages/sg721" }
sg721-base = { version = "3.14.0", path = "contracts/collections/sg721-base" }
sg721-nt = { version = "3.14.0", path = "contracts/collections/sg721-nt" }
sg721-updatable = { version = "3.14.0", path = "contracts/collections/sg721-updatable" }
sg-controllers = { version = "3.14.0", path = "packages/controllers" }
sg-metadata = { version = "3.14.0", path = "packages/sg-metadata" }
sg-mint-hooks = { version = "3.14.0", path = "packages/mint-hooks" }
sg-multi-test = { version = "3.1.0" }
sg-splits = { version = "3.14.0", path = "contracts/splits" }
sg-std = { version = "3.2.0" }
sg-whitelist = { version = "3.14.0", path = "contracts/whitelists/whitelist" }
sg-tiered-whitelist = { version = "3.14.0", path = "contracts/whitelists/tiered-whitelist" }
thiserror = "1.0.31"
url = "2.2.2"
sha2 = { version = "0.10.2", default-features = false }
base-factory = { version = "3.14.0", path = "contracts/factories/base-factory" }
base-minter = { version = "3.14.0", path = "contracts/minters/base-minter" }
cosmwasm-schema = "1.2.1"
cosmwasm-std = "1.2.1"
cw-controllers = "1.1.0"
cw2 = "1.1.0"
cw4 = "1.1.0"
cw4-group = "1.1.0"
cw721 = "0.18.0"
cw721-base = "0.18.0"
cw-multi-test = "0.16.2"
cw-storage-plus = "1.1.0"
cw-utils = "1.0.1"
schemars = "0.8.11"
serde = { version = "1.0.145", default-features = false, features = ["derive"] }
sg1 = { version = "3.14.0", path = "packages/sg1" }
sg2 = { version = "3.14.0", path = "packages/sg2" }
sg4 = { version = "3.14.0", path = "packages/sg4" }
sg721 = { version = "3.14.0", path = "packages/sg721" }
sg721-base = { version = "3.14.0", path = "contracts/collections/sg721-base" }
sg721-nt = { version = "3.14.0", path = "contracts/collections/sg721-nt" }
sg721-updatable = { version = "3.14.0", path = "contracts/collections/sg721-updatable" }
sg-controllers = { version = "3.14.0", path = "packages/controllers" }
sg-metadata = { version = "3.14.0", path = "packages/sg-metadata" }
sg-mint-hooks = { version = "3.14.0", path = "packages/mint-hooks" }
sg-multi-test = { version = "3.1.0" }
sg-splits = { version = "3.14.0", path = "contracts/splits" }
sg-std = { version = "3.2.0" }
sg-whitelist = { version = "3.14.0", path = "contracts/whitelists/whitelist" }
sg-tiered-whitelist = { version = "3.14.0", path = "contracts/whitelists/tiered-whitelist" }
thiserror = "1.0.31"
url = "2.2.2"
sha2 = { version = "0.10.2", default-features = false }

whitelist-mtree = { version = "3.14.0", path = "contracts/whitelists/whitelist-merkletree" }
tiered-whitelist-merkletree = { version = "3.14.0", path = "contracts/whitelists/tiered-whitelist-merkletree" }
tiered-whitelist-merkletree = { version = "3.14.0", path = "contracts/whitelists/tiered-whitelist-merkletree" }
vending-minter-merkle-wl = { version = "3.14.0", path = "contracts/minters/vending-minter-merkle-wl" }
vending-factory = { version = "3.14.0", path = "contracts/factories/vending-factory" }
vending-minter = { version = "3.14.0", path = "contracts/minters/vending-minter" }
open-edition-factory = { version = "3.14.0", path = "contracts/factories/open-edition-factory" }
open-edition-minter = { version = "3.14.0", path = "contracts/minters/open-edition-minter" }
token-merge-factory = { version = "3.14.0", path = "contracts/factories/token-merge-factory" }
token-merge-minter = { version = "3.14.0", path = "contracts/minters/token-merge-minter" }
whitelist-immutable = { version = "3.14.0", path = "contracts/whitelists/whitelist-immutable" }
sg-whitelist-flex = { version = "3.14.0", path = "contracts/whitelists/whitelist-flex" }
sg-tiered-whitelist-flex = { version = "3.14.0", path = "contracts/whitelists/tiered-whitelist-flex" }
ethereum-verify = { version = "3.14.0", path = "packages/ethereum-verify" }
sg-eth-airdrop = { version = "3.14.0", path = "contracts/sg-eth-airdrop" }
test-suite = { version = "3.14.0", path = "test-suite" }

semver = "1"
cw-ownable = "0.5.1"

vending-factory = { version = "3.14.0", path = "contracts/factories/vending-factory" }
vending-minter = { version = "3.14.0", path = "contracts/minters/vending-minter" }
open-edition-factory = { version = "3.14.0", path = "contracts/factories/open-edition-factory" }
open-edition-minter = { version = "3.14.0", path = "contracts/minters/open-edition-minter" }
token-merge-factory = { version = "3.14.0", path = "contracts/factories/token-merge-factory" }
token-merge-minter = { version = "3.14.0", path = "contracts/minters/token-merge-minter" }
whitelist-immutable = { version = "3.14.0", path = "contracts/whitelists/whitelist-immutable" }
sg-whitelist-flex = { version = "3.14.0", path = "contracts/whitelists/whitelist-flex" }
sg-tiered-whitelist-flex = { version = "3.14.0", path = "contracts/whitelists/tiered-whitelist-flex" }
ethereum-verify = { version = "3.14.0", path = "packages/ethereum-verify" }
sg-eth-airdrop = { version = "3.14.0", path = "contracts/sg-eth-airdrop" }
test-suite = { version = "3.14.0", path = "test-suite" }

semver = "1"
cw-ownable = "0.5.1"
anybuf = "0.5.2"
[profile.release.package.sg721]
codegen-units = 1
incremental = false
Expand Down
11 changes: 6 additions & 5 deletions packages/sg1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ repository = { workspace = true }
license = { workspace = true }

[dependencies]
cosmwasm-std = { workspace = true }
cw-utils = { workspace = true }
serde = { workspace = true }
sg-std = { workspace = true }
thiserror = { workspace = true }
cosmwasm-std = { workspace = true, features = ["stargate"] }
cw-utils = { workspace = true }
serde = { workspace = true }
sg-std = { workspace = true }
thiserror = { workspace = true }
anybuf = { workspace = true }
63 changes: 47 additions & 16 deletions packages/sg1/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use anybuf::Anybuf;
use cosmwasm_std::{
coin, coins, ensure, Addr, BankMsg, Coin, Decimal, Event, MessageInfo, Uint128,
coin, coins, ensure, Addr, BankMsg, Coin, CosmosMsg, Decimal, Event, MessageInfo, Response,
SubMsg, Uint128,
};
use cw_utils::{may_pay, must_pay, PaymentError};
use sg_std::{create_fund_fairburn_pool_msg, Response, SubMsg, NATIVE_DENOM};
use sg_std::NATIVE_DENOM;
use thiserror::Error;

// governance parameters
Expand All @@ -27,7 +29,7 @@ pub fn checked_fair_burn(
};

if payment.u128() != 0u128 {
fair_burn(fee, developer, res);
fair_burn(info.sender.to_string(), fee, developer, res);
}

Ok(())
Expand Down Expand Up @@ -154,7 +156,7 @@ pub fn distribute_mint_fees(
}

/// Burn and distribute fees, assuming the right fee is passed in
pub fn fair_burn(fee: u128, developer: Option<Addr>, res: &mut Response) {
pub fn fair_burn(sender: String, fee: u128, developer: Option<Addr>, res: &mut Response) {
let mut event = Event::new("fair-burn");

// calculate the fair burn fee
Expand All @@ -175,17 +177,33 @@ pub fn fair_burn(fee: u128, developer: Option<Addr>, res: &mut Response) {
event = event.add_attribute("dev", dev.to_string());
event = event.add_attribute("dev_amount", Uint128::from(remainder).to_string());
} else {
res.messages
.push(SubMsg::new(create_fund_fairburn_pool_msg(coins(
remainder,
NATIVE_DENOM,
))));
let msg_fund_fairburn_pool =
create_fund_fairburn_pool_msg(sender, &coin(remainder, NATIVE_DENOM));
res.messages.push(SubMsg::new(msg_fund_fairburn_pool));
event = event.add_attribute("dist_amount", Uint128::from(remainder).to_string());
}

res.events.push(event);
}

/// following the protobuf spec in
/// https://github.com/public-awesome/stargaze/blob/efdb9212e037e05fc429c0cfbcf425ad11855e15/proto/publicawesome/stargaze/alloc/v1beta1/tx.proto#L49
fn encode_msg_fund_fairburn_pool(sender: String, amount: &Coin) -> Vec<u8> {
let coin = Anybuf::new()
.append_string(1, &amount.denom)
.append_string(2, amount.amount.to_string());
Anybuf::new()
.append_string(1, sender)
.append_message(2, &coin)
.into_vec()
}

fn create_fund_fairburn_pool_msg(sender: String, amount: &Coin) -> CosmosMsg {
CosmosMsg::Stargate {
type_url: "/publicawesome.stargaze.alloc.v1beta1.MsgFundFairburnPool".to_string(),
value: encode_msg_fund_fairburn_pool(sender, amount).into(),
}
}
pub fn transfer_funds_to_launchpad_dao(
info: &MessageInfo,
fee: u128,
Expand Down Expand Up @@ -218,20 +236,23 @@ pub enum FeeError {

#[cfg(test)]
mod tests {
use cosmwasm_std::{coins, Addr, BankMsg};
use sg_std::{create_fund_fairburn_pool_msg, Response, NATIVE_DENOM};
use cosmwasm_std::{coin, coins, Addr, BankMsg, Response};
use sg_std::NATIVE_DENOM;

use crate::{fair_burn, SubMsg};
use crate::{create_fund_fairburn_pool_msg, fair_burn, SubMsg};

#[test]
fn check_fair_burn_no_dev_rewards() {
let mut res = Response::new();

fair_burn(9u128, None, &mut res);
fair_burn(Addr::unchecked("sender").to_string(), 9u128, None, &mut res);
let burn_msg = SubMsg::new(BankMsg::Burn {
amount: coins(4, "ustars".to_string()),
});
let dist_msg = SubMsg::new(create_fund_fairburn_pool_msg(coins(5, NATIVE_DENOM)));
let dist_msg = SubMsg::new(create_fund_fairburn_pool_msg(
Addr::unchecked("sender").to_string(),
&coin(5, NATIVE_DENOM),
));
assert_eq!(res.messages.len(), 2);
assert_eq!(res.messages[0], burn_msg);
assert_eq!(res.messages[1], dist_msg);
Expand All @@ -241,7 +262,12 @@ mod tests {
fn check_fair_burn_with_dev_rewards() {
let mut res = Response::new();

fair_burn(9u128, Some(Addr::unchecked("geordi")), &mut res);
fair_burn(
Addr::unchecked("sender").to_string(),
9u128,
Some(Addr::unchecked("geordi")),
&mut res,
);
let bank_msg = SubMsg::new(BankMsg::Send {
to_address: "geordi".to_string(),
amount: coins(5, NATIVE_DENOM),
Expand All @@ -258,7 +284,12 @@ mod tests {
fn check_fair_burn_with_dev_rewards_different_amount() {
let mut res = Response::new();

fair_burn(1420u128, Some(Addr::unchecked("geordi")), &mut res);
fair_burn(
Addr::unchecked("sender").to_string(),
1420u128,
Some(Addr::unchecked("geordi")),
&mut res,
);
let bank_msg = SubMsg::new(BankMsg::Send {
to_address: "geordi".to_string(),
amount: coins(710, NATIVE_DENOM),
Expand Down

0 comments on commit 8aee903

Please sign in to comment.