Skip to content

Commit

Permalink
fix #1674 (#1785)
Browse files Browse the repository at this point in the history
fix #1730

fix #1663
  • Loading branch information
bassemmagdy authored Nov 25, 2021
1 parent bfc23ba commit a112dc7
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 16 deletions.
5 changes: 5 additions & 0 deletions __tests__/components/AmountInput.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { StandardValidationRules } from '@/core/validation/StandardValidationRul
import { MaxDecimalsValidator, PositiveDecimalNumberValidator, MaxRelativeAmountValidator } from '@/core/validation/validators';
import { appConfig } from '@/config';
import { NetworkConfigurationModel } from '@/core/database/entities/NetworkConfigurationModel';
import { StartsWithZeroValidator } from '@/core/validation/validators/StartsWithZeroValidator';

StandardValidationRules.register();
appConfig.constants.DECIMAL_SEPARATOR = '.';
Expand All @@ -27,6 +28,10 @@ extend('positiveDecimal', {
validate: (value) => PositiveDecimalNumberValidator.validate(value),
message: () => i18n.t('positive_decimal_error', { decimalSeparator: appConfig.constants.DECIMAL_SEPARATOR }).toString(),
});
extend('startsWithZero', {
validate: (value) => StartsWithZeroValidator.validate(value),
message: () => i18n.t('amount_value_cannot_start_with_zero').toString(),
});
extend('maxRelativeAmount', {
validate: (value, { maxMosaicAtomicUnits, maxMosaicDivisibility }: any) => {
const maxRelativeAmount =
Expand Down
5 changes: 5 additions & 0 deletions src/core/validation/CustomValidationRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { ProfileModel } from '@/core/database/entities/ProfileModel';
import { AccountService } from '@/services/AccountService';
import { NetworkConfigurationModel } from '@/core/database/entities/NetworkConfigurationModel';
import { Values } from 'vue-i18n';
import { StartsWithZeroValidator } from '@/core/validation/validators/StartsWithZeroValidator';

// TODO CustomValidationRules needs to be created when the network configuration is resolved, UI
// needs to use the resolved CustomValidationRules
Expand Down Expand Up @@ -82,6 +83,10 @@ export class CustomValidationRules {
validate: (value) => PositiveDecimalNumberValidator.validate(value),
message: () => i18n.t('positive_decimal_error', { decimalSeparator: DECIMAL_SEPARATOR }).toString(),
});
extend('startsWithZero', {
validate: (value) => StartsWithZeroValidator.validate(value),
message: () => i18n.t('amount_value_cannot_start_with_zero').toString(),
});

extend('addressOrAlias', {
validate: async (value) => {
Expand Down
5 changes: 4 additions & 1 deletion src/core/validation/ValidationRuleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ export const createValidationRuleSet = ({
address: 'required|address|addressNetworkType:currentProfile',
profilePassword: 'required|profilePassword',
addressOrAlias: 'required|addressOrAlias|addressOrAliasNetworkType:currentProfile',
amount: `positiveDecimal|maxDecimals:${maxMosaicDivisibility}|maxRelativeAmount:${[maxMosaicAtomicUnits, maxMosaicDivisibility]}`,
amount: `positiveDecimal|startsWithZero|maxDecimals:${maxMosaicDivisibility}|maxRelativeAmount:${[
maxMosaicAtomicUnits,
maxMosaicDivisibility,
]}`,
confirmPassword: 'required|confirmPassword:@newPassword',
divisibility: 'required|min_value:0|max_value:6|integer',
duration: `required|min_value:0|max_value:${maxMosaicDuration}`,
Expand Down
29 changes: 29 additions & 0 deletions src/core/validation/validators/StartsWithZeroValidator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2020 NEM (https://nem.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*
*/
import { Validator, staticImplements } from './Validator';

@staticImplements<Validator>()
export class StartsWithZeroValidator {
/**
* Validates amount value to not starts with 0 in case of value > 1
* @static
* @param {*} value
* @returns {boolean}
*/
public static validate(value: any): boolean {
return parseInt(value) > 0 ? !value.startsWith('0') : true;
}
}
3 changes: 2 additions & 1 deletion src/language/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -1132,5 +1132,6 @@
"transaction_cosignature_warning_proceed": "I understand and wish to proceed.",
"lock_hash_algorithm": "Hash Algorithm",
"not_enough_balance": "Not enough balance",
"use_max_value": "Use maximum value"
"use_max_value": "Use maximum value",
"amount_value_cannot_start_with_zero": "Amount value cannot start with 0"
}
3 changes: 2 additions & 1 deletion src/language/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -1132,5 +1132,6 @@
"transaction_cosignature_warning_proceed": "確認しましたので署名に同意します",
"lock_hash_algorithm": "ハッシュアルゴリズム",
"not_enough_balance": "バランスが悪い",
"use_max_value": "最大値を使用"
"use_max_value": "最大値を使用",
"amount_value_cannot_start_with_zero": "金額の値を0から始めることはできません"
}
3 changes: 2 additions & 1 deletion src/language/ru-RU.json
Original file line number Diff line number Diff line change
Expand Up @@ -1132,5 +1132,6 @@
"transaction_cosignature_warning_proceed": "Я понимаю и хочу продолжить.",
"lock_hash_algorithm": "алгоритм хеширования",
"not_enough_balance": "не хватает баланса",
"use_max_value": "Используйте максимальное значение"
"use_max_value": "Используйте максимальное значение",
"amount_value_cannot_start_with_zero": "Значение суммы не может начинаться с 0"
}
3 changes: 2 additions & 1 deletion src/language/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -1132,5 +1132,6 @@
"transaction_cosignature_warning_proceed": "我理解并请继续。",
"lock_hash_algorithm": "哈希算法",
"not_enough_balance": "余额不足",
"use_maximum_value": "使用最大值"
"use_maximum_value": "使用最大值",
"amount_value_cannot_start_with_zero": "金额值不能以 0 开头"
}
16 changes: 9 additions & 7 deletions src/store/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ export default {
if (!address) {
throw new Error('Address must be provided when calling account/SET_CURRENT_SIGNER!');
}
const isOffline: boolean = rootGetters['network/isOfflineMode'];
const currentProfile: ProfileModel = rootGetters['profile/currentProfile'];
const currentAccount: AccountModel = getters.currentAccount;
const previousSignerAddress: Address = getters.currentSignerAddress;
Expand Down Expand Up @@ -426,14 +427,15 @@ export default {
dispatch('mosaic/SIGNER_CHANGED', {}, { root: true });
dispatch('transaction/SIGNER_CHANGED', {}, { root: true });
dispatch('metadata/SIGNER_CHANGED', {}, { root: true });
dispatch('harvesting/SET_CURRENT_SIGNER_HARVESTING_MODEL', currentSignerAddress.plain(), { root: true });
const networkType = rootGetters['network/networkType'];
const nodeService = new NodeService();
const nodes = await nodeService.getNodesFromStatisticService(networkType);
if (nodes && nodes.length && navigator.onLine) {
dispatch('harvesting/LOAD_HARVESTED_BLOCKS_STATS', {}, { root: true });
if (!isOffline) {
dispatch('harvesting/SET_CURRENT_SIGNER_HARVESTING_MODEL', currentSignerAddress.plain(), { root: true });
const networkType = rootGetters['network/networkType'];
const nodeService = new NodeService();
const nodes = await nodeService.getNodesFromStatisticService(networkType);
if (nodes && nodes.length && navigator.onLine) {
dispatch('harvesting/LOAD_HARVESTED_BLOCKS_STATS', {}, { root: true });
}
}

if (unsubscribeWS) {
if (previousSignerAddress) {
await dispatch('UNSUBSCRIBE', previousSignerAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import { MosaicModel } from '@/core/database/entities/MosaicModel';
// @ts-ignore
import MosaicSelector from '@/components/MosaicSelector/MosaicSelector.vue';
import { Formatters } from '@/core/utils/Formatters';

import { NetworkConfigurationModel } from '@/core/database/entities/NetworkConfigurationModel';
import { MosaicService } from '@/services/MosaicService';
@Component({
components: {
ValidationObserver,
Expand All @@ -60,7 +61,14 @@ import { Formatters } from '@/core/utils/Formatters';
MaxFeeAndSubmit,
MosaicSelector,
},
computed: { ...mapGetters({ mosaics: 'mosaic/mosaics', holdMosaics: 'mosaic/holdMosaics' }) },
computed: {
...mapGetters({
mosaics: 'mosaic/mosaics',
holdMosaics: 'mosaic/holdMosaics',
currentHeight: 'network/currentHeight',
networkConfiguration: 'network/networkConfiguration',
}),
},
})
export class FormMosaicSupplyChangeTransactionTs extends FormTransactionBase {
/**
Expand Down Expand Up @@ -110,6 +118,15 @@ export class FormMosaicSupplyChangeTransactionTs extends FormTransactionBase {
* @protected
* @var {Record<string, any>}
*/
/**
* Current network block height
*/
private currentHeight: number;
/**
* Network Configs
* @protected
*/
public networkConfiguration: NetworkConfigurationModel;
protected formItems = {
mosaicHexId: null,
action: null,
Expand Down Expand Up @@ -248,6 +265,14 @@ export class FormMosaicSupplyChangeTransactionTs extends FormTransactionBase {
get ownedTargetHexIds(): string[] {
return this.holdMosaics
.filter((m) => m.ownerRawPlain === this.currentAccount.address && m.supplyMutable)
.filter((entry) => {
const expiration = MosaicService.getExpiration(
entry,
this.currentHeight,
this.networkConfiguration.blockGenerationTargetTime,
);
return expiration !== 'expired';
})
.map(({ mosaicIdHex }) => mosaicIdHex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class FormOfflineTransferTransactionTs extends Vue {
* Hook called when the page is mounted
* @return {void}
*/
public created() {
public async created() {
// filter out invalid profiles
this.profiles = this.profileService.getProfiles().filter((p) => p.accounts.length > 0);

Expand All @@ -155,7 +155,7 @@ export class FormOfflineTransferTransactionTs extends Vue {

// accounts available, iterate to first profile
this.formItems.currentProfileName = this.profiles[0].profileName;
this.onProfileNameChange();
await this.onProfileNameChange();
}

/**
Expand Down

0 comments on commit a112dc7

Please sign in to comment.