Skip to content

Commit

Permalink
Update to most recent contracts API
Browse files Browse the repository at this point in the history
  • Loading branch information
dincho committed Oct 25, 2024
1 parent ab4b42a commit 66cd120
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 32 deletions.
20 changes: 15 additions & 5 deletions src/lib/aesdk/contractState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@ export const getContractState = async (
sdkInstance: SdkInstance
): Promise<[MainStakingState, HCElectionState]> => {
// console.log('staking contract', sdkInstance.stakingContract);
console.log('Call MainStaking:get_state');
const resp = await sdkInstance.stakingContract.$call('get_state', [], { callStatic: true });
// console.log('resp', resp.decodedResult);
console.log('MainStaking state', resp.decodedResult);
const msState = MainStakingState.parse(resp.decodedResult);


console.log('Get HCElection state');
const hcElectionState = await sdkInstance.hcElectionContract.$call('get_state', [], {
callStatic: true
});
// console.log('hcElectionState', hcElectionState);
console.log('HCElection state', hcElectionState.decodedResult);
const hcState = HCElectionState.parse(hcElectionState.decodedResult);
// console.log('state', resp.decodedResult);
const msState = MainStakingState.parse(resp.decodedResult);
// console.log('Call HCElection:leader');
// const leader = await sdkInstance.hcElectionContract.$call('leader', [], {
// callStatic: true
// });
// console.log('hcElection leader', leader.decodedResult);
// const hcState = HCElectionState.parse({leader: leader.decodedResult});

return [msState, hcState];
};

Expand Down Expand Up @@ -73,7 +83,7 @@ export const MainStakingState = z
.strict();
export type MainStakingState = z.infer<typeof MainStakingState>;

export const HCElectionState = z.object({ leader: z.string(), entropy: z.string() });
export const HCElectionState = z.object({ leader: z.string() });
export type HCElectionState = z.infer<typeof HCElectionState>;

