-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from a1300/register_publisher_asset
Register publisher, asset and issue tokens
- Loading branch information
Showing
17 changed files
with
759 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
|
||
// ctor | ||
let CreateTokens = function (config, aschJS, axios, logger, promise) { | ||
this.config = config | ||
this.aschJS = aschJS | ||
this.axios = axios | ||
this.logger = logger | ||
this.promise = promise | ||
|
||
this.getAssetBalance = () => { | ||
let publicKey = this.aschJS.crypto.getKeys(this.config.dapp.masterAccountPassword).publicKey | ||
let address = this.aschJS.crypto.getAddress(publicKey) | ||
let currency = `${this.config.uia.publisher}.${this.config.uia.asset}` | ||
let url = `${this.config.node.host}:${this.config.node.port}/api/uia/balances/${address}/${currency}` | ||
|
||
return this.axios.get(url) | ||
} | ||
|
||
this.processAssetBalance = (response) => { | ||
// 0 balance: {"success":false,"error":"Balance info not found"} | ||
// hasBalance: {"success":true,"balance":{"currency":"CCTime.XCT","balance":"2000000000000","maximum":"1000000000000000000","precision":8,"quantity":"2000000000000","writeoff":0,"allowWriteoff":0,"allowWhitelist":0,"allowBlacklist":0,"maximumShow":"10000000000","quantityShow":"20000","balanceShow":"20000"}} | ||
|
||
if (response.status === 200) { | ||
if (response.data.success === false && response.data.error === 'Balance info not found') { | ||
return true | ||
} | ||
|
||
let threshold = 10000 | ||
if (response.data.balance && response.data.balance.balanceShow > threshold) { | ||
this.logger.info(`Balance: ${response.data.balance.balanceShow} ${this.config.uia.asset}`, { meta: 'inverse' }) | ||
throw new Error('enough_tokens') | ||
} else { | ||
return true | ||
} | ||
} else { | ||
throw new Error('error_during_requesting_asset_balance') | ||
} | ||
} | ||
|
||
this.createTokens = () => { | ||
let currency = `${this.config.uia.publisher}.${this.config.uia.asset}` | ||
let amount = (20000 * 1e8).toString() | ||
|
||
let transaction = aschJS.uia.createIssue(currency, amount, this.config.dapp.masterAccountPassword, this.config.dapp.masterAccountPassword2nd) | ||
|
||
let url = `${this.config.node.host}:${this.config.node.port}/peer/transactions` | ||
let data = { | ||
transaction: transaction | ||
} | ||
let headers = { | ||
headers: { | ||
magic: this.config.node.magic, | ||
version: '' | ||
} | ||
} | ||
|
||
return axios.post(url, data, headers) | ||
} | ||
|
||
this.processCreateTokens = (response) => { | ||
if (response.status === 200) { | ||
if (response.data.success === true) { | ||
let amount = 20000 | ||
this.logger.info(`${amount} ${this.config.uia.asset} tokens successfully created`, { meta: 'inverse' }) | ||
return true | ||
} else { | ||
throw new Error(`error_during_creation_of_tokens, ${response.data.error}`) | ||
} | ||
} else { | ||
throw new Error('error_during_creation_of_tokens') | ||
} | ||
} | ||
|
||
this.start = () => { | ||
return this.getAssetBalance() | ||
.then((response) => { | ||
return this.processAssetBalance(response) | ||
}) | ||
.then(() => { | ||
return this.createTokens() | ||
}) | ||
.then((response) => { | ||
return this.processCreateTokens(response) | ||
}) | ||
.then(() => { | ||
this.logger.info(`waiting 11 sec for the create token transaction to be persisted to the next block...`) | ||
return this.promise.delay(11000) | ||
}) | ||
.catch((error) => { | ||
if (error.message.startsWith('enough_tokens')) { | ||
return true | ||
} else { | ||
throw error | ||
} | ||
}) | ||
} | ||
} | ||
|
||
module.exports = CreateTokens |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
|
||
// ctor | ||
let RegisterAsset = function (config, aschJS, axios, logger, promise) { | ||
this.config = config | ||
this.aschJS = aschJS | ||
this.axios = axios | ||
this.logger = logger | ||
this.promise = promise | ||
|
||
this.waitingMS = 11000 | ||
|
||
this.existsAsset = () => { | ||
let url = `${this.config.node.host}:${this.config.node.port}/api/uia/assets/${this.config.uia.publisher}.${this.config.uia.asset}` | ||
|
||
return axios.get(url) | ||
} | ||
|
||
this.handleExistsAsset = (result) => { | ||
/* | ||
http://localhost:4096/api/uia/assets/CCtime.XCT | ||
response assetExists: { | ||
"success":true, | ||
"asset": { | ||
"name":"CCtime.XCT", | ||
"desc":"xct", | ||
"maximum":"10000000000000", | ||
"precision":8, | ||
"strategy":"", | ||
"quantity":"0", | ||
"height":20, | ||
"issuerId":"AHMCKebuL2nRYDgszf9J2KjVZzAw95WUyB", | ||
"acl":0, | ||
"writeoff":0, | ||
"allowWriteoff":0, | ||
"allowWhitelist":0, | ||
"allowBlacklist":0, | ||
"maximumShow":"100000", | ||
"quantityShow":"0" | ||
} | ||
} | ||
response assetExistsNot: { | ||
"success":false, | ||
"error":"Asset not found" | ||
} | ||
*/ | ||
|
||
if (result.status === 200) { | ||
if (result.data.success === true) { | ||
throw new Error('already_registered') | ||
} else { | ||
return true | ||
} | ||
} else { | ||
throw new Error('could_not_load_assets') | ||
} | ||
} | ||
|
||
this.register = () => { | ||
let name = `${config.uia.publisher}.${config.uia.asset}` | ||
let desc = name | ||
let maximum = '1000000000000000000' | ||
let precision = 8 | ||
let strategy = '' | ||
let allowWriteoff = 0 | ||
let allowWhitelist = 0 | ||
let allowBlacklist = 0 | ||
|
||
let transaction = aschJS.uia.createAsset(name, desc, maximum, precision, strategy, allowWriteoff, allowWhitelist, allowBlacklist, this.config.dapp.masterAccountPassword, this.config.dapp.masterAccountPassword2nd) | ||
|
||
let url = `${this.config.node.host}:${this.config.node.port}/peer/transactions` | ||
let data = { | ||
transaction: transaction | ||
} | ||
let headers = { | ||
headers: { | ||
magic: this.config.node.magic, | ||
version: '' | ||
} | ||
} | ||
|
||
return axios.post(url, data, headers) | ||
} | ||
|
||
this.handleRegister = (response) => { | ||
if (response.data.success === true) { | ||
let asset = this.config.uia.asset | ||
this.logger.info(`asset "${asset}" registered`, { meta: 'inverse' }) | ||
return true | ||
} | ||
} | ||
|
||
this.start = () => { | ||
return this.existsAsset() | ||
.then((response) => { | ||
return this.handleExistsAsset(response) | ||
}) | ||
.then((response) => { | ||
return this.register() | ||
}) | ||
.then((response) => { | ||
return this.handleRegister(response) | ||
}) | ||
.then(() => { | ||
this.logger.info(`waiting 11 sec for asset registration transaction to be written in block...`) | ||
return this.promise.delay(this.waitingMS) | ||
}) | ||
.then(() => { | ||
return true | ||
}) | ||
.catch((error) => { | ||
if (error.message.startsWith('already_registered')) { | ||
return true | ||
} else { | ||
throw error | ||
} | ||
}) | ||
} | ||
} | ||
|
||
module.exports = RegisterAsset |
Oops, something went wrong.