Fix kredits balance handling
Kredits are stored on the Token contract as uint256 / bignumbers with 18 decimal points. Just like Ether and required by the ERC20 standard. So we need to work with bignumbers and format a bignumber value here.
This commit is contained in:
@@ -1,12 +1,13 @@
|
|||||||
import { computed } from '@ember/object';
|
import { computed } from '@ember/object';
|
||||||
import EmberObject from '@ember/object';
|
import EmberObject from '@ember/object';
|
||||||
import bignumber from 'kredits-web/utils/cps/bignumber';
|
import bignumber from 'kredits-web/utils/cps/bignumber';
|
||||||
|
import kreditsValue from 'kredits-web/utils/cps/kredits';
|
||||||
|
|
||||||
export default EmberObject.extend({
|
export default EmberObject.extend({
|
||||||
// Contract
|
// Contract
|
||||||
id: bignumber('idRaw', 'toString'),
|
id: bignumber('idRaw', 'toString'),
|
||||||
account: null,
|
account: null,
|
||||||
balance: bignumber('balanceRaw', 'toNumber'),
|
balance: kreditsValue('balanceRaw'),
|
||||||
isCore: false,
|
isCore: false,
|
||||||
ipfsHash: null,
|
ipfsHash: null,
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ import { computed } from '@ember/object';
|
|||||||
import { alias, notEmpty } from '@ember/object/computed';
|
import { alias, notEmpty } from '@ember/object/computed';
|
||||||
import { isEmpty } from '@ember/utils';
|
import { isEmpty } from '@ember/utils';
|
||||||
|
|
||||||
|
import formatKredits from 'kredits-web/utils/format-kredits';
|
||||||
|
|
||||||
import config from 'kredits-web/config/environment';
|
import config from 'kredits-web/config/environment';
|
||||||
import Contributor from 'kredits-web/models/contributor'
|
import Contributor from 'kredits-web/models/contributor'
|
||||||
import Proposal from 'kredits-web/models/proposal'
|
import Proposal from 'kredits-web/models/proposal'
|
||||||
@@ -109,7 +111,9 @@ export default Service.extend({
|
|||||||
},
|
},
|
||||||
|
|
||||||
totalSupply: computed(function() {
|
totalSupply: computed(function() {
|
||||||
return this.kredits.Token.functions.totalSupply();
|
return this.kredits.Token.functions.totalSupply().then(total => {
|
||||||
|
return Promise.resolve(formatKredits(total));
|
||||||
|
})
|
||||||
}),
|
}),
|
||||||
|
|
||||||
loadInitialData() {
|
loadInitialData() {
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import { computed } from '@ember/object';
|
||||||
|
import ethers from 'npm:ethers';
|
||||||
|
//import formatKredits from 'kredits-web/utils/format-kredits.js';
|
||||||
|
|
||||||
|
function formatKredits(value, options) {
|
||||||
|
let etherValue = ethers.utils.formatEther(value);
|
||||||
|
if (!options.decimals) {
|
||||||
|
etherValue = parseInt(etherValue).toString();
|
||||||
|
}
|
||||||
|
return etherValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function(dependentKey, options = {}) {
|
||||||
|
return computed(dependentKey, {
|
||||||
|
get () {
|
||||||
|
const value = this.get(dependentKey);
|
||||||
|
return formatKredits(value, options);
|
||||||
|
},
|
||||||
|
set (key, value) {
|
||||||
|
const bnValue = ethers.utils.bigNumberify(value);
|
||||||
|
this.set(dependentKey, bnValue);
|
||||||
|
return formatKredits(bnValue, options);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import ethers from 'npm:ethers';
|
||||||
|
|
||||||
|
export default function(value, options = {}) {
|
||||||
|
let etherValue = ethers.utils.formatEther(value);
|
||||||
|
if (!options.decimals) {
|
||||||
|
etherValue = parseInt(etherValue).toString();
|
||||||
|
}
|
||||||
|
return etherValue;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user