Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: upgrade unified bridge SDK #40

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 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-20241015101648",
"@avalabs/avalanchejs": "4.0.5",
"@avalabs/bitcoin-module": "0.8.0",
"@avalabs/bridge-unified": "2.1.0",
"@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/bitcoin-module": "0.0.0-feat-bitcoin--just-sign-tx-20241015101648",
"@avalabs/bridge-unified": "0.0.0-feat-ictt-configs-20241009072139",
"@avalabs/core-bridge-sdk": "3.1.0-alpha.8",
"@avalabs/core-chains-sdk": "3.1.0-alpha.8",
"@avalabs/core-coingecko-sdk": "3.1.0-alpha.8",
"@avalabs/core-covalent-sdk": "3.1.0-alpha.8",
"@avalabs/core-etherscan-sdk": "3.1.0-alpha.8",
"@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-alpha.8",
"@avalabs/core-token-prices-sdk": "3.1.0-alpha.8",
"@avalabs/core-utils-sdk": "3.1.0-alpha.8",
"@avalabs/core-wallets-sdk": "3.1.0-alpha.8",
"@avalabs/evm-module": "0.0.0-feat-bitcoin--just-sign-tx-20241015101648",
"@avalabs/glacier-sdk": "3.1.0-alpha.8",
"@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-20241015101648",
"@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
47 changes: 30 additions & 17 deletions src/background/services/history/HistoryService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TxHistoryItem } from './models';
import { TokenType } from '@avalabs/vm-module-types';
import { TransactionType } from '@avalabs/vm-module-types';
import { ETHEREUM_ADDRESS } from '@src/utils/bridgeTransactionUtils';
import { BridgeType } from '@avalabs/bridge-unified';

