Skip to content

Commit 63abf40

Browse files
authored
Add scaling factor to convert from voter weight to staked tokens (#320)
* Weight converter * Cleanup * Restore tests * Add scaling factor function * Add scaling factor
1 parent 16dea58 commit 63abf40

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

staking/app/StakeConnection.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,24 @@ export class StakeConnection {
6161
configAddress: PublicKey;
6262
votingProductMetadataAccount: PublicKey;
6363
votingProduct = { voting: {} };
64+
votingAccountMetadataWasm: any;
6465
governanceAddress: PublicKey;
6566

6667
private constructor(
6768
program: Program<Staking>,
6869
provider: AnchorProvider,
6970
config: GlobalConfig,
7071
configAddress: PublicKey,
71-
votingProductMetadataAccount: PublicKey
72+
votingProductMetadataAccount: PublicKey,
73+
votingAccountMetadataWasm: any
7274
) {
7375
this.program = program;
7476
this.provider = provider;
7577
this.config = config;
7678
this.configAddress = configAddress;
7779
this.votingProductMetadataAccount = votingProductMetadataAccount;
7880
this.governanceAddress = GOVERNANCE_ADDRESS();
81+
this.votingAccountMetadataWasm = votingAccountMetadataWasm;
7982
}
8083

8184
public static async connect(
@@ -127,12 +130,21 @@ export class StakeConnection {
127130
)
128131
)[0];
129132

133+
const votingProductMetadataAccountData =
134+
await program.provider.connection.getAccountInfo(
135+
votingProductMetadataAccount
136+
);
137+
const votingAccountMetadataWasm = new wasm.WasmTargetMetadata(
138+
votingProductMetadataAccountData!.data
139+
);
140+
130141
return new StakeConnection(
131142
program,
132143
provider,
133144
config,
134145
configAddress,
135-
votingProductMetadataAccount
146+
votingProductMetadataAccount,
147+
votingAccountMetadataWasm
136148
);
137149
}
138150

@@ -965,6 +977,20 @@ export class StakeConnection {
965977
.preInstructions(preInstructions)
966978
.rpc();
967979
}
980+
981+
/**
982+
* This returns the current scaling factor between staked tokens and realms voter weight.
983+
* The formula is n_staked_tokens = scaling_factor * n_voter_weight
984+
*/
985+
public getScalingFactor(): number {
986+
let currentEpoch = new BN(Date.now() / 1000).div(this.config.epochDuration);
987+
let currentAmountLocked = Number(
988+
this.votingAccountMetadataWasm.getCurrentAmountLocked(
989+
BigInt(currentEpoch.toString())
990+
)
991+
);
992+
return currentAmountLocked / Number(wasm.Constants.MAX_VOTER_WEIGHT());
993+
}
968994
}
969995

970996
export interface BalanceSummary {

0 commit comments

Comments
 (0)