Skip to content
This repository has been archived by the owner on May 24, 2022. It is now read-only.

Use new token contract's source #477

Merged
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@
"preelectron": "yarn build",
"electron": "cd packages/fether-electron && yarn electron",
"lint-files": "./scripts/lint-files.sh",
"lint": "yarn lint-files '**/*.{js,ts}'",
"lint": "yarn lint-files '**/*.js'",
"prepackage": "yarn build",
"package": "cd packages/fether-electron && yarn package",
"release": "cd packages/fether-electron && yarn release",
"start": "npm-run-all -l -p start-*",
"start-electron": "cd packages/fether-electron && yarn start",
"start-react": "cd packages/fether-react && yarn start",
"start-ui": "cd packages/fether-ui && yarn start",
"test": "semistandard '**/*.{js,ts}' --parser babel-eslint && CI=true lerna run test --parallel"
"test": "semistandard '**/*.{js,ts}' --parser babel-eslint && CI=true lerna run test --parallel",
"update-tokens": "yarn run ts-node --project scripts/updateTokens/tsconfig.json scripts/updateTokens"
},
"husky": {
"hooks": {
Expand All @@ -71,6 +72,8 @@
"dependencies": {
"download": "^7.1.0",
"node-fetch": "^2.3.0",
"semver": "^5.6.0"
"semver": "^5.6.0",
"ts-node": "^8.0.3",
"typescript": "^3.3.4000"
}
}
}
920 changes: 1 addition & 919 deletions packages/fether-react/src/assets/tokens/foundation.json

Large diffs are not rendered by default.

334 changes: 1 addition & 333 deletions packages/fether-react/src/assets/tokens/kovan.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/fether-react/src/assets/tokens/ropsten.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"name":"iExec RLC","symbol":"RLC","address":"0x7314Dc4d7794b5E7894212CA1556ae8e3De58621","decimals":9},{"name":"IsraCoin","symbol":"ILSC","address":"0x6f95a3B682F8e9aacC86D057A6DF88A0E68145A8","decimals":2},{"name":"DQR","symbol":"dqr30","address":"0xa1bAccA0e12D4091Ec1f92e7CaE3394CC9854D3D","decimals":18},{"name":"MEW V5 Test Token","symbol":"MEWV5","address":"0x4C572Fbc03D4A2B683cF4f10ffdcaFD00885E108","decimals":9},{"name":"*PLASMA","symbol":"*PLASMA","address":"0x95D7321EdCe519419ba1DbC60A89bAfbF55EAC0D","decimals":6},{"name":"None","symbol":"NONE","address":"0xFD5a69A1309595FF5121553F52C8A5B2B1B31031","decimals":0}]
7 changes: 7 additions & 0 deletions scripts/updateTokens/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Update Tokens Script

This folder contains a script which will fetch all the tokens from https://github.com/ethereum-lists/tokens, and output all the tokens in a nice JSON file.

This entire folder has been taken from https://github.com/MyCryptoHQ/MyCrypto/tree/574c628e6189de5b7848c4c10258bc22f42b337c/scripts. Thank you MyCrypto for this.

Please also see [this PR](https://github.com/MyCryptoHQ/MyCrypto/pull/1247) for a description of this script.
1 change: 1 addition & 0 deletions scripts/updateTokens/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./update-tokens');
Tbaut marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions scripts/updateTokens/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"lib": [
"es2015"
]
}
}
31 changes: 31 additions & 0 deletions scripts/updateTokens/types/CommitStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export interface Creator {
login: string;
id: number;
avatar_url: string;
gravatar_id: string;
url: string;
html_url: string;
followers_url: string;
following_url: string;
gists_url: string;
starred_url: string;
subscriptions_url: string;
organizations_url: string;
repos_url: string;
events_url: string;
received_events_url: string;
type: string;
site_admin: boolean;
}

export interface CommitStatus {
url: string;
id: number;
state: string;
description: string;
target_url: string;
context: string;
created_at: Date;
updated_at: Date;
creator: Creator;
}
90 changes: 90 additions & 0 deletions scripts/updateTokens/types/GitCommit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
export interface Author {
name: string;
email: string;
date: Date;
}

export interface Committer {
name: string;
email: string;
date: Date;
}

export interface Tree {
sha: string;
url: string;
}

export interface Verification {
verified: boolean;
reason: string;
signature?: any;
payload?: any;
}

export interface Commit {
author: Author;
committer: Committer;
message: string;
tree: Tree;
url: string;
comment_count: number;
verification: Verification;
}

export interface Author2 {
login: string;
id: number;
avatar_url: string;
gravatar_id: string;
url: string;
html_url: string;
followers_url: string;
following_url: string;
gists_url: string;
starred_url: string;
subscriptions_url: string;
organizations_url: string;
repos_url: string;
events_url: string;
received_events_url: string;
type: string;
site_admin: boolean;
}

export interface Committer2 {
login: string;
id: number;
avatar_url: string;
gravatar_id: string;
url: string;
html_url: string;
followers_url: string;
following_url: string;
gists_url: string;
starred_url: string;
subscriptions_url: string;
organizations_url: string;
repos_url: string;
events_url: string;
received_events_url: string;
type: string;
site_admin: boolean;
}

export interface Parent {
sha: string;
url: string;
html_url: string;
}

