-
Notifications
You must be signed in to change notification settings - Fork 21
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
Generate queriers for interfaces #28
Comments
This would be a great help to add. |
Hey @hashedone I wrote some such structs for use with mesh-security, focusing just on calling the traits / interfaces (which is what we are importing). Check out osmosis-labs/mesh-security#21 I think the code there could totally be auto-generated (I added the last Let me know if this looks doable. |
@ethanfrey I've checked your helpers - they are ok, but in the general case, they might not be as perfect. You create a long-living query with an address inside and then add functions to it, but the problem is - this works only if you know exactly what contract you query, so you lose the idea of interfaces. And there would be a way to achieve this for interfaces - maybe a querier is defined for the interface keeping its address. But then there is a problem with combining interfaces - maybe I care about two interfaces, but depending on the context (some methods use That is why, in the initial proposal, queriers are temp short-living objects doing all the functionality for a trait or contract. The task would be split into three small ones:
|
The PR is done, but generated structs are oversimplified, so there would be a real problem extending in the future. Let me formalize what should be generated here, splitting it into expected PRs: PR1 - Remote structureAll contracts should have a type for storing them as remote contracts with some address. I suggest the pub struct Remote<'a>(Cow<'a, Addr>);
impl Remote<'static> {
pub fn new(addr: Addr) -> Self {
todo!()
}
}
impl Remote<'a> {
pub fn borrowed(addr: &'a Addr) -> Self {
todo!()
}
} The type does nothing for now, but it exists on all interfaces and contracts, and all helper traits (querier, executors) will be implemented on this. For contracts, we need some additional things to be generated to make it easy to convert the contract-level remotes to trait-level remotes: impl<'a> From<&'a Remote<'a>> for counter::Remote<'a> {
fn from(...) -> Self {
todo!()
}
}
let remote = counter::Remote::from(&contract_remote); Note that the address is kept as
Note that all the remotes are named the same way - it would vastly improve implementation, but what is more important, it is more clear in fact. The thing is that in Rust use crate::counter;
fn foo(addr: Addr) {
let remote = counter::Remote::new(addr);
} |
PR2 - queriersHaving a remote wrapper, the queries are to be added. Every "message source" (so contract and interface) should generate a struct BoundQuerier<'a> {
addr: &'a Addr,
querier: &'a cosmwasm_std::Querier,
}
impl Remote<'_> {
fn querier(&self, querier: &cosmwasm_std::Querier) -> BoundQuerier {
todo!();
}
} The next step is to generate the pub trait Querier {
fn count(&self, /* all message arguments */) -> /* The result type */;
} The trait should be implemented on the This is everything for this PR. It still has an inconvenience known as "how to call methods from traits on the remote for contract". The answer is: create a temporary remote for trait: let remote = contract::Remote::new(addr);
let resp = counter::Remote::from(&remote).querier(&deps.querier).count()?; This could be nicer, but we want to minimize the size of PRs - this will be solved in a second. |
PR3 -
|
PR4 - interface-level
|
It would be helpful to provide utilities to easily query the contract.
For interfaces:
Additionally for contracts:
This would allow keeping the contract in storage with semantical contract type, and then directly call queries. The access to underlying address is obviously required (eg. for sending submessages). Also implementing
impl From<Cw1WhitelistContractProxy> for Addr
should probably happen for free unwrapping.The text was updated successfully, but these errors were encountered: