From c61aab06706d17aee67b24967b62ea9e3e4023f6 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Fri, 30 Jul 2021 14:29:12 +0200 Subject: [PATCH] move datatoken name generation out of class (#943) --- src/datatokens/Datatokens.ts | 18 ++---------------- src/utils/Datatokens.ts | 30 ++++++++++++++++++++++++++++++ src/utils/index.ts | 1 + 3 files changed, 33 insertions(+), 16 deletions(-) create mode 100644 src/utils/Datatokens.ts diff --git a/src/datatokens/Datatokens.ts b/src/datatokens/Datatokens.ts index 15f67745..8b4bbb53 100644 --- a/src/datatokens/Datatokens.ts +++ b/src/datatokens/Datatokens.ts @@ -4,10 +4,10 @@ import { AbiItem } from 'web3-utils/types' import defaultFactoryABI from '@oceanprotocol/contracts/artifacts/DTFactory.json' import defaultDatatokensABI from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json' import { Logger, getFairGasPrice } from '../utils' -import wordListDefault from '../data/words.json' import { TransactionReceipt } from 'web3-core' import BigNumber from 'bignumber.js' import Decimal from 'decimal.js' +import { generateDatatokenName } from '../utils/Datatokens' /** * Provides an interface to DataTokens @@ -51,21 +51,7 @@ export class DataTokens { name: string symbol: string } { - const list = wordList || wordListDefault - const random1 = Math.floor(Math.random() * list.adjectives.length) - const random2 = Math.floor(Math.random() * list.nouns.length) - const indexNumber = Math.floor(Math.random() * 100) - - // Capitalized adjective & noun - const adjective = list.adjectives[random1].replace(/^\w/, (c) => c.toUpperCase()) - const noun = list.nouns[random2].replace(/^\w/, (c) => c.toUpperCase()) - - const name = `${adjective} ${noun} Token` - // use first 3 letters of name, uppercase it, and add random number - const symbol = `${( - adjective.substring(0, 3) + noun.substring(0, 3) - ).toUpperCase()}-${indexNumber}` - + const { name, symbol } = generateDatatokenName(wordList) return { name, symbol } } diff --git a/src/utils/Datatokens.ts b/src/utils/Datatokens.ts new file mode 100644 index 00000000..b9b73c4a --- /dev/null +++ b/src/utils/Datatokens.ts @@ -0,0 +1,30 @@ +import wordListDefault from '../data/words.json' + +/** + * Generate new datatoken name & symbol from a word list + * @return {<{ name: String; symbol: String }>} datatoken name & symbol. Produces e.g. "Endemic Jellyfish Token" & "ENDJEL-45" + */ +export function generateDatatokenName(wordList?: { + nouns: string[] + adjectives: string[] +}): { + name: string + symbol: string +} { + const list = wordList || wordListDefault + const random1 = Math.floor(Math.random() * list.adjectives.length) + const random2 = Math.floor(Math.random() * list.nouns.length) + const indexNumber = Math.floor(Math.random() * 100) + + // Capitalized adjective & noun + const adjective = list.adjectives[random1].replace(/^\w/, (c) => c.toUpperCase()) + const noun = list.nouns[random2].replace(/^\w/, (c) => c.toUpperCase()) + + const name = `${adjective} ${noun} Token` + // use first 3 letters of name, uppercase it, and add random number + const symbol = `${( + adjective.substring(0, 3) + noun.substring(0, 3) + ).toUpperCase()}-${indexNumber}` + + return { name, symbol } +} diff --git a/src/utils/index.ts b/src/utils/index.ts index 922ac466..b8029047 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -6,3 +6,4 @@ export * from './SubscribablePromise' export * from './SubscribableObserver' export * from './GasUtils' export * from './AssetResolverHelper' +export * from './Datatokens'