Skip to content

Commit

Permalink
Merge pull request #93 from OlympusDAO/ohm-buyback
Browse files Browse the repository at this point in the history
Support for OHM buyback
  • Loading branch information
0xJem authored Aug 27, 2024
2 parents c4806d0 + 5441b08 commit b910b3f
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 14 deletions.
40 changes: 40 additions & 0 deletions apps/server/.wundergraph/metricHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,35 @@ const getSupplyCategories = (records: TokenSupply[], ohmIndex: number): [SupplyC
// TokenRecord metrics
//

/**
* Determines if the given record is a variant of OHM.
*
* @param record
* @returns
*/
const isOHM = (record: TokenRecord): boolean => {
return getOhmAddresses().includes(record.token.toLowerCase()) || getOhmAddresses().includes(record.tokenAddress.toLowerCase());
}

/**
* Determines if the given record is sourced from a buyback address.
*
* @param record
* @returns
*/
const isBuybackAddress = (record: TokenRecord): boolean => {
if (!record.block) {
return false;
}

// If before the inclusion block, ignore
if (Number(record.block) < 20514801) {
return false;
}

return record.sourceAddress.toLowerCase() == "0xf7deb867e65306be0cb33918ac1b8f89a72109db".toLowerCase();
}

/**
* Calculates the market value or liquid backing for the given records.
*
Expand All @@ -466,6 +495,17 @@ const getTreasuryAssetValue = (
return [previousTotalValue, previousAllRecords, previousChainValues, previousChainRecords];
}

// If it is OHM and liquidBacking is specified, ignore
const isTokenOhm = isOHM(currentRecord);
if (liquidBacking && isTokenOhm) {
return [previousTotalValue, previousAllRecords, previousChainValues, previousChainRecords];
}

// If it is OHM and not in the buyback addresses, ignore
if (isTokenOhm && !isBuybackAddress(currentRecord)) {
return [previousTotalValue, previousAllRecords, previousChainValues, previousChainRecords];
}

const currentValue: number = liquidBacking ? +currentRecord.valueExcludingOhm : +currentRecord.value;

const newTotalValue: number = previousTotalValue + currentValue;
Expand Down
2 changes: 1 addition & 1 deletion apps/server/.wundergraph/wundergraph.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const resolveSubgraphUrl = (url: string): string => {

const treasuryEthereum = introspect.graphql({
apiNamespace: "treasuryEthereum",
url: resolveSubgraphUrl("https://gateway-arbitrum.network.thegraph.com/api/[api-key]/deployments/id/Qmd7wgnNTijSpV1JDM3yUE8Exy4EGG1jw3yyQKdTKvYiig"), // 5.1.3
url: resolveSubgraphUrl("https://gateway-arbitrum.network.thegraph.com/api/[api-key]/deployments/id/Qmam2fnfYzj6srEGC49XxsFyMwngs7xKwjGWZc7jnEU97h"), // 5.2.6
schemaExtension: schemaExtension,
});

Expand Down
5 changes: 5 additions & 0 deletions apps/server/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## [v1.4.0]

- Amend treasury market value to include in market value calculations the value of OHM (and variants) in protocol buyback addresses
6 changes: 3 additions & 3 deletions apps/server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@olympusdao/treasury-subgraph",
"version": "1.3.0",
"version": "1.4.0",
"engines": {
"node": ">= 18.0.0"
},
Expand Down Expand Up @@ -34,6 +34,6 @@
"pretest:ci": "yarn build:local",
"test": "jest",
"test:ci": "WG_LOG_LEVEL=error jest --runInBand --ci",
"test:local": "dotenv -e ../../.env jest"
"test:local": "jest -e ../../.env"
}
}
}
99 changes: 93 additions & 6 deletions apps/server/tests/metrics.test.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions apps/server/tests/metricsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type TokenSupply = TokenSuppliesResponseData["treasuryEthereum_tokenSupplies"][0
const OHM_ADDRESSES: string[] = [
"0x64aa3364f17a4d01c6f1751fd97c2bd3d7e7f1d5".toLowerCase(), // Mainnet
"0xf0cb2dc0db5e6c66B9a70Ac27B06b878da017028".toLowerCase(), // Arbitrum
"0x060cb087a9730e13aa191f31a6d86bff8dfcdcc0".toLowerCase(), // Base
];

const GOHM_ADDRESSES: string[] = [
Expand Down
4 changes: 4 additions & 0 deletions apps/server/tests/tokenRecordHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe("filterCompleteRecords", () => {
getSampleRecord("2", "2021-01-02", 2),
getSampleRecord("1", "2021-01-01", 1),
],
treasuryBase_tokenRecords: [],
treasuryEthereum_tokenRecords: [
getSampleRecord("3", "2021-01-03", 3),
getSampleRecord("2", "2021-01-02", 2),
Expand Down Expand Up @@ -90,6 +91,7 @@ describe("filterCompleteRecords", () => {
getSampleRecord("2", "2021-01-02", 2),
getSampleRecord("1", "2021-01-01", 1),
],
treasuryBase_tokenRecords: [],
treasuryEthereum_tokenRecords: [
getSampleRecord("2", "2021-01-02", 2),
getSampleRecord("1", "2021-01-01", 1),
Expand Down Expand Up @@ -118,6 +120,7 @@ describe("filterCompleteRecords", () => {
getSampleRecord("2", "2021-01-02", 2),
getSampleRecord("1", "2021-01-01", 1),
],
treasuryBase_tokenRecords: [],
treasuryEthereum_tokenRecords: [],
treasuryFantom_tokenRecords: [],
treasuryPolygon_tokenRecords: []
Expand All @@ -137,6 +140,7 @@ describe("filterCompleteRecords", () => {
getSampleRecord("2", "2021-01-02", 2),
getSampleRecord("1", "2021-01-01", 1),
],
treasuryBase_tokenRecords: [],
treasuryFantom_tokenRecords: [],
treasuryPolygon_tokenRecords: []
};
Expand Down
7 changes: 6 additions & 1 deletion apps/server/tests/tokenRecords.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { addDays } from "date-fns";
import { createTestServer } from "../.wundergraph/generated/testing";
import { getISO8601DateString } from "./dateHelper";
import { CHAIN_ARBITRUM, CHAIN_ETHEREUM, CHAIN_FANTOM, CHAIN_POLYGON } from "../.wundergraph/constants";
import { CHAIN_ARBITRUM, CHAIN_BASE, CHAIN_ETHEREUM, CHAIN_FANTOM, CHAIN_POLYGON } from "../.wundergraph/constants";
import { getFirstRecord } from "./tokenRecordHelper";
import { parseNumber } from "./numberHelper";

Expand Down Expand Up @@ -294,6 +294,8 @@ describe("atBlock", () => {
// Raw data has an array property for each chain
const arbitrumRawResult = getFirstRecord(rawResult.data, CHAIN_ARBITRUM, startDate);
const arbitrumRawBlock = parseNumber(arbitrumRawResult?.block);
const baseRawResult = getFirstRecord(rawResult.data, CHAIN_BASE, startDate);
const baseRawBlock = parseNumber(baseRawResult?.block);
const ethereumRawResult = getFirstRecord(rawResult.data, CHAIN_ETHEREUM, startDate);
const ethereumRawBlock = parseNumber(ethereumRawResult?.block);
const fantomRawResult = getFirstRecord(rawResult.data, CHAIN_FANTOM, startDate);
Expand All @@ -306,6 +308,7 @@ describe("atBlock", () => {
operationName: "atBlock/tokenRecords",
input: {
arbitrumBlock: arbitrumRawBlock,
baseBlock: baseRawBlock,
ethereumBlock: ethereumRawBlock,
fantomBlock: fantomRawBlock,
polygonBlock: polygonRawBlock,
Expand All @@ -315,12 +318,14 @@ describe("atBlock", () => {
// Latest records is collapsed into a flat array
const records = result.data;
const arbitrumResult = getFirstRecord(records, CHAIN_ARBITRUM);
const baseResult = getFirstRecord(records, CHAIN_BASE);
const ethereumResult = getFirstRecord(records, CHAIN_ETHEREUM);
const fantomResult = getFirstRecord(records, CHAIN_FANTOM);
const polygonResult = getFirstRecord(records, CHAIN_POLYGON);

// Check that the block is the same
expect(parseNumber(arbitrumResult?.block)).toEqual(arbitrumRawBlock);
expect(parseNumber(baseResult?.block)).toEqual(baseRawBlock);
expect(parseNumber(ethereumResult?.block)).toEqual(ethereumRawBlock);
expect(parseNumber(fantomResult?.block)).toEqual(fantomRawBlock);
expect(parseNumber(polygonResult?.block)).toEqual(polygonRawBlock);
Expand Down
16 changes: 13 additions & 3 deletions apps/server/tests/tokenSupplies.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { addDays } from "date-fns";
import { createTestServer } from "../.wundergraph/generated/testing";
import { getISO8601DateString } from "./dateHelper";
import { CHAIN_ARBITRUM, CHAIN_ETHEREUM, CHAIN_FANTOM, CHAIN_POLYGON } from "../.wundergraph/constants";
import { CHAIN_ARBITRUM, CHAIN_BASE, CHAIN_ETHEREUM, CHAIN_FANTOM, CHAIN_POLYGON } from "../.wundergraph/constants";
import { getFirstRecord } from "./tokenSupplyHelper";
import { parseNumber } from "./numberHelper";

Expand Down Expand Up @@ -186,12 +186,13 @@ describe("latest", () => {

// Raw data has an array property for each chain
const arbitrumRawResult = rawResult.data?.treasuryArbitrum_tokenSupplies[0];
const baseRawResult = rawResult.data?.treasuryBase_tokenSupplies[0];
const ethereumRawResult = rawResult.data?.treasuryEthereum_tokenSupplies[0];
const fantomRawResult = rawResult.data?.treasuryFantom_tokenSupplies[0];
const polygonRawResult = rawResult.data?.treasuryPolygon_tokenSupplies[0];

// Calculate the expected count based on how many of the raw results were defined. This is because there may not be TokenSupply records on every chain.
const expectedCount = [arbitrumRawResult, ethereumRawResult, fantomRawResult, polygonRawResult].filter((result) => result !== undefined).length;
const expectedCount = [arbitrumRawResult, baseRawResult, ethereumRawResult, fantomRawResult, polygonRawResult].filter((result) => result !== undefined).length;

// Grab the results from the latest operation
const result = await wg.client().query({
Expand All @@ -201,12 +202,14 @@ describe("latest", () => {
// Latest records is collapsed into a flat array
const records = result.data;
const arbitrumResult = getFirstRecord(records, CHAIN_ARBITRUM);
const baseResult = getFirstRecord(records, CHAIN_BASE);
const ethereumResult = getFirstRecord(records, CHAIN_ETHEREUM);
const fantomResult = getFirstRecord(records, CHAIN_FANTOM);
const polygonResult = getFirstRecord(records, CHAIN_POLYGON);

// Check that the block is the same
expect(arbitrumResult?.block).toEqual(arbitrumRawResult?.block);
expect(baseResult?.block).toEqual(baseRawResult?.block);
expect(ethereumResult?.block).toEqual(ethereumRawResult?.block);
expect(fantomResult?.block).toEqual(fantomRawResult?.block);
expect(polygonResult?.block).toEqual(polygonRawResult?.block);
Expand Down Expand Up @@ -240,12 +243,13 @@ describe("earliest", () => {

// Raw data has an array property for each chain
const arbitrumRawResult = rawResult.data?.treasuryArbitrum_tokenSupplies[0];
const baseRawResult = rawResult.data?.treasuryBase_tokenSupplies[0];
const ethereumRawResult = rawResult.data?.treasuryEthereum_tokenSupplies[0];
const fantomRawResult = rawResult.data?.treasuryFantom_tokenSupplies[0];
const polygonRawResult = rawResult.data?.treasuryPolygon_tokenSupplies[0];

// Calculate the expected count based on how many of the raw results were defined. This is because there may not be TokenSupply records on every chain.
const expectedCount = [arbitrumRawResult, ethereumRawResult, fantomRawResult, polygonRawResult].filter((result) => result !== undefined).length;
const expectedCount = [arbitrumRawResult, baseRawResult, ethereumRawResult, fantomRawResult, polygonRawResult].filter((result) => result !== undefined).length;

// Grab the results from the earliest operation
const result = await wg.client().query({
Expand All @@ -255,12 +259,14 @@ describe("earliest", () => {
// Latest records is collapsed into a flat array
const records = result.data;
const arbitrumResult = getFirstRecord(records, CHAIN_ARBITRUM);
const baseResult = getFirstRecord(records, CHAIN_BASE);
const ethereumResult = getFirstRecord(records, CHAIN_ETHEREUM);
const fantomResult = getFirstRecord(records, CHAIN_FANTOM);
const polygonResult = getFirstRecord(records, CHAIN_POLYGON);

// Check that the block is the same
expect(arbitrumResult?.block).toEqual(arbitrumRawResult?.block);
expect(baseResult?.block).toEqual(baseRawResult?.block);
expect(ethereumResult?.block).toEqual(ethereumRawResult?.block);
expect(fantomResult?.block).toEqual(fantomRawResult?.block);
expect(polygonResult?.block).toEqual(polygonRawResult?.block);
Expand Down Expand Up @@ -299,6 +305,7 @@ describe("atBlock", () => {

// Raw data has an array property for each chain
const arbitrumRawBlock = getFirstRecord(rawResult.data, CHAIN_ARBITRUM, startDate)?.block;
const baseRawBlock = getFirstRecord(rawResult.data, CHAIN_BASE, startDate)?.block;
const ethereumRawBlock = getFirstRecord(rawResult.data, CHAIN_ETHEREUM, startDate)?.block;
const fantomRawBlock = getFirstRecord(rawResult.data, CHAIN_FANTOM, startDate)?.block;
const polygonRawBlock = getFirstRecord(rawResult.data, CHAIN_POLYGON, startDate)?.block;
Expand All @@ -308,6 +315,7 @@ describe("atBlock", () => {
operationName: "atBlock/tokenSupplies",
input: {
arbitrumBlock: parseNumber(arbitrumRawBlock),
baseBlock: parseNumber(baseRawBlock),
ethereumBlock: parseNumber(ethereumRawBlock),
fantomBlock: parseNumber(fantomRawBlock),
polygonBlock: parseNumber(polygonRawBlock),
Expand All @@ -317,12 +325,14 @@ describe("atBlock", () => {
// Latest records is collapsed into a flat array
const records = result.data;
const arbitrumResult = getFirstRecord(records, CHAIN_ARBITRUM);
const baseResult = getFirstRecord(records, CHAIN_BASE);
const ethereumResult = getFirstRecord(records, CHAIN_ETHEREUM);
const fantomResult = getFirstRecord(records, CHAIN_FANTOM);
const polygonResult = getFirstRecord(records, CHAIN_POLYGON);

// Check that the block is the same
expect(arbitrumResult?.block).toEqual(arbitrumRawBlock);
expect(baseResult?.block).toEqual(baseRawBlock);
expect(ethereumResult?.block).toEqual(ethereumRawBlock);
expect(fantomResult?.block).toEqual(fantomRawBlock);
expect(polygonResult?.block).toEqual(polygonRawBlock);
Expand Down
4 changes: 4 additions & 0 deletions apps/server/tests/tokenSupplyHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe("filterCompleteRecords", () => {
getSampleRecord("2", "2021-01-02", 2),
getSampleRecord("1", "2021-01-01", 1),
],
treasuryBase_tokenSupplies: [],
treasuryEthereum_tokenSupplies: [
getSampleRecord("3", "2021-01-03", 3),
getSampleRecord("2", "2021-01-02", 2),
Expand Down Expand Up @@ -85,6 +86,7 @@ describe("filterCompleteRecords", () => {
getSampleRecord("2", "2021-01-02", 2),
getSampleRecord("1", "2021-01-01", 1),
],
treasuryBase_tokenSupplies: [],
treasuryEthereum_tokenSupplies: [
getSampleRecord("2", "2021-01-02", 2),
getSampleRecord("1", "2021-01-01", 1),
Expand Down Expand Up @@ -113,6 +115,7 @@ describe("filterCompleteRecords", () => {
getSampleRecord("2", "2021-01-02", 2),
getSampleRecord("1", "2021-01-01", 1),
],
treasuryBase_tokenSupplies: [],
treasuryEthereum_tokenSupplies: [],
treasuryFantom_tokenSupplies: [],
treasuryPolygon_tokenSupplies: []
Expand All @@ -132,6 +135,7 @@ describe("filterCompleteRecords", () => {
getSampleRecord("2", "2021-01-02", 2),
getSampleRecord("1", "2021-01-01", 1),
],
treasuryBase_tokenSupplies: [],
treasuryFantom_tokenSupplies: [],
treasuryPolygon_tokenSupplies: []
};
Expand Down

0 comments on commit b910b3f

Please sign in to comment.