Skip to content

Commit

Permalink
Add synthetix-quadratic_2 and [synthetix-non-quadratic_2] strategies …
Browse files Browse the repository at this point in the history
…/ fix previous README.md (snapshot-labs#496)

* add new strategy

* update existing strategy for upcoming election

* update endpoints

* add into index

* revert synthetix back to original

* introduce a new strategy

* import in index

* remove console log and ensure test work

* refactor

* add synthetix-non-quadratic_2

* fix synthetix-non-quadratic_1 readme

* add synthetix-quadratic_2 strategy

* fix other strategies readme

* fix readme
  • Loading branch information
andytcf authored Mar 24, 2022
1 parent c4245ec commit 4f085c5
Show file tree
Hide file tree
Showing 10 changed files with 395 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/strategies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ import * as synthetix from './synthetix';
import * as aelinCouncil from './aelin-council';
import * as synthetixQuadratic from './synthetix-quadratic';
import * as synthetixQuadraticOne from './synthetix-quadratic_1';
import * as synthetixQuadraticTwo from './synthetix-quadratic_2';
import * as synthetixNonQuadratic from './synthetix-non-quadratic';
import * as synthetixNonQuadraticOne from './synthetix-non-quadratic_1';
import * as synthetixNonQuadraticTwo from './synthetix-non-quadratic_2';
import * as ctoken from './ctoken';
import * as cream from './cream';
import * as esd from './esd';
Expand Down Expand Up @@ -352,8 +354,10 @@ const strategies = {
'aelin-council': aelinCouncil,
'synthetix-quadratic': synthetixQuadratic,
'synthetix-quadratic_1': synthetixQuadraticOne,
'synthetix-quadratic_2': synthetixQuadraticTwo,
'synthetix-non-quadratic': synthetixNonQuadratic,
'synthetix-non-quadratic_1': synthetixNonQuadraticOne,
'synthetix-non-quadratic_2': synthetixNonQuadraticTwo,
ctoken,
cream,
'staked-uniswap': stakedUniswap,
Expand Down
2 changes: 1 addition & 1 deletion src/strategies/synthetix-non-quadratic_1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Can be used instead of the erc20-balance-of strategy, the space config will look
```JSON
{
"strategies": [
["synthetix-non-quadratic"]
["synthetix-non-quadratic_1"]
]
}
```
15 changes: 15 additions & 0 deletions src/strategies/synthetix-non-quadratic_2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Non-Quadratic Debt Percentage Strategy

Calculates the weighting of voters, based on their debt percentage in the previous fee period.

## Examples

Can be used instead of the erc20-balance-of strategy, the space config will look like this:

```JSON
{
"strategies": [
["synthetix-non-quadratic_2"]
]
}
```
27 changes: 27 additions & 0 deletions src/strategies/synthetix-non-quadratic_2/examples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"name": "Example query",
"strategy": {
"name": "synthetix-non-quadratic_2",
"params": {
"address": "0x023c66b7e13d30a3c46aa433fd2829763d5817c5",
"symbol": "WD",
"totalL2Debt": 40201854,
"lastDebtLedgerEntryL2": 10909822163955610660349890,
"L2BlockNumber": 4750287
}
},
"network": "1",
"addresses": [
"0x78b037B39704e88a82DD23CFBE1f57f6AeF8EBC5",
"0x0bc3668d2AaFa53eD5E5134bA13ec74ea195D000",
"0xcAc59F91E4536Bc0E79aB816a5cD54e89f10433C",
"0x6dc88B231Cd04Dd1b1e525161162993F47140006",
"0x935D2fD458fdf41B6F7B62471f593797866a3Ce6",
"0x24e445fe7708Bf4bC2ae8d4df1694C98Af8BDE4F",
"0x49be88f0fcc3a8393a59d3688480d7d253c37d2a",
"0x27Cc4d6bc95b55a3a981BF1F1c7261CDa7bB0931"
],
"snapshot": 14443440
}
]
153 changes: 153 additions & 0 deletions src/strategies/synthetix-non-quadratic_2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { getAddress } from '@ethersproject/address';
import { BigNumber } from '@ethersproject/bignumber';
import { Contract } from '@ethersproject/contracts';
import { Provider } from '@ethersproject/providers';
import { subgraphRequest } from '../../utils';
import {
DebtCacheABI,
debtL1,
debtL2,
returnGraphParams,
SNXHoldersResult,
SynthetixStateABI
} from '../synthetix/helper';

export const author = 'andytcf';
export const version = '1.0.0';

const MED_PRECISE_UNIT = 1e18;

// @TODO: check if most-up-to-date version (using https://contracts.synthetix.io/SynthetixState)
const SynthetixStateContractAddress =
'0x4b9Ca5607f1fF8019c1C6A3c2f0CC8de622D5B82';
// @TODO: check if most-up-to-date version (using http://contracts.synthetix.io/DebtCache)
const DebtCacheContractAddress = '0x1620Aa736939597891C1940CF0d28b82566F9390';

const defaultGraphs = {
'1': 'https://api.thegraph.com/subgraphs/name/synthetixio-team/synthetix',
'10': 'https://api.thegraph.com/subgraphs/name/synthetixio-team/optimism-main'
};

const loadLastDebtLedgerEntry = async (
provider: Provider,
snapshot: number | string
) => {
const contract = new Contract(
SynthetixStateContractAddress,
SynthetixStateABI,
provider
);

const lastDebtLedgerEntry = await contract.lastDebtLedgerEntry({
blockTag: snapshot
});

return BigNumber.from(lastDebtLedgerEntry);
};

