Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No meta tokens #504

Merged
merged 4 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
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
2 changes: 1 addition & 1 deletion src/eos/info/eosInfo.ts
Original file line number Diff line number Diff line change
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 Down
2 changes: 1 addition & 1 deletion src/eos/info/telosInfo.ts
Original file line number Diff line number Diff line change
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 Down
2 changes: 1 addition & 1 deletion src/eos/info/waxInfo.ts
Original file line number Diff line number Diff line change
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 Down
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