Skip to content

Commit

Permalink
fix: issues OK-24057 OK-23872 OK-23811 OK-23703 OK-23679 (#3726)
Browse files Browse the repository at this point in the history
* fix: stc fee info warning message

* chore: update cross in page packages

* fix: enable send action when transferable is 0 but available is not

* fix: brc20 action disabled state

* fix: dapp connection aborted when no account
  • Loading branch information
weatherstar authored Oct 26, 2023
1 parent b3da98b commit 38771de
Show file tree
Hide file tree
Showing 9 changed files with 429 additions and 239 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@
"@cmdcode/crypto-utils": "1.9.5",
"@cmdcode/tapscript": "1.2.9",
"@legendapp/state": "^1.2.9",
"@onekeyfe/cross-inpage-provider-core": "1.1.42",
"@onekeyfe/cross-inpage-provider-errors": "1.1.42",
"@onekeyfe/cross-inpage-provider-injected": "1.1.42",
"@onekeyfe/cross-inpage-provider-types": "1.1.42",
"@onekeyfe/extension-bridge-hosted": "1.1.42",
"@onekeyfe/cross-inpage-provider-core": "1.1.43",
"@onekeyfe/cross-inpage-provider-errors": "1.1.43",
"@onekeyfe/cross-inpage-provider-injected": "1.1.43",
"@onekeyfe/cross-inpage-provider-types": "1.1.43",
"@onekeyfe/extension-bridge-hosted": "1.1.43",
"@onekeyfe/hd-ble-sdk": "0.3.30",
"@onekeyfe/hd-core": "0.3.30",
"@onekeyfe/hd-shared": "0.3.30",
"@onekeyfe/hd-transport": "0.3.30",
"@onekeyfe/hd-web-sdk": "0.3.30",
"@onekeyfe/onekey-cross-webview": "1.1.42",
"@onekeyfe/onekey-cross-webview": "1.1.43",
"@starcoin/starcoin": "2.1.5",
"@web3-react/core": "8.0.35-beta.0",
"@web3-react/empty": "8.0.20-beta.0",
Expand Down
70 changes: 69 additions & 1 deletion packages/engine/src/vaults/impl/sol/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import {
KeyringImported,
KeyringWatching,
} from './keyring';
import { ClientSol } from './sdk';
import { ClientSol, PARAMS_ENCODINGS } from './sdk';
import settings from './settings';

import type { DBAccount, DBSimpleAccount } from '../../../types/account';
Expand All @@ -78,6 +78,7 @@ import type { TransactionStatus } from '../../../types/provider';
import type { KeyringSoftwareBase } from '../../keyring/KeyringSoftwareBase';
import type {
IApproveInfo,
IBalanceDetails,
IDecodedTx,
IDecodedTxAction,
IDecodedTxLegacy,
Expand All @@ -98,6 +99,7 @@ import type {
ParsedAccountInfo,
} from './types';
import type { IJsonRpcRequest } from '@onekeyfe/cross-inpage-provider-types';
import type { AccountInfo } from '@solana/web3.js';

export default class Vault extends VaultBase {
keyringMap = {
Expand Down Expand Up @@ -127,6 +129,31 @@ export default class Vault extends VaultBase {
return this.createClientFromURL(rpcURL);
}

private getMinimumBalanceForRentExemption = memoizee(
async (address): Promise<number> => {
const client = await this.getClient();
const accountInfo =
(await client.getAccountInfo(address, PARAMS_ENCODINGS.BASE64)) ?? {};

const accountData = (accountInfo as AccountInfo<[string, string]>)
.data[0];

const accountDataLength = Buffer.from(
accountData,
PARAMS_ENCODINGS.BASE64,
).length;
const minimumBalanceForRentExemption =
await client.getMinimumBalanceForRentExemption(accountDataLength);
return minimumBalanceForRentExemption;
},
{
promise: true,
primitive: true,
max: 50,
maxAge: getTimeDurationMs({ minute: 3 }),
},
);

private getAssociatedAccountInfo = memoizee(
async (ataAddress): Promise<AssociatedTokenInfo> => {
const client = await this.getClient();
Expand Down Expand Up @@ -837,6 +864,47 @@ export default class Vault extends VaultBase {
};
}

override async getFrozenBalance(): Promise<number | Record<string, number>> {
const address = await this.getAccountAddress();
const { decimals } = await this.engine.getNativeTokenInfo(this.networkId);
try {
const minimumBalance = await this.getMinimumBalanceForRentExemption(
address,
);

return {
'main': new BigNumber(minimumBalance ?? 0)
.shiftedBy(-decimals)
.toNumber(),
};
} catch {
return 0;
}
}

override async fetchBalanceDetails(): Promise<IBalanceDetails | undefined> {
const { decimals } = await this.engine.getNativeTokenInfo(this.networkId);
const address = await this.getAccountAddress();
const [[nativeTokenBalance], rent] = await Promise.all([
this.getBalances([{ address }]),
this.getFrozenBalance(),
]);

const rentBalance = new BigNumber((rent as { main: number }).main ?? '0');
const totalBalance = new BigNumber(nativeTokenBalance ?? '0').shiftedBy(
-decimals,
);

const availableBalance = totalBalance.minus(rentBalance);
return {
total: totalBalance.toFixed(),
available: availableBalance.isGreaterThan(0)
? availableBalance.toFixed()
: '0',
unavailable: rentBalance.toFixed(),
};
}

override async getFeePricePerUnit(): Promise<FeePricePerUnit> {
const client = await this.getClient();
return client.getFeePricePerUnit();
Expand Down
13 changes: 12 additions & 1 deletion packages/engine/src/vaults/impl/sol/sdk/ClientSol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export enum RPC_METHODS {
GET_TOKEN_ACCOUNTS_BY_OWNER = 'getTokenAccountsByOwner',
GET_FEES = 'getFees',
GET_TRANSACTION = 'getTransaction',
GET_MINIMUM_BALANCE_FOR_RENT_EXEMPTION = 'getMinimumBalanceForRentExemption',
}
// eslint-disable-next-line @typescript-eslint/naming-convention
export enum PARAMS_ENCODINGS {
Expand Down Expand Up @@ -147,10 +148,11 @@ export class ClientSol extends BaseClient {

async getAccountInfo(
address: string,
encoding: PARAMS_ENCODINGS = PARAMS_ENCODINGS.JSON_PARSED,
): Promise<{ [key: string]: any } | null> {
const response: { [key: string]: any } = await this.rpc.call(
RPC_METHODS.GET_ACCOUNT_INFO,
[address, { encoding: PARAMS_ENCODINGS.JSON_PARSED }],
[address, { encoding }],
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return response.value;
Expand Down Expand Up @@ -248,6 +250,15 @@ export class ClientSol extends BaseClient {
return result;
}

async getMinimumBalanceForRentExemption(dataLength: number): Promise<number> {
const response: number = await this.rpc.call(
RPC_METHODS.GET_MINIMUM_BALANCE_FOR_RENT_EXEMPTION,
[dataLength],
);

return response;
}

override getTokenInfos = async (
tokenAddresses: Array<string>,
): Promise<Array<PartialTokenInfo | undefined>> => {
Expand Down
2 changes: 2 additions & 0 deletions packages/engine/src/vaults/impl/stc/Vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,8 @@ export default class Vault extends VaultBase {
if (!encodedTx.data) {
// Native STC transfer, give a default limit.
limit = DEFAULT_GAS_LIMIT_NATIVE_TRANSFER;
} else {
throw new OneKeyInternalError('', 'msg__broadcast_tx_Insufficient_fee');
}
}

Expand Down
54 changes: 52 additions & 2 deletions packages/kit/src/views/DappModals/Connection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useIntl } from 'react-intl';

import {
Box,
Button,
Center,
Empty,
HStack,
Expand All @@ -30,6 +31,10 @@ import platformEnv from '@onekeyhq/shared/src/platformEnv';
import type { IDappSourceInfo } from '@onekeyhq/shared/types';

import backgroundApiProxy from '../../background/instance/backgroundApiProxy';
import {
NETWORK_NOT_SUPPORT_CREATE_ACCOUNT_I18N_KEY,
useCreateAccountInWallet,
} from '../../components/NetworkAccountSelector/hooks/useCreateAccountInWallet';
import walletConnectUtils from '../../components/WalletConnect/utils/walletConnectUtils';
import { useActiveWalletAccount, useAppSelector } from '../../hooks';
import useDappApproveAction from '../../hooks/useDappApproveAction';
Expand Down Expand Up @@ -81,16 +86,22 @@ function ConnectionContent({
origin,
hostname,
network,
walletId,
}: {
account: IAccount | null | undefined;
network: INetwork | null;
walletId: string;
isWalletConnectPreloading: boolean;
walletConnectError: string;
getWalletConnectBridge: () => string;
origin: string;
hostname: string;
}) {
const intl = useIntl();
const { createAccount, isCreateAccountSupported } = useCreateAccountInWallet({
walletId,
networkId: network?.id,
});
if (isWalletConnectPreloading) {
return (
<Center flex={1} minH="300px">
Expand Down Expand Up @@ -128,6 +139,38 @@ function ConnectionContent({
id: 'empty__no_account_title',
})}
/>
<Box
position="relative"
w={{ md: 'full' }}
alignItems="center"
h="56px"
justifyContent="center"
>
<Button
leftIconName={
isCreateAccountSupported ? 'PlusOutline' : 'BanOutline'
}
type="primary"
onPress={() => {
if (isCreateAccountSupported) {
// ** createAccount for current wallet directly
createAccount();
} else {
ToastManager.show({
title: intl.formatMessage(
{
id: NETWORK_NOT_SUPPORT_CREATE_ACCOUNT_I18N_KEY,
},
{ 0: network?.shortName },
),
});
}
}}
size="lg"
>
{intl.formatMessage({ id: 'action__create_account' })}
</Button>
</Box>
</Center>
);
}
Expand Down Expand Up @@ -245,8 +288,14 @@ const Connection = () => {
const [rugConfirmDialogVisible, setRugConfirmDialogVisible] = useState(false);
const intl = useIntl();

const { networkImpl, network, accountAddress, accountPubKey, account } =
useActiveWalletAccount();
const {
networkImpl,
network,
accountAddress,
accountPubKey,
account,
walletId,
} = useActiveWalletAccount();

const { sourceInfo } = useDappParams();
const { origin, scope, id } = sourceInfo ?? defaultSourceInfo;
Expand Down Expand Up @@ -465,6 +514,7 @@ const Connection = () => {
getWalletConnectBridge={getWalletConnectBridge}
network={network}
account={account}
walletId={walletId}
hostname={hostname}
origin={origin}
/>
Expand Down
Loading

0 comments on commit 38771de

Please sign in to comment.