Skip to content

Commit

Permalink
Merge pull request #504 from EdgeApp/william/no-meta-tokens
Browse files Browse the repository at this point in the history
No meta tokens
  • Loading branch information
swansontec authored Feb 1, 2023
2 parents a25b651 + 1ec71c8 commit 5121e7e
Show file tree
Hide file tree
Showing 42 changed files with 1,973 additions and 1,740 deletions.
3 changes: 0 additions & 3 deletions index.js

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"/android/src/",
"/CHANGELOG.md",
"/edge-currency-accountbased.podspec",
"/index.js",
"/ios/",
"/lib/",
"/LICENSE",
Expand All @@ -40,7 +39,7 @@
"make-checkpoints": "node -r sucrase/register scripts/makeCheckpoints.ts",
"node": "sucrase -d ./lib -q -t imports,typescript ./src",
"precommit": "lint-staged && npm-run-all types test",
"prepare": "husky install && npm-run-all clean node types webpack",
"prepare": "husky install && patch-package && npm-run-all clean node types webpack",
"start": "webpack serve",
"test": "nyc mocha 'test/**/*.test.ts'",
"types": "tsc",
Expand Down Expand Up @@ -135,6 +134,7 @@
"npm-run-all": "^4.1.5",
"nyc": "^15.0.1",
"os-browserify": "^0.3.0",
"patch-package": "^6.5.1",
"path-browserify": "^1.0.1",
"prettier": "^2.2.0",
"process": "^0.11.10",
Expand Down
31 changes: 31 additions & 0 deletions patches/@tronscan+client+0.2.81.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
diff --git a/node_modules/@tronscan/client/src/utils/tronWeb.js b/node_modules/@tronscan/client/src/utils/tronWeb.js
index 6b1bc38..3f48a11 100644
--- a/node_modules/@tronscan/client/src/utils/tronWeb.js
+++ b/node_modules/@tronscan/client/src/utils/tronWeb.js
@@ -25,7 +25,7 @@ const {
const fromHexString = hexString =>
new Uint8Array(hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16)));

