Skip to content

Commit

Permalink
chore: use analyzeTx() from UnifiedBridge SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
meeh0w committed Oct 8, 2024
1 parent 66838c8 commit 987870c
Show file tree
Hide file tree
Showing 21 changed files with 150 additions and 315 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"dependencies": {
"@avalabs/avalanche-module": "0.7.0",
"@avalabs/avalanchejs": "4.0.5",
"@avalabs/bridge-unified": "0.0.0-feat-ictt-configs-20241002113306",
"@avalabs/bridge-unified": "0.0.0-feat-ictt-configs-20241008120400",
"@avalabs/bitcoin-module": "0.7.0",
"@avalabs/core-bridge-sdk": "3.1.0-alpha.4",
"@avalabs/core-chains-sdk": "3.1.0-alpha.4",
Expand Down
8 changes: 5 additions & 3 deletions src/background/services/bridge/BridgeService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ const addressBTC = 'tb01234';

const networkBalancesService = {
getBalancesForNetworks: async () => ({
[ChainId.BITCOIN_TESTNET]: {
[addressBTC]: {
BTC: {},
tokens: {
[ChainId.BITCOIN_TESTNET]: {
[addressBTC]: {
BTC: {},
},
},
},
}),
Expand Down
2 changes: 1 addition & 1 deletion src/background/services/bridge/BridgeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export class BridgeService implements OnLock, OnStorageReady {
[this.accountsService.activeAccount]
);

const token = balances[btcNetwork.chainId]?.[addressBtc]?.[
const token = balances.tokens[btcNetwork.chainId]?.[addressBtc]?.[
'BTC'
] as TokenWithBalanceBTC;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ describe('background/services/bridge/handlers/avalanche_bridgeAsset', () => {
networkServiceMock.getBitcoinProvider.mockResolvedValue({
waitForTx: jest.fn().mockResolvedValue(btcResult),
});
balanceAggregatorServiceMock.getBalancesForNetworks.mockResolvedValue({});
balanceAggregatorServiceMock.getBalancesForNetworks.mockResolvedValue({
tokens: {},
});
jest.mocked(openApprovalWindow).mockResolvedValue({} as any);
jest.mocked(getAssets).mockReturnValue({
BTC: btcAsset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export class AvalancheBridgeAsset extends DAppRequestHandler<BridgeActionParams>
0
);

const token = balances[network.chainId]?.[addressBTC]?.[
const token = balances.tokens[network.chainId]?.[addressBTC]?.[
'BTC'
] as TokenWithBalanceBTC;

Expand Down
43 changes: 27 additions & 16 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,15 +38,14 @@ describe('src/background/services/history/HistoryService.ts', () => {
addressAVM: 'addressBtc',
},
} as any;
const bridgeHistoryHelperServiceMock = {
isBridgeTransactionBTC: jest.fn(),
} as any;
const unifiedBridgeServiceMock = {
isBridgeTx: jest.fn().mockReturnValue(false),
analyzeTx: jest.fn(),
} as any;

const txHistoryItem: TxHistoryItem = {
isBridge: false,
bridgeAnalysis: {
isBridgeTx: false,
},
isContractCall: true,
isIncoming: false,
isOutgoing: true,
Expand All @@ -70,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 @@ -100,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 @@ -135,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 @@ -151,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: true } },
]);
});
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 @@ -181,11 +186,17 @@ describe('src/background/services/history/HistoryService.ts', () => {
caipId: 'caip',
});
expect(result).toEqual([
{ ...txHistoryItem, isBridge: true, from: ETHEREUM_ADDRESS },
{
...txHistoryItem,
bridgeAnalysis: { isBridgeTx: true },
from: ETHEREUM_ADDRESS,
},
]);
});
it('should return results with an pchain transaction', async () => {
jest.mocked(unifiedBridgeServiceMock.isBridgeTx).mockReturnValue(false);
jest
.mocked(unifiedBridgeServiceMock.analyzeTx)
.mockReturnValue({ isBridgeTx: false });
jest.mocked(moduleManagereMock.loadModuleByNetwork).mockResolvedValue({
getTransactionHistory: jest.fn(() => {
return {
Expand Down
45 changes: 20 additions & 25 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,26 +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) ||
this.unifiedBridgeService.isBridgeTx(transaction)
);
#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 @@ -90,12 +93,4 @@ export class HistoryService {
return undefined;
}
}

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

return ETHEREUM_ADDRESS === address.toLowerCase();
}
}
93 changes: 0 additions & 93 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

0 comments on commit 987870c

Please sign in to comment.