Skip to content

Commit

Permalink
Add scaling factor to convert from voter weight to staked tokens (#320)
Browse files Browse the repository at this point in the history
* Weight converter

* Cleanup

* Restore tests

* Add scaling factor function

* Add scaling factor
  • Loading branch information
guibescos authored Jan 4, 2024
1 parent 16dea58 commit 63abf40
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions staking/app/StakeConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,24 @@ export class StakeConnection {
configAddress: PublicKey;
votingProductMetadataAccount: PublicKey;
votingProduct = { voting: {} };
votingAccountMetadataWasm: any;
governanceAddress: PublicKey;

private constructor(
program: Program<Staking>,
provider: AnchorProvider,
config: GlobalConfig,
configAddress: PublicKey,
votingProductMetadataAccount: PublicKey
votingProductMetadataAccount: PublicKey,
votingAccountMetadataWasm: any
) {
this.program = program;
this.provider = provider;
this.config = config;
this.configAddress = configAddress;
this.votingProductMetadataAccount = votingProductMetadataAccount;
this.governanceAddress = GOVERNANCE_ADDRESS();
this.votingAccountMetadataWasm = votingAccountMetadataWasm;
}

public static async connect(
Expand Down Expand Up @@ -127,12 +130,21 @@ export class StakeConnection {
)
)[0];

const votingProductMetadataAccountData =
await program.provider.connection.getAccountInfo(
votingProductMetadataAccount
);
const votingAccountMetadataWasm = new wasm.WasmTargetMetadata(
votingProductMetadataAccountData!.data
);

return new StakeConnection(
program,
provider,
config,
configAddress,
votingProductMetadataAccount
votingProductMetadataAccount,
votingAccountMetadataWasm
);
}

Expand Down Expand Up @@ -965,6 +977,20 @@ export class StakeConnection {
.preInstructions(preInstructions)
.rpc();
}

/**
* This returns the current scaling factor between staked tokens and realms voter weight.
* The formula is n_staked_tokens = scaling_factor * n_voter_weight
*/
public getScalingFactor(): number {
let currentEpoch = new BN(Date.now() / 1000).div(this.config.epochDuration);
let currentAmountLocked = Number(
this.votingAccountMetadataWasm.getCurrentAmountLocked(
BigInt(currentEpoch.toString())
)
);
return currentAmountLocked / Number(wasm.Constants.MAX_VOTER_WEIGHT());
}
}

export interface BalanceSummary {
Expand Down

2 comments on commit 63abf40

@vercel
Copy link

@vercel vercel bot commented on 63abf40 Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

staking-devnet – ./

staking-devnet-git-main-pyth-web.vercel.app
staking-devnet-pyth-web.vercel.app
governance-nu.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 63abf40 Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.