-export function transactionJsonToProtoBuf(transaction) {
+function transactionJsonToProtoBuf(transaction) {
const rawData = transaction["raw_data"];
const contractJson = rawData.contract[0];
const transactionObj = contractJsonToProtobuf(contractJson);
@@ -52,7 +52,7 @@ export function transactionJsonToProtoBuf(transaction) {
return transactionObj;
}

-export function contractJsonToProtobuf(contract) {
+function contractJsonToProtobuf(contract) {
const value = contract.parameter.value;
switch (contract.type) {

@@ -365,3 +365,8 @@ export function contractJsonToProtobuf(contract) {

}
}
+
+module.exports = {
+ transactionJsonToProtoBuf,
+ contractJsonToProtobuf
+};
2 changes: 1 addition & 1 deletion src/binance/bnbInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const currencyInfo: EdgeCurrencyInfo = {
symbol: 'B'
}
],
metaTokens: []
metaTokens: [] // Deprecated
}

export const binance = makeOuterPlugin<{}, BinanceTools>({
Expand Down
33 changes: 9 additions & 24 deletions src/common/innerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
EdgeCurrencyInfo,
EdgeCurrencyPlugin,
EdgeCurrencyTools,
EdgeMetaToken,
EdgeOtherMethods,
EdgeTokenMap,
EdgeWalletInfo
Expand All @@ -16,6 +15,7 @@ import {
* so we can share the same instance between sibling networks.
*/
export interface PluginEnvironment<NetworkInfo> extends EdgeCorePluginOptions {
builtinTokens: EdgeTokenMap
currencyInfo: EdgeCurrencyInfo
networkInfo: NetworkInfo
}
Expand All @@ -40,6 +40,7 @@ export interface InnerPlugin<NetworkInfo, Tools extends EdgeCurrencyTools> {
* so we don't have to load any crypto libraries.
*/
export interface OuterPlugin<NetworkInfo, Tools extends EdgeCurrencyTools> {
builtinTokens?: EdgeTokenMap
currencyInfo: EdgeCurrencyInfo
networkInfo: NetworkInfo

Expand All @@ -54,8 +55,13 @@ export function makeOuterPlugin<NetworkInfo, Tools extends EdgeCurrencyTools>(
template: OuterPlugin<NetworkInfo, Tools>
): EdgeCorePluginFactory {
return (env: EdgeCorePluginOptions): EdgeCurrencyPlugin => {
const { currencyInfo, networkInfo, otherMethodNames = [] } = template
const innerEnv = { ...env, currencyInfo, networkInfo }
const {
builtinTokens = {},
currencyInfo,
networkInfo,
otherMethodNames = []
} = template
const innerEnv = { ...env, builtinTokens, currencyInfo, networkInfo }

// Logic to load the inner plugin:
let pluginPromise: Promise<InnerPlugin<NetworkInfo, Tools>> | undefined
Expand All @@ -75,8 +81,6 @@ export function makeOuterPlugin<NetworkInfo, Tools extends EdgeCurrencyTools>(
return { plugin, tools }
}

const builtinTokens = upgradeMetaTokens(currencyInfo.metaTokens)

async function getBuiltinTokens(): Promise<EdgeTokenMap> {
return builtinTokens
}
Expand Down Expand Up @@ -131,22 +135,3 @@ export function makeOtherMethods<T>(

return out
}

function upgradeMetaTokens(metaTokens: EdgeMetaToken[]): EdgeTokenMap {
const out: EdgeTokenMap = {}
for (const metaToken of metaTokens) {
const { contractAddress } = metaToken
if (contractAddress == null) continue
out[contractToTokenId(contractAddress)] = {
currencyCode: metaToken.currencyCode,
denominations: metaToken.denominations,
displayName: metaToken.currencyName,
networkLocation: { contractAddress: metaToken.contractAddress }
}
}
return out
}

export function contractToTokenId(contractAddress: string): string {
return contractAddress.toLowerCase().replace(/^0x/, '')
}
33 changes: 33 additions & 0 deletions src/common/tokenHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { asMaybe, asObject, asString } from 'cleaners'
import { EdgeMetaToken, EdgeTokenMap } from 'edge-core-js'

/**
* The `networkLocation` field is untyped,
* but many currency plugins will put a contract address in there.
*/
export const asMaybeContractLocation = asMaybe(
asObject({
contractAddress: asString
})
)

/**
* Downgrades EdgeToken objects to the legacy EdgeMetaToken format.
*/
export function makeMetaTokens(tokens: EdgeTokenMap): EdgeMetaToken[] {
const out: EdgeMetaToken[] = []
for (const tokenId of Object.keys(tokens)) {
const { currencyCode, displayName, denominations, networkLocation } =
tokens[tokenId]

const cleanLocation = asMaybeContractLocation(networkLocation)
if (cleanLocation == null) continue
out.push({
currencyCode,
currencyName: displayName,
denominations,
contractAddress: cleanLocation.contractAddress
})
}
return out
}
9 changes: 9 additions & 0 deletions src/eos/eosInfos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { eos } from './info/eosInfo'
import { telos } from './info/telosInfo'
import { wax } from './info/waxInfo'

export const eosPlugins = {
eos,
telos,
wax
}
7 changes: 4 additions & 3 deletions src/eos/eosPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import EosApi from 'eosjs-api'
import ecc from 'eosjs-ecc'

import { PluginEnvironment } from '../common/innerPlugin'
import { asMaybeContractLocation } from '../common/tokenHelpers'
import { encodeUriCommon, parseUriCommon } from '../common/uriHelpers'
import { asyncWaterfall, getDenomInfo, getFetchCors } from '../common/utils'
import {
Expand Down Expand Up @@ -143,11 +144,11 @@ export class EosTools implements EdgeCurrencyTools {
}

async getTokenId(token: EdgeToken): Promise<string> {
const contractAddress = token?.networkLocation?.contractAddress
if (contractAddress == null || !checkAddress(contractAddress)) {
const cleanLocation = asMaybeContractLocation(token.networkLocation)
if (cleanLocation == null || !checkAddress(cleanLocation.contractAddress)) {
throw new Error('ErrorInvalidContractAddress')
}
return contractAddress.toLowerCase()
return cleanLocation.contractAddress.toLowerCase()
}

// change to fetch call in the future
Expand Down
9 changes: 0 additions & 9 deletions src/eos/index.ts

This file was deleted.

10 changes: 5 additions & 5 deletions src/eos/eosInfo.ts → src/eos/info/eosInfo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EdgeCurrencyInfo } from 'edge-core-js/types'

import { makeOuterPlugin } from '../common/innerPlugin'
import type { EosTools } from './eosPlugin'
import { EosNetworkInfo, eosOtherMethodNames } from './eosTypes'
import { makeOuterPlugin } from '../../common/innerPlugin'
import type { EosTools } from '../eosPlugin'
import { EosNetworkInfo, eosOtherMethodNames } from '../eosTypes'

// ----EOSIO MAIN NET----
export const eosNetworkInfo: EosNetworkInfo = {
Expand Down Expand Up @@ -50,7 +50,7 @@ export const eosCurrencyInfo: EdgeCurrencyInfo = {
transactionExplorer: 'https://bloks.io/transaction/%s',

denominations,
metaTokens: []
metaTokens: [] // Deprecated
}

export const eos = makeOuterPlugin<EosNetworkInfo, EosTools>({
Expand All @@ -61,7 +61,7 @@ export const eos = makeOuterPlugin<EosNetworkInfo, EosTools>({
async getInnerPlugin() {
return await import(
/* webpackChunkName: "eos" */
'./eosPlugin'
'../eosPlugin'
)
}
})
10 changes: 5 additions & 5 deletions src/eos/telosInfo.ts → src/eos/info/telosInfo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EdgeCurrencyInfo } from 'edge-core-js/types'

import { makeOuterPlugin } from '../common/innerPlugin'
import type { EosTools } from './eosPlugin'
import { EosNetworkInfo, eosOtherMethodNames } from './eosTypes'
import { makeOuterPlugin } from '../../common/innerPlugin'
import type { EosTools } from '../eosPlugin'
import { EosNetworkInfo, eosOtherMethodNames } from '../eosTypes'

// ----TELOS MAIN NET----
export const telosNetworkInfo: EosNetworkInfo = {
Expand Down Expand Up @@ -42,7 +42,7 @@ export const telosCurrencyInfo: EdgeCurrencyInfo = {
transactionExplorer: 'https://telos.bloks.io/transaction/%s',

denominations,
metaTokens: []
metaTokens: [] // Deprecated
}

export const telos = makeOuterPlugin<EosNetworkInfo, EosTools>({
Expand All @@ -51,6 +51,6 @@ export const telos = makeOuterPlugin<EosNetworkInfo, EosTools>({
otherMethodNames: eosOtherMethodNames,

async getInnerPlugin() {
return await import('./eosPlugin')
return await import('../eosPlugin')
}
})
10 changes: 5 additions & 5 deletions src/eos/waxInfo.ts → src/eos/info/waxInfo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EdgeCurrencyInfo } from 'edge-core-js/types'

import { makeOuterPlugin } from '../common/innerPlugin'
import type { EosTools } from './eosPlugin'
import { EosNetworkInfo, eosOtherMethodNames } from './eosTypes'
import { makeOuterPlugin } from '../../common/innerPlugin'
import type { EosTools } from '../eosPlugin'
import { EosNetworkInfo, eosOtherMethodNames } from '../eosTypes'

// ----WAX MAIN NET----
export const waxNetworkInfo: EosNetworkInfo = {
Expand Down Expand Up @@ -43,7 +43,7 @@ export const waxCurrencyInfo: EdgeCurrencyInfo = {
transactionExplorer: 'https://wax.bloks.io/transaction/%s',

denominations,
metaTokens: []
metaTokens: [] // Deprecated
}

export const wax = makeOuterPlugin<EosNetworkInfo, EosTools>({
Expand All @@ -52,6 +52,6 @@ export const wax = makeOuterPlugin<EosNetworkInfo, EosTools>({
otherMethodNames: eosOtherMethodNames,

async getInnerPlugin() {
return await import('./eosPlugin')
return await import('../eosPlugin')
}
})
11 changes: 6 additions & 5 deletions src/ethereum/ethPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
import EthereumUtil from 'ethereumjs-util'
import hdKey from 'ethereumjs-wallet/hdkey'

import { contractToTokenId, PluginEnvironment } from '../common/innerPlugin'
import { PluginEnvironment } from '../common/innerPlugin'
import { asMaybeContractLocation } from '../common/tokenHelpers'
import { encodeUriCommon, parseUriCommon } from '../common/uriHelpers'
import { biggyScience, getDenomInfo } from '../common/utils'
import { ethPlugins } from './ethInfos'
Expand Down Expand Up @@ -351,15 +352,15 @@ export class EthereumTools implements EdgeCurrencyTools {
}

async getTokenId(token: EdgeToken): Promise<string> {
const contractAddress = token?.networkLocation?.contractAddress
const cleanLocation = asMaybeContractLocation(token.networkLocation)
if (
contractAddress == null ||
cleanLocation == null ||
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
!EthereumUtil.isValidAddress(contractAddress)
!EthereumUtil.isValidAddress(cleanLocation.contractAddress)
) {
throw new Error('ErrorInvalidContractAddress')
}
return contractToTokenId(contractAddress)
return cleanLocation.contractAddress.toLowerCase().replace(/^0x/, '')
}
}

Expand Down
Loading

0 comments on commit 5121e7e

Please sign in to comment.