Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/__tests__/WBGLEndpoint.test.ts
Original file line number Diff line number Diff line change
@@ -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<unknown> },
'_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' }),
);
});
});
9 changes: 7 additions & 2 deletions src/bridge/WBGL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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
}

Expand Down Expand Up @@ -177,4 +183,3 @@ export class WBGL {
} /// END OF PRIVATE METHODS

}