-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
6,303 additions
and
2,816 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import Vue from 'vue'; | ||
import Big from 'big.js'; | ||
import {getFeeValue} from 'minterjs-util/src/fee'; | ||
import {COIN_NAME} from '~/assets/variables'; | ||
import {estimateCoinBuy} from '~/api/gate'; | ||
|
||
|
||
let coinPricePromiseList = {}; | ||
|
||
/** | ||
* @typedef {Object} FeeData | ||
* @property {boolean} isBaseCoin | ||
* @property {number|string} baseCoinValue | ||
* @property {number|string} value | ||
* @property {string} coinSymbol | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {string} txType | ||
* @param {Object} [txFeeOptions] | ||
* @param {number} [messageLength] | ||
* @param {string} [selectedCoinSymbol] | ||
* @param {string} [selectedFeeCoinSymbol] | ||
* @param {number} [baseCoinAmount] | ||
* @param {boolean} [isOffline] | ||
* @return {Vue} | ||
* @constructor | ||
*/ | ||
|
||
export default function FeeBus({txType, txFeeOptions, messageLength = 0, selectedCoinSymbol, selectedFeeCoinSymbol, baseCoinAmount = 0, isOffline}) { | ||
return new Vue({ | ||
data: { | ||
txType, | ||
txFeeOptions, | ||
messageLength, | ||
selectedCoinSymbol, | ||
selectedFeeCoinSymbol, | ||
baseCoinAmount, | ||
coinPriceList: {}, | ||
isOffline, | ||
}, | ||
computed: { | ||
baseCoinFeeValue() { | ||
return getFeeValue(this.txType, this.messageLength, this.txFeeOptions) || 0; | ||
}, | ||
isBaseCoinEnough() { | ||
return baseCoinAmount >= this.baseCoinFeeValue; | ||
}, | ||
isBaseCoinFee() { | ||
// use selectedFeeCoinSymbol if it is defined | ||
if (this.selectedFeeCoinSymbol && this.selectedFeeCoinSymbol !== COIN_NAME) { | ||
return false; | ||
} | ||
// no coins selected: show base | ||
if (!this.selectedCoinSymbol) { | ||
return true; | ||
} | ||
// base coin is selected or it is enough to pay fee | ||
return this.selectedFeeCoinSymbol === COIN_NAME || this.selectedCoinSymbol === COIN_NAME || this.isBaseCoinEnough; | ||
}, | ||
feeValue() { | ||
if (this.isBaseCoinFee) { | ||
return this.baseCoinFeeValue; | ||
} else { | ||
const coinEstimation = this.coinPriceList[this.feeCoinSymbol]; | ||
if (coinEstimation) { | ||
return new Big(coinEstimation.coinAmount).div(coinEstimation.baseCoinAmount).times(this.baseCoinFeeValue); | ||
} else { | ||
return 0; | ||
} | ||
} | ||
}, | ||
feeCoinSymbol() { | ||
// use selectedFeeCoinSymbol if it is defined | ||
if (this.selectedFeeCoinSymbol) { | ||
return this.selectedFeeCoinSymbol; | ||
} | ||
if (this.isBaseCoinFee) { | ||
return COIN_NAME; | ||
} else { | ||
return this.selectedCoinSymbol; | ||
} | ||
}, | ||
fee() { | ||
return { | ||
baseCoinValue: this.baseCoinFeeValue, | ||
isBaseCoin: this.isBaseCoinFee, | ||
isBaseCoinEnough: this.isBaseCoinEnough, | ||
value: this.feeValue, | ||
coinSymbol: this.feeCoinSymbol, | ||
}; | ||
}, | ||
}, | ||
watch: { | ||
fee: { | ||
handler(newVal) { | ||
this.$emit('updateFee', newVal); | ||
}, | ||
deep: true, | ||
}, | ||
}, | ||
created() { | ||
this.$on('updateParams', function(params) { | ||
Object.keys(params).forEach((key) => { | ||
this[key] = params[key]; | ||
}); | ||
if (this.isOffline) { | ||
return; | ||
} | ||
// wait for computed to recalculate | ||
this.$nextTick(() => { | ||
if (!this.isBaseCoinFee) { | ||
const feeCoinSymbol = this.feeCoinSymbol; | ||
getEstimation(feeCoinSymbol, this.baseCoinFeeValue) | ||
.then((result) => this.$set(this.coinPriceList, feeCoinSymbol, result)); | ||
} | ||
}); | ||
}); | ||
}, | ||
}); | ||
} | ||
|
||
|
||
/** | ||
* is older than 1 min? | ||
* @param coinPricePromise | ||
* @return {boolean} | ||
*/ | ||
function isEstimationOutdated(coinPricePromise) { | ||
return coinPricePromise.timestamp && (Date.now() - coinPricePromise.timestamp) > 60 * 1000; | ||
} | ||
|
||
/** | ||
* | ||
* @param coinSymbol | ||
* @param baseCoinAmount | ||
* @return {Promise<{coinSymbol: string, coinAmount: string, baseCoinAmount: string}>} | ||
*/ | ||
function getEstimation(coinSymbol, baseCoinAmount) { | ||
// if estimation exists and not outdated return it | ||
if (coinPricePromiseList[coinSymbol] && !isEstimationOutdated(coinPricePromiseList[coinSymbol])) { | ||
return coinPricePromiseList[coinSymbol].promise; | ||
} | ||
|
||
coinPricePromiseList[coinSymbol] = {}; | ||
coinPricePromiseList[coinSymbol].promise = estimateCoinBuy({ | ||
coinToSell: coinSymbol, | ||
valueToBuy: baseCoinAmount, | ||
coinToBuy: COIN_NAME, | ||
}) | ||
.then((result) => { | ||
coinPricePromiseList[coinSymbol].timestamp = Date.now(); | ||
return { | ||
coinSymbol, | ||
coinAmount: result.will_pay, | ||
baseCoinAmount, | ||
}; | ||
}) | ||
.catch((e) => { | ||
delete coinPricePromiseList[coinSymbol]; | ||
throw e; | ||
}); | ||
|
||
return coinPricePromiseList[coinSymbol].promise; | ||
} |
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
Oops, something went wrong.