Skip to content

Commit 258dcc5

Browse files
This commit only for dev
1 parent 44cc52f commit 258dcc5

File tree

9 files changed

+164
-48
lines changed

9 files changed

+164
-48
lines changed

packages/extension-base/src/koni/background/handlers/Extension.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2017,7 +2017,7 @@ export default class KoniExtension {
20172017
}
20182018

20192019
private async subscribeMaxTransferable (request: RequestSubscribeTransfer, id: string, port: chrome.runtime.Port): Promise<ResponseSubscribeTransfer> {
2020-
const { address, chain, destChain: _destChain, feeCustom, feeOption, token, tokenPayFeeSlug, value } = request;
2020+
const { address, chain, destChain: _destChain, feeCustom, feeOption, to, token, tokenPayFeeSlug, transferAll, value } = request;
20212021
const cb = createSubscription<'pri(transfer.subscribe)'>(id, port);
20222022

20232023
const transferTokenInfo = this.#koniState.chainService.getAssetBySlug(token);
@@ -2040,6 +2040,7 @@ export default class KoniExtension {
20402040

20412041
const _request: CalculateMaxTransferable = {
20422042
address: address,
2043+
to: to,
20432044
value, // todo: lazy subscribe to improve performance
20442045
cardanoApi: this.#koniState.chainService.getCardanoApi(chain),
20452046
destChain,

packages/extension-base/src/services/transaction-service/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { EvmProviderError } from '@subwallet/extension-base/background/errors/Ev
55
import { TransactionError } from '@subwallet/extension-base/background/errors/TransactionError';
66
import { AmountData, BitcoinSignatureRequest, ChainType, EvmProviderErrorType, EvmSendTransactionRequest, EvmSignatureRequest, ExtrinsicStatus, ExtrinsicType, NotificationType, TransactionAdditionalInfo, TransactionDirection, TransactionHistoryItem } from '@subwallet/extension-base/background/KoniTypes';
77
import { _SUPPORT_TOKEN_PAY_FEE_GROUP, ALL_ACCOUNT_KEY, fetchBlockedConfigObjects, fetchLatestBlockedActionsAndFeatures, getPassConfigId } from '@subwallet/extension-base/constants';
8-
import { checkBalanceWithTransactionFee, checkSupportForAction, checkSupportForFeature, checkSupportForTransaction, estimateFeeForTransaction } from '@subwallet/extension-base/core/logic-validation/transfer';
8+
import { checkBalanceWithTransactionFee, checkSigningAccountForTransaction, checkSupportForAction, checkSupportForFeature, checkSupportForTransaction, estimateFeeForTransaction } from '@subwallet/extension-base/core/logic-validation/transfer';
99
import KoniState from '@subwallet/extension-base/koni/background/handlers/State';
1010
import { cellToBase64Str, externalMessage, getTransferCellPromise } from '@subwallet/extension-base/services/balance-service/helpers/subscribe/ton/utils';
1111
import { CardanoTransactionConfig } from '@subwallet/extension-base/services/balance-service/transfer/cardano-transfer';
@@ -169,15 +169,14 @@ export default class TransactionService {
169169

170170
// Check account signing transaction
171171

172-
// TODO: If you want to test, remove the line below.
173172
// checkSigningAccountForTransaction(validationResponse, chainInfoMap);
174173

175174
const nativeTokenAvailable = await this.state.balanceService.getTransferableBalance(address, chain, nativeTokenInfo.slug, extrinsicType);
176175

177176
// Check available balance against transaction fee
178177
checkBalanceWithTransactionFee(validationResponse, transactionInput, nativeTokenInfo, nativeTokenAvailable);
179178

180-
// Warnings Ton address if bounceable and not active
179+
// Warnings Ton address if bounceable and not active`
181180
// if (transaction && isTonTransaction(transaction) && tonApi) {
182181
// await checkTonAddressBounceableAndAccountNotActive(tonApi, validationResponse);
183182
// }

packages/extension-base/src/types/balance/transfer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export interface RequestSubscribeTransfer extends TransactionFee {
1313
value: string;
1414
token: string;
1515
destChain: string;
16+
transferAll?: boolean;
17+
to?: string;
1618
}
1719

1820
export interface ResponseSubscribeTransfer {

packages/extension-base/src/utils/account/transform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const getAccountSignMode = (address: string, _meta?: KeyringPair$Meta): A
9393
return AccountSignMode.LEGACY_LEDGER;
9494
}
9595
} else if (meta.isReadOnly) {
96-
return AccountSignMode.READ_ONLY;
96+
return AccountSignMode.QR;
9797
} else {
9898
return AccountSignMode.QR;
9999
}

packages/extension-base/src/utils/bitcoin/utxo-management.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ export function filterUneconomicalUtxos ({ feeRate,
4747
sender
4848
});
4949

50-
console.log(utxosWithout, feeWithout, spendableAmountWithout.toString());
51-
console.log(utxos, fee, spendableAmount.toString());
50+
console.log('utxosWithout', utxosWithout);
51+
console.log('spendableAmountWithout.toString()', utxosWithout, feeWithout, spendableAmountWithout.toString());
52+
console.log('feeWithout', feeWithout);
53+
console.log('utxos', utxos);
54+
console.log('fee', fee);
55+
console.log('spendableAmount', spendableAmount.toString());
5256

5357
if (spendableAmount.lte(0)) {
5458
return utxosWithout;

packages/extension-base/src/utils/fee/transfer.ts

Lines changed: 111 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { AmountData } from '@subwallet/extension-base/background/KoniTypes';
66
import { _SUPPORT_TOKEN_PAY_FEE_GROUP, XCM_FEE_RATIO } from '@subwallet/extension-base/constants';
77
import { _isSnowBridgeXcm } from '@subwallet/extension-base/core/substrate/xcm-parser';
88
import { DEFAULT_CARDANO_TTL_OFFSET } from '@subwallet/extension-base/services/balance-service/helpers/subscribe/cardano/consts';
9+
import { createBitcoinTransaction } from '@subwallet/extension-base/services/balance-service/transfer/bitcoin-transfer';
910
import { createCardanoTransaction } from '@subwallet/extension-base/services/balance-service/transfer/cardano-transfer';
1011
import { getERC20TransactionObject, getEVMTransactionObject } from '@subwallet/extension-base/services/balance-service/transfer/smart-contract';
1112
import { createSubstrateExtrinsic } from '@subwallet/extension-base/services/balance-service/transfer/token';
@@ -21,11 +22,13 @@ import { calculateToAmountByReservePool, FEE_COVERAGE_PERCENTAGE_SPECIAL_CASE }
2122
import { getHydrationRate } from '@subwallet/extension-base/services/fee-service/utils/tokenPayFee';
2223
import { isCardanoTransaction, isTonTransaction } from '@subwallet/extension-base/services/transaction-service/helpers';
2324
import { ValidateTransactionResponseInput } from '@subwallet/extension-base/services/transaction-service/types';
24-
import { EvmEIP1559FeeOption, FeeChainType, FeeDetail, FeeInfo, SubstrateTipInfo, TransactionFee } from '@subwallet/extension-base/types';
25+
import { BitcoinFeeRate, DetermineUtxosForSpendArgs, EvmEIP1559FeeOption, FeeChainType, FeeDetail, FeeInfo, SubstrateTipInfo, TransactionFee } from '@subwallet/extension-base/types';
2526
import { ResponseSubscribeTransfer } from '@subwallet/extension-base/types/balance/transfer';
26-
import { BN_ZERO } from '@subwallet/extension-base/utils';
27+
import { BN_ZERO, combineBitcoinFee, determineUtxosForSpend, determineUtxosForSpendAll, filterUneconomicalUtxos, getSizeInfo, getTransferableBitcoinUtxos } from '@subwallet/extension-base/utils';
2728
import { isCardanoAddress, isTonAddress } from '@subwallet/keyring';
29+
import { isBitcoinAddress } from '@subwallet/keyring/utils/address/validate';
2830
import BigN from 'bignumber.js';
31+
import { bitcoin, testnet } from 'bitcoinjs-lib/src/networks';
2932
import { TransactionConfig } from 'web3-core';
3033

3134
import { SubmittableExtrinsic } from '@polkadot/api/types';
@@ -36,6 +39,7 @@ import { combineEthFee, combineSubstrateFee } from './combine';
3639

3740
export interface CalculateMaxTransferable extends TransactionFee {
3841
address: string;
42+
to?: string;
3943
value: string;
4044
srcToken: _ChainAsset;
4145
destToken?: _ChainAsset;
@@ -101,7 +105,7 @@ export const calculateMaxTransferable = async (id: string, request: CalculateMax
101105
};
102106

103107
export const calculateTransferMaxTransferable = async (id: string, request: CalculateMaxTransferable, freeBalance: AmountData, fee: FeeInfo): Promise<ResponseSubscribeTransfer> => {
104-
const { address, bitcoinApi, cardanoApi, destChain, evmApi, feeCustom, feeOption, isTransferLocalTokenAndPayThatTokenAsFee, isTransferNativeTokenAndPayLocalTokenAsFee, nativeToken, srcChain, srcToken, substrateApi, tonApi, value } = request;
108+
const { address, bitcoinApi, cardanoApi, destChain, evmApi, feeCustom, feeOption, isTransferLocalTokenAndPayThatTokenAsFee, isTransferNativeTokenAndPayLocalTokenAsFee, nativeToken, srcChain, srcToken, substrateApi, to, tonApi, value } = request;
105109
const feeChainType = fee.type;
106110
let estimatedFee: string;
107111
let feeOptions: FeeDetail;
@@ -170,7 +174,20 @@ export const calculateTransferMaxTransferable = async (id: string, request: Calc
170174
cardanoApi,
171175
nativeTokenInfo: nativeToken
172176
});
173-
} else { // TODO: Support maxTransferable for bitcoin
177+
} else if (isBitcoinAddress(address) && _isTokenTransferredByBitcoin(srcToken)) {
178+
const network = srcChain.isTestnet ? testnet : bitcoin;
179+
180+
[transaction] = await createBitcoinTransaction({
181+
from: address,
182+
to: address,
183+
chain: srcChain.slug,
184+
value,
185+
feeInfo: fee,
186+
transferAll: false,
187+
bitcoinApi,
188+
network
189+
});
190+
} else {
174191
[transaction] = await createSubstrateExtrinsic({
175192
transferAll: false,
176193
value,
@@ -223,8 +240,98 @@ export const calculateTransferMaxTransferable = async (id: string, request: Calc
223240
...fee,
224241
estimatedFee
225242
};
243+
} else if (feeChainType === 'bitcoin') {
244+
const _fee = fee;
245+
const _feeCustom = feeCustom as BitcoinFeeRate;
246+
const combineFee = combineBitcoinFee(_fee, feeOption, _feeCustom);
247+
const utxos = await getTransferableBitcoinUtxos(bitcoinApi, address);
248+
const determineUtxosArgs: DetermineUtxosForSpendArgs = {
249+
amount: parseInt(value || '0'),
250+
feeRate: combineFee.feeRate,
251+
recipient: to || address,
252+
sender: address,
253+
utxos
254+
};
255+
256+
console.log('maxTransfer, utxos', utxos);
257+
console.log('maxTransfer, determineUtxosArgs', determineUtxosArgs);
258+
259+
const fallbackCalculate = (recipients: string[]) => {
260+
const utxos = filterUneconomicalUtxos({
261+
utxos: determineUtxosArgs.utxos,
262+
feeRate: determineUtxosArgs.feeRate,
263+
recipients,
264+
sender: determineUtxosArgs.sender
265+
});
266+
267+
const { txVBytes: vSize } = getSizeInfo({
268+
inputLength: utxos.length || 1,
269+
sender: address,
270+
recipients
271+
});
272+
273+
return {
274+
vSize,
275+
maxTransferable: utxos.reduce((previous, input) => previous.plus(input.value), new BigN(0)),
276+
estimatedFee: Math.ceil(determineUtxosArgs.feeRate * vSize).toString()
277+
};
278+
};
279+
280+
try {
281+
const { fee: _estimatedFee, inputs } = false ? determineUtxosForSpendAll(determineUtxosArgs) : determineUtxosForSpend(determineUtxosArgs);
282+
283+
maxTransferable = inputs.reduce((previous, input) => previous.plus(input.value), new BigN(0));
284+
285+
const { txVBytes: vSize } = getSizeInfo({
286+
inputLength: inputs.length,
287+
sender: address,
288+
recipients
289+
});
290+
291+
estimatedFee = new BigN(_estimatedFee).toFixed(0);
292+
feeOptions = {
293+
..._fee,
294+
estimatedFee,
295+
vSize
296+
};
297+
298+
if (transferAll) {
299+
estimatedFeeMax = new BigN(_estimatedFee).toFixed(0);
300+
} else {
301+
try {
302+
const { fee: estimateFeeMax, inputs } = determineUtxosForSpendAll(determineUtxosArgs);
303+
304+
maxTransferable = inputs.reduce((previous, input) => previous.plus(input.value), new BigN(0));
305+
estimatedFeeMax = new BigN(estimateFeeMax).toFixed(0);
306+
} catch (_e) {
307+
const fb = fallbackCalculate([to || address]);
308+
309+
maxTransferable = fb.maxTransferable;
310+
estimatedFeeMax = fb.estimatedFee;
311+
}
312+
}
313+
} catch (_e) {
314+
const fb = fallbackCalculate([to || address]);
315+
316+
maxTransferable = fb.maxTransferable;
317+
estimatedFeeMax = fb.estimatedFee;
318+
319+
if (!feeOptions) {
320+
const fb = fallbackCalculate([address, to || address]);
321+
322+
estimatedFee = fb.estimatedFee;
323+
324+
feeOptions = {
325+
..._fee,
326+
estimatedFee,
327+
vSize: fb.vSize
328+
};
329+
}
330+
}
226331
} else {
227332
if (transaction) {
333+
console.log('transaction', transaction);
334+
228335
if (isTonTransaction(transaction)) {
229336
estimatedFee = transaction.estimateFee;
230337
feeOptions = {

packages/extension-koni-ui/src/Popup/Home/Tokens/index.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,19 +207,21 @@ const Component = (): React.ReactElement => {
207207
}, [navigate]);
208208

209209
const onOpenSendFund = useCallback(() => {
210+
console.log('currentAccountProxy', currentAccountProxy);
211+
210212
if (!currentAccountProxy) {
211213
return;
212214
}
213215

214-
if (currentAccountProxy.accountType === AccountProxyType.READ_ONLY) {
215-
notify({
216-
message: t('The account you are using is watch-only, you cannot send assets with it'),
217-
type: 'info',
218-
duration: 3
219-
});
220-
221-
return;
222-
}
216+
// if (currentAccountProxy.accountType === AccountProxyType.READ_ONLY) {
217+
// notify({
218+
// message: t('The account you are using is watch-only, you cannot send assets with it'),
219+
// type: 'info',
220+
// duration: 3
221+
// });
222+
//
223+
// return;
224+
// }
223225

224226
setStorage({
225227
...DEFAULT_TRANSFER_PARAMS,

packages/extension-koni-ui/src/Popup/Transaction/variants/SendFund.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ const Component = ({ className = '', isAllAccount, targetAccountProxy }: Compone
846846
if (fromValue && assetValue) {
847847
subscribeMaxTransfer({
848848
address: fromValue,
849+
to: toValue,
849850
chain: assetRegistry[assetValue].originChain,
850851
token: assetValue,
851852
value: transferAmountValue,
@@ -870,7 +871,7 @@ const Component = ({ className = '', isAllAccount, targetAccountProxy }: Compone
870871
cancel = true;
871872
id && cancelSubscription(id).catch(console.error);
872873
};
873-
}, [assetValue, assetRegistry, chainValue, chainStatus, form, fromValue, destChainValue, selectedTransactionFee, nativeTokenSlug, currentTokenPayFee, transferAmountValue]);
874+
}, [assetValue, assetRegistry, chainValue, chainStatus, form, fromValue, destChainValue, selectedTransactionFee, nativeTokenSlug, currentTokenPayFee, transferAmountValue, toValue]);
874875

875876
useEffect(() => {
876877
const bnTransferAmount = new BN(transferAmountValue || '0');
@@ -962,7 +963,7 @@ const Component = ({ className = '', isAllAccount, targetAccountProxy }: Compone
962963
}, [chainValue, fromValue, nativeTokenBalance, nativeTokenSlug]);
963964

964965
useEffect(() => {
965-
console.log('transferInfo', transferInfo);
966+
console.log('[TESTER] transferInfo', transferInfo);
966967
}, [transferInfo]);
967968

968969
useRestoreTransaction(form);

packages/extension-koni-ui/src/hooks/account/usePreCheckAction.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,32 +55,32 @@ const usePreCheckAction = (address?: string, blockAllAccount = true, message?: s
5555
defaultMessage = detectTranslate('You are using a {{accountTitle}}. Earning is not supported with this account type');
5656
}
5757

58-
if (!account.transactionActions.includes(action) || (mode === AccountSignMode.QR && account.chainType === 'ethereum' && isProductionMode)) {
59-
block = true;
60-
61-
switch (mode) {
62-
case AccountSignMode.ALL_ACCOUNT:
63-
if (!blockAllAccount) {
64-
block = false;
65-
}
66-
67-
break;
68-
69-
case AccountSignMode.QR:
70-
accountTitle = t('EVM QR signer account');
71-
break;
72-
73-
case AccountSignMode.LEGACY_LEDGER:
74-
case AccountSignMode.GENERIC_LEDGER:
75-
if (account.chainType === AccountChainType.ETHEREUM) {
76-
accountTitle = t('Ledger - EVM account');
77-
} else if (account.chainType === AccountChainType.SUBSTRATE) {
78-
accountTitle = t('Ledger - Substrate account');
79-
}
80-
81-
break;
82-
}
83-
}
58+
// if (!account.transactionActions.includes(action) || (mode === AccountSignMode.QR && account.chainType === 'ethereum' && isProductionMode)) {
59+
// block = true;
60+
//
61+
// switch (mode) {
62+
// case AccountSignMode.ALL_ACCOUNT:
63+
// if (!blockAllAccount) {
64+
// block = false;
65+
// }
66+
//
67+
// break;
68+
//
69+
// case AccountSignMode.QR:
70+
// accountTitle = t('EVM QR signer account');
71+
// break;
72+
//
73+
// case AccountSignMode.LEGACY_LEDGER:
74+
// case AccountSignMode.GENERIC_LEDGER:
75+
// if (account.chainType === AccountChainType.ETHEREUM) {
76+
// accountTitle = t('Ledger - EVM account');
77+
// } else if (account.chainType === AccountChainType.SUBSTRATE) {
78+
// accountTitle = t('Ledger - Substrate account');
79+
// }
80+
//
81+
// break;
82+
// }
83+
// }
8484

8585
if (mode === AccountSignMode.LEGACY_LEDGER || mode === AccountSignMode.GENERIC_LEDGER) {
8686
if (!isLedgerCapable) {

0 commit comments

Comments
 (0)