Skip to content

Commit

Permalink
Move listing date to State (#2076)
Browse files Browse the repository at this point in the history
* write the code to update the listing date

* Add initial state file

* Update script for including state data in the assetlist

* remove the temporary python script

* remove listing date from the zone_assets file

* add testnet state file
  • Loading branch information
JeremyParish69 authored Nov 21, 2024
1 parent e7253ea commit fb4d03c
Show file tree
Hide file tree
Showing 8 changed files with 1,335 additions and 197 deletions.
21 changes: 16 additions & 5 deletions .github/workflows/utility/generate_assetlist.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { getAllRelatedAssets } from "./getRelatedAssets.mjs";
import * as assetlist from "./generate_assetlist_functions.mjs";
import * as localization from "./localization.mjs";

import * as state from "./update_assetlist_state.mjs";


//-- Functions --

Expand Down Expand Up @@ -116,7 +118,7 @@ const generateAssets = async (
chain_reg_assets.push(asset_data.chain_reg);

//--Append to Frontend Assetlist--
assetlist.reformatFrontendAsset(asset_data);
assetlist.reformatFrontendAssetFromAssetData(asset_data);
frontend_assets.push(asset_data.frontend);

//--Append to Asset_Detail Assetlist--
Expand Down Expand Up @@ -173,15 +175,24 @@ async function generateAssetlist(chainName) {
asset_detail_assets
);

let frontend_assetlist = {
chainName: chainName,
assets: frontend_assets,
};

//state
state.updateState(chainName, frontend_assetlist);

//post processing for reordering properties
//frontend_assets.forEach(asset => console.log(asset.coinMinimalDenom));
frontend_assets.forEach(asset => assetlist.reformatFrontendAsset(asset));

//zone_config_assets = await getAllRelatedAssets(
// zone_config_assets,
// zoneConfig
//);

let frontend_assetlist = {
chainName: chainName,
assets: frontend_assets,
};

zone.writeToFile(
chainName,
zone.zoneConfigAssetlist,
Expand Down
44 changes: 43 additions & 1 deletion .github/workflows/utility/generate_assetlist_functions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1612,7 +1612,7 @@ export function setDescription(asset_data) {

}

export function reformatFrontendAsset(asset_data) {
export function reformatFrontendAssetFromAssetData(asset_data) {

//--Setup Frontend Asset--
let reformattedAsset = {
Expand Down Expand Up @@ -1651,6 +1651,48 @@ export function reformatFrontendAsset(asset_data) {

asset_data.frontend = reformattedAsset;
return;
}

export function reformatFrontendAsset(asset) {

//--Setup Frontend Asset--
let reformattedAsset = {
chainName: asset.chainName,
sourceDenom: asset.sourceDenom,
coinMinimalDenom: asset.coinMinimalDenom,
symbol: asset.symbol,
decimals: asset.decimals,
logoURIs: asset.logoURIs,
coingeckoId: asset.coingeckoId,
price: asset.price,
categories: asset.categories ?? [],
pegMechanism: asset.pegMechanism,
transferMethods: asset.transferMethods ?? [],
counterparty: asset.counterparty ?? [],
//identity: asset.identityGroupKey,
variantGroupKey: asset.originAsset,
name: asset.name,
//description: asset.description,
isAlloyed: asset.isAlloyed ?? false,
contract: asset.contract,
verified: asset.verified ?? false,
unstable: asset.unstable ?? false,
disabled: asset.disabled ?? false,
preview: asset.preview ?? false,
tooltipMessage: asset.tooltipMessage,
sortWith: asset.sortWith,
listingDate: asset.listingDate,
//relatedAssets: asset.relatedAssets,
};

// Check if listingDate exists and is a valid date
if (!asset.listingDate || isNaN(new Date(asset.listingDate).getTime())) {
// Remove listingDate from reformattedAsset if it's invalid or doesn't exist
delete reformattedAsset.listingDate;
}

asset = reformattedAsset;
return;

}

Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/utility/json_management.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function readFromFile(location) {
);
} catch (err) {
console.log(err);
return err;
}
}

Expand All @@ -52,5 +53,6 @@ export function writeToFile(location, value, indent = 2) {
);
} catch (err) {
console.log(err);
return err;
}
}
165 changes: 165 additions & 0 deletions .github/workflows/utility/update_assetlist_state.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@

/* Purpose:
* to manage the state for Osmosis Zone assets
* which includes data like: listingDate, ...
*
*
* State:
* "assets": []
* for each asset object{}:
* "base_denom": "uosmo",
* "listingDate": "{UTC date time format}",
* "legacyAsset": true,
* ...
*
*
* Plan:
* For each asset, see if it's verified, if so, we should have a listing date for it
* see if we have a listing date in the state
* if so, proceed to next
* if not, we should add a listing date for that asset to the state
* unless they are a legacy asset, which wew can determine from the state
*
* Note about Legacy Assets:
* however, many verified assets don't have a listing date, in which case, we should identify them as a legacy asset
* the state will keep a property 'legacyAsset: true' where the asset is verified but doesn't have a listing date
* legacyAssets will have to be populated manually, but they are easier to identify in an initial setup
* becase they are just all the verified assets without any listingDate
*
*
* */


//-- Imports --

import * as zone from "./assetlist_functions.mjs";
import * as chain_reg from "../../../chain-registry/.github/workflows/utility/chain_registry.mjs";
chain_reg.setup();
import * as path from 'path';

const stateFileName = "state.json";
const stateDirName = "state";
const stateDir = path.join(zone.generatedDirectoryName, stateDirName);

const stateLocations = [
"assets"
];
const stateAssetProperties = [
"listingDate",
"legacyAsset"
];

const currentDateUTC = new Date().toISOString();


// Problem: the listing date is stored both in the generate assetlist and in the state file. which do we generate first?

function setAssetListingDate(stateAsset, assetlistAsset) {

//legacy assets don't need a listing date
if (stateAsset.legacyAsset) { return; }

//use the recorded listing date
if (!stateAsset.listingDate) {
//or else it's current datetime
stateAsset.listingDate = currentDateUTC;
}

//save to assetlist
assetlistAsset.listingDate = stateAsset.listingDate;

}

//get the state asset
function getStateAsset(base_denom, state) {
let stateAsset = state.assets?.find(stateAsset => stateAsset.base_denom === base_denom);
if (!stateAsset) {
stateAsset = {
base_denom: base_denom
};
state.assets.push(stateAsset);
}
return stateAsset;
}

// Main function to convert one JSON file to another
const generateState = (chainName, assetlist) => {

// Read the existing State file
let state = {};
try {
state = zone.readFromFile(chainName, stateDir, stateFileName);
} catch (error) {
if (error.code === 'ENOENT') {
console.warn(`File not found: ${error.message}`);
state = {}; // Assign empty object if file doesn't exist
} else {
throw error; // Re-throw for unexpected errors
}
}


// Iterate each asset
if (!state.assets) { state.assets = [] }
for (let assetlistAsset of assetlist?.assets) {

let stateAsset;

//see if it's verified, and skip if not
if (assetlistAsset.verified) {

//get the state asset
stateAsset = getStateAsset(assetlistAsset.coinMinimalDenom, state);

// Property 1: Get the listing Date
setAssetListingDate(stateAsset, assetlistAsset);


// Property 2: anything else...


}

}


// Write the state to the state file
zone.writeToFile(chainName, stateDir, stateFileName, state);
console.log(`Update state completed. Data saved.`);

};


function getAssetlistFromFile(chainNAme) {

// Read the generate assetlist
const assetlist = zone.readFromFile(chainName, zone.zoneConfigAssetlist, zone.assetlistFileName);
//console.log(`Generated Assetlist is: ${assetlist}`);

}

function saveAssetlistToFile(chainName, assetlist) {

// Write the state to the state file
zone.writeToFile(chainName, zone.zoneConfigAssetlist, zone.assetlistFileName, assetlist);
console.log(`Update assetlist completed. Data saved.`);

}


export function updateState(chainName, assetlist) {
let assetlistFromFile = false;
if (!assetlist) {
assetlist = getAssetlistFromFile(chainName);
assetlistFromFile = true;
}
generateState(chainName, assetlist);
if (assetlistFromFile) {
saveAssetlistToFile(chainName, assetlist);
}
}


function main() {
zone.chainNames.forEach(chainName => generateState(chainName));
}
Loading

0 comments on commit fb4d03c

Please sign in to comment.