1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

move datatoken name generation out of class (#943)

This commit is contained in:
Matthias Kretschmann 2021-07-30 14:29:12 +02:00 committed by GitHub
parent 1af59c1c85
commit c61aab0670
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 16 deletions

View File

@ -4,10 +4,10 @@ import { AbiItem } from 'web3-utils/types'
import defaultFactoryABI from '@oceanprotocol/contracts/artifacts/DTFactory.json' import defaultFactoryABI from '@oceanprotocol/contracts/artifacts/DTFactory.json'
import defaultDatatokensABI from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json' import defaultDatatokensABI from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json'
import { Logger, getFairGasPrice } from '../utils' import { Logger, getFairGasPrice } from '../utils'
import wordListDefault from '../data/words.json'
import { TransactionReceipt } from 'web3-core' import { TransactionReceipt } from 'web3-core'
import BigNumber from 'bignumber.js' import BigNumber from 'bignumber.js'
import Decimal from 'decimal.js' import Decimal from 'decimal.js'
import { generateDatatokenName } from '../utils/Datatokens'
/** /**
* Provides an interface to DataTokens * Provides an interface to DataTokens
@ -51,21 +51,7 @@ export class DataTokens {
name: string name: string
symbol: string symbol: string
} { } {
const list = wordList || wordListDefault const { name, symbol } = generateDatatokenName(wordList)
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 } return { name, symbol }
} }

30
src/utils/Datatokens.ts Normal file
View File

@ -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 }
}

View File

@ -6,3 +6,4 @@ export * from './SubscribablePromise'
export * from './SubscribableObserver' export * from './SubscribableObserver'
export * from './GasUtils' export * from './GasUtils'
export * from './AssetResolverHelper' export * from './AssetResolverHelper'
export * from './Datatokens'