Skip to content

Commit

Permalink
Add eos token support
Browse files Browse the repository at this point in the history
  • Loading branch information
Xaber20110202 committed Dec 4, 2018
1 parent 03b8b44 commit b6fed84
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 87 deletions.
23 changes: 23 additions & 0 deletions eos-token/$template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"symbol": "Token Symbol",
"account_name": "Account Name",
"overview": {
"en": "Token Description",
"zh": ""
},
"email": "Email Address",
"website": "https://your-awesome-project.com/",
"whitepaper": "https://your-awesome-project.com/file.pdf",
"published_on": "2018-01-01",
"initial_price": {
"EOS": "0.0016 EOS",
"USD": "0.7 USD"
},
"links": {
"steemit": "https://steemit.com/@your-awesome-project",
"twitter": "https://twitter.com/your-awesome-project",
"telegram": "https://t.me/joinchat/your-awesome-project",
"github": "https://github.com/your-awesome-project",
"reddit": "https://www.reddit.com/r/your-awesome-project/"
}
}
5 changes: 5 additions & 0 deletions eos-token/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# eosio.token
This eosio contract allows users to create, issue, and manage tokens on eosio based blockchains.

- https://github.com/EOSIO/eosio.token
- https://developers.eos.io/eosio-cpp/docs/token-tutorial
218 changes: 131 additions & 87 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ const jsonExp = /\.json$/

const imgExp = /\.png$/

const addressExp = /^(0x)?[0-9a-f]{40}$/i
const ethAddressExp = /^(0x)?[0-9a-f]{40}$/i

const isAddress = address => addressExp.test(address)
const eosTokenExp = /@/

const isAddressJson = (filename) => jsonExp.test(filename) && isAddress(filename.replace(jsonExp, ''))
const isEthAddress = address => ethAddressExp.test(address)

const isAddressPng = (filename) => imgExp.test(filename) && isAddress(filename.replace(imgExp, ''))
const isEosToken = address => eosTokenExp.test(address)

const isEthAddressJson = (filename) => jsonExp.test(filename) && isEthAddress(filename.replace(jsonExp, ''))

const isEosTokenJson = (filename) => jsonExp.test(filename) && isEosToken(filename.replace(jsonExp, ''))

const isEthAddressPng = (filename) => imgExp.test(filename) && isEthAddress(filename.replace(imgExp, ''))

const isEosTokenPng = (filename) => imgExp.test(filename) && isEosToken(filename.replace(jsonExp, ''))

