Skip to content

Commit db319cd

Browse files
authored
Merge pull request #634 from multiversx/TOOL-639-add-governance-in-docs
Add governance documantation and expose governance via entrypoints
2 parents df4d53f + d2644e2 commit db319cd

File tree

9 files changed

+365
-14
lines changed

9 files changed

+365
-14
lines changed

cookbook/cookbook.md

Lines changed: 171 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ When manually instantiating a network provider, you can provide a configuration
208208
}
209209
```
210210

211-
A full list of available methods for `ApiNetworkProvider` can be found [here](https://multiversx.github.io/mx-sdk-js-core/v14/classes/ApiNetworkProvider.html).
211+
Here you can find a full list of available methods for [`ApiNetworkProvider`](https://multiversx.github.io/mx-sdk-js-core/v14/classes/ApiNetworkProvider.html).
212212

213213
Both `ApiNetworkProvider` and `ProxyNetworkProvider` implement a common interface, which can be found [here](https://multiversx.github.io/mx-sdk-js-core/v14/interfaces/INetworkProvider.html). This allows them to be used interchangeably.
214214

@@ -1749,8 +1749,8 @@ For scripts or quick network interactions, we recommend using the controller. Ho
17491749

17501750
These are just a few examples of what you can do using the token management controller or factory. For a complete list of supported methods, please refer to the autogenerated documentation:
17511751

1752-
- [TokenManagementController](https://multiversx.github.io/mx-sdk-js-core/v14/classes/TokenManagementController.html)
1753-
- [TokenManagementTransactionsFactory](https://multiversx.github.io/mx-sdk-js-core/v14/classes/TokenManagementTransactionsFactory.html)
1752+
- [`TokenManagementController`](https://multiversx.github.io/mx-sdk-js-core/v14/classes/TokenManagementController.html)
1753+
- [`TokenManagementTransactionsFactory`](https://multiversx.github.io/mx-sdk-js-core/v14/classes/TokenManagementTransactionsFactory.html)
17541754

17551755
### Account management
17561756

@@ -1997,8 +1997,8 @@ In this section, we'll cover how to:
19971997
- Undelegate and withdraw funds
19981998

19991999
These operations can be performed using both the controller and the **factory**. For a complete list of supported methods, please refer to the autogenerated documentation:
2000-
- [DelegationController](https://multiversx.github.io/mx-sdk-js-core/v14/classes/DelegationController.html)
2001-
- [DelegationTransactionsFactory](https://multiversx.github.io/mx-sdk-js-core/v14/classes/DelegationTransactionsFactory.html)
2000+
- [`DelegationController`](https://multiversx.github.io/mx-sdk-js-core/v14/classes/DelegationController.html)
2001+
- [`DelegationTransactionsFactory`](https://multiversx.github.io/mx-sdk-js-core/v14/classes/DelegationTransactionsFactory.html)
20022002

20032003
#### Creating a New Delegation Contract Using the Controller
20042004
```js
@@ -2613,6 +2613,172 @@ Flow for Creating Guarded Relayed Transactions:
26132613
4. Then, the guardian signs.
26142614
5. Finally, the relayer signs before broadcasting.
26152615

