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

Cosmos Treasury Contract #43

Draft
wants to merge 55 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
2d2d4f0
Add nft treasury contract
sms5030 Oct 2, 2022
d80de59
Modify nft treasury contract (Delete Mint & Burn)
sms5030 Oct 2, 2022
a2fc0a7
added contract.rs
Oct 7, 2022
f5474c9
added lib, msg & corrected contract
Oct 9, 2022
57b105c
corrected contract
Oct 9, 2022
1b19bf4
Modify contract codes / Add error codes
sms5030 Oct 10, 2022
e7115f3
Modify contract codes
sms5030 Oct 10, 2022
9689e69
Delete NFT Treasury Contract
sms5030 Oct 11, 2022
ce0bf1f
Modify Treasury Contract(interaction with Light Client)
sms5030 Oct 11, 2022
1fcfaa2
Modify Treasury Codes (Modify Transfer, Balance logic (using Bank and…
sms5030 Oct 16, 2022
226c281
Modify Treasury Contract
sms5030 Oct 22, 2022
0f3f705
add and change folder name
Oct 27, 2022
7c3579a
copied test-light-client-cross to treasury
Oct 27, 2022
0319607
combined treasury & light client
Oct 27, 2022
6f3b0ef
Modify Test Contracts for achiving Cross Contract
sms5030 Oct 31, 2022
5d783c0
Modify Contract Codes / Add Integration Tests Code (but not sure)
sms5030 Nov 19, 2022
9ae8221
updated
Nov 19, 2022
f2212b5
Merge branch 'main' of https://github.com/sms5030/cosmos
Nov 19, 2022
2426fba
test configuration fixed
Nov 23, 2022
2e68c5f
Modify Error codes
sms5030 Nov 23, 2022
824fa48
Modify Error Codes
sms5030 Nov 23, 2022
0615b08
Modify Error Codes
sms5030 Nov 23, 2022
0ef192d
Modify Error Codes
sms5030 Nov 23, 2022
63414b0
Modify Error Codes
sms5030 Nov 23, 2022
8755a4e
Modify Error Codes
sms5030 Nov 23, 2022
ede74cf
Modify Error Codes
sms5030 Nov 23, 2022
f80f15b
Modify Error Codes
sms5030 Nov 23, 2022
e27b415
Modify Error Codes
sms5030 Nov 23, 2022
2faa767
Modify Error Codes
sms5030 Nov 23, 2022
f2d3190
Modify Error Codes
sms5030 Nov 23, 2022
6305e0d
Modify Error Codes
sms5030 Nov 23, 2022
e063e54
Rename contract (treasuy -> treasury)
sms5030 Nov 23, 2022
5141385
Modify Error Codes
sms5030 Nov 23, 2022
160eb9b
Modify Error Codes
sms5030 Nov 23, 2022
eeb3a2a
Modify Error Codes
sms5030 Nov 23, 2022
2eb9af1
Modify Error Codes
sms5030 Nov 23, 2022
8324ab9
Modify Error Codes
sms5030 Nov 23, 2022
181e128
Modify Error Codes
sms5030 Nov 23, 2022
bdf0278
Modify Error Codes
sms5030 Nov 23, 2022
19b447f
Modify Error Codes
sms5030 Nov 23, 2022
ad56790
Modify Error Codes
sms5030 Nov 23, 2022
69a4330
Modify Error Codes
sms5030 Nov 23, 2022
bdad39d
Add Query Balance Test
sms5030 Nov 23, 2022
3a8d1e6
Add query all balances test (not complete)
sms5030 Nov 23, 2022
a4a9791
Modify Query All Balances Test (Partially Successed)
sms5030 Nov 23, 2022
5bb03c5
Modify query_balance (more test cases) / Modify query_all_balances (c…
sms5030 Nov 23, 2022
cee5df0
Modify Error Code in execute_tranfer / Resolve All Warning Msgs
sms5030 Nov 24, 2022
ab3ea83
transfer_test incompleted
Nov 27, 2022
9c3351b
completed transfer_test
Nov 27, 2022
5216689
incompleted transfer_fail_test
Nov 27, 2022
37aefbc
Add 2 tests (Verify Fail Test, Zero Amount)
sms5030 Nov 27, 2022
b929cc4
Adjust rustfmt
sms5030 Nov 27, 2022
7974ed0
Adjust clippy
sms5030 Nov 27, 2022
e4e2c64
Adjust rustfmt
sms5030 Nov 27, 2022
0e86f2f
Adjust clippy
sms5030 Nov 27, 2022
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace]
members = [
"contracts/simple-counter",
"contracts/light-client",
"contracts/treasury",
"interact",
"colony-chain"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cw2::set_contract_version;
use pdao_beacon_chain_common::message::DeliverableMessage;

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, GetHeaderResponse, InstantiateMsg, QueryMsg};
use crate::msg::{ExecuteMsg, GetHeaderResponse, CheckVerifyResponse, InstantiateMsg, QueryMsg};
use crate::state::{State, STATE};
use pdao_colony_contract_common::LightClient;

Expand Down Expand Up @@ -91,6 +91,11 @@ pub fn execute_verify(
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::GetHeader {} => to_binary(&query_header(deps)?),
QueryMsg::CheckVerify {
message,
block_height,
proof,
} => to_binary(&query_verify(deps, _env, info, message, block_height, proof)?),
}
}

Expand All @@ -100,3 +105,22 @@ fn query_header(deps: Deps) -> StdResult<GetHeaderResponse> {
header: state.light_client.last_header,
})
}

