From d4eca95b85c122f52954f3409bcfcbff79f163fc Mon Sep 17 00:00:00 2001 From: 0xButterfield <100517047+0xButterfield@users.noreply.github.com> Date: Sun, 17 Dec 2023 12:50:41 +0100 Subject: [PATCH] fix: delegation-with-cap (#1374) - Ensure the `delegation-with-cap` strategy caps votes against the total supply, not total cast votes Co-authored-by: Chaitanya --- src/strategies/delegation-with-cap/README.md | 6 +++++- src/strategies/delegation-with-cap/examples.json | 4 +++- src/strategies/delegation-with-cap/index.ts | 11 ++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/strategies/delegation-with-cap/README.md b/src/strategies/delegation-with-cap/README.md index 88a59ed79..354213c84 100644 --- a/src/strategies/delegation-with-cap/README.md +++ b/src/strategies/delegation-with-cap/README.md @@ -1,11 +1,13 @@ # delegation-with-cap -This strategy is based on the [delegation](https://github.com/snapshot-labs/snapshot-strategies/tree/master/src/strategies/delegation) strategy, with an additional `capPercentage` parameter that caps the voting power of any address to a percentage of the total votes. +This strategy is based on the [delegation](https://github.com/snapshot-labs/snapshot-strategies/tree/master/src/strategies/delegation) strategy, with an additional `capPercentage` parameter that caps the voting power of any address to a percentage of the total supply, along with `address` and `decimals` parameters to get the total supply that the voting power should be capped against. | Param Name | Description | |----------------------------|-----------------------------------------------------------------------------------------| | strategies | list of sub strategies to calculate voting power based on delegation | | capPercentage | Maximum voting power for any address as a percentage of total votes | +| address | Address of the token contract used to cap total supply against | +| decimals | Decimals of the token contract used to cap total supply against | | delegationSpace (optional) | Get delegations of a particular space (by default it take delegations of current space) | Here is an example of parameters: @@ -13,6 +15,8 @@ Here is an example of parameters: ```json { "symbol": "veBAL (delegated)", + "address": "0xC128a9954e6c874eA3d62ce62B468bA073093F25", + "decimals": 18, "strategies": [ { "name": "erc20-balance-of", diff --git a/src/strategies/delegation-with-cap/examples.json b/src/strategies/delegation-with-cap/examples.json index 89442defa..8f554a491 100644 --- a/src/strategies/delegation-with-cap/examples.json +++ b/src/strategies/delegation-with-cap/examples.json @@ -5,6 +5,8 @@ "name": "delegation-with-cap", "params": { "symbol": "veBAL (delegated)", + "address": "0xC128a9954e6c874eA3d62ce62B468bA073093F25", + "decimals": 18, "strategies": [ { "name": "erc20-balance-of", @@ -16,7 +18,7 @@ } ], "delegationSpace": "balancer.eth", - "capPercentage": 30 + "capPercentage": 10 } }, "network": "1", diff --git a/src/strategies/delegation-with-cap/index.ts b/src/strategies/delegation-with-cap/index.ts index 9dc2bceaa..daa31413c 100644 --- a/src/strategies/delegation-with-cap/index.ts +++ b/src/strategies/delegation-with-cap/index.ts @@ -1,5 +1,5 @@ import { getDelegations } from '../../utils/delegation'; -import { getScoresDirect } from '../../utils'; +import { call, getScoresDirect } from '../../utils'; export const author = '0xButterfield'; export const version = '0.1.0'; @@ -57,8 +57,13 @@ export async function strategy( }) ); - const totalScore = Object.values(addressScores).reduce((a, b) => a + b, 0); - const maxScore = (options.capPercentage * totalScore) / 100; + const totalSupply = await call( + provider, + ['function totalSupply() public view returns (uint256)'], + [options.address, 'totalSupply'] + ).then((res) => Number(res.toString()) / 10 ** options.decimals); + + const maxScore = (options.capPercentage * totalSupply) / 100; return Object.fromEntries( Object.entries(addressScores).map(([address, addressScore]) => [