Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@fortawesome/free-solid-svg-icons": "^5.12.1",
"@fortawesome/vue-fontawesome": "^0.1.9",
"@zxing/library": "^0.15.2",
"big.js": "^5.2.2",
"big.js": "6.0.3",
"bn.js": "^5.1.1",
"core-js": "^3.6.4",
"vue": "^2.6.11",
Expand Down
18 changes: 8 additions & 10 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
<hr>

<bignum_input :value="bigVal"
:denomination="9"
:max="big_max"
:min="0"
:denomination="18"
@change="bigInChange"
:step="stepSize"
placeholder="0.0000000"
placeholder="Amount"
ref="big_in"
></bignum_input>
<p v-if="big_in_out">Out: {{big_in_out.toString()}}</p>
<button @click="clearBigIn">Clear</button>
<button @click="maxOutBig">MAX</button>
<div style="display: flex">
<p>Denomination 9</p>
<p>MAX: {{big_max.toString()}}</p>
Expand Down Expand Up @@ -56,13 +54,10 @@
computed: {
big_max(){
// return null;
return new BN('1000000000000000');
return new BN('36000000000000000');
},
stepSize(){
// let expo = 9-3;
// expo = new BN(expo)
// let tens = new BN('10').pow(expo);
return new BN(1);
return new BN(1000000);
}
},
methods: {
Expand All @@ -71,6 +66,9 @@
},
clearBigIn(){
this.$refs.big_in.clear();
},
maxOutBig(){
this.$refs.big_in.maxout();
}
},
created(){
Expand Down
126 changes: 82 additions & 44 deletions src/bignum_input.vue
Original file line number Diff line number Diff line change
@@ -1,43 +1,41 @@
<template>
<CurrencyInput :allow-negative="false"
v-model="val"
locale="default"
:currency="null"
:value-as-integer="false"
:precision="denomination"
:auto-decimal-mode="true"
:value-range="range"
ref="curIn"
@keydown.up.native="keyUp"
@keydown.down.native="keyDown"
></CurrencyInput>
<input type="number" :placeholder="placeholder" v-model="val" :min="min" :max="maxNum" :step="stepNum" @change="onChange">
</template>
<script>
import Big from 'big.js';
import * as BN from 'bn.js';
import {CurrencyInput} from 'vue-currency-input';

export default {
components:{
CurrencyInput
},
data(){
return {
val: 0,
val: null,
}
},
computed: {
range(){
let maxStr = Number.MAX_SAFE_INTEGER.toString();
if(this.max){
let bnStr = this.max.toString();
let big = Big(bnStr).div(Math.pow(10,this.denomination));
maxStr = big.toString();
maxNum(){
if(this.max===null) return null
try{
return this.bnToNum(this.max)
}catch (e){
console.error(e)
return null
}
},
stepNum(){
if(!this.step) {
if(this.denomination >= 2){
return 0.01
}else{
return Math.pow(10, -this.denomination)
}
}
return{
min: this.min.toString(),
max: maxStr
try{
return this.bnToNum(this.step)
}catch (e){
console.error(e)
return 0.01
}

}
},
props: {
Expand All @@ -46,6 +44,7 @@
default: 0
},
max: {
default: null,
type: [BN, Object],
},
min: {
Expand All @@ -65,39 +64,78 @@
},
watch: {
val(val){
let mult = Math.pow(10,this.denomination);
let valBig = Big(val).times(mult);
let strVal = valBig.toString();
let bnVal = new BN(strVal);
this.$emit('change', bnVal);

//
try{
let splitVal = val.toString().split('.')
let wholeVal = splitVal[0]
let denomVal = splitVal[1]
if(denomVal){
if(denomVal.length > this.denomination){
let newDenom = denomVal.substring(0,this.denomination)
this.val = `${wholeVal}.${newDenom}`
return
}
}
}catch (e){
console.log(e)
}

if(!val){
this.$emit('change', new BN(0));
return
}

let valBn = this.stringToBN(val)
this.$emit('change', valBn);
},
value(valBn){
this.val = this.bnToNum(valBn)
}
},
methods: {
keyUp(ev){
ev.preventDefault();
let dif = Big(1).div(Math.pow(10,this.denomination))
let newVal = Big(this.val).add(dif);
this.$refs.curIn.setValue(newVal.toString());
bnToNum(bnVal){
let pow = (new Big(bnVal.toString())).div(Math.pow(10,this.denomination))
return pow.toNumber()
},
keyDown(ev){
ev.preventDefault();
let dif = Big(1).div(Math.pow(10,this.denomination))
let newVal = Big(this.val).sub(dif);
this.$refs.curIn.setValue(newVal.toString());
stringToBN(strVal){
let tens = Big(10).pow(this.denomination)
let satoshis = Big(strVal).times(tens)
return new BN(satoshis.toString())
},
maxout(){
if(this.max){
this.$refs.curIn.setValue(this.max.toString());
if(this.maxNum != null){
this.val = this.maxNum
}
},
clear(){
this.$refs.curIn.setValue('0');
this.val = 0
},
onChange(){
// If number is above max amount, correct it
if(this.maxNum != null){
if(this.val > this.maxNum){
this.val = this.maxNum
}
}
}
}
}
</script>
<style scoped>
input{
text-align: right;
outline: none;
}
/* Chrome, Safari, Edge, Opera */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}

/* Firefox */
input[type=number] {
-moz-appearance: textfield;
}
</style>
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1657,6 +1657,11 @@ bfj@^6.1.1:
hoopy "^0.1.4"
tryer "^1.0.1"

big.js@6.0.3:
version "6.0.3"
resolved "https://registry.yarnpkg.com/big.js/-/big.js-6.0.3.tgz#8b4d99ac7023668e0e465d3f78c23b8ac29ad381"
integrity sha512-n6yn1FyVL1EW2DBAr4jlU/kObhRzmr+NNRESl65VIOT8WBJj/Kezpx2zFdhJUqYI6qrtTW7moCStYL5VxeVdPA==

big.js@^3.1.3:
version "3.2.0"
resolved "https://registry.yarnpkg.com/big.js/-/big.js-3.2.0.tgz#a5fc298b81b9e0dca2e458824784b65c52ba588e"
Expand Down