Skip to content

Commit

Permalink
sample-app: add support for querying the counter application
Browse files Browse the repository at this point in the history
  • Loading branch information
Twey committed Nov 6, 2024
1 parent 0238b3f commit 0b9548e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
20 changes: 8 additions & 12 deletions sample-app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@
<head>
<script src="linera.js"></script>
<script>
async function makeQuery() {
const response = await Linera.sendRequest({
'function': 'query',
'arguments': [3],
});
console.log('response: ', response);
async function queryValidators() {
const response = await Linera.callClientFunction('validator_version_info');
console.log(response);
}


async function queryValidators() {
const response = await Linera.sendRequest({
'function': 'query_validators',
'arguments': [],
});
async function queryApplication() {
const COUNTER_APP_ID = '95ad45ed433db1e40c9e5f677e9e1369fa4c207aeae67098ae668a2fdf241328557f90979fe8f4ef93225c8b3a9e5c9afb1e31884cd092e4ee1bdb5be90dcd0ee476187f6ddfeb9d588c7b45d3df334d5501d6499b3f9ad5595cae86cce16a65010000000000000000000000';
const response = await Linera.callClientFunction('query_application', COUNTER_APP_ID, '{ "query": "query { value }" }');
console.log(JSON.parse(response));
}
</script>
</head>
<body>
<button onclick="makeQuery()" type="button">Make a query</button>
<button onclick="queryValidators()" type="button">Query validators</button>
<button onclick="queryApplication()" type="button">Query application</button>
</body>
</html>
39 changes: 23 additions & 16 deletions sample-app/linera.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,30 @@ const Linera = (() => {
let nextMessageId = 0;
let responses = new Map();

return {
load: async () => await loaded,
sendRequest: async request => {
await loaded;
async function sendRequest(request) {
await loaded;

return new Promise(resolve => {
responses.set(nextMessageId, resolve);
window.dispatchEvent(new CustomEvent(
"linera-wallet-request",
{
detail: {
id: nextMessageId,
message: request,
},
return await new Promise(resolve => {
responses.set(nextMessageId, resolve);
window.dispatchEvent(new CustomEvent(
"linera-wallet-request",
{
detail: {
id: nextMessageId++,
message: request,
},
));
});
},
},
));
});
}

return {
load: async () => await loaded,
sendRequest,
callClientFunction: async (func, ...args) => await sendRequest({
type: 'client_call',
function: func,
arguments: args,
}),
};
})();

0 comments on commit 0b9548e

Please sign in to comment.