Skip to content

Commit

Permalink
feat: base tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ratik committed Dec 29, 2023
1 parent 736fb83 commit 52c3fe1
Show file tree
Hide file tree
Showing 10 changed files with 337 additions and 436 deletions.
17 changes: 10 additions & 7 deletions contracts/hook-tester/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use lido_helpers::answer::response;
use lido_puppeteer_base::msg::{ResponseHookErrorMsg, ResponseHookMsg, ResponseHookSuccessMsg};
use lido_staking_base::{
msg::hook_tester::{ExecuteMsg, InstantiateMsg, QueryMsg},
state::hook_tester::{ANSWERS, CONFIG, ERRORS},
state::hook_tester::{Config, ANSWERS, CONFIG, ERRORS},
};
use neutron_sdk::{
bindings::{msg::NeutronMsg, query::NeutronQuery},
Expand Down Expand Up @@ -51,7 +51,7 @@ pub fn execute(
validator,
amount,
timeout,
} => execute_delegate(deps, env, validator, amount, timeout),
} => execute_delegate(deps, env, info, validator, amount, timeout),
ExecuteMsg::Undelegate {
validator,
amount,
Expand Down Expand Up @@ -87,6 +87,8 @@ fn hook_success(
_info: MessageInfo,
answer: ResponseHookSuccessMsg,
) -> ContractResult<Response<NeutronMsg>> {
deps.api
.debug(&format!("WASMDEBUG: hook_success: {:?}", answer));
let attrs = vec![attr("action", "hook-success")];
ANSWERS.update(deps.storage, |mut answers| -> ContractResult<_> {
answers.push(answer);
Expand All @@ -101,6 +103,8 @@ fn hook_error(
_info: MessageInfo,
answer: ResponseHookErrorMsg,
) -> ContractResult<Response<NeutronMsg>> {
deps.api
.debug(&format!("WASMDEBUG: hook_error: {:?}", answer));
let attrs = vec![attr("action", "hook-success")];
ERRORS.update(deps.storage, |mut errors| -> ContractResult<_> {
errors.push(answer);
Expand All @@ -116,16 +120,14 @@ fn execute_set_config(
puppeteer_addr: String,
) -> ContractResult<Response<NeutronMsg>> {
let attrs = vec![attr("action", "set-config")];
CONFIG.update(deps.storage, |mut config| -> ContractResult<_> {
config.puppeteer_addr = puppeteer_addr;
Ok(config)
})?;
CONFIG.save(deps.storage, &Config { puppeteer_addr })?;
Ok(response("set-config", "hook-tester", attrs))
}

fn execute_delegate(
deps: DepsMut<NeutronQuery>,
env: Env,
info: MessageInfo,
validator: String,
amount: Uint128,
timeout: Option<u64>,
Expand All @@ -135,6 +137,7 @@ fn execute_delegate(
attr("action", "delegate"),
attr("validator", validator.clone()),
attr("amount", amount.to_string()),
attr("funds", format!("{:?}", info.funds)),
];
let msg = CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: config.puppeteer_addr,
Expand All @@ -144,7 +147,7 @@ fn execute_delegate(
timeout,
reply_to: env.contract.address.to_string(),
})?,
funds: vec![],
funds: info.funds,
});
Ok(response("execute-delegate", "hook-tester", attrs).add_message(msg))
}
Expand Down
63 changes: 43 additions & 20 deletions contracts/puppeteer/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use neutron_sdk::{
use lido_puppeteer_base::{
error::ContractResult,
msg::{
PuppeteerHook, QueryMsg, ResponseHookErrorMsg, ResponseHookMsg, ResponseHookSuccessMsg,
Transaction,
QueryMsg, ReceiverExecuteMsg, ResponseHookErrorMsg, ResponseHookMsg,
ResponseHookSuccessMsg, Transaction,
},
state::{IcaState, PuppeteerBase, State, TxState, TxStateStatus, ICA_ID},
};
Expand Down Expand Up @@ -61,7 +61,6 @@ pub fn instantiate(
msg: InstantiateMsg,
) -> NeutronResult<Response> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

let owner = deps.api.addr_validate(&msg.owner)?;
let allowed_senders = msg
.allowed_senders
Expand Down Expand Up @@ -178,6 +177,10 @@ fn execute_delegate(
amount: amount.to_string(),
}),
};
deps.api.debug(&format!(
"WASMDEBUG: execute_delegate: delegate_msg: {delegate_msg:?}",
delegate_msg = delegate_msg
));

let submsg = compose_submsg(
deps.branch(),
Expand Down Expand Up @@ -445,6 +448,7 @@ fn sudo_response(
request: RequestPacket,
data: Binary,
) -> NeutronResult<Response> {
deps.api.debug(&format!("WASMDEBUG: sudo response"));
let attrs = vec![
attr("action", "sudo_response"),
attr("request_id", request.sequence.unwrap_or(0).to_string()),
Expand All @@ -454,15 +458,20 @@ fn sudo_response(
.sequence
.ok_or_else(|| StdError::generic_err("sequence not found"))?;
let tx_state = puppeteer_base.tx_state.load(deps.storage)?;
if tx_state.status != TxStateStatus::InProgress {
return Err(NeutronError::Std(StdError::generic_err(
"Transaction state is not in progress",
)));
}
deps.api
.debug(&format!("WASMDEBUG: tx_state {:?}", tx_state));
ensure_eq!(
tx_state.status,
TxStateStatus::WaitingForAck,
NeutronError::Std(StdError::generic_err(
"Transaction state is not waiting for ack",
))
);
deps.api.debug(&format!("WASMDEBUG: sudo response 5"));
let reply_to = tx_state
.reply_to
.ok_or_else(|| StdError::generic_err("reply_to not found"))?;

deps.api.debug(&format!("WASMDEBUG: sudo response 6"));
let transaction = tx_state
.transaction
.ok_or_else(|| StdError::generic_err("transaction not found"))?;
Expand All @@ -475,7 +484,6 @@ fn sudo_response(
reply_to: None,
},
)?;

let msg_data: TxMsgData = TxMsgData::decode(data.as_slice())?;
deps.api
.debug(&format!("WASMDEBUG: msg_data: data: {msg_data:?}"));
Expand Down Expand Up @@ -529,16 +537,31 @@ fn sudo_response(
lido_puppeteer_base::msg::ResponseAnswer::UnknownResponse {}
}
};
deps.api.debug(&format!(
"WASMDEBUG: sudo_response: answer: {answer:?}",
answer = answer
));
deps.api.debug(&format!(
"WASMDEBUG: json: {request:?}",
request = to_json_binary(&ReceiverExecuteMsg::PuppeteerHook(
ResponseHookMsg::Success(ResponseHookSuccessMsg {
request_id: seq_id,
request: request.clone(),
transaction: transaction.clone(),
answer: answer.clone(),
},)
))?
));
msgs.push(CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: reply_to.clone(),
msg: to_json_binary(&PuppeteerHook(ResponseHookMsg::Success(
ResponseHookSuccessMsg {
msg: to_json_binary(&ReceiverExecuteMsg::PuppeteerHook(
ResponseHookMsg::Success(ResponseHookSuccessMsg {
request_id: seq_id,
request: request.clone(),
transaction: transaction.clone(),
answer,
},
)))?,
}),
))?,
funds: vec![],
}))
}
Expand Down Expand Up @@ -572,9 +595,9 @@ fn sudo_error(
let tx_state = puppeteer_base.tx_state.load(deps.storage)?;
ensure_eq!(
tx_state.status,
TxStateStatus::InProgress,
TxStateStatus::WaitingForAck,
NeutronError::Std(StdError::generic_err(
"Transaction state is not in progress",
"Transaction state is not waiting for ack",
))
);
let seq_id = request
Expand All @@ -585,7 +608,7 @@ fn sudo_error(
contract_addr: tx_state
.reply_to
.ok_or_else(|| StdError::generic_err("reply_to not found"))?,
msg: to_json_binary(&PuppeteerHook(ResponseHookMsg::Error(
msg: to_json_binary(&ReceiverExecuteMsg::PuppeteerHook(ResponseHookMsg::Error(
ResponseHookErrorMsg {
request_id: seq_id,
request,
Expand Down Expand Up @@ -622,9 +645,9 @@ fn sudo_timeout(
let tx_state = puppeteer_base.tx_state.load(deps.storage)?;
ensure_eq!(
tx_state.status,
TxStateStatus::InProgress,
TxStateStatus::WaitingForAck,
NeutronError::Std(StdError::generic_err(
"Transaction state is not in progress",
"Transaction state is not waiting for ack",
))
);
let puppeteer_base: PuppeteerBase<'_, Config> = Puppeteer::default();
Expand Down Expand Up @@ -653,7 +676,7 @@ fn sudo_timeout(
contract_addr: tx_state
.reply_to
.ok_or_else(|| StdError::generic_err("reply_to not found"))?,
msg: to_json_binary(&PuppeteerHook(ResponseHookMsg::Error(
msg: to_json_binary(&ReceiverExecuteMsg::PuppeteerHook(ResponseHookMsg::Error(
ResponseHookErrorMsg {
request_id: seq_id,
request,
Expand Down
18 changes: 9 additions & 9 deletions integration_tests/src/generated/contractLib/lidoPuppeteer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ export type Addr = string;
* let c = Uint128::from(70u32); assert_eq!(c.u128(), 70); ```
*/
export type Uint128 = string;
export type ArrayOfTransfer = Transfer[];
export type IcaState = "none" | "in_progress" | "registered" | "timeout";
export type ArrayOfTransfer = Transfer[];

export interface LidoPuppeteerSchema {
responses: Config | DelegationsResponse | ArrayOfTransfer | State;
responses: Config | DelegationsResponse | State | ArrayOfTransfer;
execute:
| RegisterDelegatorDelegationsQueryArgs
| SetFeesArgs
Expand Down Expand Up @@ -81,17 +81,17 @@ export interface Coin {
denom: string;
[k: string]: unknown;
}
export interface State {
ica?: string | null;
ica_state: IcaState;
last_processed_height?: number | null;
}
export interface Transfer {
amount: string;
denom: string;
recipient: string;
sender: string;
}
export interface State {
ica?: string | null;
ica_state: IcaState;
last_processed_height?: number | null;
}
export interface RegisterDelegatorDelegationsQueryArgs {
validators: string[];
}
Expand Down Expand Up @@ -171,8 +171,8 @@ export class Client {
queryState = async(): Promise<State> => {
return this.client.queryContractSmart(this.contractAddress, { state: {} });
}
queryInterchainTransactions = async(): Promise<ArrayOfTransfer> => {
return this.client.queryContractSmart(this.contractAddress, { interchain_transactions: {} });
queryTransactions = async(): Promise<ArrayOfTransfer> => {
return this.client.queryContractSmart(this.contractAddress, { transactions: {} });
}
queryDelegations = async(): Promise<DelegationsResponse> => {
return this.client.queryContractSmart(this.contractAddress, { delegations: {} });
Expand Down
Loading

0 comments on commit 52c3fe1

Please sign in to comment.