2616+
### Governance
2617+
2618+
We can create transactions for creating a new governance proposal, vote for a proposal or query the governance contract.
2619+
2620+
These operations can be performed using both the **controller** and the **factory**. For a complete list of supported methods, please refer to the autogenerated documentation:
2621+
- [`GovernanceController`](https://multiversx.github.io/mx-sdk-js-core/v14/classes/GovernanceController.html)
2622+
- [`GovernanceTransactionsFactory`](https://multiversx.github.io/mx-sdk-js-core/v14/classes/GovernanceTransactionsFactory.html)
2623+
2624+
#### Creating a new proposal using the controller
2625+
```js
2626+
{
2627+
// create the entrypoint and the governance controller
2628+
const entrypoint = new DevnetEntrypoint();
2629+
const controller = entrypoint.createGovernanceController();
2630+
2631+
const filePath = path.join("../src", "testdata", "testwallets", "alice.pem");
2632+
const alice = await Account.newFromPem(filePath);
2633+
2634+
// fetch the nonce of the network
2635+
alice.nonce = await entrypoint.recallAccountNonce(alice.address);
2636+
const commitHash = "1db734c0315f9ec422b88f679ccfe3e0197b9d67";
2637+
2638+
const transaction = await controller.createTransactionForNewProposal(alice, alice.getNonceThenIncrement(), {
2639+
commitHash: commitHash,
2640+
startVoteEpoch: 10,
2641+
endVoteEpoch: 15,
2642+
nativeTokenAmount: 500_000000000000000000n,
2643+
});
2644+
2645+
// sending the transaction
2646+
const txHash = await entrypoint.sendTransaction(transaction);
2647+
2648+
// wait for transaction completion, extract proposal's details
2649+
const outcome = await controller.awaitCompletedProposeProposal(txHash);
2650+
2651+
const proposalNonce = outcome[0].proposalNonce;
2652+
const proposalCommitHash = outcome[0].commitHash;
2653+
const proposalStartVoteEpoch = outcome[0].startVoteEpoch;
2654+
const proposalEndVoteEpoch = outcome[0].endVoteEpoch;
2655+
}
2656+
```
2657+
2658+
#### Creating a new proposal using the factory
2659+
```js
2660+
{
2661+
// create the entrypoint and the governance factory
2662+
const entrypoint = new DevnetEntrypoint();
2663+
const factory = entrypoint.createGovernanceTransactionsFactory();
2664+
2665+
const filePath = path.join("../src", "testdata", "testwallets", "alice.pem");
2666+
const alice = await Account.newFromPem(filePath);
2667+
2668+
const commitHash = "1db734c0315f9ec422b88f679ccfe3e0197b9d67";
2669+
2670+
const transaction = factory.createTransactionForNewProposal(alice.address, {
2671+
commitHash: commitHash,
2672+
startVoteEpoch: 10,
2673+
endVoteEpoch: 15,
2674+
nativeTokenAmount: 500_000000000000000000n,
2675+
});
2676+
// fetch the nonce of the network
2677+
alice.nonce = await entrypoint.recallAccountNonce(alice.address);
2678+
2679+
// set the nonce
2680+
transaction.nonce = alice.getNonceThenIncrement();
2681+
2682+
// sign the transaction
2683+
transaction.signature = await alice.signTransaction(transaction);
2684+
2685+
// sending the transaction
2686+
const txHash = await entrypoint.sendTransaction(transaction);
2687+
2688+
// waits until the transaction is processed and fetches it from the network
2689+
const transactionOnNetwork = await entrypoint.awaitCompletedTransaction(txHash);
2690+
2691+
const parser = new GovernanceTransactionsOutcomeParser({});
2692+
const outcome = parser.parseNewProposal(transactionOnNetwork);
2693+
const proposalNonce = outcome[0].proposalNonce;
2694+
const proposalCommitHash = outcome[0].commitHash;
2695+
const proposalStartVoteEpoch = outcome[0].startVoteEpoch;
2696+
const proposalEndVoteEpoch = outcome[0].endVoteEpoch;
2697+
}
2698+
```
2699+
2700+
#### Vote for a proposal using the controller
2701+
2702+
```js
2703+
{
2704+
// create the entrypoint and the governance controller
2705+
const entrypoint = new DevnetEntrypoint();
2706+
const controller = entrypoint.createGovernanceController();
2707+
2708+
const filePath = path.join("../src", "testdata", "testwallets", "alice.pem");
2709+
const alice = await Account.newFromPem(filePath);
2710+
2711+
// fetch the nonce of the network
2712+
alice.nonce = await entrypoint.recallAccountNonce(alice.address);
2713+
2714+
const transaction = await controller.createTransactionForVoting(alice, alice.getNonceThenIncrement(), {
2715+
proposalNonce: 1,
2716+
vote: Vote.YES,
2717+
});
2718+
2719+
// sending the transaction
2720+
const txHash = await entrypoint.sendTransaction(transaction);
2721+
2722+
// wait for transaction completion, extract proposal's details
2723+
const outcome = await controller.awaitCompletedVote(txHash);
2724+
const proposalNonce = outcome[0].proposalNonce;
2725+
const vote = outcome[0].vote;
2726+
const voteTotalStake = outcome[0].totalStake;
2727+
const voteVotingPower = outcome[0].votingPower;
2728+
}
2729+
```
2730+
2731+
#### Vote for a proposal using the factory
2732+
```js
2733+
{
2734+
// create the entrypoint and the governance factory
2735+
const entrypoint = new DevnetEntrypoint();
2736+
const factory = entrypoint.createGovernanceTransactionsFactory();
2737+
2738+
const filePath = path.join("../src", "testdata", "testwallets", "alice.pem");
2739+
const alice = await Account.newFromPem(filePath);
2740+
2741+
const transaction = factory.createTransactionForVoting(alice.address, {
2742+
proposalNonce: 1,
2743+
vote: Vote.YES,
2744+
});
2745+
// fetch the nonce of the network
2746+
alice.nonce = await entrypoint.recallAccountNonce(alice.address);
2747+
2748+
// set the nonce
2749+
transaction.nonce = alice.getNonceThenIncrement();
2750+
2751+
// sign the transaction
2752+
transaction.signature = await alice.signTransaction(transaction);
2753+
2754+
// sending the transaction
2755+
const txHash = await entrypoint.sendTransaction(transaction);
2756+
2757+
// wait for transaction completion, extract proposal's details
2758+
const transactionOnNetwork = await entrypoint.awaitCompletedTransaction(txHash);
2759+
const parser = new GovernanceTransactionsOutcomeParser({});
2760+
const outcome = parser.parseVote(transactionOnNetwork);
2761+
const proposalNonce = outcome[0].proposalNonce;
2762+
const vote = outcome[0].vote;
2763+
const voteTotalStake = outcome[0].totalStake;
2764+
const voteVotingPower = outcome[0].votingPower;
2765+
}
2766+
```
2767+
2768+
#### Querying the governance contract
2769+
Unlike creating transactions, querying the contract is only possible using the controller. Let's query the contract to get more details about a proposal.
2770+
2771+
```js
2772+
{
2773+
// create the entrypoint and the governance controller
2774+
const entrypoint = new DevnetEntrypoint();
2775+
const controller = entrypoint.createGovernanceController();
2776+
2777+
const proposalInfo = await controller.getProposal(1);
2778+
console.log({ proposalInfo });
2779+
}
2780+
```
2781+
26162782
## Addresses
26172783

26182784
Create an `Address` object from a bech32-encoded string:

cookbook/delegation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import { Account, Address, DevnetEntrypoint, TokenManagementTransactionsOutcomeP
1414
// - Undelegate and withdraw funds
1515

1616
// These operations can be performed using both the controller and the **factory**. For a complete list of supported methods, please refer to the autogenerated documentation:
17-
// - [DelegationController](https://multiversx.github.io/mx-sdk-js-core/v14/classes/DelegationController.html)
18-
// - [DelegationTransactionsFactory](https://multiversx.github.io/mx-sdk-js-core/v14/classes/DelegationTransactionsFactory.html)
17+
// - `class:DelegationController`
18+
// - `class:DelegationTransactionsFactory`
1919

2020
// #### Creating a New Delegation Contract Using the Controller
2121
// ```js

cookbook/generate.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
current_dir / "delegation.ts",
1616
current_dir / "relayed.ts",
1717
current_dir / "guarded.ts",
18+
current_dir / "governance.ts",
1819
current_dir / "addresses.ts",
1920
current_dir / "wallets.ts",
2021
current_dir / "signingObjects.ts",

cookbook/governance.ts

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import path from "path"; // md-ignore
2+
import { Account, DevnetEntrypoint, GovernanceTransactionsOutcomeParser, Vote } from "../src"; // md-ignore
3+
// md-start
4+
(async () => {
5+
// ### Governance
6+
7+
// We can create transactions for creating a new governance proposal, vote for a proposal or query the governance contract.
8+
9+
// These operations can be performed using both the **controller** and the **factory**. For a complete list of supported methods, please refer to the autogenerated documentation:
10+
// - `class:GovernanceController`
11+
// - `class:GovernanceTransactionsFactory`
12+
13+
// #### Creating a new proposal using the controller
14+
// ```js
15+
{
16+
// create the entrypoint and the governance controller // md-as-comment
17+
const entrypoint = new DevnetEntrypoint();
18+
const controller = entrypoint.createGovernanceController();
19+
20+
const filePath = path.join("../src", "testdata", "testwallets", "alice.pem");
21+
const alice = await Account.newFromPem(filePath);
22+
23+
// fetch the nonce of the network // md-as-comment
24+
alice.nonce = await entrypoint.recallAccountNonce(alice.address);
25+
const commitHash = "1db734c0315f9ec422b88f679ccfe3e0197b9d67";
26+
27+
const transaction = await controller.createTransactionForNewProposal(alice, alice.getNonceThenIncrement(), {
28+
commitHash: commitHash,
29+
startVoteEpoch: 10,
30+
endVoteEpoch: 15,
31+
nativeTokenAmount: 500_000000000000000000n,
32+
});
33+
34+
// sending the transaction // md-as-comment
35+
const txHash = await entrypoint.sendTransaction(transaction);
36+
37+
// wait for transaction completion, extract proposal's details // md-as-comment
38+
const outcome = await controller.awaitCompletedProposeProposal(txHash);
39+
40+
const proposalNonce = outcome[0].proposalNonce;
41+
const proposalCommitHash = outcome[0].commitHash;
42+
const proposalStartVoteEpoch = outcome[0].startVoteEpoch;
43+
const proposalEndVoteEpoch = outcome[0].endVoteEpoch;
44+
}
45+
// ```
46+
47+
// #### Creating a new proposal using the factory
48+
// ```js
49+
{
50+
// create the entrypoint and the governance factory // md-as-comment
51+
const entrypoint = new DevnetEntrypoint();
52+
const factory = entrypoint.createGovernanceTransactionsFactory();
53+
54+
const filePath = path.join("../src", "testdata", "testwallets", "alice.pem");
55+
const alice = await Account.newFromPem(filePath);
56+
57+
const commitHash = "1db734c0315f9ec422b88f679ccfe3e0197b9d67";
58+
59+
const transaction = factory.createTransactionForNewProposal(alice.address, {
60+
commitHash: commitHash,
61+
startVoteEpoch: 10,
62+
endVoteEpoch: 15,
63+
nativeTokenAmount: 500_000000000000000000n,
64+
});
65+
// fetch the nonce of the network // md-as-comment
66+
alice.nonce = await entrypoint.recallAccountNonce(alice.address);
67+
68+
// set the nonce // md-as-comment
69+
transaction.nonce = alice.getNonceThenIncrement();
70+
71+
// sign the transaction // md-as-comment
72+
transaction.signature = await alice.signTransaction(transaction);
73+
74+
// sending the transaction // md-as-comment
75+
const txHash = await entrypoint.sendTransaction(transaction);
76+
77+
// waits until the transaction is processed and fetches it from the network // md-as-comment
78+
const transactionOnNetwork = await entrypoint.awaitCompletedTransaction(txHash);
79+
80+
const parser = new GovernanceTransactionsOutcomeParser({});
81+
const outcome = parser.parseNewProposal(transactionOnNetwork);
82+
const proposalNonce = outcome[0].proposalNonce;
83+
const proposalCommitHash = outcome[0].commitHash;
84+
const proposalStartVoteEpoch = outcome[0].startVoteEpoch;
85+
const proposalEndVoteEpoch = outcome[0].endVoteEpoch;
86+
}
87+
// ```
88+
89+
// #### Vote for a proposal using the controller
90+
91+
// ```js
92+
{
93+
// create the entrypoint and the governance controller // md-as-comment
94+
const entrypoint = new DevnetEntrypoint();
95+
const controller = entrypoint.createGovernanceController();
96+
97+
const filePath = path.join("../src", "testdata", "testwallets", "alice.pem");
98+
const alice = await Account.newFromPem(filePath);
99+
100+
// fetch the nonce of the network // md-as-comment
101+
alice.nonce = await entrypoint.recallAccountNonce(alice.address);
102+
103+
const transaction = await controller.createTransactionForVoting(alice, alice.getNonceThenIncrement(), {
104+
proposalNonce: 1,
105+
vote: Vote.YES,
106+
});
107+
108+
// sending the transaction // md-as-comment
109+
const txHash = await entrypoint.sendTransaction(transaction);
110+
111+
// wait for transaction completion, extract proposal's details // md-as-comment
112+
const outcome = await controller.awaitCompletedVote(txHash);
113+
const proposalNonce = outcome[0].proposalNonce;
114+
const vote = outcome[0].vote;
115+
const voteTotalStake = outcome[0].totalStake;
116+
const voteVotingPower = outcome[0].votingPower;
117+
}
118+
// ```
119+
120+
// #### Vote for a proposal using the factory
121+
// ```js
122+
{
123+
// create the entrypoint and the governance factory // md-as-comment
124+
const entrypoint = new DevnetEntrypoint();
125+
const factory = entrypoint.createGovernanceTransactionsFactory();
126+
127+
const filePath = path.join("../src", "testdata", "testwallets", "alice.pem");
128+
const alice = await Account.newFromPem(filePath);
129+
130+
const transaction = factory.createTransactionForVoting(alice.address, {
131+
proposalNonce: 1,
132+
vote: Vote.YES,
133+
});
134+
// fetch the nonce of the network // md-as-comment
135+
alice.nonce = await entrypoint.recallAccountNonce(alice.address);
136+
137+
// set the nonce // md-as-comment
138+
transaction.nonce = alice.getNonceThenIncrement();
139+
140+
// sign the transaction // md-as-comment
141+
transaction.signature = await alice.signTransaction(transaction);
142+
143+
// sending the transaction // md-as-comment
144+
const txHash = await entrypoint.sendTransaction(transaction);
145+
146+
// wait for transaction completion, extract proposal's details // md-as-comment
147+
const transactionOnNetwork = await entrypoint.awaitCompletedTransaction(txHash);
148+
const parser = new GovernanceTransactionsOutcomeParser({});
149+
const outcome = parser.parseVote(transactionOnNetwork);
150+
const proposalNonce = outcome[0].proposalNonce;
151+
const vote = outcome[0].vote;
152+
const voteTotalStake = outcome[0].totalStake;
153+
const voteVotingPower = outcome[0].votingPower;
154+
}
155+
// ```
156+
157+
// #### Querying the governance contract
158+
// Unlike creating transactions, querying the contract is only possible using the controller. Let's query the contract to get more details about a proposal.
159+
160+
// ```js
161+
{
162+
// create the entrypoint and the governance controller // md-as-comment
163+
const entrypoint = new DevnetEntrypoint();
164+
const controller = entrypoint.createGovernanceController();
165+
166+
const proposalInfo = await controller.getProposal(1);
167+
console.log({ proposalInfo });
168+
}
169+
// ```
170+
})().catch((e) => {
171+
console.log({ e });
172+
});

0 commit comments

Comments
 (0)