Skip to content

Commit

Permalink
Hub: rework discount to composable
Browse files Browse the repository at this point in the history
  • Loading branch information
shrpne committed Dec 10, 2021
1 parent 229e001 commit 4b631d7
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 24 deletions.
24 changes: 12 additions & 12 deletions components/HubDepositForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import QrcodeVue from 'qrcode.vue';
import autosize from 'v-autosize';
import * as web3 from '@/api/web3.js';
import {getTokenDecimals, getEvmNetworkName, fromErcDecimals, toErcDecimals, getHubNetworkByChain} from '@/api/web3.js';
import {getDiscountForHolder} from '@/api/hub.js';
import Big from '~/assets/big.js';
import {pretty, prettyPrecise, prettyRound} from '~/assets/utils.js';
import erc20ABI from '~/assets/abi-erc20.js';
Expand All @@ -17,6 +16,7 @@ import wethAbi from '~/assets/abi-weth.js';
import {HUB_CHAIN_BY_ID, ETHEREUM_CHAIN_ID, BSC_CHAIN_ID, ETHEREUM_API_URL, BSC_API_URL} from '~/assets/variables.js';
import {getErrorText} from '~/assets/server-error.js';
import checkEmpty from '~/assets/v-check-empty.js';
import useHubDiscount from '@/composables/useHubDiscount.js';
import Loader from '~/components/common/Loader.vue';
import TxListItem from '~/components/HubDepositTxListItem.vue';
import Account from '~/components/HubDepositAccount.vue';
Expand Down Expand Up @@ -76,6 +76,14 @@ export default {
required: true,
},
},
setup() {
const { discount, discountProps } = useHubDiscount();
return {
discount,
discountProps,
};
},
data() {
return {
ethAddress: "",
Expand All @@ -94,9 +102,6 @@ export default {
isIgnorePending: true,
isUnwrapAll: true,
},
// @TODO refactor to composable
discountEth: 0,
discountMinter: 0,
isFormSending: false,
serverError: '',
waitUnwrapConfirmation: false,
Expand Down Expand Up @@ -246,9 +251,6 @@ export default {
// don't unlock delta, it will overwrite total unlocked
// return new Big(this.amountToSpend).minus(this.selectedUnlocked).toString();
},
discount() {
return Math.max(this.discountEth, this.discountMinter);
},
suggestionList() {
const network = getHubNetworkByChain(this.chainId);
return this.hubCoinList
Expand All @@ -274,15 +276,13 @@ export default {
this.getAllowance();
}
this.$store.commit('hub/setEthAddress', newVal);
getDiscountForHolder(newVal)
.then((discount) => this.discountEth = discount);
this.discountProps.ethAddress = this.ethAddress;
},
immediate: true,
},
'form.address': {
handler(newVal) {
//@TODO debounce
getDiscountForHolder(newVal)
.then((discount) => this.discountMinter = discount);
this.discountProps.minterAddress = newVal;
},
immediate: true,
},
Expand Down
29 changes: 17 additions & 12 deletions components/HubWithdrawForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import autosize from 'v-autosize';
import {TX_TYPE} from 'minterjs-util/src/tx-types.js';
import {convertToPip} from 'minterjs-util/src/converter.js';
import {postTx} from '~/api/gate.js';
import {getOracleFee, getDiscountForHolder} from '@/api/hub.js';
import {getOracleFee} from '@/api/hub.js';
import {getExplorerTxUrl, pretty, prettyPrecise, prettyRound} from '~/assets/utils.js';
import {HUB_MINTER_MULTISIG_ADDRESS, HUB_CHAIN_ID, HUB_CHAIN_DATA} from '~/assets/variables.js';
import checkEmpty from '~/assets/v-check-empty.js';
import {getErrorText} from '~/assets/server-error.js';
import useHubDiscount from '@/composables/useHubDiscount.js';
import FieldQr from '@/components/common/FieldQr.vue';
import FieldUseMax from '~/components/common/FieldUseMax';
import FieldCoin from '@/components/common/FieldCoin.vue';
Expand Down Expand Up @@ -58,9 +59,15 @@ export default {
required: true,
},
},
setup() {
const { discount, discountProps } = useHubDiscount();
return {
discount,
discountProps,
};
},
fetch() {
getDiscountForHolder(this.$store.getters.address)
.then((discount) => this.discountMinter = discount);
return this.getDestinationFee();
},
data() {
Expand All @@ -82,9 +89,6 @@ export default {
serverWarning: '',
isConfirmModalVisible: false,
isSuccessModalVisible: false,
// @TODO refactor to composable
discountEth: 0,
discountMinter: 0,
};
},
computed: {
Expand Down Expand Up @@ -146,9 +150,6 @@ export default {
return maxAmount.toString();
}
},
discount() {
return Math.max(this.discountEth, this.discountMinter);
},
suggestionList() {
return this.hubCoinList.map((item) => item.symbol);
// intersection of address balance and hub supported coins
Expand Down Expand Up @@ -192,9 +193,13 @@ export default {
},
'form.address': {
handler(newVal) {
//@TODO debounce
getDiscountForHolder(newVal)
.then((discount) => this.discountEth = discount);
this.discountProps.ethAddress = newVal;
},
immediate: true,
},
'$store.getters.address': {
handler(newVal) {
this.discountProps.minterAddress = newVal;
},
immediate: true,
},
Expand Down
46 changes: 46 additions & 0 deletions composables/useHubDiscount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ref, reactive, computed, watch } from '@vue/composition-api';
import debounce from 'debounce-promise';
import {getDiscountForHolder as _getDiscountForHolder} from '@/api/hub.js';

export default function useHubDiscount() {
// two different debounced functions
const debouncedGetDiscountMinter = makeDebouncedGetDiscount();
const debouncedGetDiscountEth = makeDebouncedGetDiscount();

const discountProps = reactive({
minterAddress: '',
ethAddress: '',
});
const discountMinter = ref(0);
const discountEth = ref(0);
const discount = computed(() => {
return Math.max(discountEth.value, discountMinter.value);
});

const getMinterDiscount = async () => {
discountMinter.value = await debouncedGetDiscountMinter(discountProps.minterAddress);
};
const getEthDiscount = async () => {
discountEth.value = await debouncedGetDiscountEth(discountProps.ethAddress);
};

watch(() => discountProps.minterAddress, getMinterDiscount);
watch(() => discountProps.ethAddress, getEthDiscount);

return {
discount,
discountProps,
};
}

function getDiscountForHolder(address) {
return _getDiscountForHolder(address)
.catch((error) => {
console.log(error);
return 0;
});
}

function makeDebouncedGetDiscount() {
return debounce(getDiscountForHolder, 1000);
}
1 change: 1 addition & 0 deletions nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export default {
},
plugins: [
{ src: '~/plugins/base-url-prefix.js'},
{ src: '~/plugins/composition-api.js'},
{ src: '~/plugins/persistedState.js', ssr: false },
{ src: '~/plugins/online.js', ssr: false },
{ src: '~/plugins/click-blur.js', ssr: false },
Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@material/snackbar": "^12.0.0",
"@uniswap/sdk": "^3.0.3",
"@uniswap/v2-periphery": "^1.1.0-beta.0",
"@vue/composition-api": "^1.4.1",
"@walletconnect/client": "^1.6.6",
"@walletconnect/qrcode-modal": "^1.6.6",
"aes-js": "^3.1.2",
Expand Down
4 changes: 4 additions & 0 deletions plugins/composition-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import Vue from 'vue';
import VueCompositionAPI from '@vue/composition-api';

Vue.use(VueCompositionAPI);

0 comments on commit 4b631d7

Please sign in to comment.