Skip to content

Commit

Permalink
Update contract to do price calculation locally
Browse files Browse the repository at this point in the history
  • Loading branch information
josefleventon committed Feb 25, 2023
1 parent 8010672 commit 3b54ae2
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 167 deletions.
2 changes: 1 addition & 1 deletion .cargo/config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[alias]
wasm = "build --release --target wasm32-unknown-unknown"
unit-test = "test --lib"
schema = "run --example schema"
schema = "run --bin schema"
192 changes: 96 additions & 96 deletions schema/cw-wager.json
Original file line number Diff line number Diff line change
Expand Up @@ -257,30 +257,30 @@
"Currency": {
"type": "string",
"enum": [
"d_o_t",
"a_v_a_x",
"u_n_i",
"a_t_o_m",
"l_i_n_k",
"n_e_a_r",
"i_c_p",
"s_a_n_d",
"b_t_c",
"e_t_h",
"b_n_b",
"x_r_p",
"a_d_a",
"d_o_g_e",
"s_o_l",
"m_a_n_a",
"c_a_k_e",
"a_r",
"o_s_m_o",
"r_u_n_e",
"l_u_n_a",
"u_s_t_c",
"s_t_a_r_s",
"m_i_r"
"dot",
"avax",
"uni",
"atom",
"link",
"near",
"icp",
"sand",
"btc",
"eth",
"bnb",
"xrp",
"ada",
"doge",
"sol",
"mana",
"cake",
"ar",
"osmo",
"rune",
"luna",
"ustc",
"stars",
"mir"
]
},
"ParamInfo": {
Expand Down Expand Up @@ -567,30 +567,30 @@
"Currency": {
"type": "string",
"enum": [
"d_o_t",
"a_v_a_x",
"u_n_i",
"a_t_o_m",
"l_i_n_k",
"n_e_a_r",
"i_c_p",
"s_a_n_d",
"b_t_c",
"e_t_h",
"b_n_b",
"x_r_p",
"a_d_a",
"d_o_g_e",
"s_o_l",
"m_a_n_a",
"c_a_k_e",
"a_r",
"o_s_m_o",
"r_u_n_e",
"l_u_n_a",
"u_s_t_c",
"s_t_a_r_s",
"m_i_r"
"dot",
"avax",
"uni",
"atom",
"link",
"near",
"icp",
"sand",
"btc",
"eth",
"bnb",
"xrp",
"ada",
"doge",
"sol",
"mana",
"cake",
"ar",
"osmo",
"rune",
"luna",
"ustc",
"stars",
"mir"
]
},
"MatchmakingItemExport": {
Expand Down Expand Up @@ -767,30 +767,30 @@
"Currency": {
"type": "string",
"enum": [
"d_o_t",
"a_v_a_x",
"u_n_i",
"a_t_o_m",
"l_i_n_k",
"n_e_a_r",
"i_c_p",
"s_a_n_d",
"b_t_c",
"e_t_h",
"b_n_b",
"x_r_p",
"a_d_a",
"d_o_g_e",
"s_o_l",
"m_a_n_a",
"c_a_k_e",
"a_r",
"o_s_m_o",
"r_u_n_e",
"l_u_n_a",
"u_s_t_c",
"s_t_a_r_s",
"m_i_r"
"dot",
"avax",
"uni",
"atom",
"link",
"near",
"icp",
"sand",
"btc",
"eth",
"bnb",
"xrp",
"ada",
"doge",
"sol",
"mana",
"cake",
"ar",
"osmo",
"rune",
"luna",
"ustc",
"stars",
"mir"
]
},
"NFT": {
Expand Down Expand Up @@ -899,30 +899,30 @@
"Currency": {
"type": "string",
"enum": [
"d_o_t",
"a_v_a_x",
"u_n_i",
"a_t_o_m",
"l_i_n_k",
"n_e_a_r",
"i_c_p",
"s_a_n_d",
"b_t_c",
"e_t_h",
"b_n_b",
"x_r_p",
"a_d_a",
"d_o_g_e",
"s_o_l",
"m_a_n_a",
"c_a_k_e",
"a_r",
"o_s_m_o",
"r_u_n_e",
"l_u_n_a",
"u_s_t_c",
"s_t_a_r_s",
"m_i_r"
"dot",
"avax",
"uni",
"atom",
"link",
"near",
"icp",
"sand",
"btc",
"eth",
"bnb",
"xrp",
"ada",
"doge",
"sol",
"mana",
"cake",
"ar",
"osmo",
"rune",
"luna",
"ustc",
"stars",
"mir"
]
},
"NFT": {
Expand Down
8 changes: 6 additions & 2 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,13 @@ pub fn execute(
admin_only(deps.as_ref(), info)?;
execute_update_params(deps, params)
}
ExecuteMsg::SetWinner { wager_key, winner } => {
ExecuteMsg::SetWinner {
wager_key,
prev_prices,
current_prices,
} => {
admin_only(deps.as_ref(), info)?;
execute_set_winner(deps, env, wager_key, winner)
execute_set_winner(deps, env, wager_key, prev_prices, current_prices)
}
ExecuteMsg::Wager {
token,
Expand Down
62 changes: 44 additions & 18 deletions src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,40 +190,66 @@ pub fn execute_set_winner(
deps: DepsMut,
env: Env,
wager_key: (Token, Token),
winner: Token,
prev_prices: (u64, u64),
current_prices: (u64, u64),
) -> Result<Response, ContractError> {
let (collection, token_id) = winner.clone();

// Verify that all token data conforms to the contract's config
let config = CONFIG.load(deps.storage)?;
if wager_key.0 .0 != config.collection_address || wager_key.1 .0 != config.collection_address {
return Err(ContractError::Unauthorized {});
}

// Verify that the winner is part of the wager key
if winner != wager_key.0 && winner != wager_key.1 {
return Err(ContractError::InvalidParameter {
param: "winner".into(),
});
}

// Get the wager info
let wager = wagers()
.load(deps.storage, wager_key.clone())
.or_else(|_| wagers().load(deps.storage, (wager_key.clone().1, wager_key.clone().0)))?;
let token_owner = Cw721Contract(collection)
.owner_of(&deps.querier, token_id.to_string(), true)?
.owner;

// Verify that the wager has expired
if env.block.time < wager.expires_at {
return Err(ContractError::WagerActive {});
}

// Remove the wager and pay out the winner
// Remove the wager
wagers().remove(deps.storage, wager_key.clone())?;

// Determine the winner of the wager
let token_1_change = current_prices.0 / prev_prices.0 - 1;
let token_2_change = current_prices.1 / prev_prices.1 - 1;

let winner;

wagers().remove(deps.storage, wager_key)?;
if token_1_change > token_2_change {
winner = wager_key.0
} else if token_2_change > token_1_change {
winner = wager_key.1
} else {
let res = Response::new().add_attribute("action", "wager_tie");

// If the wager is a tie, send the wager amount back to both parties
let msgs = vec![
send_tokens(
deps.api.addr_validate(
&Cw721Contract(wager.id.0 .0)
.owner_of(&deps.querier, wager.id.0 .1.to_string(), true)?
.owner,
)?,
coin(wager.amount.u128(), NATIVE_DENOM),
)?,
send_tokens(
deps.api.addr_validate(
&Cw721Contract(wager.id.1 .0)
.owner_of(&deps.querier, wager.id.1 .1.to_string(), true)?
.owner,
)?,
coin(wager.amount.u128(), NATIVE_DENOM),
)?,
];
return Ok(res.add_submessages(msgs));
};

let winner_addr = Cw721Contract(winner.0)
.owner_of(&deps.querier, winner.1.to_string(), true)?
.owner;

// Pay out the winner
let wager_total = wager.amount * Uint128::from(2u128);

let app_fee = wager_total * config.fee_percent / Uint128::from(100u128);
Expand All @@ -239,7 +265,7 @@ pub fn execute_set_winner(
coin(app_fee.u128(), NATIVE_DENOM),
)?;
let winner_msg = send_tokens(
deps.api.addr_validate(&token_owner)?,
deps.api.addr_validate(&winner_addr)?,
coin(winner_amount.u128(), NATIVE_DENOM),
)?;

Expand Down
3 changes: 2 additions & 1 deletion src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ pub enum ExecuteMsg {
/// Use Authz
SetWinner {
wager_key: (Token, Token),
winner: Token,
prev_prices: (u64, u64),
current_prices: (u64, u64),
},

/// User-facing
Expand Down
Loading

0 comments on commit 3b54ae2

Please sign in to comment.