2021-01-25 14:23:03 +00:00
|
|
|
import { readJsonFile, writeJsonFile } from "../generic/json";
|
|
|
|
import { diff } from "jsondiffpatch";
|
2021-01-23 00:31:03 +00:00
|
|
|
|
2020-10-14 01:20:42 +00:00
|
|
|
class Version {
|
|
|
|
major: number
|
|
|
|
minor: number
|
|
|
|
patch: number
|
|
|
|
|
|
|
|
constructor(major: number, minor: number, patch: number) {
|
|
|
|
this.major = major
|
|
|
|
this.minor = minor
|
|
|
|
this.patch = patch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 14:23:03 +00:00
|
|
|
export class List {
|
2020-10-14 01:20:42 +00:00
|
|
|
name: string
|
|
|
|
logoURI: string
|
|
|
|
timestamp: string
|
2021-01-23 00:04:56 +00:00
|
|
|
tokens: TokenItem[]
|
|
|
|
pairs: Pair[]
|
2020-10-14 01:20:42 +00:00
|
|
|
version: Version
|
|
|
|
|
2021-01-23 00:04:56 +00:00
|
|
|
constructor(name: string, logoURI: string, timestamp: string, tokens: TokenItem[], version: Version) {
|
2020-10-14 01:20:42 +00:00
|
|
|
this.name = name
|
|
|
|
this.logoURI = logoURI
|
|
|
|
this.timestamp = timestamp;
|
|
|
|
this.tokens = tokens
|
|
|
|
this.version = version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-23 00:04:56 +00:00
|
|
|
export class TokenItem {
|
2020-10-14 01:20:42 +00:00
|
|
|
asset: string;
|
2020-10-23 04:10:51 +00:00
|
|
|
type: string;
|
2020-10-14 01:20:42 +00:00
|
|
|
address: string;
|
|
|
|
name: string;
|
|
|
|
symbol: string;
|
|
|
|
decimals: number;
|
|
|
|
logoURI: string;
|
2021-01-23 00:04:56 +00:00
|
|
|
pairs: Pair[];
|
2020-10-14 01:20:42 +00:00
|
|
|
|
2021-01-23 00:04:56 +00:00
|
|
|
constructor(asset: string, type: string, address: string, name: string, symbol: string, decimals: number, logoURI: string, pairs: Pair[]) {
|
2020-10-14 01:20:42 +00:00
|
|
|
this.asset = asset
|
2020-10-23 04:10:51 +00:00
|
|
|
this.type = type
|
2020-10-14 01:20:42 +00:00
|
|
|
this.address = address
|
|
|
|
this.name = name;
|
|
|
|
this.symbol = symbol
|
|
|
|
this.decimals = decimals
|
|
|
|
this.logoURI = logoURI
|
|
|
|
this.pairs = pairs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-23 00:04:56 +00:00
|
|
|
export class Pair {
|
2020-10-14 01:20:42 +00:00
|
|
|
base: string;
|
2021-01-25 14:23:03 +00:00
|
|
|
lotSize?: string;
|
|
|
|
tickSize?: string;
|
2020-10-14 01:20:42 +00:00
|
|
|
|
2021-01-25 14:23:03 +00:00
|
|
|
constructor(base: string, lotSize?: string, tickSize?: string) {
|
2020-10-14 01:20:42 +00:00
|
|
|
this.base = base
|
|
|
|
this.lotSize = lotSize
|
|
|
|
this.tickSize = tickSize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 14:23:03 +00:00
|
|
|
export function generateTokensList(titleCoin: string, tokens: TokenItem[], time: string, versionMajor: number, versionMinor = 1, versionPatch = 0): List {
|
|
|
|
if (!time) {
|
|
|
|
time = (new Date()).toISOString();
|
|
|
|
}
|
|
|
|
const list = new List(
|
2021-01-23 00:04:56 +00:00
|
|
|
`Trust Wallet: ${titleCoin}`,
|
2020-10-14 01:20:42 +00:00
|
|
|
"https://trustwallet.com/assets/images/favicon.png",
|
2021-01-25 14:23:03 +00:00
|
|
|
time,
|
|
|
|
tokens,
|
|
|
|
new Version(versionMajor, versionMinor, versionPatch)
|
|
|
|
);
|
|
|
|
sort(list);
|
|
|
|
return list;
|
2020-10-14 01:20:42 +00:00
|
|
|
}
|
2021-01-23 00:31:03 +00:00
|
|
|
|
|
|
|
function totalPairs(list: List): number {
|
|
|
|
let c = 0;
|
|
|
|
list.tokens.forEach(t => c += (t.pairs || []).length);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function writeToFile(filename: string, list: List): void {
|
|
|
|
writeJsonFile(filename, list);
|
|
|
|
console.log(`Tokenlist: list with ${list.tokens.length} tokens and ${totalPairs(list)} pairs written to ${filename}.`);
|
|
|
|
}
|
2021-01-25 14:23:03 +00:00
|
|
|
|
|
|
|
// Write out to file, updating version+timestamp if there was change
|
|
|
|
export function writeToFileWithUpdate(filename: string, list: List): void {
|
|
|
|
let listOld: List = undefined;
|
|
|
|
try {
|
|
|
|
listOld = readJsonFile(filename) as List;
|
|
|
|
} catch (err) {
|
|
|
|
listOld = undefined;
|
|
|
|
}
|
|
|
|
let changed = false;
|
|
|
|
if (listOld === undefined) {
|
|
|
|
changed = true;
|
|
|
|
} else {
|
2021-01-25 14:35:52 +00:00
|
|
|
list.version = listOld.version; // take over
|
2021-01-25 14:23:03 +00:00
|
|
|
const diffs = diffTokenlist(list, listOld);
|
|
|
|
if (diffs != undefined) {
|
|
|
|
//console.log("List has Changed", JSON.stringify(diffs));
|
|
|
|
changed = true;
|
2021-01-25 14:35:52 +00:00
|
|
|
list.version = new Version(list.version.major + 1, 0, 0);
|
2021-01-25 14:23:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changed) {
|
2021-01-25 14:35:52 +00:00
|
|
|
// update timestqamp
|
2021-01-25 14:23:03 +00:00
|
|
|
list.timestamp = (new Date()).toISOString();
|
|
|
|
console.log(`Version and timestamp updated, ${list.version.major}.${list.version.minor}.${list.version.patch} timestamp ${list.timestamp}`);
|
|
|
|
}
|
|
|
|
writeToFile(filename, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
function sort(list: List) {
|
|
|
|
list.tokens.sort((t1, t2) => {
|
|
|
|
const t1pairs = (t1.pairs || []).length;
|
|
|
|
const t2pairs = (t2.pairs || []).length;
|
|
|
|
if (t1pairs != t2pairs) { return t2pairs - t1pairs; }
|
|
|
|
return t1.address.localeCompare(t2.address);
|
|
|
|
});
|
|
|
|
list.tokens.forEach(t => {
|
|
|
|
t.pairs.sort((p1, p2) => p1.base.localeCompare(p2.base));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearUnimportantFields(list: List) {
|
|
|
|
list.timestamp = "";
|
|
|
|
list.version = new Version(0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function diffTokenlist(listOrig1: List, listOrig2: List): unknown {
|
|
|
|
// deep copy, to avoid changes
|
|
|
|
const list1 = JSON.parse(JSON.stringify(listOrig1));
|
|
|
|
const list2 = JSON.parse(JSON.stringify(listOrig2));
|
|
|
|
clearUnimportantFields(list1);
|
|
|
|
clearUnimportantFields(list2);
|
|
|
|
sort(list1);
|
|
|
|
sort(list2);
|
|
|
|
// compare
|
|
|
|
const diffs = diff(list1, list2);
|
|
|
|
return diffs;
|
|
|
|
}
|