Skip to content

Commit

Permalink
chore: upgrade unified bridge SDK (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeh0w authored Oct 24, 2024
1 parent 437a56f commit 68a849c
Show file tree
Hide file tree
Showing 46 changed files with 1,073 additions and 556 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@avalabs/avalanche-module": "0.11.2",
"@avalabs/avalanchejs": "4.1.0-alpha.7",
"@avalabs/bitcoin-module": "0.11.2",
"@avalabs/bridge-unified": "2.1.0",
"@avalabs/bridge-unified": "0.0.0-feat-ictt-configs-20241009072139",
"@avalabs/core-bridge-sdk": "3.1.0-alpha.10",
"@avalabs/core-chains-sdk": "3.1.0-alpha.10",
"@avalabs/core-coingecko-sdk": "3.1.0-alpha.10",
Expand Down Expand Up @@ -256,7 +256,10 @@
"@avalabs/bitcoin-module>@avalabs/vm-module-types>@avalabs/core-wallets-sdk>hdkey>secp256k1": false,
"@avalabs/avalanche-module>@avalabs/core-wallets-sdk>@avalabs/hw-app-avalanche>@ledgerhq/hw-app-eth>@ledgerhq/domain-service>eip55>keccak": false,
"@avalabs/avalanche-module>@avalabs/core-wallets-sdk>@ledgerhq/hw-app-btc>bitcoinjs-lib>bip32>tiny-secp256k1": false,
"@avalabs/avalanche-module>@avalabs/core-wallets-sdk>hdkey>secp256k1": false
"@avalabs/avalanche-module>@avalabs/core-wallets-sdk>hdkey>secp256k1": false,
"@avalabs/avalanche-module>@avalabs/vm-module-types>@avalabs/core-wallets-sdk>@avalabs/hw-app-avalanche>@ledgerhq/hw-app-eth>@ledgerhq/domain-service>eip55>keccak": false,
"@avalabs/avalanche-module>@avalabs/vm-module-types>@avalabs/core-wallets-sdk>@ledgerhq/hw-app-btc>bitcoinjs-lib>bip32>tiny-secp256k1": false,
"@avalabs/avalanche-module>@avalabs/vm-module-types>@avalabs/core-wallets-sdk>hdkey>secp256k1": false
}
}
}
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.

55 changes: 5 additions & 50 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;
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;
};
export interface TxHistoryItem<VMType = NetworkVMType> extends Transaction {
bridgeAnalysis: AnalyzeTxResult;
vmType?: VMType;
}
Loading

0 comments on commit 68a849c

Please sign in to comment.