Skip to content

Commit

Permalink
Add kusama society space, #1034
Browse files Browse the repository at this point in the history
  • Loading branch information
hyifeng committed Sep 2, 2024
1 parent 52a8796 commit e5f1d6b
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/packages/backend/src/consts/voting.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const strategies = Object.freeze({
quadraticBalanceOf: "quadratic-balance-of",
quorumQuadraticBalanceOf: "quorum-quadratic-balance-of",
biasedVoting: "biased-voting",
onePersonOneVote: "one-person-one-vote",
});

module.exports = {
Expand Down
2 changes: 2 additions & 0 deletions backend/packages/backend/src/scripts/init-spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const { phalaConfig } = require("./spaces/phala");
const { karuraConfig } = require("./spaces/karura");
const { rmrkConfig, rmrkCurationConfig } = require("./spaces/rmrk");
const { kusamaConfig } = require("./spaces/kusama");
const { kusamaSocietyConfig } = require("./spaces/kusamaSociety");
const { chrwnaConfig } = require("./spaces/chrwna");
const { interlayConfig } = require("./spaces/interlay");
const { acalaConfig } = require("./spaces/acala");
Expand Down Expand Up @@ -59,6 +60,7 @@ const spaces = [
shidenConfig,
// crabConfig,
dotsamaConfig,
kusamaSocietyConfig,
lksmConfig,
centrifugeConfig,
altairConfig,
Expand Down
31 changes: 31 additions & 0 deletions backend/packages/backend/src/scripts/spaces/kusamaSociety.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { networks, strategies } = require("./consts");

const config = {
id: "kusamasociety",
name: "KusamaSociety",
symbol: "KSM",
decimals: 12,
networks: [
{
network: networks.kusama,
ss58Format: 2,
assets: [
{
symbol: "KSM",
decimals: 12,
votingThreshold: "10000000000",
},
],
},
],
proposeThreshold: "10000000000",
weightStrategy: [strategies.onePersonOneVote],
version: "4",
spaceIcon: "kusama.svg",
seoImage: "QmYB3YVU3Sa27EfqTk3cGZ9S3GhiH6Z2ANpQdHKETpRrEZ",
admins: [],
};

module.exports = {
kusamaSocietyConfig: config,
};
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ async function getProposalById(proposalId) {
)
.plus(vote.weights.quadraticBalanceOf)
.toString();
votedWeights.onePersonOneVote = new BigNumber(
votedWeights.onePersonOneVote || 0,
)
.plus(1)
.toString();
}
proposal.votesCount = votesCount;
proposal.votedWeights = votedWeights;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ async function getStats(proposalCid) {
choice,
balanceOf: "0",
quadraticBalanceOf: "0",
onePersonOneVote: "0",
votesCount: 0,
},
]),
Expand All @@ -40,6 +41,10 @@ async function getStats(proposalCid) {
)
.plus(vote.weights.quadraticBalanceOf)
.toString();
weights.onePersonOneVote = new BigNumber(weights.onePersonOneVote || 0)
.plus(1)
.toString();

weights.votesCount = (weights.votesCount || 0) + 1;
}
}
Expand Down
12 changes: 12 additions & 0 deletions next/components/postDetail/postResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Divider from "../styled/divider";
import { VoteItem } from "./strategyResult/common/styled";
import QuorumBalanceOfResult from "./strategyResult/quorumBalanceOfResult";
import QuorumQuadraticBalanceOfResult from "./strategyResult/quorumQuadraticBalanceOfResult";
import OnePersonOneVoteResult from "./strategyResult/onePersonOneVoteResult";

export default function PostResult({ data, voteStatus, space }) {
const votedAmount = data?.votedWeights?.balanceOf || 0;
Expand Down Expand Up @@ -57,6 +58,17 @@ export default function PostResult({ data, voteStatus, space }) {
);
}

if (strategy === "one-person-one-vote") {
return (
<OnePersonOneVoteResult
key={strategy}
proposal={data}
space={space}
voteStatus={voteStatus}
/>
);
}

return null;
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Tooltip } from "@osn/common-ui";
import {
Divider,
FlexAround,
OptionIndex,
ProgressBackground,
ProgressBar,
ProgressItem,
ResultHead,
ResultName,
} from "./styled";
import { toPrecision } from "@osn/common";

export default function OnePersonOneVoteOptionList({
optionList,
strategy,
space,
}) {
const symbol = space?.symbol;

return (
<>
<ResultHead>
<ResultName>{strategy}</ResultName>
</ResultHead>
<Divider />
{optionList.map((vote, index) => {
return (
<div key={index}>
<ProgressItem>
<Tooltip content={vote.choice}>
<OptionIndex>{vote.choice}</OptionIndex>
</Tooltip>
<FlexAround>
<Tooltip
content={`${toPrecision(
vote.voteBalance,
space?.decimals,
)} ${symbol} (${vote.percentage}%)`}
>
<div>{vote.voteBalance} VOTE</div>
</Tooltip>
{vote.icon && <>&nbsp;{vote.icon}</>}
</FlexAround>
</ProgressItem>
<ProgressBackground>
<ProgressBar percent={`${vote.percentage}%`} />
</ProgressBackground>
</div>
);
})}
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import BigNumber from "bignumber.js";
import { Tooltip } from "@osn/common-ui";
import { SystemFail, SystemPass } from "@osn/icons/opensquare";
import OnePersonOneVoteOptionList from "./common/onePersonOneVoteOptionList";

export default function OnePersonOneVoteResult({
proposal,
space,
voteStatus,
}) {
let winnerChoice = null;
for (let item of voteStatus) {
if (new BigNumber(item.onePersonOneVote).isZero()) {
continue;
}
if (
!winnerChoice ||
new BigNumber(item.onePersonOneVote).gte(winnerChoice.onePersonOneVote)
) {
winnerChoice = item;
}
}

const isEnded = new Date().getTime() > proposal?.endDate;
const passStatusText = isEnded ? "Passed" : "Passing";
const failStatusText = isEnded ? "Failed" : "Failing";

const total = proposal?.votedWeights?.onePersonOneVote || 0;

const optionList = [];
proposal?.choices?.forEach((choice, index) => {
for (let voteStat of voteStatus) {
if (voteStat.choice !== choice) {
continue;
}

const voteBalance = new BigNumber(voteStat.onePersonOneVote || 0);
const percentage = (
voteStat.onePersonOneVote > 0 ? voteBalance.dividedBy(total) * 100 : 0
).toFixed(2);

const isWin = voteStat.choice === winnerChoice?.choice;

optionList.push({
index: index + 1,
choice,
voteBalance: voteBalance.toString(),
percentage,
icon: (
<Tooltip content={isWin ? passStatusText : failStatusText}>
<div className="inline-flex">
{isWin ? <SystemPass /> : <SystemFail />}
</div>
</Tooltip>
),
});

return;
}

optionList.push({
index: index + 1,
choice,
voteBalance: "0",
percentage: "0",
icon: (
<Tooltip content={failStatusText}>
<div className="inline-flex">
<SystemFail />
</div>
</Tooltip>
),
});
});

return (
<OnePersonOneVoteOptionList
strategy="one-person-one-vote"
optionList={optionList}
space={space}
/>
);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added next/public/imgs/icons/space/kusamasociety.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e5f1d6b

Please sign in to comment.