Skip to content

Commit 33c4b46

Browse files
committed
fix: correct numbers in voting results
1 parent f7ae337 commit 33c4b46

File tree

4 files changed

+67
-13
lines changed

4 files changed

+67
-13
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"start:dev:debug": "dotenv -e .env.dev -e .env.service -- nest start --debug --watch",
1515
"start:local-dev": "dotenv -e .env.local-dev -e .env.service -- nest start --watch",
1616
"start:production": "dotenv -e .env.production -e .env.service -- nest start --watch",
17+
"start:production:debug": "dotenv -e .env.production -e .env.service -- nest start --debug --watch",
1718
"start:local-dev:debug": "dotenv -e .env.local-dev -e .env.service -- nest start --debug --watch",
1819
"client:start:dev": "dotenv -e .env.dev -e .env.client -- ts-node test/client.ts",
1920
"client:start:local-dev": "dotenv -e .env.local-dev -e .env.client -- ts-node test/client.ts"
@@ -32,6 +33,7 @@
3233
"@nestjs/terminus": "^9.1.0",
3334
"@solana/spl-governance": "^0.3.28",
3435
"@solana/spl-token": "0.2.0",
36+
"@solana/web3.js": "^1.91.7",
3537
"bn.js": "^5.1.3",
3638
"borsh": "^0.3.1",
3739
"bs58": "^4.0.1",

src/formatting-utilts.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const toPrecise3 = new Intl.NumberFormat('en-US', {
2+
maximumSignificantDigits: 3,
3+
});
4+
function removeTrailingZeros(value: string): string {
5+
return value.replace(/\.?0+$/, '');
6+
}
7+
8+
export function amountToShortString(num: number): string {
9+
if (num < 1) {
10+
return toPrecise3.format(num);
11+
}
12+
if (num >= 1 && num < 1e3) {
13+
return removeTrailingZeros(num.toFixed(2));
14+
}
15+
if (num >= 1e3 && num < 1e6) {
16+
return removeTrailingZeros((num / 1e3).toFixed(1)) + 'K';
17+
}
18+
if (num >= 1e6 && num < 1e9) {
19+
return removeTrailingZeros((num / 1e6).toFixed(1)) + 'M';
20+
}
21+
if (num >= 1e9 && num < 1e12) {
22+
return removeTrailingZeros((num / 1e9).toFixed(1)) + 'B';
23+
}
24+
if (num >= 1e12) {
25+
return removeTrailingZeros((num / 1e12).toFixed(1)) + 'T';
26+
}
27+
return num.toFixed(2);
28+
}

src/proposal-state-monitoring.service.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ import {
1919
} from '@solana/spl-governance';
2020
import { CachingEventType, fmtTokenAmount, RealmMints } from './realms-cache';
2121
import { OnEvent } from '@nestjs/event-emitter';
22-
import { DialectSdk } from '@dialectlabs/sdk';
22+
import { DappMessageActionType, DialectSdk } from '@dialectlabs/sdk';
2323
import { Solana } from '@dialectlabs/blockchain-sdk-solana';
24+
import { amountToShortString } from './formatting-utilts';
2425

2526
interface ProposalVotingStats {
2627
yesCount: number;
@@ -162,14 +163,25 @@ export class ProposalStateChangeMonitoringService {
162163
const { yesCount, noCount, relativeYesCount, relativeNoCount } =
163164
getVotingStats(account, realm);
164165

166+
const yesVotesFormatted = amountToShortString(yesCount);
167+
const noVotesFormatted = amountToShortString(noCount);
165168
if (account.state === ProposalState.Succeeded) {
166169
return {
167170
title: `Proposal for ${realmName} is succeeded`,
168171
message: `✅ Proposal ${
169172
account.name
170173
} for ${realmName} is succeeded with ${relativeYesCount.toFixed(
171174
1,
172-
)}% of 👍 votes (${yesCount} 👍 / ${noCount} 👎): ${proposalLink}`,
175+
)}% of 👍 votes (${yesVotesFormatted} 👍 / ${noVotesFormatted} 👎): ${proposalLink}`,
176+
actions: {
177+
type: DappMessageActionType.LINK,
178+
links: [
179+
{
180+
label: 'View Proposal',
181+
url: proposalLink,
182+
},
183+
],
184+
},
173185
};
174186
}
175187
if (account.state === ProposalState.Defeated) {
@@ -179,7 +191,16 @@ export class ProposalStateChangeMonitoringService {
179191
account.name
180192
} for ${realmName} is defeated with ${relativeNoCount.toFixed(
181193
1,
182-
)}% of 👎 votes (${yesCount} 👍 / ${noCount} 👎): ${proposalLink}`,
194+
)}% of 👎 votes (${yesVotesFormatted} 👍 / ${noVotesFormatted} 👎): ${proposalLink}`,
195+
actions: {
196+
type: DappMessageActionType.LINK,
197+
links: [
198+
{
199+
label: 'View Proposal',
200+
url: proposalLink,
201+
},
202+
],
203+
},
183204
};
184205
}
185206
return {

src/realms-sdk.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
import { RealmsRestService } from './realms-rest-service';
1616
import { HttpService } from '@nestjs/axios';
1717
import { chunk, compact, keyBy, uniqBy, zip } from 'lodash';
18-
import { parseMintAccountData } from './realms-cache';
18+
import { parseMintAccountData, RealmMints } from './realms-cache';
1919
import { sleepSecs } from 'twitter-api-v2/dist/v1/media-helpers.v1';
2020

2121
const connection = new Connection(process.env.DIALECT_SDK_SOLANA_RPC_URL!);
@@ -111,15 +111,18 @@ export async function fetchRealmsWithMints(realms: ProgramAccount<Realm>[]) {
111111
(it) => it.address.toBase58(),
112112
);
113113

114-
const realmsWithMints = realms.map((it) => ({
115-
...it,
116-
mints: {
117-
mint: parsedRawMints[it.account.communityMint.toBase58()]?.parsed,
118-
councilMint:
119-
it.account.config.councilMint &&
120-
parsedRawMints[it.account.config.councilMint.toBase58()]?.parsed,
121-
},
122-
}));
114+
const realmsWithMints: ProgramAccount<Realm & RealmMints>[] = realms.map(
115+
(it) => ({
116+
...it,
117+
account: {
118+
...it.account,
119+
mint: parsedRawMints[it.account.communityMint.toBase58()]?.parsed,
120+
councilMint:
121+
it.account.config.councilMint &&
122+
parsedRawMints[it.account.config.councilMint.toBase58()]?.parsed,
123+
},
124+
}),
125+
);
123126

124127
return realmsWithMints;
125128
}

0 commit comments

Comments
 (0)