Skip to content

Commit

Permalink
feat: support new x/posts features (#301)
Browse files Browse the repository at this point in the history
~~Deps: #300~~
~~Deps #308
Deps: #362

This PR intends to support new posts msgs and queries.
  • Loading branch information
dadamu authored Nov 9, 2023
1 parent 06d176a commit b8f20ab
Show file tree
Hide file tree
Showing 27 changed files with 648 additions and 129 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/chain-interaction.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
uses: actions/checkout@v3
with:
repository: desmos-labs/desmos
ref: v6.1.0
ref: v6.2.0
path: ./desmos-src

- name: Build desmos chain ⚙
Expand Down
10 changes: 7 additions & 3 deletions packages/bindings-test/src/chain_communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ impl DesmosCli {
/// Gets a contract address by it's id.
///
/// * `id` - ID of the contract of interest.
pub fn get_contract_by_code(&self, id: u64) -> String {
/// * `offset` - Offset of the target contract.
pub fn get_contract_by_code(&self, id: u64, offset: usize) -> String {
let mut cmd = self.desmos();

cmd.arg("query")
Expand All @@ -144,8 +145,11 @@ impl DesmosCli {

result
.contracts
.get(0)
.expect(&format!("can't find smart contract with id {}", id))
.get(offset)
.expect(&format!(
"can't find smart contract with id {} and offset {}",
id, offset
))
.to_string()
}

Expand Down
6 changes: 6 additions & 0 deletions packages/bindings-test/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ pub const USER1_ADDRESS: &str = "desmos1jnpfa06xhflyjh6klwlrq8mk55s53czh6ncdm3";
pub const USER2_KEY: &str = "user2";
pub const USER2_ADDRESS: &str = "desmos1ptvq7l4jt7n9sc3fky22mfvc6waf2jd8nuc0jv";

// x/subspaces
pub const TEST_SUBSPACE: u64 = 1;
pub const TEST_SUBSPACE_USER_GROUP: u32 = 1;

// x/posts
pub const TEST_SUBSPACE_EDITABLE_POST_ID: u64 = 1;
pub const TEST_SUBSPACE_DELETABLE_POST_ID: u64 = 2;
pub const TEST_DELETABLE_ATTACHMENT_ID: u32 = 2;
pub const TEST_POLL_ID: u32 = 1;

// x/reports
pub const TEST_REASON_ID: u32 = 1;
pub const TEST_DELETABLE_REASON_ID: u32 = 2;
pub const TEST_REPORT_ID_WITH_USER_TARGET: u64 = 1;
pub const TEST_DELETABLE_REPORT_ID: u64 = 3;

// x/reactions
pub const TEST_EDITABLE_REGISTERED_REACTION_ID: u32 = 1;
pub const TEST_DELETABLE_REGISTERED_REACTION_ID: u32 = 2;
pub const TEST_REACTIONS_POST_ID: u64 = 3;
Expand Down
66 changes: 66 additions & 0 deletions packages/bindings-test/src/posts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,68 @@
mod msg;
mod query;

use crate::chain_communication::DesmosCli;
use cosmwasm_std::Addr;

use test_contract::msg::QueryMsg::DesmosChain;

use desmos_bindings::cosmos_types::PageRequest;
use desmos_bindings::posts::msg::PostsMsg;
use desmos_bindings::posts::types::{
Entities, Post, QuerySubspacePostsRequest, QuerySubspacePostsResponse, ReplySetting, Url,
};

/// Creates a post inside the given subspace for interaction testing
pub fn create_test_post(subspace_id: u64, contract_address: &str) -> Post {
let desmos_cli = DesmosCli::default();

// Create a post
let create_post_msg = PostsMsg::create_post(
subspace_id,
0,
None,
"Sample post",
Some(Entities {
urls: vec![Url {
start: 0,
end: 1,
url: "https://ipfs.infura.io/ipfs/QmT3AenKHkhCeesTUdnarqUVu91mmBk1cxQknxnUd79gY7"
.into(),
display_url: "IPFS".into(),
}],
hashtags: vec![],
mentions: vec![],
}),
vec![],
vec![],
Addr::unchecked(contract_address),
None,
ReplySetting::Everyone,
vec![],
);
desmos_cli
.execute_contract(contract_address, vec![create_post_msg.into()])
.assert_success();

// query the created post
let result: QuerySubspacePostsResponse = desmos_cli
.wasm_query(
contract_address,
&DesmosChain {
request: QuerySubspacePostsRequest {
subspace_id,
pagination: Some(PageRequest {
key: vec![],
limit: 1,
offset: 0,
count_total: false,
reverse: true,
}),
}
.into(),
},
)
.to_object();

result.posts.first().unwrap().clone()
}
157 changes: 151 additions & 6 deletions packages/bindings-test/src/posts/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ mod test {
TEST_DELETABLE_ATTACHMENT_ID, TEST_POLL_ID, TEST_SUBSPACE, TEST_SUBSPACE_DELETABLE_POST_ID,
TEST_SUBSPACE_EDITABLE_POST_ID,
};
use crate::posts::create_test_post;

use chrono::DateTime;
use cosmwasm_std::Addr;
use desmos_bindings::posts::msg::PostsMsg;
use desmos_bindings::posts::types::{
poll::ProvidedAnswer, AttachmentContent, Entities, Media, Poll, PostReference,
PostReferenceType, ReplySetting, Url,
};
use desmos_bindings::subspaces::msg::SubspacesMsg;
use test_contract::msg::ExecuteMsg;

#[test]
fn test_create_post() {
let desmos_cli = DesmosCli::default();
let contract_address = desmos_cli.get_contract_by_code(1);
let contract_address = desmos_cli.get_contract_by_code(1, 0);

let msg = PostsMsg::create_post(
TEST_SUBSPACE,
Expand Down Expand Up @@ -50,7 +53,7 @@ mod test {
#[test]
fn test_edit_post() {
let desmos_cli = DesmosCli::default();
let contract_address = desmos_cli.get_contract_by_code(1);
let contract_address = desmos_cli.get_contract_by_code(1, 0);

let msg = PostsMsg::edit_post(
TEST_SUBSPACE,
Expand Down Expand Up @@ -85,7 +88,7 @@ mod test {
#[test]
fn test_delete_post() {
let desmos_cli = DesmosCli::default();
let contract_address = desmos_cli.get_contract_by_code(1);
let contract_address = desmos_cli.get_contract_by_code(1, 0);

let msg = PostsMsg::delete_post(
TEST_SUBSPACE,
Expand All @@ -106,7 +109,7 @@ mod test {
#[test]
fn test_add_media_post_attachment() {
let desmos_cli = DesmosCli::default();
let contract_address = desmos_cli.get_contract_by_code(1);
let contract_address = desmos_cli.get_contract_by_code(1, 0);

let msg_add_media = PostsMsg::add_post_attachment(
TEST_SUBSPACE,
Expand Down Expand Up @@ -159,7 +162,7 @@ mod test {
#[test]
fn test_remove_post_attachment() {
let desmos_cli = DesmosCli::default();
let contract_address = desmos_cli.get_contract_by_code(1);
let contract_address = desmos_cli.get_contract_by_code(1, 0);

let msg = PostsMsg::remove_post_attachment(
TEST_SUBSPACE,
Expand All @@ -181,7 +184,7 @@ mod test {
#[test]
fn test_answer_poll() {
let desmos_cli = DesmosCli::default();
let contract_address = desmos_cli.get_contract_by_code(1);
let contract_address = desmos_cli.get_contract_by_code(1, 0);

let msg = PostsMsg::answer_poll(
TEST_SUBSPACE,
Expand All @@ -200,4 +203,146 @@ mod test {
)
.assert_success();
}

#[test]
fn test_move_post() {
let desmos_cli = DesmosCli::default();
let contract_address = desmos_cli.get_contract_by_code(1, 0);

// Create target subspace for moving post
desmos_cli
.execute_contract(
&contract_address,
vec![SubspacesMsg::create_subspace(
"Test target subspace",
"",
Addr::unchecked(&contract_address),
Addr::unchecked(&contract_address),
)
.into()],
)
.assert_success();

// Get target subspace and post
let target_subspace = desmos_cli
.query_subspaces(None)
.subspaces
.last()
.unwrap()
.clone();
let post = create_test_post(TEST_SUBSPACE, &contract_address);

let move_post_msg = PostsMsg::move_post(
TEST_SUBSPACE,
post.id,
target_subspace.id,
0,
Addr::unchecked(&contract_address),
);

desmos_cli
.execute_contract(&contract_address, vec![move_post_msg.into()])
.assert_success()
}

#[test]
fn test_request_then_cancel_post_owner_transfer() {
let desmos_cli = DesmosCli::default();
let contract_address = desmos_cli.get_contract_by_code(1, 0);
let receiver_contract_address = desmos_cli.get_contract_by_code(1, 1);

let post = create_test_post(TEST_SUBSPACE, &contract_address);
let request_post_owner_transfer_msg = PostsMsg::request_post_owner_transfer(
TEST_SUBSPACE,
post.id,
Addr::unchecked(&receiver_contract_address),
Addr::unchecked(&contract_address),
);

let cancel_post_owner_transfer_request_msg = PostsMsg::cancel_post_owner_transfer_request(
TEST_SUBSPACE,
post.id,
Addr::unchecked(&contract_address),
);

desmos_cli
.execute_contract(
&contract_address,
vec![
request_post_owner_transfer_msg.into(),
cancel_post_owner_transfer_request_msg.into(),
],
)
.assert_success()
}

#[test]
fn test_request_then_accept_post_owner_transfer() {
let desmos_cli = DesmosCli::default();
let contract_address = desmos_cli.get_contract_by_code(1, 0);
let receiver_contract_address = desmos_cli.get_contract_by_code(1, 1);

// Create a post owner transfer request to receiver
let post = create_test_post(TEST_SUBSPACE, &contract_address);
let request_post_owner_transfer_msg = PostsMsg::request_post_owner_transfer(
TEST_SUBSPACE,
post.id,
Addr::unchecked(&receiver_contract_address),
Addr::unchecked(&contract_address),
);
desmos_cli
.execute_contract(
&contract_address,
vec![request_post_owner_transfer_msg.into()],
)
.assert_success();

// Receiver accepts a post owner transfer request
let accept_post_owner_transfer_request_msg = PostsMsg::accept_post_owner_transfer_request(
TEST_SUBSPACE,
post.id,
Addr::unchecked(&receiver_contract_address),
);
desmos_cli
.execute_contract(
&receiver_contract_address,
vec![accept_post_owner_transfer_request_msg.into()],
)
.assert_success()
}

#[test]
fn test_request_then_refuse_post_owner_transfer() {
let desmos_cli = DesmosCli::default();
let contract_address = desmos_cli.get_contract_by_code(1, 0);
let receiver_contract_address = desmos_cli.get_contract_by_code(1, 1);

// Create a post owner transfer request to receiver
let post = create_test_post(TEST_SUBSPACE, &contract_address);
let request_post_owner_transfer_msg = PostsMsg::request_post_owner_transfer(
TEST_SUBSPACE,
post.id,
Addr::unchecked(&receiver_contract_address),
Addr::unchecked(&contract_address),
);
desmos_cli
.execute_contract(
&contract_address,
vec![request_post_owner_transfer_msg.into()],
)
.assert_success();

// Receiver refuses a post owner transfer request
let refuse_post_owner_transfer_request_msg = PostsMsg::refuse_post_owner_transfer_request(
TEST_SUBSPACE,
post.id,
Addr::unchecked(&receiver_contract_address),
);
desmos_cli
.execute_contract(
&receiver_contract_address,
vec![refuse_post_owner_transfer_request_msg.into()],
)
.assert_success()
}
}
Loading

0 comments on commit b8f20ab

Please sign in to comment.