Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

feat(providers): Return http error when network is unsupported #44 #43

Merged
merged 11 commits into from
Aug 26, 2023
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"type": "module",
"license": "MIT",
"dependencies": {
"@airswap/libraries": "4.0.10",
"@airswap/libraries": "4.0.19",
"@airswap/types": "4.0.5",
"@airswap/utils": "4.0.6",
"@airswap/constants": "4.0.3",
Expand Down
38 changes: 25 additions & 13 deletions src/client/Web3SwapClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Contract, ethers } from 'ethers';
import { Contract, ethers, providers } from 'ethers';
import { Database } from '../database/Database.js';
import { Swap } from '@airswap/libraries'
import { getProviderUrl } from './getProviderUrl.js';

export class Web3SwapClient {
private contracts: Contract[] = [];
Expand All @@ -13,19 +14,28 @@ export class Web3SwapClient {
this.apiKey = apiKey;
}

public connectToChain(network: number | string) {
const chainId = ethers.providers.getNetwork(network)?.chainId;
if (!chainId) {
console.warn("Tried to add this network but it does not work :", network)
return;
}
if (this.keyExists(String(chainId))) {
console.log("Already connected");
return;
}
public connectToChain(network: number | string): boolean {
let chainId: number
let contract: Contract
let provider: providers.Provider

const provider = ethers.providers.InfuraProvider.getWebSocketProvider(chainId, this.apiKey);
const contract = Swap.getContract(provider, chainId);
try {
chainId = Number(network);
if (!chainId || isNaN(chainId)) {
console.warn("Tried to add this network but it does not work :", network)
return false
}
if (this.keyExists(String(chainId))) {
console.log("Already connected");
return true
}

provider = new ethers.providers.JsonRpcProvider(getProviderUrl(chainId, this.apiKey))
contract = Swap.getContract(provider, chainId);
} catch (err) {
return false
}

contract.on("Swap", (nonce, signerWallet) => {
this.onEvent(nonce, signerWallet);
});
Expand All @@ -35,6 +45,7 @@ export class Web3SwapClient {
this.contracts.push(contract);
this.registeredChains.push(String(chainId));
console.log("Registered event SWAP from chain", chainId)
return true
}

private keyExists(network: string): boolean {
Expand All @@ -45,6 +56,7 @@ export class Web3SwapClient {
if (nonce && signerWallet) {
const decodedNonce = parseInt(nonce._hex, 16);
if (isNaN(decodedNonce)) return;

this.database.deleteOrder(decodedNonce, signerWallet.toLocaleLowerCase());
}
}
Expand Down
34 changes: 22 additions & 12 deletions src/client/Web3SwapERC20Client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Contract, ethers } from 'ethers';
import { Contract, ethers, providers } from 'ethers';
import { Database } from '../database/Database.js';
import { SwapERC20 } from '@airswap/libraries'
import { getProviderUrl } from './getProviderUrl.js';

export class Web3SwapERC20Client {
private contracts: Contract[] = [];
Expand All @@ -13,19 +14,27 @@ export class Web3SwapERC20Client {
this.apiKey = apiKey;
}

public connectToChain(network: number|string) {
const chainId = ethers.providers.getNetwork(network)?.chainId;
if (!chainId) {
console.warn("Tried to add this network but it does not work :", network)
return;
}
if (this.keyExists(String(chainId))) {
console.log("Already connected");
return;
public connectToChain(network: number | string): boolean {
let chainId: number
let contract: Contract
let provider: providers.Provider

try {
chainId = Number(network);
if (!chainId || isNaN(chainId)) {
console.warn("Tried to add this network but it does not work :", network)
return false
}
if (this.keyExists(String(chainId))) {
console.log("Already connected");
return true
}
provider = new ethers.providers.JsonRpcProvider(getProviderUrl(chainId, this.apiKey))
contract = SwapERC20.getContract(provider, chainId);
} catch (err) {
return false
}

const provider = ethers.providers.InfuraProvider.getWebSocketProvider(chainId, this.apiKey);
const contract = SwapERC20.getContract(provider, chainId);
contract.on("SwapERC20", (nonce, signerWallet, signerToken, signerAmount, protocolFee, senderWallet, senderToken, senderAmount) => {
this.onEvent(nonce, signerWallet);
});
Expand All @@ -35,6 +44,7 @@ export class Web3SwapERC20Client {
this.contracts.push(contract);
this.registeredChains.push(String(chainId));
console.log("Registered event SWAP ERC20 from chain", chainId)
return true
}

private keyExists(network: string): boolean {
Expand Down
57 changes: 18 additions & 39 deletions src/client/__tests__/Web3SwapClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,37 @@ describe("Web3SwapClient", () => {
//@ts-ignore
mockedEther.providers = {
//@ts-ignore
InfuraProvider: {
getWebSocketProvider: jest.fn()
},
getNetwork: jest.fn(() => ({ chainId: 5, name: "a_custom" }))
JsonRpcProvider: jest.fn(),
};
//@ts-ignore
Swap.getContract = jest.fn(() => ({ on: jest.fn() }))

const client = new Web3SwapClient(apiKey, fakeDatabase as Database);
client.connectToChain(5);

expect(mockedEther.providers.getNetwork).toHaveBeenCalledWith(5);
expect(mockedEther.providers.InfuraProvider.getWebSocketProvider).toHaveBeenCalledWith(5, "apikey");
expect(mockedEther.providers.JsonRpcProvider).toHaveBeenCalledWith("https://https://goerli.infura.io/v3/apikey");
});

it("Network is not found", () => {
//@ts-ignore
mockedEther.providers = {
//@ts-ignore
InfuraProvider: {
getWebSocketProvider: jest.fn()
},
//@ts-ignore
getNetwork: jest.fn(() => undefined)
JsonRpcProvider: jest.fn(),
};
//@ts-ignore
Swap.getContract = jest.fn(() => ({ on: jest.fn() }))

const client = new Web3SwapClient(apiKey, fakeDatabase as Database);
client.connectToChain(5);
client.connectToChain("aze");

expect(mockedEther.providers.getNetwork).toHaveBeenCalledWith(5);
expect(mockedEther.providers.InfuraProvider.getWebSocketProvider).not.toHaveBeenCalled();
expect(mockedEther.providers.JsonRpcProvider).not.toHaveBeenCalled();
});

it("Network can't be added twice", () => {
//@ts-ignore
mockedEther.providers = {
//@ts-ignore
InfuraProvider: {
getWebSocketProvider: jest.fn()
},
//@ts-ignore
getNetwork: jest.fn(() => ({ chainId: 5 }))
JsonRpcProvider: jest.fn(),
};
//@ts-ignore
Swap.getContract = jest.fn(() => ({ on: jest.fn() }))
Expand All @@ -80,9 +67,8 @@ describe("Web3SwapClient", () => {
client.connectToChain(5);
client.connectToChain(5);

expect(mockedEther.providers.getNetwork).toHaveBeenCalledWith(5);
expect(mockedEther.providers.InfuraProvider.getWebSocketProvider).toHaveBeenCalledTimes(1)
expect(mockedEther.providers.InfuraProvider.getWebSocketProvider).toBeCalledWith(5, "apikey")
expect(mockedEther.providers.JsonRpcProvider).toHaveBeenCalledTimes(1)
expect(mockedEther.providers.JsonRpcProvider).toBeCalledWith("https://https://goerli.infura.io/v3/apikey");
});
});

Expand All @@ -95,13 +81,10 @@ describe("Web3SwapClient", () => {

//@ts-ignore
Swap.getContract = jest.fn(() => ({ on: mockedOn }))
//@ts-ignore
mockedEther.providers = {
//@ts-ignore
InfuraProvider: {
getWebSocketProvider: jest.fn()
},
//@ts-ignore
getNetwork: jest.fn(() => ({ chainId: 5 }))
JsonRpcProvider: jest.fn(),
};

new Web3SwapClient(apiKey, fakeDatabase as Database).connectToChain(network);
Expand All @@ -120,11 +103,7 @@ describe("Web3SwapClient", () => {
//@ts-ignore
mockedEther.providers = {
//@ts-ignore
InfuraProvider: {
getWebSocketProvider: jest.fn()
},
//@ts-ignore
getNetwork: jest.fn(() => ({ chainId: 5 }))
JsonRpcProvider: jest.fn(),
};
//@ts-ignore
Swap.getContract = jest.fn(() => ({ on: mockedOn }))
Expand All @@ -142,9 +121,7 @@ describe("Web3SwapClient", () => {
callback();
});
//@ts-ignore
mockedEther.providers.InfuraProvider = {
getWebSocketProvider: jest.fn()
};
mockedEther.providers.JsonRpcProvider = jest.fn();
//@ts-ignore
Swap.getContract = jest.fn(() => ({ on: mockedOn }))

Expand All @@ -158,8 +135,9 @@ describe("Web3SwapClient", () => {
callback({});
});
//@ts-ignore
mockedEther.providers.InfuraProvider = {
getWebSocketProvider: jest.fn()
mockedEther.providers = {
//@ts-ignore
JsonRpcProvider: jest.fn(),
};
//@ts-ignore
Swap.getContract = jest.fn(() => ({ on: mockedOn }))
Expand All @@ -174,8 +152,9 @@ describe("Web3SwapClient", () => {
callback({ _hex: undefined, _isBigNumber: true }, "a_wallet");
});
//@ts-ignore
mockedEther.providers.InfuraProvider = {
getWebSocketProvider: jest.fn()
mockedEther.providers = {
//@ts-ignore
JsonRpcProvider: jest.fn(),
};
//@ts-ignore
Swap.getContract = jest.fn(() => ({ on: mockedOn }))
Expand Down
58 changes: 20 additions & 38 deletions src/client/__tests__/Web3SwapERC20Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,37 @@ describe("Web3SwapERC20Client", () => {
//@ts-ignore
mockedEther.providers = {
//@ts-ignore
InfuraProvider: {
getWebSocketProvider: jest.fn()
},
getNetwork: jest.fn(() => ({ chainId: 5, name: "a_custom" }))
JsonRpcProvider: jest.fn(),
};
//@ts-ignore
SwapERC20.getContract = jest.fn(() => ({ on: jest.fn() }))

const client = new Web3SwapERC20Client(apiKey, fakeDatabase as Database);
client.connectToChain(5);

expect(mockedEther.providers.getNetwork).toHaveBeenCalledWith(5);
expect(mockedEther.providers.InfuraProvider.getWebSocketProvider).toHaveBeenCalledWith(5, "apikey");
expect(mockedEther.providers.JsonRpcProvider).toHaveBeenCalledWith("https://https://goerli.infura.io/v3/apikey");
});

it("Network is not found", () => {
//@ts-ignore
mockedEther.providers = {
//@ts-ignore
InfuraProvider: {
getWebSocketProvider: jest.fn()
},
//@ts-ignore
getNetwork: jest.fn(() => undefined)
JsonRpcProvider: jest.fn(),
};
//@ts-ignore
SwapERC20.getContract = jest.fn(() => ({ on: jest.fn() }))

const client = new Web3SwapERC20Client(apiKey, fakeDatabase as Database);
client.connectToChain(5);
client.connectToChain("aze");

expect(mockedEther.providers.getNetwork).toHaveBeenCalledWith(5);
expect(mockedEther.providers.InfuraProvider.getWebSocketProvider).not.toHaveBeenCalled();
expect(mockedEther.providers.JsonRpcProvider).not.toHaveBeenCalled();
});

it("Network can't be added twice", () => {
//@ts-ignore
mockedEther.providers = {
//@ts-ignore
InfuraProvider: {
getWebSocketProvider: jest.fn()
},
//@ts-ignore
getNetwork: jest.fn(() => ({ chainId: 5 }))
JsonRpcProvider: jest.fn(),
};
//@ts-ignore
SwapERC20.getContract = jest.fn(() => ({ on: jest.fn() }))
Expand All @@ -80,9 +67,8 @@ describe("Web3SwapERC20Client", () => {
client.connectToChain(5);
client.connectToChain(5);

expect(mockedEther.providers.getNetwork).toHaveBeenCalledWith(5);
expect(mockedEther.providers.InfuraProvider.getWebSocketProvider).toHaveBeenCalledTimes(1)
expect(mockedEther.providers.InfuraProvider.getWebSocketProvider).toBeCalledWith(5, "apikey")
expect(mockedEther.providers.JsonRpcProvider).toHaveBeenCalledTimes(1)
expect(mockedEther.providers.JsonRpcProvider).toBeCalledWith("https://https://goerli.infura.io/v3/apikey")
});
});

Expand All @@ -95,13 +81,10 @@ describe("Web3SwapERC20Client", () => {

//@ts-ignore
SwapERC20.getContract = jest.fn(() => ({ on: mockedOn }))
//@ts-ignore
mockedEther.providers = {
//@ts-ignore
InfuraProvider: {
getWebSocketProvider: jest.fn()
},
//@ts-ignore
getNetwork: jest.fn(() => ({ chainId: 5 }))
JsonRpcProvider: jest.fn(),
};

new Web3SwapERC20Client(apiKey, fakeDatabase as Database).connectToChain(network);
Expand All @@ -120,11 +103,7 @@ describe("Web3SwapERC20Client", () => {
//@ts-ignore
mockedEther.providers = {
//@ts-ignore
InfuraProvider: {
getWebSocketProvider: jest.fn()
},
//@ts-ignore
getNetwork: jest.fn(() => ({ chainId: 5 }))
JsonRpcProvider: jest.fn(),
};
//@ts-ignore
SwapERC20.getContract = jest.fn(() => ({ on: mockedOn }))
Expand All @@ -142,8 +121,9 @@ describe("Web3SwapERC20Client", () => {
callback();
});
//@ts-ignore
mockedEther.providers.InfuraProvider = {
getWebSocketProvider: jest.fn()
mockedEther.providers = {
//@ts-ignore
JsonRpcProvider: jest.fn(),
};
//@ts-ignore
SwapERC20.getContract = jest.fn(() => ({ on: mockedOn }))
Expand All @@ -158,8 +138,9 @@ describe("Web3SwapERC20Client", () => {
callback({});
});
//@ts-ignore
mockedEther.providers.InfuraProvider = {
getWebSocketProvider: jest.fn()
mockedEther.providers = {
//@ts-ignore
JsonRpcProvider: jest.fn(),
};
//@ts-ignore
SwapERC20.getContract = jest.fn(() => ({ on: mockedOn }))
Expand All @@ -174,8 +155,9 @@ describe("Web3SwapERC20Client", () => {
callback({ _hex: undefined, _isBigNumber: true }, "A_wallet");
});
//@ts-ignore
mockedEther.providers.InfuraProvider = {
getWebSocketProvider: jest.fn()
mockedEther.providers = {
//@ts-ignore
JsonRpcProvider: jest.fn(),
};
//@ts-ignore
SwapERC20.getContract = jest.fn(() => ({ on: mockedOn }))
Expand Down
Loading