Skip to content

Commit

Permalink
fix: bridging BTC.b -> BTC via Core Web (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeh0w authored Aug 26, 2024
1 parent ccb08b9 commit 9b02d87
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { openApprovalWindow } from '@src/background/runtime/openApprovalWindow';
import { buildRpcCall } from '@src/tests/test-utils';
import { FeatureGates } from '../../featureFlags/models';
import { getBtcInputUtxos } from '@src/utils/send/btcSendUtils';
import { getProviderForNetwork } from '@src/utils/network/getProviderForNetwork';

jest.mock('@src/background/runtime/openApprovalWindow');
jest.mock('@src/utils/send/btcSendUtils');
Expand All @@ -40,6 +41,7 @@ jest.mock('@avalabs/core-bridge-sdk', () => {
};
});

jest.mock('@src/utils/network/getProviderForNetwork');
jest.mock('../../analytics/utils/encryptAnalyticsData');

const frontendTabId = 654;
Expand Down Expand Up @@ -81,6 +83,7 @@ describe('background/services/bridge/handlers/avalanche_bridgeAsset', () => {
getAvalancheProvider: jest.fn(),
getEthereumProvider: jest.fn(),
getBitcoinProvider: jest.fn(),
sendTransaction: jest.fn(),
} as any;

const walletServiceMock = {
Expand Down Expand Up @@ -547,6 +550,30 @@ describe('background/services/bridge/handlers/avalanche_bridgeAsset', () => {
asset: ethAction.displayData.asset,
})
);

// Mock signAndSendEVM callback being called
const signerFn = (transferAssetEVM as jest.Mock).mock.calls[0][0]
.signAndSendEVM;

jest.mocked(getProviderForNetwork).mockReturnValue({
async getTransactionCount() {
return 14;
},
} as any);

signerFn({});

await new Promise(process.nextTick);

expect(walletServiceMock.sign).toHaveBeenCalledWith(
expect.objectContaining({
maxFeePerGas: 1337,
maxPriorityFeePerGas: 42,
nonce: 14,
}),
{ chainId: 5 },
expect.any(Number)
);
});

it('transferAssetBTC is called when network is Bitcoin', async () => {
Expand Down
32 changes: 27 additions & 5 deletions src/background/services/bridge/handlers/avalanche_bridgeAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import {
} from '@src/utils/send/btcSendUtils';
import { resolve } from '@src/utils/promiseResolver';
import { TokenWithBalanceBTC } from '@avalabs/vm-module-types';
import { getProviderForNetwork } from '@src/utils/network/getProviderForNetwork';
import { JsonRpcBatchInternal } from '@avalabs/core-wallets-sdk';

type BridgeActionParams = [
currentBlockchain: Blockchain,
Expand Down Expand Up @@ -279,6 +281,8 @@ export class AvalancheBridgeAsset extends DAppRequestHandler<BridgeActionParams>
return;
}

const feeData = await this.networkFeeService.getNetworkFee(network);

if (currentBlockchain === Blockchain.BITCOIN) {
try {
const account = this.#getSourceAccount();
Expand Down Expand Up @@ -411,14 +415,32 @@ export class AvalancheBridgeAsset extends DAppRequestHandler<BridgeActionParams>
signAndSendEVM: async (txData) => {
const tx = txData as ContractTransaction; // TODO: update types in the SDK?

const provider = getProviderForNetwork(
network
) as JsonRpcBatchInternal;

const nonce = await provider.getTransactionCount(
this.#getSourceAccount().addressC
);

// Get fee-related properties from the approval screen first,
// then use current instant (high) fee rate as a fallback.
const customGasSettings = pendingAction.displayData.gasSettings;
const maxFeePerGas =
customGasSettings?.maxFeePerGas ?? feeData?.high.maxFee;
const maxPriorityFeePerGas =
customGasSettings?.maxPriorityFeePerGas ?? feeData?.high.maxTip;

if (!maxFeePerGas) {
throw new Error('Required option missing: maxFeePerGas');
}

const signResult = await this.walletService.sign(
{
...tx,
// erase gasPrice if maxFeePerGas can be used
gasPrice: tx.maxFeePerGas
? undefined
: tx.gasPrice ?? undefined,
type: tx.maxFeePerGas ? undefined : 0, // use type: 0 if it's not an EIP-1559 transaction
nonce,
maxFeePerGas,
maxPriorityFeePerGas,
},
network,
frontendTabId
Expand Down

0 comments on commit 9b02d87

Please sign in to comment.