Skip to content

Commit

Permalink
feat: provide custom headers to Glacier calls (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeh0w authored Oct 7, 2024
1 parent a8ca274 commit 2b8a852
Show file tree
Hide file tree
Showing 17 changed files with 210 additions and 383 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.7.0",
"@avalabs/avalanche-module": "0.7.3",
"@avalabs/avalanchejs": "4.0.5",
"@avalabs/bitcoin-module": "0.7.0",
"@avalabs/bitcoin-module": "0.7.3",
"@avalabs/bridge-unified": "2.1.0",
"@avalabs/core-bridge-sdk": "3.1.0-alpha.4",
"@avalabs/core-chains-sdk": "3.1.0-alpha.4",
"@avalabs/core-coingecko-sdk": "3.1.0-alpha.4",
"@avalabs/core-covalent-sdk": "3.1.0-alpha.4",
"@avalabs/core-etherscan-sdk": "3.1.0-alpha.4",
"@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-k2-components": "4.18.0-alpha.47",
"@avalabs/core-snowtrace-sdk": "3.1.0-alpha.4",
"@avalabs/core-token-prices-sdk": "3.1.0-alpha.4",
"@avalabs/core-utils-sdk": "3.1.0-alpha.4",
"@avalabs/core-wallets-sdk": "3.1.0-alpha.4",
"@avalabs/evm-module": "0.7.0",
"@avalabs/glacier-sdk": "3.1.0-alpha.4",
"@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.7.3",
"@avalabs/glacier-sdk": "3.1.0-alpha.7",
"@avalabs/hw-app-avalanche": "0.14.1",
"@avalabs/types": "3.1.0-alpha.3",
"@avalabs/vm-module-types": "0.7.0",
"@avalabs/vm-module-types": "0.7.3",
"@blockaid/client": "0.10.0",
"@coinbase/cbpay-js": "1.6.0",
"@cubist-labs/cubesigner-sdk": "0.3.28",
Expand Down
24 changes: 12 additions & 12 deletions src/background/services/balances/BalancesService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ describe('src/background/services/balances/BalancesService.ts', () => {
atomicMemoryLocked: [],
},
balancePerType: {
lockedStaked: 1,
lockedStakeable: 0,
lockedPlatform: 0,
atomicMemoryLocked: 0,
atomicMemoryUnlocked: 0,
unlockedUnstaked: 0,
unlockedStaked: 0,
pendingStaked: 0,
lockedStaked: 1n,
lockedStakeable: 0n,
lockedPlatform: 0n,
atomicMemoryLocked: 0n,
atomicMemoryUnlocked: 0n,
unlockedUnstaked: 0n,
unlockedStaked: 0n,
pendingStaked: 0n,
},
},
},
Expand Down Expand Up @@ -128,10 +128,10 @@ describe('src/background/services/balances/BalancesService.ts', () => {
},

balancePerType: {
locked: 3,
unlocked: 3,
atomicMemoryLocked: 0,
atomicMemoryUnlocked: 0,
locked: 3n,
unlocked: 3n,
atomicMemoryLocked: 0n,
atomicMemoryUnlocked: 0n,
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ describe('src/background/services/balances/utils/isTokenWithBalanceAVM.ts', () =
description: 'description',
logoUri: 'logoUri',
balancePerType: {
locked: 1,
unlocked: 2,
atomicMemoryUnlocked: 3,
atomicMemoryLocked: 4,
locked: 1n,
unlocked: 2n,
atomicMemoryUnlocked: 3n,
atomicMemoryLocked: 4n,
},
};

Expand All @@ -38,14 +38,14 @@ describe('src/background/services/balances/utils/isTokenWithBalanceAVM.ts', () =
description: 'description',
logoUri: 'logoUri',
balancePerType: {
atomicMemoryLocked: 4,
atomicMemoryUnlocked: 5,
lockedStaked: 1,
lockedStakeable: 2,
lockedPlatform: 3,
unlockedUnstaked: 6,
unlockedStaked: 7,
pendingStaked: 8,
atomicMemoryLocked: 4n,
atomicMemoryUnlocked: 5n,
lockedStaked: 1n,
lockedStakeable: 2n,
lockedPlatform: 3n,
unlockedUnstaked: 6n,
unlockedStaked: 7n,
pendingStaked: 8n,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ describe('src/background/services/balances/utils/isTokenWithBalancePVM.ts', () =
description: 'description',
logoUri: 'logoUri',
balancePerType: {
locked: 1,
unlocked: 2,
atomicMemoryUnlocked: 3,
atomicMemoryLocked: 4,
locked: 1n,
unlocked: 2n,
atomicMemoryUnlocked: 3n,
atomicMemoryLocked: 4n,
},
};

Expand All @@ -37,14 +37,14 @@ describe('src/background/services/balances/utils/isTokenWithBalancePVM.ts', () =
description: 'description',
logoUri: 'logoUri',
balancePerType: {
lockedStaked: 1,
lockedStakeable: 2,
lockedPlatform: 3,
atomicMemoryLocked: 4,
atomicMemoryUnlocked: 5,
unlockedUnstaked: 6,
unlockedStaked: 7,
pendingStaked: 8,
lockedStaked: 1n,
lockedStakeable: 2n,
lockedPlatform: 3n,
atomicMemoryLocked: 4n,
atomicMemoryUnlocked: 5n,
unlockedUnstaked: 6n,
unlockedStaked: 7n,
pendingStaked: 8n,
},
};

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,10 @@ describe('background/services/bridge/handlers/avalanche_bridgeAsset', () => {
networkServiceMock.getBitcoinProvider.mockResolvedValue({
waitForTx: jest.fn().mockResolvedValue(btcResult),
});
balanceAggregatorServiceMock.getBalancesForNetworks.mockResolvedValue({});
balanceAggregatorServiceMock.getBalancesForNetworks.mockResolvedValue({
tokens: {},
nfts: {},
});
jest.mocked(openApprovalWindow).mockResolvedValue({} as any);
jest.mocked(getAssets).mockReturnValue({
BTC: btcAsset,
Expand Down Expand Up @@ -607,7 +610,7 @@ describe('background/services/bridge/handlers/avalanche_bridgeAsset', () => {
expect(transferAssetBTC).toHaveBeenCalledTimes(1);
expect(transferAssetBTC).toHaveBeenCalledWith(
expect.objectContaining({
amount: String(btcToSatoshi(amount)),
amount: btcToSatoshi(amount),
})
);

Expand Down
21 changes: 12 additions & 9 deletions src/background/services/bridge/handlers/avalanche_bridgeAsset.ts
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 All @@ -319,17 +319,20 @@ export class AvalancheBridgeAsset extends DAppRequestHandler<BridgeActionParams>
const utxos = await getBtcInputUtxos(btcProvider, token, highFeeRate);

const hash = await transferAssetBTC({
fromAccount: addressBTC,
config: this.#getConfig(),
amount: String(btcToSatoshi(amount)),
feeRate: highFeeRate,
amount: btcToSatoshi(amount),
feeRate:
Number(pendingAction.displayData.gasSettings?.maxFeePerGas ?? 0) ||
highFeeRate,
onStatusChange: () => {},
onTxHashChange: () => {},
signAndSendBTC: async ([address, amountAsString, feeRate]) => {
signAndSendBTC: async ({ amount: signAmount, feeRate, to, from }) => {
const error = validateBtcSend(
addressBTC,
from,
{
address,
amount: Number(amountAsString),
address: to,
amount: signAmount,
feeRate,
token,
},
Expand All @@ -347,8 +350,8 @@ export class AvalancheBridgeAsset extends DAppRequestHandler<BridgeActionParams>
addressBTC,
btcProvider,
{
amount: Number(amountAsString),
address,
amount: signAmount,
address: to,
token,
feeRate,
}
Expand Down
76 changes: 0 additions & 76 deletions src/background/services/glacier/GlacierService.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {
BlockchainId,
Glacier,
Network,
PrimaryNetwork,
PrimaryNetworkChainName,
} from '@avalabs/glacier-sdk';
Expand Down Expand Up @@ -38,11 +36,6 @@ const pchainBalance = {
},
};

const waitForFirstHealthCheck = async () => {
jest.runOnlyPendingTimers();
await new Promise(jest.requireActual('timers').setImmediate);
};

describe('src/background/services/glacier/GlacierService.ts', () => {
beforeEach(() => {
jest.resetAllMocks();
Expand Down Expand Up @@ -72,14 +65,6 @@ describe('src/background/services/glacier/GlacierService.ts', () => {
afterEach(() => {
jest.useRealTimers();
});
it('should call a `setTimeout` which sets `isGlacierHealthy` to true after 5 mins', async () => {
jest.spyOn(global, 'setTimeout');

const glacierService = new GlacierService();
glacierService.setGlacierToUnhealthy();

expect(setTimeout).toHaveBeenCalled();
});

describe('reindexNft', () => {
it('schedules reindexing of the given NFT', async () => {
Expand Down Expand Up @@ -176,65 +161,4 @@ describe('src/background/services/glacier/GlacierService.ts', () => {
expect(getTokenDetails).toHaveBeenCalledTimes(10);
});
});

describe('isNetworkSupported', () => {
it('should set the `isNetworkSupported` to false (and set the `isGlacierHealthy` to false as well)', async () => {
const glacierService = new GlacierService();
glacierService.setGlacierToUnhealthy();

const isNetworkSupported = await glacierService.isNetworkSupported(1);
expect(isNetworkSupported).toBe(false);
});

it('returns false if fetching supported chains fails', async () => {
supportedChainsMock.mockRejectedValue('some error');

const glacierService = new GlacierService();
supportedChainsMock.mockReset(); // It's first called when GlacierService is instantiated, so we need to reset the counter.

await waitForFirstHealthCheck();

const result = await glacierService.isNetworkSupported(1);

expect(result).toBe(false);
expect(supportedChainsMock).toHaveBeenCalledTimes(1);
});

it('returns true if the provided chain id is supported', async () => {
supportedChainsMock.mockResolvedValue({ chains: [{ chainId: '1' }] });

const glacierService = new GlacierService();
await waitForFirstHealthCheck();

const result = await glacierService.isNetworkSupported(1);
expect(result).toBe(true);
});

it('fetches supported chains only once', async () => {
supportedChainsMock.mockResolvedValue({ chains: [{ chainId: '1' }] });

const glacierService = new GlacierService();
await waitForFirstHealthCheck();

const result_1 = await glacierService.isNetworkSupported(1);
const result_2 = await glacierService.isNetworkSupported(1);
const result_3 = await glacierService.isNetworkSupported(1);

expect([result_1, result_2, result_3]).toStrictEqual([true, true, true]);
expect(supportedChainsMock).toHaveBeenCalledTimes(1);
});
});

describe('getChainBalance', () => {
it('should return response from getBalancesByAddresses', async () => {
const glacierService = new GlacierService();
const result = await glacierService.getChainBalance({
blockchainId: BlockchainId.P_CHAIN,
network: Network.FUJI,
addresses: 'address',
});

expect(result).toEqual(pchainBalance);
});
});
});
Loading

0 comments on commit 2b8a852

Please sign in to comment.