export interface GitCommit {
sha: string;
commit: Commit;
url: string;
html_url: string;
comments_url: string;
author: Author2;
committer: Committer2;
parents: Parent[];
}
6 changes: 6 additions & 0 deletions scripts/updateTokens/types/Token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Token {
address: string;
symbol: string;
decimal: number;
error?: string | null;
}
20 changes: 20 additions & 0 deletions scripts/updateTokens/types/TokensJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export interface RawTokenJSON {
name?: string;
symbol?: string;
address?: string;
decimals?: number | string;
}

export interface ValidatedTokenJSON {
name: string;
symbol: string;
address: string;
decimals: number | string;
}

export interface NormalizedTokenJSON {
name: string;
symbol: string;
address: string;
decimal: number;
}
160 changes: 160 additions & 0 deletions scripts/updateTokens/update-tokens-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import {
RawTokenJSON,
ValidatedTokenJSON,
NormalizedTokenJSON
} from "./types/TokensJson";
import { Token } from "./types/Token";
interface StrIdx<T> {
[key: string]: T;
}

const networks = [
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added this

{
networkName: "foundation",
networkNameIPFS: "eth"
},
{
networkName: "kovan",
networkNameIPFS: "kov"
},
{
networkName: "ropsten",
networkNameIPFS: "rop"
}
];

function processTokenJson(tokensJson: RawTokenJSON[]): Token[] {
const normalizedTokens = tokensJson
.map(validateTokenJSON)
.map(normalizeTokenJSON);
checkForDuplicateAddresses(normalizedTokens);
return handleDuplicateSymbols(normalizedTokens).map(
({ name: _, ...rest }) => rest
);
}

function validateTokenJSON(token: RawTokenJSON): ValidatedTokenJSON {
const isValid = (t: RawTokenJSON): t is ValidatedTokenJSON =>
!!(t.address && (t.decimals || t.decimals === 0) && t.name && t.symbol);

if (isValid(token)) {
return token;
}
throw Error(`Token failed validation, missing part of schema
Symbol: ${token.symbol}
Name: ${token.name}
Address: ${token.address}
Decimals: ${token.decimals}`);
}

function normalizeTokenJSON(token: ValidatedTokenJSON): NormalizedTokenJSON {
const { address, decimals, symbol, name } = token;
const t: NormalizedTokenJSON = { address, symbol, decimal: +decimals, name };
return t;
}

/**
*
* @description Checks for any duplicated addresses and halts the program if so
* @param {NormalizedTokenJSON[]} tokens
*/
function checkForDuplicateAddresses(tokens: NormalizedTokenJSON[]) {
const map: StrIdx<boolean> = {};
const errors: string[] = [];
for (const token of tokens) {
const { address } = token;
// We might want to strip hex prefixes here, and make all characters lowercase
if (map[address]) {
errors.push(
`Token ${token.symbol} has a duplicate address of ${token.address}`
);
}
map[address] = true;
}

if (errors.length) {
const err = errors.join("\n");
throw Error(err);
}
}

/**
*
* @description Finds any duplicated names in the fetched token json
* @param {NormalizedTokenJSON[]} tokens
* @returns
*/
function getDuplicatedNames(tokens: NormalizedTokenJSON[]) {
const checkedNames: StrIdx<boolean> = {};
const duplicatedNames: StrIdx<boolean> = {};
for (const token of tokens) {
const { name } = token;
if (checkedNames[name]) {
duplicatedNames[name] = true;
}
checkedNames[name] = true;
}
return duplicatedNames;
}

/**
*
* @description Handles any tokens with duplicated symbols by placing them in a map with each value being a bucket
* of other tokens with the same symbol, then renaming them appropriately so they do not conflict anymore
* @param {NormalizedTokenJSON[]} tokens
* @returns
*/
function handleDuplicateSymbols(tokens: NormalizedTokenJSON[]) {
// start by building a map of symbols => tokens
const map = new Map<string, NormalizedTokenJSON[]>();
for (const token of tokens) {
const { symbol } = token;
const v = map.get(symbol);
if (v) {
map.set(symbol, [...v, token]);
} else {
map.set(symbol, [token]);
}
}
const duplicatedNames = getDuplicatedNames(tokens);
const dedupedTokens: NormalizedTokenJSON[] = [];
map.forEach(tokenBucket =>
dedupedTokens.push(...renameSymbolCollisions(tokenBucket, duplicatedNames))
);
return dedupedTokens;
}

/**
*
* @description Any token collisions are handled in this manner:
* 1) If the name isnt a duplicate, the token symbol is prefixed with the token name
* 2) if it is a duplicate, then we simply use the token index + 1 (so we dont start at 0)
* @param {NormalizedTokenJSON[]} tokens
* @param {StrIdx<boolean>} duplicatedNames
* @returns
*/
function renameSymbolCollisions(
tokens: NormalizedTokenJSON[],
duplicatedNames: StrIdx<boolean>
) {
const renamedTokens: NormalizedTokenJSON[] = [];
if (tokens.length === 1) {
return tokens;
}

return tokens.reduce((prev, curr, idx) => {
const newName = `${curr.symbol} (${
duplicatedNames[curr.name] ? idx + 1 : curr.name
})`;
const tokenToInsert: NormalizedTokenJSON = {
...curr,
symbol: newName
};
console.warn(
`WARN: "${curr.symbol}" has a duplicate symbol, renaming to "${newName}"`
);
return [...prev, tokenToInsert];
}, renamedTokens);
}

module.exports = { networks, processTokenJson };
Loading