mirror of
https://github.com/Chia-Network/chia-blockchain.git
synced 2026-09-12 02:24:38 -05:00
use units, chia - mojo
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
var Big = require('big.js');
|
||||
var units = require('./units');
|
||||
|
||||
const convert = (amount, from, to) => {
|
||||
if (Number.isNaN(parseFloat(amount)) || !Number.isFinite(amount)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const amountInFromUnit = Big(amount).times(units.getUnit(from));
|
||||
|
||||
return parseFloat(amountInFromUnit.div(units.getUnit(to)));
|
||||
};
|
||||
|
||||
class Chia {
|
||||
constructor(value, unit) {
|
||||
this._value = value;
|
||||
this._unit = unit;
|
||||
}
|
||||
|
||||
to(newUnit) {
|
||||
this._value = convert(this._value, this._unit, newUnit);
|
||||
this._unit = newUnit;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
value() {
|
||||
return this._value;
|
||||
}
|
||||
|
||||
format() {
|
||||
const displayUnit = units.getDisplay(this._unit);
|
||||
|
||||
const { format, fractionDigits, trailing } = displayUnit;
|
||||
|
||||
let options = { maximumFractionDigits: fractionDigits };
|
||||
|
||||
if (trailing) {
|
||||
options = { minimumFractionDigits: fractionDigits };
|
||||
}
|
||||
|
||||
let value;
|
||||
|
||||
if (fractionDigits !== undefined) {
|
||||
const fractionPower = Big(10).pow(fractionDigits);
|
||||
value = parseFloat(Big(Math.floor(Big(this._value).times(fractionPower))).div(fractionPower));
|
||||
} else {
|
||||
value = this._value;
|
||||
}
|
||||
|
||||
let formatted = format.replace('{amount}', parseFloat(value).toLocaleString(undefined, options));
|
||||
|
||||
if (displayUnit.pluralize && this._value !== 1) {
|
||||
formatted += 's';
|
||||
}
|
||||
|
||||
return formatted;
|
||||
}
|
||||
|
||||
toString() {
|
||||
const displayUnit = units.getDisplay(this._unit);
|
||||
const { format, fractionDigits, trailing } = displayUnit;
|
||||
let options = { maximumFractionDigits: fractionDigits };
|
||||
return parseFloat(this._value).toLocaleString(undefined, options)
|
||||
}
|
||||
}
|
||||
|
||||
const chia_formatter = (value, unit) => new Chia(value, unit);
|
||||
|
||||
chia_formatter.convert = convert;
|
||||
chia_formatter.setDisplay = units.setDisplay;
|
||||
chia_formatter.setUnit = units.setUnit;
|
||||
chia_formatter.getUnit = units.getUnit;
|
||||
chia_formatter.setFiat = (currency, rate, display = null) => {
|
||||
units.setUnit(currency, 1 / rate, display);
|
||||
};
|
||||
|
||||
module.exports = chia_formatter
|
||||
Generated
+5
@@ -172,6 +172,11 @@
|
||||
"tweetnacl": "^0.14.3"
|
||||
}
|
||||
},
|
||||
"big.js": {
|
||||
"version": "5.2.2",
|
||||
"resolved": "https://registry.npmjs.org/big.js/-/big.js-5.2.2.tgz",
|
||||
"integrity": "sha512-vyL2OymJxmarO8gxMr0mhChsO9QGwhynfuu4+MHTAW6czfq9humCB7rKpUjDd9YUiDPU4mzpyupFSvOClAwbmQ=="
|
||||
},
|
||||
"binary": {
|
||||
"version": "0.3.0",
|
||||
"resolved": "https://registry.npmjs.org/binary/-/binary-0.3.0.tgz",
|
||||
|
||||
@@ -13,11 +13,12 @@
|
||||
"author": "Straya Markovic",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"dialogs": "^2.0.1",
|
||||
"electron-rebuild": "^1.10.0",
|
||||
"jquery": "^3.4.1",
|
||||
"qrcode": "^1.4.4",
|
||||
"ws": "^6.2.1"
|
||||
"dialogs": "2.0.1",
|
||||
"electron-rebuild": "1.10.0",
|
||||
"jquery": "3.4.1",
|
||||
"qrcode": "1.4.4",
|
||||
"ws": "6.2.1",
|
||||
"big.js": "5.2.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"electron": "^1.8.8",
|
||||
|
||||
@@ -6,7 +6,9 @@ const Dialogs = require('dialogs')
|
||||
const dialogs = Dialogs()
|
||||
const WebSocket = require('ws');
|
||||
var ws = new WebSocket(host);
|
||||
let chia_formatter = require('./chia');
|
||||
|
||||
// HTML
|
||||
let send = document.querySelector('#send')
|
||||
let farm_button = document.querySelector('#farm_block')
|
||||
let new_address = document.querySelector('#new_address')
|
||||
@@ -14,19 +16,22 @@ let copy = document.querySelector("#copy")
|
||||
let receiver_address = document.querySelector("#receiver_puzzle_hash")
|
||||
let amount = document.querySelector("#amount_to_send")
|
||||
let table = document.querySelector("#tx_table").getElementsByTagName('tbody')[0]
|
||||
let green_checkmark = "<i class=\"icon ion-md-checkmark-circle-outline green\"></i>"
|
||||
let red_checkmark = "<i class=\"icon ion-md-close-circle-outline red\"></i>"
|
||||
let balance_textfield = document.querySelector('#balance_textfield')
|
||||
let pending_textfield = document.querySelector('#pending_textfield')
|
||||
let connection_textfield = document.querySelector('#connection_textfield')
|
||||
let syncing_textfield = document.querySelector('#syncing_textfield')
|
||||
let block_height_textfield = document.querySelector('#block_height_textfield')
|
||||
let standard_wallet_balance = document.querySelector('#standard_wallet_balance')
|
||||
|
||||
let lock = "<i class=\"icon ion-md-lock\"></i>"
|
||||
var myBalance = 0
|
||||
var myUnconfirmedBalance = 0
|
||||
// UI checkmarks and lock icons
|
||||
const green_checkmark = "<i class=\"icon ion-md-checkmark-circle-outline green\"></i>"
|
||||
const red_checkmark = "<i class=\"icon ion-md-close-circle-outline red\"></i>"
|
||||
const lock = "<i class=\"icon ion-md-lock\"></i>"
|
||||
|
||||
// Global variables
|
||||
var global_syncing = true
|
||||
|
||||
|
||||
console.log(global.location.search)
|
||||
function getQueryVariable(variable) {
|
||||
var query = global.location.search.substring(1);
|
||||
@@ -128,10 +133,13 @@ send.addEventListener('click', () => {
|
||||
return
|
||||
}
|
||||
puzzlehash = receiver_address.value;
|
||||
amount_value = amount.value;
|
||||
amount_value = parseFloat(amount.value);
|
||||
mojo_amount = chia_formatter(amount_value, 'chia').to('mojo')
|
||||
console.log("Mojo amount: " + mojo_amount);
|
||||
|
||||
data = {
|
||||
"puzzlehash": puzzlehash,
|
||||
"amount": amount_value
|
||||
"amount": mojo_amount
|
||||
}
|
||||
|
||||
request = {
|
||||
@@ -236,11 +244,18 @@ function get_wallet_balance_response(response) {
|
||||
var confirmed = parseInt(response["confirmed_wallet_balance"])
|
||||
var unconfirmed = parseInt(response["unconfirmed_wallet_balance"])
|
||||
var pending = confirmed - unconfirmed
|
||||
balance_textfield.innerHTML = confirmed + " CH"
|
||||
chia_confirmed = chia_formatter(confirmed, 'mojo').to('chia').toString()
|
||||
chia_pending = chia_formatter(pending, 'mojo').to('chia').toString()
|
||||
|
||||
balance_textfield.innerHTML = chia_confirmed
|
||||
standard_wallet_balance.innerHTML = chia_confirmed.toString() + " CH"
|
||||
standard_wallet_balance.innerHTML = chia_confirmed.toString() + " CH"
|
||||
if (pending > 0) {
|
||||
pending_textfield.innerHTML = lock + " -" + pending + " CH"
|
||||
pending_textfield.innerHTML = lock + " - " + chia_pending + " CH"
|
||||
standard_wallet_pending.innerHTML = lock + " - " + chia_pending + " CH"
|
||||
} else {
|
||||
pending_textfield.innerHTML = lock + " 0" + " CH"
|
||||
pending_textfield.innerHTML = lock + " " + chia_pending + " CH"
|
||||
standard_wallet_pending.innerHTML = lock + " - " + chia_pending + " CH"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -359,7 +374,14 @@ async function get_connection_info_response(response) {
|
||||
connections = response["connections"]
|
||||
console.log("Connections:" + connections)
|
||||
console.log("Connected to: " + connections.length + " peers")
|
||||
connection_textfield.innerHTML = connections.length + " connections"
|
||||
count = connections.length;
|
||||
if (count == 0) {
|
||||
connection_textfield.innerHTML = "Not Connected!!!"
|
||||
} else if (count == 1) {
|
||||
connection_textfield.innerHTML = connections.length + " connection"
|
||||
} else {
|
||||
connection_textfield.innerHTML = connections.length + " connections"
|
||||
}
|
||||
}
|
||||
|
||||
function handle_state_changed(data) {
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
const units = {
|
||||
chia: 1,
|
||||
mojo: 1 / 1E12,
|
||||
};
|
||||
|
||||
const aliases = {
|
||||
chia: ['ch', 'chia', 'Chia'],
|
||||
mojo: ['mj', 'mojo'],
|
||||
};
|
||||
|
||||
const display = {
|
||||
chia: {
|
||||
format: '{amount} CH',
|
||||
fractionDigits: 12
|
||||
},
|
||||
mojo: {
|
||||
format: '{amount} MJ',
|
||||
fractionDigits: 0
|
||||
},
|
||||
};
|
||||
|
||||
const getUnitNameByAlias = (unitName) => {
|
||||
const name = unitName.toLowerCase();
|
||||
|
||||
const alias = Object.keys(aliases).find(key => aliases[key].includes(name));
|
||||
|
||||
if (alias === undefined) {
|
||||
throw new Error(`Unit '${unitName}' is not supported`);
|
||||
}
|
||||
|
||||
return alias;
|
||||
};
|
||||
|
||||
const getUnitName = (unitName) => {
|
||||
const name = unitName.toLowerCase();
|
||||
|
||||
const unit = units[name];
|
||||
|
||||
if (unit !== undefined) return name;
|
||||
|
||||
return getUnitNameByAlias(unitName);
|
||||
};
|
||||
|
||||
const getUnit = unit => units[getUnitName(unit)];
|
||||
|
||||
const setDisplay = (unit, options) => {
|
||||
display[unit.toLowerCase()] = options;
|
||||
};
|
||||
|
||||
const getDisplay = unit => display[getUnitName(unit)];
|
||||
|
||||
const setUnit = (unit, value, displayOptions = null) => {
|
||||
units[unit.toLowerCase()] = value;
|
||||
display[unit.toLowerCase()] = displayOptions !== null ? displayOptions : { format: `{amount} ${unit}` };
|
||||
};
|
||||
|
||||
|
||||
module.exports = {
|
||||
getUnit: getUnit,
|
||||
setUnit: setUnit,
|
||||
getDisplay: getDisplay,
|
||||
setDisplay: setDisplay
|
||||
}
|
||||
@@ -54,8 +54,8 @@
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<h3>0.00</h3>
|
||||
<p class="text-right"><i class="icon ion-md-lock"></i> 0.00</p>
|
||||
<h3 class="text-right" id="standard_wallet_balance">0.00</h3>
|
||||
<p class="text-right" id="standard_wallet_pending"><i class="icon ion-md-lock"></i> 0.00</p>
|
||||
</div>
|
||||
</a>
|
||||
<a class="nav-link d-flex justify-content-between align-items-center" data-toggle="pill" href="#rlwallet"
|
||||
|
||||
Reference in New Issue
Block a user