Skip to content

Commit

Permalink
fix: delegation-with-cap (#1374)
Browse files Browse the repository at this point in the history
- Ensure the `delegation-with-cap` strategy caps votes against the total supply, not total cast votes

Co-authored-by: Chaitanya <yourchaitu@gmail.com>
  • Loading branch information
0xButterfield and ChaituVR committed Dec 17, 2023
1 parent ddda8e9 commit d4eca95
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/strategies/delegation-with-cap/README.md
@@ -1,18 +1,22 @@
# 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:

```json
{
"symbol": "veBAL (delegated)",
"address": "0xC128a9954e6c874eA3d62ce62B468bA073093F25",
"decimals": 18,
"strategies": [
{
"name": "erc20-balance-of",
Expand Down
4 changes: 3 additions & 1 deletion src/strategies/delegation-with-cap/examples.json
Expand Up @@ -5,6 +5,8 @@
"name": "delegation-with-cap",
"params": {
"symbol": "veBAL (delegated)",
"address": "0xC128a9954e6c874eA3d62ce62B468bA073093F25",
"decimals": 18,
"strategies": [
{
"name": "erc20-balance-of",
Expand All @@ -16,7 +18,7 @@
}
],
"delegationSpace": "balancer.eth",
"capPercentage": 30
"capPercentage": 10
}
},
"network": "1",
Expand Down
11 changes: 8 additions & 3 deletions 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';
Expand Down Expand Up @@ -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]) => [
Expand Down

0 comments on commit d4eca95

Please sign in to comment.