fn query_verify(deps: DepsMut,
_env: Env,
_info: MessageInfo,
message: DeliverableMessage,
block_height: u64,
proof: String,
) -> Result<Response, ContractError> {
let state = STATE.load(deps.storage)?;

let is_verified: bool = false;
if state.light_client.verify_commitment(message, block_height, proof) {
is_verified = true;
}

OK(CheckVerifyResponse {
is_verified: is_verified,
})
}
49 changes: 49 additions & 0 deletions contracts/test-light-client-cross/src/msg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use pdao_beacon_chain_common::message::DeliverableMessage;
use pdao_colony_contract_common::light_client::{BlockFinalizationProof, Header, MerkleProof};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct InstantiateMsg {
pub header: Header,
pub chain_name: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
Update {
header: Header,
proof: BlockFinalizationProof,
},
Verify {
message: DeliverableMessage,
block_height: u64,
proof: MerkleProof,
},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
GetHeader {},
CheckVerify {
message: DeliverableMessage,
block_height: u64,
proof: MerkleProof,
},
}

// We define a custom struct for each query response
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct GetHeaderResponse {
pub header: Header,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
#[serde(rename_all = "snake_case")]
pub struct CheckVerifyResponse {
pub is_verified: bool,
}
3 changes: 3 additions & 0 deletions contracts/test-treasury-cross/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[alias]
wasm = "build --release --target wasm32-unknown-unknown"
schema = "run --example schema"
20 changes: 20 additions & 0 deletions contracts/test-treasury-cross/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::env::current_dir;
use std::fs::create_dir_all;

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

use pdao_cosmos_light_client::msg::{ExecuteMsg, GetHeaderResponse, InstantiateMsg, QueryMsg};
use pdao_cosmos_light_client::state::State;

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!(State), &out_dir);
export_schema(&schema_for!(GetHeaderResponse), &out_dir);
}
39 changes: 39 additions & 0 deletions contracts/test-treasury-cross/schema/execute_msg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ExecuteMsg",
"anyOf": [
{
"type": "object",
"required": [
"increment"
],
"properties": {
"increment": {
"type": "object"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"reset"
],
"properties": {
"reset": {
"type": "object",
"required": [
"count"
],
"properties": {
"count": {
"type": "integer",
"format": "int32"
}
}
}
},
"additionalProperties": false
}
]
}
14 changes: 14 additions & 0 deletions contracts/test-treasury-cross/schema/get_count_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "GetCountResponse",
"type": "object",
"required": [
"count"
],
"properties": {
"count": {
"type": "integer",
"format": "int32"
}
}
}
14 changes: 14 additions & 0 deletions contracts/test-treasury-cross/schema/instantiate_msg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "InstantiateMsg",
"type": "object",
"required": [
"count"
],
"properties": {
"count": {
"type": "integer",
"format": "int32"
}
}
}
18 changes: 18 additions & 0 deletions contracts/test-treasury-cross/schema/query_msg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "QueryMsg",
"anyOf": [
{
"type": "object",
"required": [
"get_count"
],
"properties": {
"get_count": {
"type": "object"
}
},
"additionalProperties": false
}
]
}
24 changes: 24 additions & 0 deletions contracts/test-treasury-cross/schema/state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "State",
"type": "object",
"required": [
"count",
"owner"
],
"properties": {
"count": {
"type": "integer",
"format": "int32"
},
"owner": {
"$ref": "#/definitions/Addr"
}
},
"definitions": {
"Addr": {
"description": "A human readable address.\n\nIn Cosmos, this is typically bech32 encoded. But for multi-chain smart contracts no assumptions should be made other than being UTF-8 encoded and of reasonable length.\n\nThis type represents a validated address. It can be created in the following ways 1. Use `Addr::unchecked(input)` 2. Use `let checked: Addr = deps.api.addr_validate(input)?` 3. Use `let checked: Addr = deps.api.addr_humanize(canonical_addr)?` 4. Deserialize from JSON. This must only be done from JSON that was validated before such as a contract's state. `Addr` must not be used in messages sent by the user because this would result in unvalidated instances.\n\nThis type is immutable. If you really need to mutate it (Really? Are you sure?), create a mutable copy using `let mut mutable = Addr::to_string()` and operate on that `String` instance.",
"type": "string"
}
}
}
116 changes: 116 additions & 0 deletions contracts/test-treasury-cross/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_sts::{CosmosMsg, coins, BankMsg, WasmMsg, Uint128, BankQuery}
use cosmwasm_std::{to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};
use cw2::set_contract_version;
use crate::error::ContractError;
use crate::msg::{ExecuteMsg, QueryMsg, VerifyQueryMsg, VerifyResponse, BalanceResponse, BalanceAllResponse};

