Skip to content

Commit

Permalink
fix: correct numbers in voting results
Browse files Browse the repository at this point in the history
  • Loading branch information
tsmbl committed Apr 26, 2024
1 parent f7ae337 commit 33c4b46
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 13 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"start:dev:debug": "dotenv -e .env.dev -e .env.service -- nest start --debug --watch",
"start:local-dev": "dotenv -e .env.local-dev -e .env.service -- nest start --watch",
"start:production": "dotenv -e .env.production -e .env.service -- nest start --watch",
"start:production:debug": "dotenv -e .env.production -e .env.service -- nest start --debug --watch",
"start:local-dev:debug": "dotenv -e .env.local-dev -e .env.service -- nest start --debug --watch",
"client:start:dev": "dotenv -e .env.dev -e .env.client -- ts-node test/client.ts",
"client:start:local-dev": "dotenv -e .env.local-dev -e .env.client -- ts-node test/client.ts"
Expand All @@ -32,6 +33,7 @@
"@nestjs/terminus": "^9.1.0",
"@solana/spl-governance": "^0.3.28",
"@solana/spl-token": "0.2.0",
"@solana/web3.js": "^1.91.7",
"bn.js": "^5.1.3",
"borsh": "^0.3.1",
"bs58": "^4.0.1",
Expand Down
28 changes: 28 additions & 0 deletions src/formatting-utilts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const toPrecise3 = new Intl.NumberFormat('en-US', {
maximumSignificantDigits: 3,
});
function removeTrailingZeros(value: string): string {
return value.replace(/\.?0+$/, '');
}

export function amountToShortString(num: number): string {
if (num < 1) {
return toPrecise3.format(num);
}
if (num >= 1 && num < 1e3) {
return removeTrailingZeros(num.toFixed(2));
}
if (num >= 1e3 && num < 1e6) {
return removeTrailingZeros((num / 1e3).toFixed(1)) + 'K';
}
if (num >= 1e6 && num < 1e9) {
return removeTrailingZeros((num / 1e6).toFixed(1)) + 'M';
}
if (num >= 1e9 && num < 1e12) {
return removeTrailingZeros((num / 1e9).toFixed(1)) + 'B';
}
if (num >= 1e12) {
return removeTrailingZeros((num / 1e12).toFixed(1)) + 'T';
}
return num.toFixed(2);
}
27 changes: 24 additions & 3 deletions src/proposal-state-monitoring.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import {
} from '@solana/spl-governance';
import { CachingEventType, fmtTokenAmount, RealmMints } from './realms-cache';
import { OnEvent } from '@nestjs/event-emitter';
import { DialectSdk } from '@dialectlabs/sdk';
import { DappMessageActionType, DialectSdk } from '@dialectlabs/sdk';
import { Solana } from '@dialectlabs/blockchain-sdk-solana';
import { amountToShortString } from './formatting-utilts';

interface ProposalVotingStats {
yesCount: number;
Expand Down Expand Up @@ -162,14 +163,25 @@ export class ProposalStateChangeMonitoringService {
const { yesCount, noCount, relativeYesCount, relativeNoCount } =
getVotingStats(account, realm);

const yesVotesFormatted = amountToShortString(yesCount);
const noVotesFormatted = amountToShortString(noCount);
if (account.state === ProposalState.Succeeded) {
return {
title: `Proposal for ${realmName} is succeeded`,
message: `✅ Proposal ${
account.name
} for ${realmName} is succeeded with ${relativeYesCount.toFixed(
1,
)}% of 👍 votes (${yesCount} 👍 / ${noCount} 👎): ${proposalLink}`,
)}% of 👍 votes (${yesVotesFormatted} 👍 / ${noVotesFormatted} 👎): ${proposalLink}`,
actions: {
type: DappMessageActionType.LINK,
links: [
{
label: 'View Proposal',
url: proposalLink,
},
],
},
};
}
if (account.state === ProposalState.Defeated) {
Expand All @@ -179,7 +191,16 @@ export class ProposalStateChangeMonitoringService {
account.name
} for ${realmName} is defeated with ${relativeNoCount.toFixed(
1,
)}% of 👎 votes (${yesCount} 👍 / ${noCount} 👎): ${proposalLink}`,
)}% of 👎 votes (${yesVotesFormatted} 👍 / ${noVotesFormatted} 👎): ${proposalLink}`,
actions: {
type: DappMessageActionType.LINK,
links: [
{
label: 'View Proposal',
url: proposalLink,
},
],
},
};
}
return {
Expand Down
23 changes: 13 additions & 10 deletions src/realms-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { RealmsRestService } from './realms-rest-service';
import { HttpService } from '@nestjs/axios';
import { chunk, compact, keyBy, uniqBy, zip } from 'lodash';
import { parseMintAccountData } from './realms-cache';
import { parseMintAccountData, RealmMints } from './realms-cache';
import { sleepSecs } from 'twitter-api-v2/dist/v1/media-helpers.v1';

const connection = new Connection(process.env.DIALECT_SDK_SOLANA_RPC_URL!);

Check warning on line 21 in src/realms-sdk.ts

View workflow job for this annotation

GitHub Actions / Integration

Forbidden non-null assertion
Expand Down Expand Up @@ -111,15 +111,18 @@ export async function fetchRealmsWithMints(realms: ProgramAccount<Realm>[]) {
(it) => it.address.toBase58(),
);

const realmsWithMints = realms.map((it) => ({
...it,
mints: {
mint: parsedRawMints[it.account.communityMint.toBase58()]?.parsed,
councilMint:
it.account.config.councilMint &&
parsedRawMints[it.account.config.councilMint.toBase58()]?.parsed,
},
}));
const realmsWithMints: ProgramAccount<Realm & RealmMints>[] = realms.map(
(it) => ({
...it,
account: {
...it.account,
mint: parsedRawMints[it.account.communityMint.toBase58()]?.parsed,
councilMint:
it.account.config.councilMint &&
parsedRawMints[it.account.config.councilMint.toBase58()]?.parsed,
},
}),
);

return realmsWithMints;
}
Expand Down

0 comments on commit 33c4b46

Please sign in to comment.