Skip to content

Commit

Permalink
[synthetix] Update existing strategy with the upcoming election value…
Browse files Browse the repository at this point in the history
…s and create a non-weighted version (snapshot-labs#67)

* 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
  • Loading branch information
andytcf authored Sep 20, 2021
1 parent ae2a18a commit 9d748a7
Show file tree
Hide file tree
Showing 9 changed files with 552 additions and 35 deletions.
4 changes: 4 additions & 0 deletions src/strategies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import * as faralandStaking from './faraland-staking';
import * as flashstake from './flashstake';
import * as pancake from './pancake';
import * as synthetix from './synthetix';
import * as synthetixQuadratic from './synthetix-quadratic';
import * as synthetixNonQuadratic from './synthetix-non-quadratic';
import * as ctoken from './ctoken';
import * as cream from './cream';
import * as esd from './esd';
Expand Down Expand Up @@ -196,6 +198,8 @@ const strategies = {
flashstake,
pancake,
synthetix,
'synthetix-quadratic': synthetixQuadratic,
'synthetix-non-quadratic': synthetixNonQuadratic,
ctoken,
cream,
'staked-uniswap': stakedUniswap,
Expand Down
15 changes: 15 additions & 0 deletions src/strategies/synthetix-non-quadratic/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"]
]
}
```
24 changes: 24 additions & 0 deletions src/strategies/synthetix-non-quadratic/examples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"name": "Example query",
"strategy": {
"name": "synthetix-non-quadratic",
"params": {
"address": "0x023c66b7e13d30a3c46aa433fd2829763d5817c5",
"symbol": "WD"
}
},
"network": "1",
"addresses": [
"0x78b037B39704e88a82DD23CFBE1f57f6AeF8EBC5",
"0x0bc3668d2AaFa53eD5E5134bA13ec74ea195D000",
"0xcAc59F91E4536Bc0E79aB816a5cD54e89f10433C",
"0x6dc88B231Cd04Dd1b1e525161162993F47140006",
"0x935D2fD458fdf41B6F7B62471f593797866a3Ce6",
"0x24e445fe7708Bf4bC2ae8d4df1694C98Af8BDE4F",
"0x49be88f0fcc3a8393a59d3688480d7d253c37d2a",
"0x27Cc4d6bc95b55a3a981BF1F1c7261CDa7bB0931"
],
"snapshot": 13228907
}
]
185 changes: 185 additions & 0 deletions src/strategies/synthetix-non-quadratic/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import { getAddress } from '@ethersproject/address';
import { BigNumber } from '@ethersproject/bignumber';
import { Contract } from '@ethersproject/contracts';
import { Provider } from '@ethersproject/providers';
import {
subgraphRequest
// ipfsGet
} 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 = '0xe92B4c7428152052B0930c81F4c687a5F1A12292';

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

// @TODO: update with the latest ovm snapshot
// const ovmSnapshotJSON = 'QmNwvhq4By1Mownjycg7bWSXqbJWMVyAWRZ1K4mjxuvGXg';

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,
_,
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 = 22617610;
// @TODO update the lastDebtLedgerEntry from (https://contracts.synthetix.io/ovm/SynthetixState)
const lastDebtLedgerEntryL2 = 20222730523217499684984991;
// @TODO update the comparison between OVM:ETH c-ratios at the time of snapshot
const normalisedL2CRatio = 600 / 450;
// @TODO update the L2 block number to use
const L2BlockNumber = 1770186;

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;

// @notice fallback for when subgraph is down
/*
const OVMSnapshot = await ipfsGet('gateway.pinata.cloud', ovmSnapshotJSON);
const array = Object.assign(
{},
...OVMSnapshot.data.snxholders.map((key) => ({
[getAddress(key.id)]: {
initialDebtOwnership: key.initialDebtOwnership,
debtEntryAtIndex: key.debtEntryAtIndex
}
}))
);
for (let k = 0; k < _addresses.length; k++) {
const address = _addresses[k];
if (array[getAddress(address)]) {
score[getAddress(address)] += await quadraticWeightedVoteL2(
array[getAddress(address)].initialDebtOwnership,
array[getAddress(address)].debtEntryAtIndex,
totalL1Debt,
scaledTotalL2Debt,
lastDebtLedgerEntryL2
);
} else {
continue;
}
}
*/

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 || {};
}
15 changes: 15 additions & 0 deletions src/strategies/synthetix-quadratic/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"]
]
}
```
24 changes: 24 additions & 0 deletions src/strategies/synthetix-quadratic/examples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"name": "Example query",
"strategy": {
"name": "synthetix-quadratic",
"params": {
"address": "0x023c66b7e13d30a3c46aa433fd2829763d5817c5",
"symbol": "WD"
}
},
"network": "1",
"addresses": [
"0x78b037B39704e88a82DD23CFBE1f57f6AeF8EBC5",
"0x0bc3668d2AaFa53eD5E5134bA13ec74ea195D000",
"0xcAc59F91E4536Bc0E79aB816a5cD54e89f10433C",
"0x6dc88B231Cd04Dd1b1e525161162993F47140006",
"0x935D2fD458fdf41B6F7B62471f593797866a3Ce6",
"0x24e445fe7708Bf4bC2ae8d4df1694C98Af8BDE4F",
"0x49be88f0fcc3a8393a59d3688480d7d253c37d2a",
"0x27Cc4d6bc95b55a3a981BF1F1c7261CDa7bB0931"
],
"snapshot": 13228907
}
]
Loading

0 comments on commit 9d748a7

Please sign in to comment.