const CONTRACT_NAME: &str = "crates.io:treasury";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
const LIGHT_CLIENT_CONTRACT_ADDRESS = "emtpy"

#[cfg_attr(not(feature="library"), entry_point)]
pub fn instantiate(
deps: DepsMut,
_envs: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError>{
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
Ok(Response::new().add_attribute("method", "instantiate"))
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
_env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Transfer {recipient, amount, denom, message, block_height, proof} => execute_varified_transfer(deps, _env, info, recipient, amount, denom, message, block_height, proof),
}
}

fn execute_varified_transfer(
deps: DepsMut<'_>,
_env: Env,
info: MessageInfo,
recipient: String,
amount: Uint128,
denom: String,
message: DeliverableMessage,
block_height: u64,
proof: MerkleProof,
) -> Result<Response, ContractError> {

if amount == Uint128::zero() {
return Err(ContractError::InvalidZeroAmount {});
}

let mut msgs: Vec<CosmosMsg> = vec![];

// TO DO (Begin)
let query_msg: VerifyQueryMsg = VerifyQueryMsg::CheckVerify({
message: message,
block_height: block_height,
proof: proof,
});
let query_response: VerifyResponse = deps.querier.query(
&QueryRequest::Wasm(WasmQuery::Smart {
contract_addr: LIGHT_CLIENT_CONTRACT_ADDRESS,
message: to_binary(&query_msg)?,
}))?;

if (query_response.is_verified) {
let amount_int: u128 = amount.parse().unwrap();
msgs.push(CosmosMsg::Bank(BankMsg::Send{
to_address: recipient,
amount: coins(
amount_int,
denom,
),
}));
} else {
Err(ContractError::VerifyFail {})
}

Ok(
Response::new()
.add_attribute("method", "execute_verified_transfer")
.add_messages(msgs)
)
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(
deps: Deps,
_env: Env,
msg: QueryMsg
) -> StdResult<Binary> {

match msg {
QueryMsg::GetBalance {address, denom} => to_binary(&query_balance(deps, address, denom)?),
QueryMsg::GetAllBalance {address} => to_binary(&query_all_balance(address)?),
}
}

fn query_balance(
deps:Deps,
address: String,
denom: String,
) -> StdResult<Coin> {

let balanceResponse: BalanceResponse = deps.querier.query_balance(address, denom)?;
Ok(balanceResponse)
}

fn query_all_balance(
deps:Deps,
address: String,
) -> StdResult<Coin> {

let balanceResponse: BalanceAllResponse = deps.querier.query_all_balance(address)?;
Ok(balanceResponse)
}
19 changes: 19 additions & 0 deletions contracts/test-treasury-cross/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use cosmwasm_std::StdError;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),

#[error("Verification failed")]
VerifyFail {},

#[error("Invalid zero amount")]
InvalidZeroAmount {},

#[error("Custom Error val: {val:?}")]
CustomError { val: String },
// Add any other custom errors you like here.
// Look at https://docs.rs/thiserror/1.0.21/thiserror/ for details.
}
Loading