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

Commit

Permalink
Revert "remove api key"
Browse files Browse the repository at this point in the history
This reverts commit 6e9066b.
  • Loading branch information
Mitsichury committed Aug 24, 2023
1 parent 6e9066b commit 5405fee
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28 deletions.
6 changes: 4 additions & 2 deletions src/client/Web3SwapClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { getProviderUrl } from './getProviderUrl.js';
export class Web3SwapClient {
private contracts: Contract[] = [];
private database: Database;
private apiKey: string;
private registeredChains: string[] = [];

constructor(database: Database) {
constructor(apiKey: string, database: Database) {
this.database = database;
this.apiKey = apiKey;
}

public connectToChain(network: number | string): boolean {
Expand All @@ -28,7 +30,7 @@ export class Web3SwapClient {
return true
}

provider = new ethers.providers.JsonRpcProvider(getProviderUrl(chainId))
provider = new ethers.providers.JsonRpcProvider(getProviderUrl(chainId, this.apiKey))
contract = Swap.getContract(provider, chainId);
} catch (err) {
return false
Expand Down
6 changes: 4 additions & 2 deletions src/client/Web3SwapERC20Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { getProviderUrl } from './getProviderUrl.js';
export class Web3SwapERC20Client {
private contracts: Contract[] = [];
private database: Database;
private apiKey: string;
private registeredChains: string[] = [];

constructor(database: Database) {
constructor(apiKey: string, database: Database) {
this.database = database;
this.apiKey = apiKey;
}

public connectToChain(network: number | string): boolean {
Expand All @@ -27,7 +29,7 @@ export class Web3SwapERC20Client {
console.log("Already connected");
return true
}
provider = new ethers.providers.JsonRpcProvider(getProviderUrl(chainId))
provider = new ethers.providers.JsonRpcProvider(getProviderUrl(chainId, this.apiKey))
contract = SwapERC20.getContract(provider, chainId);
} catch (err) {
return false
Expand Down
20 changes: 10 additions & 10 deletions src/client/__tests__/Web3SwapClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ describe("Web3SwapClient", () => {
//@ts-ignore
Swap.getContract = jest.fn(() => ({ on: jest.fn() }))

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

expect(mockedEther.providers.JsonRpcProvider).toHaveBeenCalledWith("https://https://goerli.infura.io/v3");
expect(mockedEther.providers.JsonRpcProvider).toHaveBeenCalledWith("https://https://goerli.infura.io/v3/apikey");
});

it("Network is not found", () => {
Expand All @@ -48,7 +48,7 @@ describe("Web3SwapClient", () => {
//@ts-ignore
Swap.getContract = jest.fn(() => ({ on: jest.fn() }))

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

expect(mockedEther.providers.JsonRpcProvider).not.toHaveBeenCalled();
Expand All @@ -63,12 +63,12 @@ describe("Web3SwapClient", () => {
//@ts-ignore
Swap.getContract = jest.fn(() => ({ on: jest.fn() }))

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

expect(mockedEther.providers.JsonRpcProvider).toHaveBeenCalledTimes(1)
expect(mockedEther.providers.JsonRpcProvider).toBeCalledWith("https://https://goerli.infura.io/v3");
expect(mockedEther.providers.JsonRpcProvider).toBeCalledWith("https://https://goerli.infura.io/v3/apikey");
});
});

Expand All @@ -87,7 +87,7 @@ describe("Web3SwapClient", () => {
JsonRpcProvider: jest.fn(),
};

new Web3SwapClient(fakeDatabase as Database).connectToChain(network);
new Web3SwapClient(apiKey, fakeDatabase as Database).connectToChain(network);

expect(mockedOn).toHaveBeenCalledTimes(2);
expect(fakeDatabase.deleteOrder).toHaveBeenCalledTimes(1);
Expand All @@ -108,7 +108,7 @@ describe("Web3SwapClient", () => {
//@ts-ignore
Swap.getContract = jest.fn(() => ({ on: mockedOn }))

new Web3SwapClient(fakeDatabase as Database).connectToChain(network);
new Web3SwapClient(apiKey, fakeDatabase as Database).connectToChain(network);

expect(mockedOn).toHaveBeenCalledTimes(2);
expect(fakeDatabase.deleteOrder).toHaveBeenCalledTimes(1);
Expand All @@ -125,7 +125,7 @@ describe("Web3SwapClient", () => {
//@ts-ignore
Swap.getContract = jest.fn(() => ({ on: mockedOn }))

new Web3SwapClient(fakeDatabase as Database).connectToChain(network);
new Web3SwapClient(apiKey, fakeDatabase as Database).connectToChain(network);

expect(fakeDatabase.deleteOrder).not.toHaveBeenCalled();
});
Expand All @@ -142,7 +142,7 @@ describe("Web3SwapClient", () => {
//@ts-ignore
Swap.getContract = jest.fn(() => ({ on: mockedOn }))

new Web3SwapClient(fakeDatabase as Database).connectToChain(network);
new Web3SwapClient(apiKey, fakeDatabase as Database).connectToChain(network);

expect(fakeDatabase.deleteOrder).not.toHaveBeenCalled();
});
Expand All @@ -159,7 +159,7 @@ describe("Web3SwapClient", () => {
//@ts-ignore
Swap.getContract = jest.fn(() => ({ on: mockedOn }))

new Web3SwapClient(fakeDatabase as Database).connectToChain(network);
new Web3SwapClient(apiKey, fakeDatabase as Database).connectToChain(network);

expect(fakeDatabase.deleteOrder).not.toHaveBeenCalled();
});
Expand Down
20 changes: 10 additions & 10 deletions src/client/__tests__/Web3SwapERC20Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ describe("Web3SwapERC20Client", () => {
//@ts-ignore
SwapERC20.getContract = jest.fn(() => ({ on: jest.fn() }))

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

expect(mockedEther.providers.JsonRpcProvider).toHaveBeenCalledWith("https://https://goerli.infura.io/v3");
expect(mockedEther.providers.JsonRpcProvider).toHaveBeenCalledWith("https://https://goerli.infura.io/v3/apikey");
});

it("Network is not found", () => {
Expand All @@ -48,7 +48,7 @@ describe("Web3SwapERC20Client", () => {
//@ts-ignore
SwapERC20.getContract = jest.fn(() => ({ on: jest.fn() }))

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

expect(mockedEther.providers.JsonRpcProvider).not.toHaveBeenCalled();
Expand All @@ -63,12 +63,12 @@ describe("Web3SwapERC20Client", () => {
//@ts-ignore
SwapERC20.getContract = jest.fn(() => ({ on: jest.fn() }))

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

expect(mockedEther.providers.JsonRpcProvider).toHaveBeenCalledTimes(1)
expect(mockedEther.providers.JsonRpcProvider).toBeCalledWith("https://https://goerli.infura.io/v3")
expect(mockedEther.providers.JsonRpcProvider).toBeCalledWith("https://https://goerli.infura.io/v3/apikey")
});
});

Expand All @@ -87,7 +87,7 @@ describe("Web3SwapERC20Client", () => {
JsonRpcProvider: jest.fn(),
};

new Web3SwapERC20Client(fakeDatabase as Database).connectToChain(network);
new Web3SwapERC20Client(apiKey, fakeDatabase as Database).connectToChain(network);

expect(mockedOn).toHaveBeenCalledTimes(2);
expect(fakeDatabase.deleteOrderERC20).toHaveBeenCalledTimes(1);
Expand All @@ -108,7 +108,7 @@ describe("Web3SwapERC20Client", () => {
//@ts-ignore
SwapERC20.getContract = jest.fn(() => ({ on: mockedOn }))

new Web3SwapERC20Client(fakeDatabase as Database).connectToChain(network);
new Web3SwapERC20Client(apiKey, fakeDatabase as Database).connectToChain(network);

expect(mockedOn).toHaveBeenCalledTimes(2);
expect(fakeDatabase.deleteOrderERC20).toHaveBeenCalledTimes(1);
Expand All @@ -128,7 +128,7 @@ describe("Web3SwapERC20Client", () => {
//@ts-ignore
SwapERC20.getContract = jest.fn(() => ({ on: mockedOn }))

new Web3SwapERC20Client(fakeDatabase as Database).connectToChain(network);
new Web3SwapERC20Client(apiKey, fakeDatabase as Database).connectToChain(network);

expect(fakeDatabase.deleteOrderERC20).not.toHaveBeenCalled();
});
Expand All @@ -145,7 +145,7 @@ describe("Web3SwapERC20Client", () => {
//@ts-ignore
SwapERC20.getContract = jest.fn(() => ({ on: mockedOn }))

new Web3SwapERC20Client(fakeDatabase as Database).connectToChain(network);
new Web3SwapERC20Client(apiKey, fakeDatabase as Database).connectToChain(network);

expect(fakeDatabase.deleteOrderERC20).not.toHaveBeenCalled();
});
Expand All @@ -162,7 +162,7 @@ describe("Web3SwapERC20Client", () => {
//@ts-ignore
SwapERC20.getContract = jest.fn(() => ({ on: mockedOn }))

new Web3SwapERC20Client(fakeDatabase as Database).connectToChain(network);
new Web3SwapERC20Client(apiKey, fakeDatabase as Database).connectToChain(network);

expect(fakeDatabase.deleteOrderERC20).not.toHaveBeenCalled();
});
Expand Down
4 changes: 2 additions & 2 deletions src/client/getProviderUrl.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {apiUrls} from '@airswap/constants'

export function getProviderUrl(chainId: number) {
export function getProviderUrl(chainId: number, apiKey: string) {
const host = apiUrls[chainId];

if (!host) {
throw new Error("Unknown chain ID");
}

return `https://${host}`;
return host.includes("infura.io/v3") ? `https://${host}/${apiKey}` : `https://${host}`;
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ function getWeb3SwapClient(database: Database, network: number) {
}
const apiKey: string = process.env.API_KEY as string;

const swapClientOrderERC20 = new Web3SwapERC20Client(database);
const swapClientOrder = new Web3SwapClient(database);
const swapClientOrderERC20 = new Web3SwapERC20Client(apiKey, database);
const swapClientOrder = new Web3SwapClient(apiKey, database);

swapClientOrder.connectToChain(network);
swapClientOrderERC20.connectToChain(network);
Expand Down

0 comments on commit 5405fee

Please sign in to comment.