describe('src/background/services/history/HistoryService.ts', () => {
let service: HistoryService;
Expand Down Expand Up @@ -37,17 +38,14 @@ describe('src/background/services/history/HistoryService.ts', () => {
addressAVM: 'addressBtc',
},
} as any;
const bridgeHistoryHelperServiceMock = {
isBridgeTransactionBTC: jest.fn(),
} as any;
const unifiedBridgeServiceMock = {
state: {
addresses: [],
},
analyzeTx: jest.fn(),
} as any;

const txHistoryItem: TxHistoryItem = {
isBridge: false,
bridgeAnalysis: {
isBridgeTx: false,
},
isContractCall: true,
isIncoming: false,
isOutgoing: true,
Expand All @@ -72,7 +70,9 @@ describe('src/background/services/history/HistoryService.ts', () => {
};

const btcTxHistoryItem: TxHistoryItem = {
isBridge: false,
bridgeAnalysis: {
isBridgeTx: false,
},
isContractCall: true,
isIncoming: false,
isOutgoing: true,
Expand Down Expand Up @@ -102,9 +102,12 @@ describe('src/background/services/history/HistoryService.ts', () => {
service = new HistoryService(
moduleManagereMock,
accountsServiceMock,
bridgeHistoryHelperServiceMock,
unifiedBridgeServiceMock
);

jest
.mocked(unifiedBridgeServiceMock.analyzeTx)
.mockReturnValue({ isBridgeTx: false });
});

it('should return empty array when network is not supported', async () => {
Expand Down Expand Up @@ -137,9 +140,6 @@ describe('src/background/services/history/HistoryService.ts', () => {
return { transactions: [btcTxHistoryItem] };
}),
});
jest
.mocked(bridgeHistoryHelperServiceMock.isBridgeTransactionBTC)
.mockReturnValue(false);
const result = await service.getTxHistory({
...network1,
vmName: NetworkVMType.BITCOIN,
Expand All @@ -153,18 +153,21 @@ describe('src/background/services/history/HistoryService.ts', () => {
return { transactions: [btcTxHistoryItem] };
}),
});
jest
.mocked(bridgeHistoryHelperServiceMock.isBridgeTransactionBTC)
.mockReturnValue(true);
const result = await service.getTxHistory({
...network1,
vmName: NetworkVMType.BITCOIN,
caipId: 'bip122:000000000019d6689c085ae165831e93',
});

expect(result).toEqual([{ ...btcTxHistoryItem, isBridge: true }]);
expect(result).toEqual([
{ ...btcTxHistoryItem, bridgeAnalysis: { isBridgeTx: false } },
]);
});
it('should return results with an ETH bridge transaction', async () => {
jest.mocked(unifiedBridgeServiceMock.analyzeTx).mockReturnValue({
isBridgeTx: true,
bridgeType: BridgeType.AVALANCHE_EVM,
});
jest.mocked(moduleManagereMock.loadModuleByNetwork).mockResolvedValue({
getTransactionHistory: jest.fn(() => {
return {
Expand All @@ -183,10 +186,20 @@ describe('src/background/services/history/HistoryService.ts', () => {
caipId: 'caip',
});
expect(result).toEqual([
{ ...txHistoryItem, isBridge: true, from: ETHEREUM_ADDRESS },
{
...txHistoryItem,
bridgeAnalysis: {
bridgeType: BridgeType.AVALANCHE_EVM,
isBridgeTx: true,
},
from: ETHEREUM_ADDRESS,
},
]);
});
it('should return results with an pchain transaction', async () => {
jest
.mocked(unifiedBridgeServiceMock.analyzeTx)
.mockReturnValue({ isBridgeTx: false });
jest.mocked(moduleManagereMock.loadModuleByNetwork).mockResolvedValue({
getTransactionHistory: jest.fn(() => {
return {
Expand Down
47 changes: 20 additions & 27 deletions src/background/services/history/HistoryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ import { NetworkWithCaipId } from '../network/models';
import { ModuleManager } from '@src/background/vmModules/ModuleManager';
import { AccountsService } from '../accounts/AccountsService';
import { TxHistoryItem } from './models';
import { HistoryServiceBridgeHelper } from './HistoryServiceBridgeHelper';
import { Transaction } from '@avalabs/vm-module-types';
import { ETHEREUM_ADDRESS } from '@src/utils/bridgeTransactionUtils';
import { UnifiedBridgeService } from '../unifiedBridge/UnifiedBridgeService';
import { resolve } from '@src/utils/promiseResolver';
import sentryCaptureException, {
SentryExceptionTypes,
} from '@src/monitoring/sentryCaptureException';
import { AnalyzeTxParams } from '@avalabs/bridge-unified';

@singleton()
export class HistoryService {
constructor(
private moduleManager: ModuleManager,
private accountsService: AccountsService,
private bridgeHistoryHelperService: HistoryServiceBridgeHelper,
private unifiedBridgeService: UnifiedBridgeService
) {}

Expand Down Expand Up @@ -52,25 +50,31 @@ export class HistoryService {
});

const txHistoryItem = transactions.map((transaction) => {
const isBridge = this.#getIsBirdge(network, transaction);
const result = this.#analyze(network, transaction);
const vmType = network.vmName;
return { ...transaction, vmType, isBridge };
return {
...transaction,
vmType,
bridgeAnalysis: result,
};
}) as TxHistoryItem[];

return txHistoryItem;
}

#getIsBirdge(network: NetworkWithCaipId, transaction: Transaction) {
if (network.vmName === NetworkVMType.BITCOIN) {
return this.bridgeHistoryHelperService.isBridgeTransactionBTC([
transaction.from,
transaction.to,
]);
}
return (
this.#isBridgeAddress(transaction.from) ||
this.#isBridgeAddress(transaction.to)
);
#analyze(network: NetworkWithCaipId, transaction: Transaction) {
const params: AnalyzeTxParams = {
from: transaction.from as `0x${string}`,
to: transaction.to as `0x${string}`,
chainId: network.caipId,
tokenTransfers: transaction.tokens.map((transfer) => ({
symbol: transfer.symbol,
from: (transfer.from?.address ?? transaction.from) as `0x${string}`,
to: (transfer.to?.address ?? transaction.to) as `0x${string}`,
})),
};

return this.unifiedBridgeService.analyzeTx(params);
}

#getAddress(network: NetworkWithCaipId) {
Expand All @@ -89,15 +93,4 @@ export class HistoryService {
return undefined;
}
}

#isBridgeAddress(address?: string) {
if (!address) {
return false;
}

return [
ETHEREUM_ADDRESS,
...this.unifiedBridgeService.state.addresses,
].includes(address.toLowerCase());
}
}
97 changes: 0 additions & 97 deletions src/background/services/history/HistoryServiceBridgeHelper.ts

This file was deleted.

51 changes: 3 additions & 48 deletions src/background/services/history/models.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,7 @@
import {
NetworkVMType,
Transaction,
TransactionType,
} from '@avalabs/vm-module-types';
import { AnalyzeTxResult } from '@avalabs/bridge-unified';
import { NetworkVMType, Transaction } from '@avalabs/vm-module-types';

export interface TxHistoryItem extends Transaction {
isBridge: boolean;
bridgeAnalysis: AnalyzeTxResult;
vmType?: NetworkVMType;
}

export const NonContractCallTypes = [
TransactionType.BRIDGE,
TransactionType.SEND,
TransactionType.RECEIVE,
TransactionType.TRANSFER,
];

export interface HistoryItemCategories {
isBridge: boolean;
isSwap: boolean;
isNativeSend: boolean;
isNativeReceive: boolean;
isNFTPurchase: boolean;
isApprove: boolean;
isTransfer: boolean;
isAirdrop: boolean;
isUnwrap: boolean;
isFillOrder: boolean;
isContractCall: boolean;
method: string;
type: TransactionType;
}

export interface SubnetHistoryItem {
hash: string;
status: number;
gasPrice: number;
gasUsed: number;
timestamp: number;
from: string;
to: string;
value: string;
method: string;
type: number;
block: number;
toContract?: {
name: string;
symbol: string;
decimals: number;
};
}
Loading
Loading