Skip to content

Commit

Permalink
node: Updated governor token list update script
Browse files Browse the repository at this point in the history
  • Loading branch information
djb15 committed Dec 8, 2023
1 parent 10a3a65 commit 94504dc
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
3 changes: 2 additions & 1 deletion node/hack/governor/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
lib
node_modules
node_modules
changes.txt
82 changes: 81 additions & 1 deletion node/hack/governor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Connection, JsonRpcProvider } from "@mysten/sui.js";
import { arrayify, zeroPad } from "ethers/lib/utils";

const MinNotional = 0;
const PriceDeltaTolerance = 30; // Price change tolerance in %

const axios = require("axios");
const fs = require("fs");
Expand Down Expand Up @@ -48,6 +49,21 @@ if (fs.existsSync(IncludeFileName)) {
},
*/

// Get the existing token list to check for any extreme price changes and removed tokens
var existingTokenPrices = {};
var existingTokenKeys: string[] = [];
var newTokenKeys = {};

fs.readFile("../../pkg/governor/generated_mainnet_tokens.go", "utf8", function(_, doc) {
var matches = doc.matchAll(/{chain: (?<chain>[0-9]+).+addr: "(?<addr>[0-9a-fA-F]+)".*symbol: "(?<symbol>.*)", coin.*price: (?<price>.*)}.*\n/g);
for(let result of matches) {
let {chain, addr, symbol, price} = result.groups;
if (!existingTokenPrices[chain]) existingTokenPrices[chain] = {};
existingTokenPrices[chain][addr] = parseFloat(price);
existingTokenKeys.push(chain + "-" + addr + "-" + symbol);
}
});

axios
.get(
"https://europe-west3-wormhole-message-db-mainnet.cloudfunctions.net/tvl"
Expand All @@ -73,6 +89,11 @@ axios
content += "func generatedMainnetTokenList() []tokenConfigEntry {\n";
content += "\treturn []tokenConfigEntry {\n";

var significantPriceChanges = [];
var addedTokens = [];
var removedTokens = [];
var newTokensCount = 0;

for (let chain in res.data.AllTime) {
for (let addr in res.data.AllTime[chain]) {
if (addr !== "*") {
Expand Down Expand Up @@ -141,6 +162,34 @@ axios
}
}

// This is a new token
if (!existingTokenPrices[chain][wormholeAddr]) {
addedTokens.push(chain + "-" + wormholeAddr + "-" + data.Symbol);
}
// This is an existing token
else {
var previousPrice = existingTokenPrices[chain][wormholeAddr];

// Price has increased by > tolerance
if (data.TokenPrice > previousPrice * ((100 + PriceDeltaTolerance) / 100)) {
significantPriceChanges.push({
token: chain + "-" + wormholeAddr + "-" + data.Symbol,
previousPrice: previousPrice,
newPrice: data.TokenPrice,
percentageChange: (notional / previousPrice) * 100
});
}
// Price has decreased by > tolerance
else if (data.TokenPrice < previousPrice - (previousPrice * (PriceDeltaTolerance / 100))){
significantPriceChanges.push({
token: chain + "-" + wormholeAddr + "-" + data.Symbol,
previousPrice: previousPrice,
newPrice: data.TokenPrice,
percentageChange: 100 - (notional / previousPrice) * 100
});
}
}

content +=
"\t{ chain: " +
chain +
Expand All @@ -160,12 +209,43 @@ axios
notional +
"\n";

//console.log("chain: " + chain + ", addr: " + data.Address + ", symbol: " + data.Symbol + ", notional: " + notional + ", price: " + data.TokenPrice + ", amount: " + data.Amount)
newTokenKeys[chain + "-" + wormholeAddr + "-" + data.Symbol] = true;
newTokensCount += 1;
}
}
}
}

for (var token of existingTokenKeys) {
// A token has been removed from the token list
if (!newTokenKeys[token]) {
removedTokens.push(token);
}
}

// Sanity check to make sure the script is doing what we think it is
if (existingTokenKeys.length + addedTokens.length - removedTokens.length != newTokensCount) {
console.error("The new number of tokens doesn't make sense");
process.exit(1);
}

var changedContent = "Tokens before = " + existingTokenKeys.length;
changedContent += "\nTokens after = " + newTokensCount;
changedContent += "\n\nTokens added = " + addedTokens.length + ":\n\n";
changedContent += JSON.stringify(addedTokens, null, 1);
changedContent += "\n\nTokens removed = " + removedTokens.length + ":\n\n";
changedContent += JSON.stringify(removedTokens, null, 1);
changedContent += "\n\nTokens with significant price changes = " + significantPriceChanges.length + ":\n\n"
changedContent += JSON.stringify(significantPriceChanges, null, 1);

await fs.writeFileSync(
"./changes.txt",
changedContent,
{
flag: "w+",
}
);

content += "\t}\n";
content += "}\n";

Expand Down
7 changes: 4 additions & 3 deletions node/pkg/governor/mainnet_tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import (
func TestTokenListSize(t *testing.T) {
tokenConfigEntries := tokenList()

/* Assuming that governed tokens will need to be updated every time
we regenerate it */
assert.Equal(t, 1034, len(tokenConfigEntries))
// We should have a sensible number of tokens
// These numbers shouldn't have to change frequently
assert.Greater(t, len(tokenConfigEntries), 1000)
assert.Less(t, len(tokenConfigEntries), 2000)
}

func TestTokenListAddressSize(t *testing.T) {
Expand Down

0 comments on commit 94504dc

Please sign in to comment.