const isStringWithCharacter = (str) => {
return str && typeof str === 'string' && str.trim()
Expand All @@ -37,117 +45,142 @@ const notice = (msg) => {
}

const jsonFileNames = fs.readdirSync('./erc20')
const eosJsonFileNames = fs.readdirSync('./eos-token')
const imageFileNames = fs.readdirSync('./images')
const imageAddrs = imageFileNames.map(n => n.slice(0, 42)).filter(n => n.startsWith('0x'))
const lowerCaseImageAddrs =imageAddrs.map(x => x.toLowerCase())
const jsonFileCheck = (jsonFileName, type) => {
const addr = jsonFileName.replace(jsonExp, '')
let prepath = ''

if (
(type === 'ETHEREUM' && !isEthAddress(addr)) ||
(type === 'EOS' && !isEosToken(addr))
) {
exitWithMsg(`ERROR! json file name ${jsonFileName} is not like a ${type} address.json`)
}

jsonFileNames
.filter(jsonFileName => {
return jsonFileName !== '$template.json' && jsonFileName.endsWith('.json')
})
.forEach(jsonFileName => {
const addr = jsonFileName.replace(jsonExp, '')
if (!isAddress(addr)) {
exitWithMsg(`ERROR! json file name ${jsonFileName} is not like a address.json`)
}
if (type === 'ETHEREUM') {
prepath = './erc20/'
} else if (type === 'EOS') {
prepath = './eos-token/'
}

const content = fs.readFileSync(`./erc20/${addr}.json`).toString()
let obj = null
let parseErr = null
const content = fs.readFileSync(`${prepath}${addr}.json`).toString()
let obj = null
let parseErr = null

if (content.indexOf('�') !== -1) {
exitWithMsg(`ERROR! json file name ${jsonFileName} must be utf-8 encoding`)
}
if (content.indexOf('�') !== -1) {
exitWithMsg(`ERROR! json file name ${jsonFileName} must be utf-8 encoding`)
}

try {
obj = JSON.parse(content)
} catch (e) {
parseErr = e
}
try {
obj = JSON.parse(content)
} catch (e) {
parseErr = e
}

if (parseErr) {
exitWithMsg(`ERROR! json file name ${jsonFileName} parse error, please check first (maybe has some unnecessary space or comma symbol like ",")`)
}
if (parseErr) {
exitWithMsg(`ERROR! json file name ${jsonFileName} parse error, please check first (maybe has some unnecessary space or comma symbol like ",")`)
}

if (!lowerCaseImageAddrs.includes(addr.toLowerCase())) {
notice(`Warning! dose not have ${addr + '.png'} in images dir, please check first`)
} else if (!imageAddrs.includes(obj.address)) {
const imgAddr = imageAddrs.find(imgad => {
return imgad.toLowerCase() === addr.toLowerCase()
})
exitWithMsg(`Warning! ${imgAddr + '.png'} in images dir, that capital and small letter isn't quite the same with ${addr}`)
}
if (!lowerCaseImageAddrs.includes(addr.toLowerCase())) {
notice(`Warning! dose not have ${addr + '.png'} in images dir, please check first`)
} else if (!imageAddrs.includes(obj.address)) {
const imgAddr = imageAddrs.find(imgad => {
return imgad.toLowerCase() === addr.toLowerCase()
})
exitWithMsg(`Warning! ${imgAddr + '.png'} in images dir, that capital and small letter isn't quite the same with ${addr}`)
}

if (!obj.symbol) {
exitWithMsg(`ERROR! json file ${jsonFileName} content must have symbol field`)
}
if (!obj.symbol) {
exitWithMsg(`ERROR! json file ${jsonFileName} content must have symbol field`)
}

if (!obj.address) {
exitWithMsg(`ERROR! json file ${jsonFileName} content must have address field`)
if (!obj.address) {
exitWithMsg(`ERROR! json file ${jsonFileName} content must have address field`)
}

if (!isEthAddress(obj.address)) {
exitWithMsg(`ERROR! json file ${jsonFileName} address field must be an ethereum address`)
}

if (obj.address.toLowerCase() !== addr.toLowerCase()) {
exitWithMsg(`ERROR! json file ${jsonFileName} should be the same with address field ${obj.address}`)
} else if (obj.address !== addr) {
// exitWithMsg(`Warning! json file ${jsonFileName}, that capital and small letter isn't quite the same with object.address ${obj.address}`)
}

if (obj.published_on !== undefined) {
if (obj.published_on.search(dateExp) === -1) {
exitWithMsg(`ERROR! json file ${jsonFileName}'s published_on field ${obj.published_on} must be format of YYYY-MM-DD or YYYY-MM-DD`)
}
}

if (!isAddress(obj.address)) {
exitWithMsg(`ERROR! json file ${jsonFileName} address field must be an ethereum address`)
if (obj.email !== undefined) {
if (obj.email.search(emailExp) === -1) {
exitWithMsg(`ERROR! json file ${jsonFileName}'s email field ${obj.email} must be an email`)
}
}

if (obj.address.toLowerCase() !== addr.toLowerCase()) {
exitWithMsg(`ERROR! json file ${jsonFileName} should be the same with address field ${obj.address}`)
} else if (obj.address !== addr) {
// exitWithMsg(`Warning! json file ${jsonFileName}, that capital and small letter isn't quite the same with object.address ${obj.address}`)
if (obj.overview !== undefined) {
if (!['zh', 'en'].some(k => isStringWithCharacter(obj.overview[k]))) {
exitWithMsg(`ERROR! json file ${jsonFileName}'s overview field must have zh and en field, and must be a string (not empty)`)
}
}

if (obj.published_on !== undefined) {
if (obj.published_on.search(dateExp) === -1) {
exitWithMsg(`ERROR! json file ${jsonFileName}'s published_on field ${obj.published_on} must be format of YYYY-MM-DD or YYYY-MM-DD`)
}
if (obj.links !== undefined) {
if (!Object.keys(obj.links).every(k => isUrl(obj.links[k]))) {
exitWithMsg(`ERROR! json file ${jsonFileName}'s links every field must be an url`)
}
}

if (obj.email !== undefined) {
if (obj.email.search(emailExp) === -1) {
exitWithMsg(`ERROR! json file ${jsonFileName}'s email field ${obj.email} must be an email`)
}
if (obj.state !== undefined) {
if (!['LOCKED', 'NORMAL'].includes(obj.state)) {
exitWithMsg(`ERROR! json file ${jsonFileName}'s state field ${obj.state} must be 'LOCKED' or 'NORMAL'`)
}
}

if (obj.overview !== undefined) {
if (!['zh', 'en'].some(k => isStringWithCharacter(obj.overview[k]))) {
exitWithMsg(`ERROR! json file ${jsonFileName}'s overview field must have zh and en field, and must be a string (not empty)`)
}
if (obj.initial_price !== undefined) {
const keys = Object.keys(obj.initial_price)
if (keys.some(k => !['BTC', 'ETH', 'USD'].includes(k))) {
exitWithMsg(`ERROR! json file ${jsonFileName}'s initial_price field ${JSON.stringify(obj.initial_price)} only support BTC ETH USD`)
}
if (obj.links !== undefined) {
if (!Object.keys(obj.links).every(k => isUrl(obj.links[k]))) {
exitWithMsg(`ERROR! json file ${jsonFileName}'s links every field must be an url`)

keys.forEach(k => {
if (!obj.initial_price[k].endsWith(` ${k}`)) {
exitWithMsg(`ERROR! json file ${jsonFileName}'s initial_price field ${obj.initial_price[k]} must end with ${'space+' + k}, just see example`)
}
}
})
}

if (obj.state !== undefined) {
if (!['LOCKED', 'NORMAL'].includes(obj.state)) {
exitWithMsg(`ERROR! json file ${jsonFileName}'s state field ${obj.state} must be 'LOCKED' or 'NORMAL'`)
['website', 'whitepaper'].forEach(k => {
if (obj[k] !== undefined) {
if (!isUrl(obj[k])) {
exitWithMsg(`ERROR! json file ${jsonFileName}'s ${k} field ${obj[k]} must an url`)
}
}
})
}

if (obj.initial_price !== undefined) {
const keys = Object.keys(obj.initial_price)
if (keys.some(k => !['BTC', 'ETH', 'USD'].includes(k))) {
exitWithMsg(`ERROR! json file ${jsonFileName}'s initial_price field ${JSON.stringify(obj.initial_price)} only support BTC ETH USD`)
}
const jsonFileNameFilter = jsonFileName => {
return jsonFileName !== '$template.json' && jsonFileName.endsWith('.json')
}

keys.forEach(k => {
if (!obj.initial_price[k].endsWith(` ${k}`)) {
exitWithMsg(`ERROR! json file ${jsonFileName}'s initial_price field ${obj.initial_price[k]} must end with ${'space+' + k}, just see example`)
}
})
}
jsonFileNames
.filter(jsonFileNameFilter)
.forEach(jsonFileName => {
jsonFileCheck(jsonFileName, 'ETHEREUM')
})

['website', 'whitepaper'].forEach(k => {
if (obj[k] !== undefined) {
if (!isUrl(obj[k])) {
exitWithMsg(`ERROR! json file ${jsonFileName}'s ${k} field ${obj[k]} must an url`)
}
}
})
eosJsonFileNames
.filter(jsonFileNameFilter)
.forEach(jsonFileName => {
jsonFileCheck(jsonFileName, 'EOS')
})



imageFileNames.forEach(n => {
const path = `./images/${n}`

Expand All @@ -174,12 +207,19 @@ imageFileNames.forEach(n => {
const checkWrongDirectoryItem = (directory, filename) => {
const errorMsg = `${filename} in the wrong directory ${directory}/`
if (directory === './erc20') {
if (['$template.json', 'README.md'].indexOf(filename) === -1 && !isAddressJson(filename)) {
if (['$template.json', 'README.md'].indexOf(filename) === -1 && !isEthAddressJson(filename)) {
exitWithMsg(errorMsg)
}

} else if (directory === './eos-token') {
if (['$template.json', 'README.md'].indexOf(filename) === -1 && !isEosTokenJson(filename)) {
exitWithMsg(errorMsg)
}

} else if (directory === './images') {
if (['bitcoin.png', 'eos.png', 'ethereum.png'].indexOf(filename) === -1 && !isAddressPng(filename)) {
if (['bitcoin.png', 'eos.png', 'ethereum.png'].indexOf(filename) === -1 &&
!isEthAddressPng(filename) &&
!isEosTokenPng(filename)) {
// temporality not throw
if (filename === '0x4488ed050cd13ccfe0b0fcf3d168216830142775.jpg') {
notice(errorMsg)
Expand All @@ -188,7 +228,11 @@ const checkWrongDirectoryItem = (directory, filename) => {
}
}

} else if (isAddressJson(filename) || isAddressPng(filename)) {
} else if (isEthAddressJson(filename) ||
isEthAddressPng(filename) ||
isEosTokenJson(filename) ||
isEosTokenPng(filename)
) {
exitWithMsg(errorMsg)
}
}
Expand Down

0 comments on commit b6fed84

Please sign in to comment.