diff --git a/src/__tests__/WBGLEndpoint.test.ts b/src/__tests__/WBGLEndpoint.test.ts new file mode 100644 index 0000000..ac17cf6 --- /dev/null +++ b/src/__tests__/WBGLEndpoint.test.ts @@ -0,0 +1,78 @@ +import fetch from 'node-fetch'; +import { ethers } from 'ethers'; + +import { ChaindIds, ChainNames } from '../chains'; +import { WBGL } from '../bridge/WBGL'; + +jest.mock('node-fetch', () => jest.fn()); + +const mockedFetch = fetch as unknown as jest.Mock; + +describe('WBGL bridge endpoint configuration', () => { + const createWbgl = (bridgeEndpoint?: string) => { + const wbgl = new WBGL({ + evmPrivateKey: `0x${'1'.repeat(64)}`, + provider: new ethers.providers.JsonRpcProvider(), + chainName: ChainNames.BNBSmartChain, + chainId: ChaindIds.BNBSmartChain, + bridgeEndpoint, + bglPrivateKeyOrSeed: 'unused-for-wbgl', + }); + + jest + .spyOn( + wbgl as unknown as { _sendWbgl: () => Promise }, + '_sendWbgl', + ) + .mockResolvedValue({ + transactionHash: '0xabc', + wbglBalance: '10', + balance: '1', + }); + + return wbgl; + }; + + const submitSwap = async (wbgl: WBGL) => + wbgl.swapWBGLforBGL({ + bglAddress: 'bgl1qh3tsz3a7l3m49xaq4xcdx8aefthchuqagmspcn', + to: '0x0000000000000000000000000000000000000001', + wbglAmount: 5, + }); + + beforeEach(() => { + mockedFetch.mockResolvedValue({ + json: jest.fn().mockResolvedValue({ + address: '0x0000000000000000000000000000000000000001', + }), + }); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it.each([ + ['https://bridge.example.test/app/'], + ['https://bridge.example.test/app'], + ])( + 'submits WBGL swaps to the configured bridge endpoint %s', + async (bridgeEndpoint) => { + await submitSwap(createWbgl(bridgeEndpoint)); + + expect(mockedFetch).toHaveBeenCalledWith( + 'https://bridge.example.test/app/submit/wbgl', + expect.objectContaining({ method: 'POST' }), + ); + }, + ); + + it('uses the production default when the bridge endpoint is empty', async () => { + await submitSwap(createWbgl('')); + + expect(mockedFetch).toHaveBeenCalledWith( + 'https://bglswap.com/app/submit/wbgl', + expect.objectContaining({ method: 'POST' }), + ); + }); +}); diff --git a/src/bridge/WBGL.ts b/src/bridge/WBGL.ts index d1223e0..70dfea0 100644 --- a/src/bridge/WBGL.ts +++ b/src/bridge/WBGL.ts @@ -33,6 +33,11 @@ export interface WBGLBGLExchangePairResult { } type Provider = ethers.providers.JsonRpcProvider | ethers.providers.Web3Provider +const DEFAULT_BRIDGE_ENDPOINT = 'https://bglswap.com/app/' + +const normalizeBridgeEndpoint = (endpoint: string): string => ( + endpoint.endsWith('/') ? endpoint : `${endpoint}/` +) export class WBGL { @@ -42,13 +47,14 @@ export class WBGL { private readonly provider: Provider private readonly chainId: number | string | ChaindIds private readonly chainName: string - private readonly bridgeEndpoint = 'https://bglswap.com/app/' + private readonly bridgeEndpoint: string private readonly evmPrivateKey: string constructor(config: IBridgeConfig) { this.provider = config.provider this.chainId = config.chainId this.chainName = config.chainName + this.bridgeEndpoint = normalizeBridgeEndpoint(config.bridgeEndpoint || DEFAULT_BRIDGE_ENDPOINT) this.evmPrivateKey = config.evmPrivateKey } @@ -177,4 +183,3 @@ export class WBGL { } /// END OF PRIVATE METHODS } -