-
Notifications
You must be signed in to change notification settings - Fork 9
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
Pass full data to callback #19
Changes from 3 commits
01d2688
08d5644
22defb1
b181538
31ddf84
120baf3
db4b307
91fd9f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
use cosmwasm_std::{Addr, Binary}; | ||
use cosmwasm_std::{Addr, SubMsgResponse}; | ||
use cw_storage_plus::Item; | ||
|
||
/// Stores the instantiator of the contract. | ||
pub const INSTANTIATOR: Item<Addr> = Item::new("owner"); | ||
|
||
/// Stores a list of callback's currently being collected. Has no | ||
/// value if none are being collected. | ||
pub const COLLECTOR: Item<Vec<Option<Binary>>> = Item::new("callbacks"); | ||
pub const COLLECTOR: Item<Vec<Option<SubMsgResponse>>> = Item::new("callbacks"); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
use cosmwasm_schema::cw_serde; | ||
use cosmwasm_std::{ | ||
to_binary, Addr, Api, Binary, CosmosMsg, IbcPacketAckMsg, IbcPacketTimeoutMsg, StdResult, | ||
Storage, WasmMsg, | ||
Storage, SubMsgResponse, WasmMsg, | ||
}; | ||
use cw_storage_plus::{Item, Map}; | ||
|
||
|
@@ -30,9 +30,17 @@ pub struct CallbackMessage { | |
|
||
#[cw_serde] | ||
pub enum Callback { | ||
/// Callback data from a query. | ||
Query(CallbackData<Vec<Binary>>), | ||
/// Callback data from message execution. | ||
Execute(CallbackData<Vec<SubMsgResponse>>), | ||
} | ||
|
||
#[cw_serde] | ||
pub enum CallbackData<T> { | ||
/// Data returned from the host chain. Index n corresponds to the | ||
/// result of executing the nth message/query. | ||
Success(Vec<Option<Binary>>), | ||
Success(T), | ||
/// The first error that occured while executing the requested | ||
/// messages/queries. | ||
Error(String), | ||
|
@@ -63,6 +71,7 @@ pub fn request_callback( | |
api: &dyn Api, | ||
initiator: Addr, | ||
request: Option<CallbackRequest>, | ||
request_type: RequestType, | ||
) -> StdResult<()> { | ||
let seq = SEQ.may_load(storage)?.unwrap_or_default() + 1; | ||
SEQ.save(storage, &seq)?; | ||
|
@@ -78,6 +87,7 @@ pub fn request_callback( | |
initiator, | ||
initiator_msg, | ||
receiver, | ||
request_type, | ||
}, | ||
)?; | ||
} | ||
|
@@ -119,7 +129,7 @@ pub fn on_ack( | |
return None | ||
}; | ||
CALLBACKS.remove(storage, original_packet.sequence); | ||
let result = unmarshal_ack(acknowledgement); | ||
let result = unmarshal_ack(acknowledgement, request.request_type.clone()); | ||
Some(callback_msg(request, result)) | ||
} | ||
|
||
|
@@ -133,17 +143,25 @@ pub fn on_timeout( | |
return None | ||
}; | ||
CALLBACKS.remove(storage, packet.sequence); | ||
Some(callback_msg( | ||
request, | ||
Callback::Error("timeout".to_string()), | ||
)) | ||
let result = match request.request_type { | ||
RequestType::Execute => Callback::Execute(CallbackData::Error("timeout".to_string())), | ||
RequestType::Query => Callback::Query(CallbackData::Error("timeout".to_string())), | ||
}; | ||
Some(callback_msg(request, result)) | ||
} | ||
|
||
#[cw_serde] | ||
struct PendingCallback { | ||
initiator: Addr, | ||
initiator_msg: Binary, | ||
receiver: Addr, | ||
request_type: RequestType, | ||
} | ||
|
||
#[cw_serde] | ||
pub enum RequestType { | ||
Execute, | ||
Query, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Created this type to we can figure out what type of callback we need to do. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe a little docstring?
|
||
} | ||
|
||
const CALLBACKS: Map<u64, PendingCallback> = Map::new("polytone-callbacks"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some errors where we don't know what the type of the callback is, so created this type to match against before executing the callback
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
two thoughts about this type:
unmarshal_ack
?InternalError
also, a small test for you: