Skip to content

Commit ebaec2e

Browse files
author
Victor Nordam Suadicani
authored
Adjust voting dapp to work with new voting smart contract interface (#485)
* Fix voting dapp * Changelog * Bump version
1 parent b20ea1c commit ebaec2e

File tree

7 files changed

+37
-37
lines changed

7 files changed

+37
-37
lines changed

examples/voting/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.1.4
4+
5+
- Adjusted according to the simplified voting smart contract example, see https://github.com/Concordium/concordium-rust-smart-contracts/pull/441.
6+
37
## 1.1.3
48

59
- Fixed problems on the result page due to breaking changes in the web SDK.

examples/voting/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "voting",
33
"license": "Apache-2.0",
4-
"version": "1.1.3",
4+
"version": "1.1.4",
55
"packageManager": "[email protected]",
66
"dependencies": {
77
"@concordium/browser-wallet-api-helpers": "^3.0.0",

examples/voting/src/Results.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function Results() {
7979
// Attempt to get the voting results.
8080
useMemo(() => {
8181
if (viewResult && client && electionId) {
82-
getVotes(client, electionId, viewResult?.numOptions).then(setVotes).catch(console.error);
82+
getVotes(client, electionId, viewResult?.opts).then(setVotes).catch(console.error);
8383
}
8484
}, [viewResult, client, electionId]);
8585

@@ -90,11 +90,11 @@ function Results() {
9090
}
9191
}, [votes]);
9292

93-
const endsInMillis = viewResult?.endTime.diff(now);
93+
const endsInMillis = viewResult?.deadline.diff(now);
9494

9595
// Refresh tally periodically until deadline.
9696
useEffect(() => {
97-
if (client && viewResult?.endTime.isAfter()) {
97+
if (client && viewResult?.deadline.isAfter()) {
9898
const interval = setInterval(() => {
9999
console.log('refreshing');
100100
setView(undefined);

examples/voting/src/VotePage.jsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ function VotePage() {
7676
</div>
7777
<Button
7878
className="w-100"
79-
onClick={() =>
80-
castVote(client, electionId, viewResult?.opts.indexOf(selectedOption), connectedAccount)
81-
}
79+
onClick={() => castVote(client, electionId, selectedOption, connectedAccount)}
8280
>
8381
<strong>Cast Vote!</strong>
8482
</Button>

examples/voting/src/Wallet.jsx

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export async function createElection(
8080
const parameter = {
8181
description,
8282
options,
83-
end_time: deadlineTimestamp,
83+
deadline: deadlineTimestamp,
8484
};
8585

8686
const txHash = await client.sendTransaction(
@@ -107,17 +107,15 @@ export async function getView(client, contractIndex) {
107107
});
108108
}
109109

110-
export async function getVotes(client, contractIndex, numOptions) {
110+
export async function getVotes(client, contractIndex, options) {
111111
const promises = [];
112112

113113
const grpcClient = new ConcordiumGRPCClient(client.grpcTransport);
114-
for (let i = 0; i < numOptions; i++) {
114+
for (const option of options) {
115115
const param = serializeUpdateContractParameters(
116116
ContractName.fromString('voting'),
117117
EntrypointName.fromString('getNumberOfVotes'),
118-
{
119-
vote_index: i,
120-
},
118+
option,
121119
toBuffer(RAW_SCHEMA_BASE64, 'base64')
122120
);
123121

@@ -134,28 +132,30 @@ export async function getVotes(client, contractIndex, numOptions) {
134132
}
135133

136134
export async function castVote(client, contractIndex, vote, senderAddress) {
137-
if (vote === -1) {
135+
if (!vote) {
138136
window.alert('Select one option.');
139137
return;
140138
}
141139

142140
const connectedToTestnet = await checkConnectedToTestnet(client);
143-
if (connectedToTestnet) {
144-
const txHash = await client.sendTransaction(
145-
senderAddress,
146-
AccountTransactionType.Update,
147-
{
148-
amount: CcdAmount.fromMicroCcd(BigInt(0)),
149-
address: { index: BigInt(contractIndex), subindex: BigInt(0) },
150-
receiveName: 'voting.vote',
151-
maxContractExecutionEnergy: BigInt(30000),
152-
},
153-
{ vote_index: vote },
154-
RAW_SCHEMA_BASE64
155-
);
156-
console.log({ txHash });
157-
return txHash;
141+
if (!connectedToTestnet) {
142+
return;
158143
}
144+
145+
const txHash = await client.sendTransaction(
146+
senderAddress,
147+
AccountTransactionType.Update,
148+
{
149+
amount: CcdAmount.fromMicroCcd(BigInt(0)),
150+
address: { index: BigInt(contractIndex), subindex: BigInt(0) },
151+
receiveName: 'voting.vote',
152+
maxContractExecutionEnergy: BigInt(30000),
153+
},
154+
vote,
155+
RAW_SCHEMA_BASE64
156+
);
157+
console.log({ txHash });
158+
return txHash;
159159
}
160160

161161
export default function Wallet(props) {

examples/voting/src/buffer.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,13 @@ export function decodeView(result) {
2626
const offset0 = 0;
2727
const buffer = toBuffer(result, 'hex');
2828
const [descriptionText, offset1] = decodeString(buffer, offset0);
29-
const endTimestamp = buffer.readBigUInt64LE(offset1);
30-
const endTime = moment.unix(Number(endTimestamp / BigInt(1000)));
31-
const [opts, offset2] = decodeStrings(buffer, offset1 + 8);
32-
const numOptions = buffer.readUInt32LE(offset2);
29+
const [opts, offset2] = decodeStrings(buffer, offset1);
30+
const deadlineTimestamp = buffer.readBigUInt64LE(offset2);
31+
const deadline = moment.unix(Number(deadlineTimestamp / BigInt(1000)));
3332
return {
3433
descriptionText,
35-
endTime,
3634
opts,
37-
numOptions,
35+
deadline,
3836
};
3937
}
4038

examples/voting/src/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import moment from 'moment';
22

3-
export const MODULE_REF = '16c0b28417d287114d2fd59537f678cae5e1416a34e3680326359b6bbfe57e72';
3+
export const MODULE_REF = '9ee6fcfbb1c210a1c920f692a7db0cfd0cfe3c78df45873ab339234be7d5279b';
44
export const CONTRACT_NAME = 'voting';
55
export const RAW_SCHEMA_BASE64 =
6-
'//8CAQAAAAYAAAB2b3RpbmcBABQAAwAAAAsAAABkZXNjcmlwdGlvbhYCBwAAAG9wdGlvbnMQAhYCCAAAAGVuZF90aW1lDQMAAAAQAAAAZ2V0TnVtYmVyT2ZWb3RlcwIUAAEAAAAKAAAAdm90ZV9pbmRleAQEBAAAAHZpZXcBFAAEAAAACwAAAGRlc2NyaXB0aW9uFgIIAAAAZW5kX3RpbWUNBwAAAG9wdGlvbnMQAhYCCwAAAG51bV9vcHRpb25zBAQAAAB2b3RlBBQAAQAAAAoAAAB2b3RlX2luZGV4BBUGAAAADQAAAFBhcnNpbmdGYWlsZWQCBwAAAExvZ0Z1bGwCDAAAAExvZ01hbGZvcm1lZAIOAAAAVm90aW5nRmluaXNoZWQCEAAAAEludmFsaWRWb3RlSW5kZXgCDQAAAENvbnRyYWN0Vm90ZXIC';
6+
'//8DAQAAAAYAAAB2b3RpbmcBABQAAwAAAAsAAABkZXNjcmlwdGlvbhYCBwAAAG9wdGlvbnMQAhYCCAAAAGRlYWRsaW5lDQMAAAAQAAAAZ2V0TnVtYmVyT2ZWb3RlcwIWAgQEAAAAdmlldwEUAAMAAAALAAAAZGVzY3JpcHRpb24WAgcAAABvcHRpb25zEAIWAggAAABkZWFkbGluZQ0EAAAAdm90ZQQWAhUGAAAADQAAAFBhcnNpbmdGYWlsZWQCBwAAAExvZ0Z1bGwCDAAAAExvZ01hbGZvcm1lZAIOAAAAVm90aW5nRmluaXNoZWQCCwAAAEludmFsaWRWb3RlAg0AAABDb250cmFjdFZvdGVyAgEUAAIAAAAFAAAAdm90ZXILBgAAAG9wdGlvbhYC';
77
export const BASE_URL = 'http://localhost:3000/';
88
export const REFRESH_INTERVAL = moment.duration(10, 'seconds');
99
// The TESTNET_GENESIS_BLOCK_HASH is used to check that the user has its browser wallet connected to testnet and not to mainnet.

0 commit comments

Comments
 (0)