Skip to content

Commit

Permalink
multiple-rolls
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisbaccour committed Oct 18, 2022
1 parent dc0c6b2 commit d8755c3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
37 changes: 37 additions & 0 deletions contracts/double-dice-roll/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ pub fn execute(
match msg {
//RollDice should be called by a player who wants to roll the dice
ExecuteMsg::RollDice { job_id } => execute_roll_dice(deps, env, info, job_id),
ExecuteMsg::RollDiceMultipleTimes { job_id, n_times } => {
execute_roll_dice_multiple_times(deps, env, info, job_id, n_times)
}
//Receive should be called by the proxy contract. The proxy is forwarding the randomness from the nois chain to this contract.
ExecuteMsg::Receive { callback } => execute_receive(deps, env, info, callback),
}
Expand Down Expand Up @@ -79,6 +82,40 @@ pub fn execute_roll_dice(
Ok(response)
}

pub fn execute_roll_dice_multiple_times(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
job_id: String,
n_times: u64,
) -> Result<Response, ContractError> {
//Prevent a player from paying for an already existing randomness.
//The actual immutability of the history comes in the execute_receive function
if DOUBLE_DICE_OUTCOME
.may_load(deps.storage, &job_id)?
.is_some()
{
return Err(ContractError::JobIdAlreadyPresent);
}
let response = Response::new();
for job in 1..n_times {
let nois_proxy = NOIS_PROXY.load(deps.storage)?;

response.to_owned().add_message(WasmMsg::Execute {
contract_addr: nois_proxy.into(),
//GetNextRandomness requests the randomness from the proxy
//The job id is needed to know what randomness we are referring to upon reception in the callback
//In this example, the job_id represents one round of dice rolling.
msg: to_binary(&ProxyExecuteMsg::GetNextRandomness {
job_id: job_id.to_owned() + "-" + &job.to_string(),
})?,
//For now the randomness is for free. You don't need to send any funds to request randomness
funds: vec![],
});
}
Ok(response)
}

//The execute_receive function is triggered upon reception of the randomness from the proxy contract
//The callback contains the randomness from drand (HexBinary) and the job_id
pub fn execute_receive(
Expand Down
1 change: 1 addition & 0 deletions contracts/double-dice-roll/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct InstantiateMsg {
pub enum ExecuteMsg {
// job_id for this job which allows for gathering the results.
RollDice { job_id: String },
RollDiceMultipleTimes { job_id: String, n_times: u64 },
//callback contains the randomness from drand (HexBinary) and job_id
//callback should only be allowed to be called by the proxy contract
Receive { callback: NoisCallback },
Expand Down

0 comments on commit d8755c3

Please sign in to comment.