Skip to content

Commit

Permalink
Clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drewstaylor committed Aug 4, 2023
1 parent 192cd7c commit e979a1f
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 100 deletions.
85 changes: 58 additions & 27 deletions schema/execute_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"properties": {
"create": {
"$ref": "#/definitions/CreateMsg"
"$ref": "#/definitions/SwapMsg"
}
},
"additionalProperties": false
Expand All @@ -21,7 +21,7 @@
],
"properties": {
"finish": {
"$ref": "#/definitions/CreateMsg"
"$ref": "#/definitions/SwapMsg"
}
},
"additionalProperties": false
Expand All @@ -37,6 +37,26 @@
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"update_config"
],
"properties": {
"update_config": {
"type": "object",
"required": [
"config"
],
"properties": {
"config": {
"$ref": "#/definitions/Config"
}
}
}
},
"additionalProperties": false
}
],
"definitions": {
Expand All @@ -55,38 +75,18 @@
}
}
},
"CreateMsg": {
"Config": {
"type": "object",
"required": [
"contract",
"expires",
"id",
"payment_token",
"price",
"swap_type",
"token_id"
"admin",
"cw721"
],
"properties": {
"contract": {
"admin": {
"$ref": "#/definitions/Addr"
},
"expires": {
"$ref": "#/definitions/Expiration"
},
"id": {
"type": "string"
},
"payment_token": {
"cw721": {
"$ref": "#/definitions/Addr"
},
"price": {
"$ref": "#/definitions/Uint128"
},
"swap_type": {
"type": "boolean"
},
"token_id": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -136,6 +136,37 @@
}
]
},
"SwapMsg": {
"type": "object",
"required": [
"expires",
"id",
"payment_token",
"price",
"swap_type",
"token_id"
],
"properties": {
"expires": {
"$ref": "#/definitions/Expiration"
},
"id": {
"type": "string"
},
"payment_token": {
"$ref": "#/definitions/Addr"
},
"price": {
"$ref": "#/definitions/Uint128"
},
"swap_type": {
"type": "boolean"
},
"token_id": {
"type": "string"
}
}
},
"Timestamp": {
"description": "A point in time in nanosecond precision.\n\nThis type can represent times from 1970-01-01T00:00:00Z to 2554-07-21T23:34:33Z.\n\n## Examples\n\n``` # use cosmwasm_std::Timestamp; let ts = Timestamp::from_nanos(1_000_000_202); assert_eq!(ts.nanos(), 1_000_000_202); assert_eq!(ts.seconds(), 1); assert_eq!(ts.subsec_nanos(), 202);\n\nlet ts = ts.plus_seconds(2); assert_eq!(ts.nanos(), 3_000_000_202); assert_eq!(ts.seconds(), 3); assert_eq!(ts.subsec_nanos(), 202); ```",
"allOf": [
Expand Down
20 changes: 19 additions & 1 deletion schema/instantiate_msg.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "InstantiateMsg",
"type": "object"
"type": "object",
"required": [
"admin",
"cw721"
],
"properties": {
"admin": {
"$ref": "#/definitions/Addr"
},
"cw721": {
"$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"
}
}
}
26 changes: 11 additions & 15 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use cw721_base::{
};

use crate::msg::{
CancelMsg, CreateMsg, DetailsResponse, ExecuteMsg, InstantiateMsg,
CancelMsg, SwapMsg, DetailsResponse, ExecuteMsg, InstantiateMsg,
QueryMsg, ListResponse, MigrateMsg,
};
use crate::state::{
Expand Down Expand Up @@ -133,7 +133,7 @@ pub fn execute_create(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: CreateMsg,
msg: SwapMsg,
) -> Result<Response, ContractError> {
if msg.expires.is_expired(&env.block) {
return Err(ContractError::Expired {});
Expand Down Expand Up @@ -164,19 +164,10 @@ pub fn execute_create(
.add_attribute("price", swap.price))
}

// pub fn execute_create_default(
// deps: DepsMut,
// env: Env,
// info: MessageInfo,
// msg: CreateDefaultMsg,
// )-> Result<Response, ContractError> {

// }

pub fn execute_finish(deps: DepsMut,
env: Env,
info: MessageInfo,
msg: CreateMsg
msg: SwapMsg
)-> Result<Response, ContractError> {
let swap = SWAPS.load(deps.storage, &msg.id)?;
let can = CANCELLED.may_load(deps.storage, &msg.id)?;
Expand All @@ -192,6 +183,7 @@ pub fn execute_finish(deps: DepsMut,
return Err(ContractError::Completed {});
}

// XXX: @jjj This part is pretty confusing
let transfer_results = match msg.swap_type {
true => handle_swap_transfers(&swap.creator, &info.sender, swap.clone())?,
false => handle_swap_transfers(&info.sender, &swap.creator, swap.clone())?,
Expand Down Expand Up @@ -247,7 +239,11 @@ pub fn execute_update_config(
.add_attribute("action", "update_config"))
}

fn handle_swap_transfers(nft_sender:&Addr,nft_receiver: &Addr,details:CW721Swap) -> StdResult<Vec<CosmosMsg>> {
fn handle_swap_transfers(
nft_sender: &Addr,
nft_receiver: &Addr,
details: CW721Swap
) -> StdResult<Vec<CosmosMsg>> {
let token_transfer_msg = Cw20ExecuteMsg::TransferFrom {
owner: nft_receiver.to_string(),
recipient:nft_sender.to_string(),
Expand Down Expand Up @@ -309,7 +305,7 @@ mod tests {
let res = instantiate(deps.as_mut(), mock_env(), info, instantiate_msg).unwrap();
assert_eq!(0, res.messages.len());

let creation_msg = CreateMsg {
let creation_msg = SwapMsg {
id: "firstswap".to_string(),
payment_token: Addr::unchecked(MOCK_CONTRACT_ADDR),
token_id: "2343".to_string(),
Expand All @@ -322,7 +318,7 @@ mod tests {

execute(deps.as_mut(), mock_env(), info2, ExecuteMsg::Create(creation_msg)).unwrap();

let creation_msg2 = CreateMsg{
let creation_msg2 = SwapMsg {
id: "2ndswap".to_string(),
payment_token: Addr::unchecked(MOCK_CONTRACT_ADDR),
token_id: "2343".to_string(),
Expand Down
112 changes: 64 additions & 48 deletions src/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use cw721_base::{
use cw721::OwnerOfResponse;

use crate::msg::{
ExecuteMsg, DetailsResponse, QueryMsg, CreateMsg, InstantiateMsg,
ExecuteMsg, DetailsResponse, QueryMsg, SwapMsg, InstantiateMsg,
};

fn mock_app() -> App {
Expand Down Expand Up @@ -119,96 +119,112 @@ pub fn query<M,T>(router: &mut App, target_contract: Addr, msg: M) -> Result<T,
fn test_buy() {
let mut app = mock_app();

let owner = Addr::unchecked("owner");
let nft_owner = Addr::unchecked("nft_owner");
let nft = create_cw721(&mut app, &owner);
let swap = create_swap(&mut app, &owner, nft.clone());
// Swap owner deploys
let swap_admin = Addr::unchecked("swap_deployer");
// cw721_owner owns the cw721
let cw721_owner = Addr::unchecked("original_owner");
// cw20_owner owns the cw20
let cw20_owner = Addr::unchecked("cw20_owner");

// cw721_owner creates the cw721
let nft = create_cw721(&mut app, &cw721_owner);

// swap_admin creates the swap contract
let swap = create_swap(&mut app, &swap_admin, nft.clone());
let swap_inst = swap.clone();
let erc20 = create_cw20(

// cw20_owner creates a cw20 coin
let cw20 = create_cw20(
&mut app,
&owner,
&cw20_owner,
"testcw".to_string(),
"tscw".to_string(),
Uint128::from(100000_u32)
);
let erc20_inst = erc20.clone();
let cw20_inst = cw20.clone();

// cw721_owner mints a cw721
let token_id = "petrify".to_string();
let token_uri = "https://www.merriam-webster.com/dictionary/petrify".to_string();


let mint_msg = Cw721ExecuteMsg::Mint(MintMsg::<Extension> {
token_id: token_id.clone(),
owner: String::from("nft_owner"),
owner: cw721_owner.to_string(),
token_uri: Some(token_uri.clone()),
extension: None,
});

let _res = app
.execute_contract(owner.clone(), nft.clone(), &mint_msg, &[])
.execute_contract(cw721_owner.clone(), nft.clone(), &mint_msg, &[])
.unwrap();

let nft_approve_msg = Cw721ExecuteMsg::Approve::<Extension> {
spender: swap.to_string(),
token_id: token_id.clone(),
expires: None,
};
app
.execute_contract(nft_owner.clone(), nft.clone(), &nft_approve_msg, &[])
.unwrap();

let cw20_approve_msg = Cw20ExecuteMsg::IncreaseAllowance {
spender: swap.to_string(),
amount: Uint128::from(100000_u32),
expires: None,
};

app
.execute_contract(owner.clone(), erc20.clone(), &cw20_approve_msg, &[])
.unwrap();

let creation_msg = CreateMsg {
// Create a SwapMsg for creating / finishing a swap
let creation_msg = SwapMsg {
id: "firstswap".to_string(),
payment_token: Addr::unchecked(erc20),
payment_token: Addr::unchecked(cw20.clone()),
token_id: token_id.clone(),
expires: Expiration::from(cw20::Expiration::AtHeight(384798573487439743)),
price: Uint128::from(100000_u32),
swap_type: true,
};

let finish_msg = creation_msg.clone();

// Seller (cw721_owner) must approve the swap contract to spend their NFT
let nft_approve_msg = Cw721ExecuteMsg::Approve::<Extension> {
spender: swap.to_string(),
token_id: token_id.clone(),
expires: None,
};
app
.execute_contract(cw721_owner.clone(), nft.clone(), &nft_approve_msg, &[])
.unwrap();

// cw721 seller (cw721_owner) creates a swap
app
.execute_contract(nft_owner.clone(), swap_inst.clone(), &ExecuteMsg::Create(creation_msg), &[])
.execute_contract(cw721_owner.clone(), swap_inst.clone(), &ExecuteMsg::Create(creation_msg), &[])
.unwrap();

// cw721 buyer (cw20_owner) must approve swap contract to spend their cw20
let cw20_approve_msg = Cw20ExecuteMsg::IncreaseAllowance {
spender: swap.to_string(),
amount: Uint128::from(100000_u32),
expires: None,
};
app
.execute_contract(owner.clone(), swap_inst.clone(), &ExecuteMsg::Finish(finish_msg), &[])
.execute_contract(cw20_owner.clone(), cw20, &cw20_approve_msg, &[])
.unwrap();

let qres: DetailsResponse = query(

// Buyer purchases cw721, consuming the swap
app
.execute_contract(cw20_owner.clone(), swap_inst.clone(), &ExecuteMsg::Finish(finish_msg), &[])
.unwrap();

// Swap is now closed (open == false)
let swap_query: DetailsResponse = query(
&mut app,
swap_inst.clone(),
QueryMsg::Details{
id: "firstswap".to_string()
}
).unwrap();

let new_owner: OwnerOfResponse = query(

// cw20_owner has received the NFT
let owner_query: OwnerOfResponse = query(
&mut app,nft.clone(),
Cw721QueryMsg::OwnerOf {
token_id: token_id,
include_expired: None
}
).unwrap();

let new_balance: BalanceResponse =query(

// cw721_owner has received the cw20 amount
let balance_query: BalanceResponse = query(
&mut app,
erc20_inst,
cw20_inst,
Cw20QueryMsg::Balance {
address: nft_owner.to_string()
address: cw721_owner.to_string()
}
).unwrap();

assert_eq!(qres.open, false);
assert_eq!(new_owner.owner, owner);
assert_eq!(new_balance.balance, Uint128::from(100000_u32));
assert_eq!(swap_query.open, false);
assert_eq!(owner_query.owner, cw20_owner);
assert_eq!(balance_query.balance, Uint128::from(100000_u32));
}
Loading

0 comments on commit e979a1f

Please sign in to comment.