virto-network / virto-sdk

Virto toolset to build simple to use decentralized applications
GNU General Public License v3.0
21 stars 5 forks source link

Sube Examples: Multiple Queries At once for multiple Chains/Parachains #28

Open S0c5 opened 1 year ago

S0c5 commented 1 year ago

As a developer in the Dotsama ecosystem, usually i have to interact with multiple parachains (including the Kusama relychain) at once, the main idea with this example is to show how to interact or make multiple queries at once.

let [account1, account2] = sube_multi!([
"http://kusama.node/balances/account/0x123123123",
"http://statemine.node/balances/account/0x123123123",
])
[
 sube!(""),
 sube!("")
]
olanod commented 1 year ago

I think we can adapt the sube! macro to change based on different combinations of parameters to fit the simple and more complex cases.

I think something along this lines should be possible

// simple usage for storage queries, under the hood uses the builder to create a type that
// implements `IntoFuture` that can be awaited(returns a scales::Value ?).
let account = sube!("balances/account/0x1234").await?;
// multi query
let (balanceKSM, balanceUSDT) = sube!(
    "balances/account/0x1234", 
    "statemine://assets/account/1984/0x1234",
).await?;
// subscribe to updates, the type returned by the builder has a method(e.g. `next`) to get updates
// when async itereators are supported there is a some form of for-await it will be even more convenient to use 
while let Some(n) = sube!("system/number").next().await {
    // handle update
}
// Single extrinsic
sube!("balances/transfer" => { dest: "0x1234", value: 100_000  }).await?;
// Multi extrinsic
sube!{
    "balances/transfer" => { dest: "0x1234", value: 100_000  },
    "statemine://assets/transfer" => { id: 1984, target: "0x1234", amount: 100_000  },
}.await?;