Skip to content
Open
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
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,88 +1,95 @@
# Smart Contract basic usage/deployment

Examples provided for the Archway blockchain using `archwayd` CLI tool

## Deployment

### Store contract code

```
archwayd tx wasm store cosmos_reward_pool_contract.wasm --from admin --gas-prices $(archwayd q rewards estimate-fees 1 | jq -r '.gas_unit_price | (.amount + .denom)') -y --gas 5000000 | jq -r '.txhash'
```

### Query code id

```
archwayd q tx <previous-tx-hash> | jq -r '.logs [0] .events[] | select(.type=="store_code").attributes[] | select(.key=="code_id") | .value'
```

### Instantiate contract

```
archwayd tx wasm instantiate <code-id> '{}' --gas-prices $(archwayd q rewards estimate-fees 1 | jq -r '.gas_unit_price | (.amount + .denom)') -y --gas 5000000 --label test123 --no-admin --from admin | jq -r '.txhash'
```

### Get the contract address

```
archwayd q tx <previous-tx-hash> | jq -r '.logs [0] .events[] | select(.type=="instantiate").attributes[] | select(.key=="_contract_address") | .value'
```

The latest deployed contract is archway124ljgdsns7zqngyx0jengsh90kh06jv9dqq8kxuvnw4509mhrwgqmlnrg7

## Function calls

**deposit** — Create a new reward pool
```
archwayd tx wasm execute archway124ljgdsns7zqngyx0jengsh90kh06jv9dqq8kxuvnw4509mhrwgqmlnrg7 '{ "deposit" : { "campaign_id": "12345" } }' --amount 1000000aconst --from admin --gas-prices $(archwayd q rewards estimate-fees 1 | jq -r '.gas_unit_price | (.amount + .denom)') -y --gas 400000 | jq -r '.txhash'
```

**check** — Check if the reward pool has been created and deduct creation fee
```
archwayd tx wasm execute archway124ljgdsns7zqngyx0jengsh90kh06jv9dqq8kxuvnw4509mhrwgqmlnrg7 '{ "check" : {"requests": [{"campaign_id": "12345", "amount": "900000"}]} }' --from admin --gas-prices $(archwayd q rewards estimate-fees 1 | jq -r '.gas_unit_price | (.amount + .denom)') -y --gas 400000 | jq -r '.txhash'
archwayd tx wasm execute archway124ljgdsns7zqngyx0jengsh90kh06jv9dqq8kxuvnw4509mhrwgqmlnrg7 '{ "deposit" : { "campaign_id": "12345" } }' --amount 1000000aconst --from admin --gas-prices $(archwayd q rewards estimate-fees 1 | jq -r '.gas_unit_price | (.amount + .denom)') -y --gas 400000 | jq -r '.txhash'
```

**reward_all** — Assign reward to a user

```
archwayd tx wasm execute archway124ljgdsns7zqngyx0jengsh90kh06jv9dqq8kxuvnw4509mhrwgqmlnrg7 '{ "reward_all" : {"user_rewards": [{"campaign_id": "12345", "user_address": "archway18v032krrt0sud25y2vk9vj49lvwkg2hxlxupwf", "amount": "10000"}]} }' --from admin --gas-prices $(archwayd q rewards estimate-fees 1 | jq -r '.gas_unit_price | (.amount + .denom)') -y --gas 400000 | jq -r '.txhash'
```

**claim** — Claim a reward that was assigned to a user

```
archwayd tx wasm execute archway124ljgdsns7zqngyx0jengsh90kh06jv9dqq8kxuvnw4509mhrwgqmlnrg7 '{ "claim" : {"campaign_id": "12345"} }' --from admin--gas-prices $(archwayd q rewards estimate-fees 1 | jq -r '.gas_unit_price | (.amount + .denom)') -y --gas 400000 | jq -r '.txhash'
```

**withdraw** — Withdraw a specific amount of coins from the contract
```
archwayd tx wasm execute archway124ljgdsns7zqngyx0jengsh90kh06jv9dqq8kxuvnw4509mhrwgqmlnrg7 '{ "withdraw" : {"amount": "1000"} }' --from admin --gas-prices $(archwayd q rewards estimate-fees 1 | jq -r '.gas_unit_price | (.amount + .denom)') -y --gas 400000 | jq -r '.txhash'
```

**withdraw_fee** — Withdraw only campaign creation fees
```
archwayd tx wasm execute archway124ljgdsns7zqngyx0jengsh90kh06jv9dqq8kxuvnw4509mhrwgqmlnrg7 '{ "withdraw_fee" : {} }' --from admin --gas-prices $(archwayd q rewards estimate-fees 1 | jq -r '.gas_unit_price | (.amount + .denom)') -y --gas 400000 | jq -r '.txhash'
archwayd tx wasm execute archway124ljgdsns7zqngyx0jengsh90kh06jv9dqq8kxuvnw4509mhrwgqmlnrg7 '{ "withdraw" : {"amount": "1000"} }' --from admin --gas-prices $(archwayd q rewards estimate-fees 1 | jq -r '.gas_unit_price | (.amount + .denom)') -y --gas 400000 | jq -r '.txhash'
```

**set_cpool** — Modify the amount of a campaign pool, create new if it doesn’t exist

```
archwayd tx wasm execute archway124ljgdsns7zqngyx0jengsh90kh06jv9dqq8kxuvnw4509mhrwgqmlnrg7 '{ "set_cpool" : {"campaign_id": "1111", "amount": "1000"} }' --from admin --gas-prices $(archwayd q rewards estimate-fees 1 | jq -r '.gas_unit_price | (.amount + .denom)') -y --gas 400000 | jq -r '.txhash'
```

**set_upool** — Modify the amount of a campaign pool, create new if it doesn’t exist

```
archwayd tx wasm execute archway124ljgdsns7zqngyx0jengsh90kh06jv9dqq8kxuvnw4509mhrwgqmlnrg7 '{ "set_upool" : {"user_address": "", "reward_pool_id": "2222", "amount": "1000"} }' --from admin --gas-prices $(archwayd q rewards estimate-fees 1 | jq -r '.gas_unit_price | (.amount + .denom)') -y --gas 400000 | jq -r '.txhash'
```

**set_claim_fee** — Set the new value for the fee that user must send when claiming the reward

```
archwayd tx wasm execute archway124ljgdsns7zqngyx0jengsh90kh06jv9dqq8kxuvnw4509mhrwgqmlnrg7 '{ "set_claim_fee" : {"claim_fee": "10000000"} }' --from admin --gas-prices $(archwayd q rewards estimate-fees 1 | jq -r '.gas_unit_price | (.amount + .denom)') -y --gas 400000 | jq -r '.txhash'
```

## Queries

**get_cpool** — Query campaign pool

```
archwayd q wasm contract-state smart archway124ljgdsns7zqngyx0jengsh90kh06jv9dqq8kxuvnw4509mhrwgqmlnrg7 '{ "get_cpool" : { "campaign_id": "12345" } }'
```

**get_upool** — Query user pool for specific user and reward pool

```
archwayd q wasm contract-state smart archway124ljgdsns7zqngyx0jengsh90kh06jv9dqq8kxuvnw4509mhrwgqmlnrg7 '{ "get_upool" : { "user_address": "archway18v032krrt0sud25y2vk9vj49lvwkg2hxlxupwf", "campaign_id": "12345" } }'
```

**get_claim_fee** — Query campaign pool

```
archwayd q wasm contract-state smart archway124ljgdsns7zqngyx0jengsh90kh06jv9dqq8kxuvnw4509mhrwgqmlnrg7 '{ "get_claim_fee" : {} }'
```
92 changes: 1 addition & 91 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use cosmwasm_std::entry_point;
use cosmwasm_std::{DepsMut, Env, Response, MessageInfo, StdError, CosmosMsg, BankMsg, Coin, Uint128, Deps, StdResult, Binary, to_binary, Empty};
use crate::state::{Campaign, CAMPAIGN_POOL, State, STATE, USER_POOL};
use crate::msg::{CampaignCheckRequest, CampaignCheckResponse, ExecuteMsg, InstantiateMsg, QueryMsg, UserRewardRequest, UserRewardResponse};
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg, UserRewardRequest, UserRewardResponse};
use cw2::{get_contract_version, set_contract_version};
use semver::Version;

Expand All @@ -19,7 +19,6 @@ pub fn instantiate(

let state = State {
owner: deps.api.addr_canonicalize(info.sender.as_str())?,
withdrawable_creation_fee: Uint128::zero(),
claim_reward_fee: msg.claim_reward_fee.unwrap_or(Uint128::new(1000000000000000000)),
};

Expand Down Expand Up @@ -79,23 +78,12 @@ pub fn execute(
info,
campaign_id,
),
ExecuteMsg::Check { requests } => check(
deps,
env,
info,
requests,
),
ExecuteMsg::Withdraw { amount } => withdraw(
deps,
env,
info,
amount,
),
ExecuteMsg::WithdrawFee {} => withdraw_fee(
deps,
env,
info,
),
ExecuteMsg::SetRefundable { campaign_id } => set_refundable(
deps,
env,
Expand Down Expand Up @@ -283,56 +271,6 @@ pub fn claim(
}
}

pub fn check(
deps: DepsMut,
_env: Env,
info: MessageInfo,
requests: Vec<CampaignCheckRequest>,
) -> Result<Response, StdError> {
let mut state = STATE.load(deps.storage)?;

if deps.api.addr_canonicalize(info.sender.as_str())? != state.owner {
return Err(StdError::generic_err("Only contract owner can call this function"));
}

if requests.is_empty() {
return Err(StdError::generic_err("No reward requests provided"));
}

let mut res = vec![];

for request in requests {
match CAMPAIGN_POOL.may_load(deps.storage, request.campaign_id.clone())? {
Some(mut campaign) => {
let delta = campaign.amount.checked_sub(request.amount);
if delta.is_err() {
return Err(StdError::generic_err("Provided amount is greater than the current campaign amount"));
}

state.withdrawable_creation_fee += delta.unwrap();
STATE.save(deps.storage, &state)?;

res.push(CampaignCheckResponse {
campaign_id: request.campaign_id.clone(),
owner: campaign.owner.clone().to_string(),
amount_before_deduction: campaign.amount,
});

campaign.amount = request.amount;

CAMPAIGN_POOL.save(deps.storage, request.campaign_id.clone(), &campaign)?;
}
None => {
return Err(StdError::generic_err("Campaign does not exist"))
}
}
}
Ok(Response::new()
.add_attribute("method", "check")
.set_data(to_binary(&res).unwrap())
)
}

pub fn withdraw(
deps: DepsMut,
env: Env,
Expand Down Expand Up @@ -368,34 +306,6 @@ pub fn withdraw(
return Ok(res)
}

pub fn withdraw_fee(
deps: DepsMut,
_env: Env,
info: MessageInfo,
) -> Result<Response, StdError> {
let mut state = STATE.load(deps.storage)?;

if deps.api.addr_canonicalize(info.sender.as_str())? != state.owner {
return Err(StdError::generic_err("Only contract owner can withdraw"));
}

let amount = state.withdrawable_creation_fee;
state.withdrawable_creation_fee = Uint128::zero();

STATE.save(deps.storage, &state)?;
let bond_denom = deps.querier.query_bonded_denom()?;
let to_address = deps.api.addr_humanize(&state.owner)?.to_string();

let res = Response::new()
.add_attribute("method", "withdraw_fee")
.add_message(CosmosMsg::Bank(BankMsg::Send {
to_address,
amount: vec![Coin { denom: bond_denom.clone(), amount }],
}));

return Ok(res)
}

pub fn set_refundable(
deps: DepsMut,
_env: Env,
Expand Down
17 changes: 0 additions & 17 deletions src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@ pub struct UserRewardResponse {
pub status: bool,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct CampaignCheckRequest {
pub campaign_id: String,
pub amount: Uint128,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct CampaignCheckResponse {
pub campaign_id: String,
pub owner: String,
pub amount_before_deduction: Uint128,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
Expand All @@ -45,13 +32,9 @@ pub enum ExecuteMsg {
Claim {
campaign_id: String
},
Check {
requests: Vec<CampaignCheckRequest>
},
Withdraw {
amount: Uint128,
},
WithdrawFee {},
SetRefundable {
campaign_id: String
},
Expand Down
1 change: 0 additions & 1 deletion src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub struct Campaign {
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct State {
pub owner: CanonicalAddr,
pub withdrawable_creation_fee: Uint128,
pub claim_reward_fee: Uint128,
}

Expand Down
6 changes: 3 additions & 3 deletions src/tests/execute/cancel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use cosmwasm_std::{
};

use crate::contract::{
cancel, check, claim, deposit, instantiate, reward_all, set_claim_fee, set_cpool,
set_refundable, set_upool, withdraw, withdraw_fee,
cancel, claim, deposit, instantiate, reward_all, set_claim_fee, set_cpool,
set_refundable, set_upool, withdraw,
};
use crate::msg::{
CampaignCheckRequest, CampaignCheckResponse, InstantiateMsg, UserRewardRequest,
InstantiateMsg, UserRewardRequest,
UserRewardResponse,
};
use crate::state::{Campaign, State, CAMPAIGN_POOL, STATE, USER_POOL};
Expand Down
Loading