export const ContractState = z.object({
Expand Down
95 changes: 71 additions & 24 deletions src/lib/contracts/HCElection.aci.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
{ "namespace": { "name": "ListInternal", "typedefs": [] } },
{ "namespace": { "name": "List", "typedefs": [] } },
{ "namespace": { "name": "Pair", "typedefs": [] } },
{
"contract": {
"functions": [
Expand All @@ -11,6 +12,13 @@
"returns": { "list": [{ "tuple": ["address", "int"] }] },
"stateful": false
},
{
"arguments": [{ "name": "_1", "type": "address" }],
"name": "is_validator",
"payable": false,
"returns": "bool",
"stateful": false
},
{
"arguments": [],
"name": "total_stake",
Expand All @@ -36,34 +44,36 @@
"contract": {
"functions": [
{
"arguments": [
{ "name": "main_staking_ct", "type": "MainStaking" },
{ "name": "entropy_str", "type": "string" }
],
"arguments": [{ "name": "main_staking_ct", "type": "MainStaking" }],
"name": "init",
"payable": false,
"returns": "HCElection.state",
"stateful": false
},
{
"arguments": [
{ "name": "entropy_str", "type": "string" },
{ "name": "commitments", "type": { "map": ["hash", { "list": ["address"] }] } }
],
"name": "elect",
"arguments": [{ "name": "epoch_length", "type": "int" }],
"name": "init_epochs",
"payable": false,
"returns": { "tuple": [] },
"stateful": true
},
{
"arguments": [{ "name": "leader", "type": "address" }],
"name": "step",
"payable": false,
"returns": { "tuple": ["address", "int"] },
"returns": { "tuple": [] },
"stateful": true
},
{
"arguments": [
{ "name": "entropy_str", "type": "string" },
{ "name": "commitments", "type": { "map": ["hash", { "list": ["address"] }] } }
{ "name": "leader", "type": "address" },
{ "name": "seed", "type": { "bytes": "any" } },
{ "name": "epoch_adjust", "type": "int" }
],
"name": "elect_next",
"name": "step_eoe",
"payable": false,
"returns": { "tuple": ["address", "int"] },
"stateful": false
"returns": { "tuple": [] },
"stateful": true
},
{
"arguments": [],
Expand All @@ -72,18 +82,51 @@
"returns": "address",
"stateful": false
},
{ "arguments": [], "name": "epoch", "payable": false, "returns": "int", "stateful": false },
{
"arguments": [],
"name": "added_stake",
"name": "epoch_length",
"payable": false,
"returns": "int",
"stateful": false
},
{
"arguments": [],
"name": "epoch_info",
"payable": false,
"returns": { "tuple": ["int", "HCElection.epoch_info"] },
"stateful": false
},
{
"arguments": [{ "name": "epoch", "type": "int" }],
"name": "epoch_info_epoch",
"payable": false,
"returns": "HCElection.epoch_info",
"stateful": false
},
{
"arguments": [],
"name": "staking_contract",
"payable": false,
"returns": "MainStaking",
"stateful": false
},
{
"arguments": [
{ "name": "seed", "type": { "bytes": "any" } },
{ "name": "validators", "type": { "list": [{ "tuple": ["address", "int"] }] } },
{ "name": "length", "type": "int" }
],
"name": "validator_schedule",
"payable": false,
"returns": { "list": ["address"] },
"stateful": false
},
{
"arguments": [],
"name": "get_state",
"payable": false,
"returns": "HCElection.get_state_response",
"returns": "HCElection.state",
"stateful": false
}
],
Expand All @@ -93,20 +136,24 @@
"state": {
"record": [
{ "name": "main_staking_ct", "type": "MainStaking" },
{ "name": "entropy", "type": "string" },
{ "name": "leader", "type": "address" },
{ "name": "added_stake", "type": "int" }
{ "name": "added_stake", "type": "int" },
{ "name": "epoch", "type": "int" },
{ "name": "epochs", "type": { "map": ["int", "HCElection.epoch_info"] } }
]
},
"typedefs": [
{
"name": "get_state_response",
"name": "epoch_info",
"typedef": {
"record": [
{ "name": "main_staking_ct", "type": "MainStaking" },
{ "name": "entropy", "type": "string" },
{ "name": "leader", "type": "address" },
{ "name": "added_stake", "type": "int" }
{ "name": "start", "type": "int" },
{ "name": "length", "type": "int" },
{ "name": "seed", "type": { "option": [{ "bytes": "any" }] } },
{
"name": "staking_distribution",
"type": { "option": [{ "list": [{ "tuple": ["address", "int"] }] }] }
}
]
},
"vars": []
Expand Down
7 changes: 7 additions & 0 deletions src/lib/contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ This directory contains the contracts which are used by this application.

The script `get_and_compile_contracts.sh` will download the contracts from a given URL, and generate the ACI files.

THIS SCRIPT MUST BE RUN IN THE CURRENT DIRECTORY.

Usage:
```
./get_and_compile_contract.sh --compiler <path to AE CLI> --url "<contract URL>"
```

Example:
```bash
./get_and_compile_contract.sh --compiler aesophia_cli --url "https://raw.githubusercontent.com/aeternity/aeternity/refs/heads/master/test/contracts/HCElection.aes"

```
2 changes: 1 addition & 1 deletion src/lib/contracts/get_and_compile_contract.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
README.md#!/bin/bash
#!/bin/bash

# Parsing CLI arguments
while [[ "$#" -gt 0 ]]; do
Expand Down
3 changes: 1 addition & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@
</div>
{#if st}
<div class="card border border-primary">
<div class="card-body p-4">
<div class="card-body p-3">
<div class=" card-title"><h3>Stats</h3></div>
<p>Block Height: {st.height}</p>
<p class="break-all">Entropy String: {st.hcElection.entropy}</p>
<p>
Current Leader: <a class="link link-primary" href={`/validator/${leader?.address}`}>
{leader?.state.name}</a
Expand Down

0 comments on commit 66cd120

Please sign in to comment.