const loadL1TotalDebt = async (
provider: Provider,
snapshot: number | string
) => {
const contract = new Contract(
DebtCacheContractAddress,
DebtCacheABI,
provider
);

const currentDebtObject = await contract.currentDebt({
blockTag: snapshot
});

return Number(currentDebtObject.debt) / MED_PRECISE_UNIT;
};

export async function strategy(
_space,
_network,
_provider,
_addresses,
_options,
snapshot
) {
const score = {};
const blockTag = typeof snapshot === 'number' ? snapshot : 'latest';

/* Global Constants */

const totalL1Debt = await loadL1TotalDebt(_provider, snapshot); // (high-precision 1e18)
const lastDebtLedgerEntry = await loadLastDebtLedgerEntry(
_provider,
snapshot
);

/* EDIT THESE FOR OVM */

// @TODO update the currentDebt for the snapshot from (https://contracts.synthetix.io/ovm/DebtCache)
// const totalL2Debt = 40201854;
const totalL2Debt = _options.totalL2Debt;
// @TODO update the lastDebtLedgerEntry from (https://contracts.synthetix.io/ovm/SynthetixState)
// const lastDebtLedgerEntryL2 = 10909822163955610660349890;
const lastDebtLedgerEntryL2 = _options.lastDebtLedgerEntryL2;
// @TODO update the comparison between OVM:ETH c-ratios at the time of snapshot
const normalisedL2CRatio = 500 / 400;
// @TODO update the L2 block number to use
// const L2BlockNumber = 4750287;
const L2BlockNumber = _options.L2BlockNumber;

const scaledTotalL2Debt = totalL2Debt * normalisedL2CRatio;

/* --------------- */

/* Using the subgraph, we get the relevant L1 calculations */

const l1Results = (await subgraphRequest(
defaultGraphs[1],
returnGraphParams(blockTag, _addresses)
)) as SNXHoldersResult;

if (l1Results && l1Results.snxholders) {
for (let i = 0; i < l1Results.snxholders.length; i++) {
const holder = l1Results.snxholders[i];
const vote = await debtL1(
holder.initialDebtOwnership,
holder.debtEntryAtIndex,
totalL1Debt,
scaledTotalL2Debt,
lastDebtLedgerEntry,
false
);
score[getAddress(holder.id)] = vote;
}
}

/* Using the subgraph, we get the relevant L2 calculations */

const l2Results = (await subgraphRequest(
defaultGraphs[10],
returnGraphParams(L2BlockNumber, _addresses)
)) as SNXHoldersResult;

if (l2Results && l2Results.snxholders) {
for (let i = 0; i < l2Results.snxholders.length; i++) {
const holder = l2Results.snxholders[i];

const vote = await debtL2(
holder.initialDebtOwnership,
holder.debtEntryAtIndex,
totalL1Debt,
scaledTotalL2Debt,
lastDebtLedgerEntryL2,
false
);

if (score[getAddress(holder.id)]) {
score[getAddress(holder.id)] += vote;
} else {
score[getAddress(holder.id)] = vote;
}
}
}

return score || {};
}
2 changes: 1 addition & 1 deletion src/strategies/synthetix-quadratic/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Can be used instead of the erc20-balance-of strategy, the space config will look
```JSON
{
"strategies": [
["synthetix"]
["synthetix-quadratic"]
]
}
```
2 changes: 1 addition & 1 deletion src/strategies/synthetix-quadratic_1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Can be used instead of the erc20-balance-of strategy, the space config will look
```JSON
{
"strategies": [
["synthetix"]
["synthetix-quadratic_1"]
]
}
```
15 changes: 15 additions & 0 deletions src/strategies/synthetix-quadratic_2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Quadratic Debt Percentage Strategy

Calculates the quadratic weighting of voters, based on their debt percentage in the previous fee period.

## Examples

Can be used instead of the erc20-balance-of strategy, the space config will look like this:

```JSON
{
"strategies": [
["synthetix-quadratic_2"]
]
}
```
27 changes: 27 additions & 0 deletions src/strategies/synthetix-quadratic_2/examples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"name": "Example query",
"strategy": {
"name": "synthetix-quadratic_2",
"params": {
"address": "0x023c66b7e13d30a3c46aa433fd2829763d5817c5",
"symbol": "WD",
"totalL2Debt": 40201854,
"lastDebtLedgerEntryL2": 10909822163955610660349890,
"L2BlockNumber": 4750287
}
},
"network": "1",
"addresses": [
"0x78b037B39704e88a82DD23CFBE1f57f6AeF8EBC5",
"0x0bc3668d2AaFa53eD5E5134bA13ec74ea195D000",
"0xcAc59F91E4536Bc0E79aB816a5cD54e89f10433C",
"0x6dc88B231Cd04Dd1b1e525161162993F47140006",
"0x935D2fD458fdf41B6F7B62471f593797866a3Ce6",
"0x24e445fe7708Bf4bC2ae8d4df1694C98Af8BDE4F",
"0x49be88f0fcc3a8393a59d3688480d7d253c37d2a",
"0x27Cc4d6bc95b55a3a981BF1F1c7261CDa7bB0931"
],
"snapshot": 14443440
}
]
Loading

0 comments on commit 4f085c5

Please sign in to comment.