-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathdelegate-validate-model.ts
More file actions
138 lines (128 loc) · 4.23 KB
/
delegate-validate-model.ts
File metadata and controls
138 lines (128 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { type ApiPromise } from '@polkadot/api';
import { BN, BN_ZERO } from '@polkadot/util';
import { type Store, attach, createEffect, sample } from 'effector';
import { type Asset, type BalanceMap, type Chain, type ID, type Transaction } from '@/shared/core';
import { getAssetById, getNativeAsset, transferableAmount } from '@/shared/lib/utils';
import { balanceModel, balanceUtils } from '@/entities/balance';
import { networkModel } from '@/entities/network';
import { transactionService } from '@/entities/transaction';
import { lockPeriodsModel } from '@/features/governance/model/lockPeriods';
import { type BalanceMap as TransferBalanceMap, type NetworkStore } from '@/features/transfer';
import { DelegateRules } from '../lib/delegate-rules';
import { validationUtils } from '../lib/validation-utils';
import {
type DelegateFeeStore,
type FeeMap,
type TransferAccountStore,
type TransferSignatoryFeeStore,
type ValidationStartedParams,
} from '../types/types';
type ValidateParams = {
id: ID;
api: ApiPromise;
chain: Chain;
asset: Asset;
transaction: Transaction;
balances: BalanceMap;
feeMap: FeeMap;
};
const rootValidateFx = createEffect(
async ({ id, api, chain, asset, transaction, balances, feeMap }: ValidateParams) => {
const accountId = transaction.accountId;
const fee =
feeMap?.[chain.chainId]?.[transaction.type] || (await transactionService.getTransactionFee(transaction, api));
const rules = [
{
value: transaction.accountId,
form: {},
...DelegateRules.account.noProxyFee({} as Store<TransferAccountStore>),
source: {
fee,
// TODO: Add support proxy
isProxy: false,
proxyBalance: { native: '0' },
},
},
{
value: undefined,
form: {},
...DelegateRules.signatory.notEnoughTokens({} as Store<TransferSignatoryFeeStore>),
source: {
fee: new BN(fee),
isMultisig: false,
multisigDeposit: BN_ZERO,
balance: '0',
} as TransferSignatoryFeeStore,
},
{
value: transaction.args.balance,
form: {},
...DelegateRules.amount.notEnoughBalance(
{} as Store<{ network: NetworkStore | null; balance: TransferBalanceMap }>,
{
withFormatAmount: false,
},
),
source: {
network: { chain: chain, asset: asset },
balance: {
native: transferableAmount(
balanceUtils.getBalance(balances, accountId, chain.chainId, getNativeAsset(chain.assets).assetId),
),
balance: transferableAmount(balanceUtils.getBalance(balances, accountId, chain.chainId, asset.assetId)),
},
} as { network: NetworkStore | null; balance: TransferBalanceMap },
},
{
value: transaction.args.value,
form: {},
...DelegateRules.amount.insufficientBalanceForFee({} as Store<DelegateFeeStore>, {
withFormatAmount: false,
}),
source: {
network: { chain, asset },
fee,
isMultisig: false,
// TODO: Add support proxy
balance: {
native: transferableAmount(
balanceUtils.getBalance(balances, accountId, chain.chainId, getNativeAsset(chain.assets).assetId),
),
},
} as DelegateFeeStore,
},
];
return { id, result: validationUtils.applyValidationRules(rules) };
},
);
const validateFx = attach({
source: {
chains: networkModel.$chains,
apis: networkModel.$apis,
balances: balanceModel.$balanceMap,
},
async effect({ chains, balances, apis }, { id, transaction, feeMap }: ValidationStartedParams) {
const chain = chains[transaction.chainId];
const api = apis[transaction.chainId];
if (!chain || !api) {
return { id, result: undefined };
}
const asset = getAssetById(transaction.args.asset, chain.assets) || getNativeAsset(chain.assets);
return rootValidateFx({
id,
api,
transaction,
chain,
asset,
balances,
feeMap,
});
},
});
sample({
clock: rootValidateFx,
target: lockPeriodsModel.events.requestLockPeriods,
});
export const delegateValidateModel = {
validate: validateFx,
};