Skip to content

Commit

Permalink
chore: another sdk update
Browse files Browse the repository at this point in the history
  • Loading branch information
meeh0w committed Oct 11, 2024
1 parent 4a57a94 commit bf64965
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 130 deletions.
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@
"sentry": "node sentryscript.js"
},
"dependencies": {
"@avalabs/avalanche-module": "0.8.0",
"@avalabs/avalanche-module": "0.0.0-feat-bitcoin--just-sign-tx-20241011064558",
"@avalabs/avalanchejs": "4.0.5",
"@avalabs/bitcoin-module": "0.8.0",
"@avalabs/bitcoin-module": "0.0.0-feat-bitcoin--just-sign-tx-20241011064558",
"@avalabs/bridge-unified": "0.0.0-feat-ictt-configs-20241009072139",
"@avalabs/core-bridge-sdk": "3.1.0-alpha.7",
"@avalabs/core-chains-sdk": "3.1.0-alpha.7",
"@avalabs/core-coingecko-sdk": "3.1.0-alpha.7",
"@avalabs/core-covalent-sdk": "3.1.0-alpha.7",
"@avalabs/core-etherscan-sdk": "3.1.0-alpha.7",
"@avalabs/core-bridge-sdk": "3.1.0-canary.43ef750.0+43ef750",
"@avalabs/core-chains-sdk": "3.1.0-canary.43ef750.0+43ef750",
"@avalabs/core-coingecko-sdk": "3.1.0-canary.43ef750.0+43ef750",
"@avalabs/core-covalent-sdk": "3.1.0-canary.43ef750.0+43ef750",
"@avalabs/core-etherscan-sdk": "3.1.0-canary.43ef750.0+43ef750",
"@avalabs/core-k2-components": "4.18.0-alpha.47",
"@avalabs/core-snowtrace-sdk": "3.1.0-alpha.7",
"@avalabs/core-token-prices-sdk": "3.1.0-alpha.7",
"@avalabs/core-utils-sdk": "3.1.0-alpha.7",
"@avalabs/core-wallets-sdk": "3.1.0-alpha.7",
"@avalabs/evm-module": "0.8.0",
"@avalabs/glacier-sdk": "3.1.0-alpha.7",
"@avalabs/core-snowtrace-sdk": "3.1.0-canary.43ef750.0+43ef750",
"@avalabs/core-token-prices-sdk": "3.1.0-canary.43ef750.0+43ef750",
"@avalabs/core-utils-sdk": "3.1.0-canary.43ef750.0+43ef750",
"@avalabs/core-wallets-sdk": "3.1.0-canary.43ef750.0+43ef750",
"@avalabs/evm-module": "0.0.0-feat-bitcoin--just-sign-tx-20241011064558",
"@avalabs/glacier-sdk": "3.1.0-canary.43ef750.0+43ef750",
"@avalabs/hw-app-avalanche": "0.14.1",
"@avalabs/types": "3.1.0-alpha.3",
"@avalabs/vm-module-types": "0.8.0",
"@avalabs/vm-module-types": "0.0.0-feat-bitcoin--just-sign-tx-20241011064558",
"@blockaid/client": "0.10.0",
"@coinbase/cbpay-js": "1.6.0",
"@cubist-labs/cubesigner-sdk": "0.3.28",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const RESTRICTED_METHODS = Object.freeze([] as string[]);
* "method not found" error.
*/
export const UNRESTRICTED_METHODS = Object.freeze([
'bitcoin_signTransaction',
'eth_accounts',
'eth_requestAccounts',
'eth_baseFee',
Expand Down
1 change: 1 addition & 0 deletions src/background/vmModules/ApprovalController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export class ApprovalController implements IApprovalController {

switch (signingData.type) {
case RpcMethod.BITCOIN_SEND_TRANSACTION:
case RpcMethod.BITCOIN_SIGN_TRANSACTION:
case RpcMethod.ETH_SEND_TRANSACTION:
return await this.#walletService.sign(signingData.data, network);

Expand Down
1 change: 1 addition & 0 deletions src/background/vmModules/ModuleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class ModuleManager {
approvalController: this.#approvalController,
appInfo,
}),

new AvalancheModule({
environment,
approvalController: this.#approvalController,
Expand Down
55 changes: 55 additions & 0 deletions src/components/common/approval/TransactionDetailItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
type LinkItem,
type TextItem,
DetailItemType,
FundsRecipientItem,
} from '@avalabs/vm-module-types';
import { TokenUnit } from '@avalabs/core-utils-sdk';

Expand All @@ -21,6 +22,9 @@ import { useSettingsContext } from '@src/contexts/SettingsProvider';
import { TxDetailsRow } from './TxDetailsRow';
import { useBalancesContext } from '@src/contexts/BalancesProvider';
import { runtime } from 'webextension-polyfill';
import { useContactsContext } from '@src/contexts/ContactsProvider';
import { truncateAddress } from '@src/utils/truncateAddress';
import { useAccountsContext } from '@src/contexts/AccountsProvider';

export const TransactionDetailItem = ({ item }: { item: DetailItem }) => {
if (typeof item === 'string') {
Expand All @@ -40,6 +44,9 @@ export const TransactionDetailItem = ({ item }: { item: DetailItem }) => {
case DetailItemType.CURRENCY:
return <CurrencyInfo item={item} />;

case DetailItemType.FUNDS_RECIPIENT:
return <FundsRecipientInfo item={item} />;

default:
return null;
}
Expand Down Expand Up @@ -100,6 +107,54 @@ const AddressInfo = ({ item }: { item: AddressItem }) => (
<AccountDetails label={item.label} address={item.value} />
);

const FundsRecipientInfo = ({ item }: { item: FundsRecipientItem }) => {
const { currencyFormatter } = useSettingsContext();
const { getContactByAddress } = useContactsContext();
const { getAccount } = useAccountsContext();
const { getTokenPrice } = useBalancesContext();

const token = new TokenUnit(item.value, item.maxDecimals, item.symbol);
const tokenPrice = getTokenPrice(item.symbol);
const contact = getAccount(item.label) ?? getContactByAddress(item.label);

return (
<TxDetailsRow
label={
<Tooltip title={item.label}>
<Typography variant="caption" color="text.secondary">
{contact?.name || truncateAddress(item.label)}
</Typography>
</Tooltip>
}
>
<Stack>
<Typography
variant="body2"
sx={{
textAlign: 'right',
fontWeight: 'fontWeightSemibold',
}}
>
{token.toDisplay()} {token.getSymbol()}
</Typography>
{tokenPrice ? (
<Typography
variant="caption"
sx={{
textAlign: 'right',
color: 'text.secondary',
}}
>
{currencyFormatter(
tokenPrice * token.toDisplay({ asNumber: true })
)}
</Typography>
) : null}
</Stack>
</TxDetailsRow>
);
};

const CurrencyInfo = ({ item }: { item: CurrencyItem }) => {
const { currencyFormatter } = useSettingsContext();
const { getTokenPrice } = useBalancesContext();
Expand Down
13 changes: 9 additions & 4 deletions src/components/common/approval/TxDetailsRow.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Stack, Typography, useTheme } from '@avalabs/core-k2-components';
import React from 'react';

type TxDetailsRowProps = { label: string };
type TxDetailsRowProps = { label: string | React.ReactNode };

export const TxDetailsRow: React.FC<TxDetailsRowProps> = ({
children,
Expand All @@ -17,9 +18,13 @@ export const TxDetailsRow: React.FC<TxDetailsRowProps> = ({
gap: 1,
}}
>
<Typography variant="caption" color="text.secondary">
{label}
</Typography>
{typeof label === 'string' ? (
<Typography variant="caption" color="text.secondary">
{label}
</Typography>
) : (
label
)}
<Stack
sx={{
flexDirection: 'row',
Expand Down
20 changes: 19 additions & 1 deletion src/contexts/ContactsProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { ExtensionRequest } from '@src/background/connections/extensionConnection/models';
import { createContext, useContext, useEffect, useState } from 'react';
import {
createContext,
useCallback,
useContext,
useEffect,
useState,
} from 'react';
import { filter, map } from 'rxjs';
import { useConnectionContext } from './ConnectionProvider';
import { ContactsState } from '@src/background/services/contacts/models';
Expand All @@ -15,6 +21,7 @@ type ContactsFromProvider = ContactsState & {
removeContact(contact: Contact): Promise<any>;
updateContact(contact: Contact): Promise<any>;
getContactById(contactId: string): Contact | undefined;
getContactByAddress(address: string): Contact | undefined;
};

const ContactsContext = createContext<ContactsFromProvider>({} as any);
Expand Down Expand Up @@ -76,6 +83,16 @@ export function ContactsContextProvider({ children }: { children: any }) {
});
}

const getContactByAddress = useCallback(
(lookupAddress: string) =>
contacts.contacts.find(({ address, addressBTC, addressXP }) =>
[address, addressBTC, addressXP]
.map((a) => (a ?? '').toLowerCase())
.includes(lookupAddress)
),
[contacts.contacts]
);

return (
<ContactsContext.Provider
value={{
Expand All @@ -84,6 +101,7 @@ export function ContactsContextProvider({ children }: { children: any }) {
removeContact,
updateContact,
getContactById,
getContactByAddress,
}}
>
{children}
Expand Down
14 changes: 8 additions & 6 deletions src/pages/ApproveAction/GenericApprovalScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export function GenericApprovalScreen() {
});

const { displayData, context } = action ?? {};
const hasFeeSelector = action?.displayData.networkFeeSelector;
const isFeeValid =
!hasFeeSelector ||
(!feeError && !isCalculatingFee && hasEnoughForNetworkFee);

useEffect(() => {
if (!displayData?.network?.chainId) {
Expand Down Expand Up @@ -147,15 +151,15 @@ export function GenericApprovalScreen() {
<ApprovalSectionHeader label={section.title} />
)}
<ApprovalSectionBody sx={{ py: 1 }}>
{sectionIndex === 0 && network && (
<NetworkDetails network={network} />
)}
{section.items.map((item, index) => (
<TransactionDetailItem
key={`tx-detail.${sectionIndex}.${index}`}
item={item}
/>
))}
{sectionIndex === 0 && network && (
<NetworkDetails network={network} />
)}
</ApprovalSectionBody>
</ApprovalSection>
))}
Expand Down Expand Up @@ -210,9 +214,7 @@ export function GenericApprovalScreen() {
disabled={
!displayData ||
action.status === ActionStatus.SUBMITTING ||
Boolean(feeError) ||
isCalculatingFee ||
!hasEnoughForNetworkFee
!isFeeValid
}
isLoading={
action.status === ActionStatus.SUBMITTING || isCalculatingFee
Expand Down
27 changes: 16 additions & 11 deletions src/pages/ApproveAction/hooks/useFeeCustomizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const useFeeCustomizer = ({
const [gasFeeModifier, setGasFeeModifier] = useState<GasFeeModifier>(
GasFeeModifier.NORMAL
);
const isFeeSelectorEnabled = Boolean(action?.displayData.networkFeeSelector);

const tokens = useTokensWithBalances({
chainId: network?.chainId,
Expand All @@ -59,6 +60,10 @@ export const useFeeCustomizer = ({
) as NetworkTokenWithBalance | null;

const signingData = useMemo(() => {
if (!isFeeSelectorEnabled) {
return undefined;
}

switch (action?.signingData?.type) {
// Request types that we know may require a fee
case RpcMethod.BITCOIN_SEND_TRANSACTION:
Expand All @@ -69,11 +74,11 @@ export const useFeeCustomizer = ({
default:
return undefined;
}
}, [action]);
}, [action, isFeeSelectorEnabled]);

const updateFee = useCallback(
async (maxFeeRate: bigint, maxTipRate?: bigint) => {
if (!actionId) {
if (!actionId || !isFeeSelectorEnabled) {
return;
}

Expand All @@ -87,7 +92,7 @@ export const useFeeCustomizer = ({
params: [actionId, newFeeConfig],
});
},
[actionId, request, signingData?.type]
[actionId, isFeeSelectorEnabled, request, signingData?.type]
);

const getFeeInfo = useCallback((data: SigningData) => {
Expand Down Expand Up @@ -136,7 +141,7 @@ export const useFeeCustomizer = ({
useEffect(() => {
const nativeBalance = nativeToken?.balance;

if (!nativeBalance || !signingData) {
if (!nativeBalance || !signingData || !isFeeSelectorEnabled) {
return;
}

Expand All @@ -148,7 +153,7 @@ export const useFeeCustomizer = ({
? undefined
: SendErrorMessage.INSUFFICIENT_BALANCE_FOR_FEE
);
}, [getFeeInfo, nativeToken?.balance, signingData]);
}, [getFeeInfo, isFeeSelectorEnabled, nativeToken?.balance, signingData]);

const [maxFeePerGas, setMaxFeePerGas] = useState(
getInitialFeeRate(signingData)
Expand All @@ -158,14 +163,14 @@ export const useFeeCustomizer = ({
);

useEffect(() => {
if (!networkFee) {
if (!networkFee || !isFeeSelectorEnabled) {
return;
}

// Initialize fee config with default values if they are not set at all
setMaxFeePerGas((previous) => previous ?? networkFee.low.maxFee);
setMaxPriorityFeePerGas((previous) => previous ?? networkFee.low.maxTip);
}, [networkFee]);
}, [networkFee, isFeeSelectorEnabled]);

const setCustomFee = useCallback(
(values: {
Expand All @@ -183,7 +188,7 @@ export const useFeeCustomizer = ({
useEffect(() => {
let isMounted = true;

if (!network) {
if (!network || !isFeeSelectorEnabled) {
return;
}
// If the request comes from a dApp, a different network may be active,
Expand All @@ -197,10 +202,10 @@ export const useFeeCustomizer = ({
return () => {
isMounted = false;
};
}, [getNetworkFee, network]);
}, [getNetworkFee, isFeeSelectorEnabled, network]);

useEffect(() => {
if (typeof maxFeePerGas === 'undefined') {
if (typeof maxFeePerGas === 'undefined' || !isFeeSelectorEnabled) {
return;
}

Expand All @@ -225,7 +230,7 @@ export const useFeeCustomizer = ({
return () => {
isMounted = false;
};
}, [maxFeePerGas, maxPriorityFeePerGas, updateFee]);
}, [isFeeSelectorEnabled, maxFeePerGas, maxPriorityFeePerGas, updateFee]);

const renderFeeWidget = useCallback(() => {
if (!networkFee || !signingData) {
Expand Down
Loading

0 comments on commit bf64965

Please sign in to comment.