Skip to content

Commit

Permalink
Merge 408c9f0 into fcb04b5
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmonette committed Nov 28, 2021
2 parents fcb04b5 + 408c9f0 commit 30e2eb2
Show file tree
Hide file tree
Showing 18 changed files with 328 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.env.*
.vscode
coverage
docs
Expand Down
3 changes: 2 additions & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export default {
preset: "ts-jest",
testEnvironment: "node",
testPathIgnorePatterns: ["/node_modules/", "__tests__/util/"],
collectCoverageFrom: ["src/**/*.{ts,tsx}", "!**/*.d.ts", "!src/typings/**", "!src/__tests__/**"],
testPathIgnorePatterns: ["/node_modules/"],
testRegex: "(/__tests__/.*\\.test)\\.(jsx?|tsx?)$",
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jpmonette/bscscan",
"version": "0.2.1",
"version": "0.2.3",
"description": "Explore the Binance Smart Chain using TypeScript and JavaScript",
"homepage": "https://github.com/jpmonette/bscscan-ts",
"author": "Jean-Philippe Monette <contact@jpmonette.net>",
Expand All @@ -16,6 +16,7 @@
},
"scripts": {
"build": "rimraf lib/ && mkdir lib && tsc",
"prepublish": "yarn build",
"test": "export NODE_ENV=test && npx jest --passWithNoTests",
"test-travis": "export NODE_ENV=test && npx jest --silent --passWithNoTests --coverage"
},
Expand All @@ -25,6 +26,7 @@
"@typescript-eslint/parser": "^4.23.0",
"codeclimate-test-reporter": "^0.5.1",
"coveralls": "^3.1.0",
"dotenv": "^10.0.0",
"eslint": "^7.26.0",
"isomorphic-fetch": "^3.0.0",
"jest": "^26.6.3",
Expand Down
47 changes: 47 additions & 0 deletions src/__tests__/data/contracts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export const contractResponses: Record<string, Record<string, any>> = {
get: {
getabi: {
status: "1",
message: "OK",
result: "[]",
},
getsourcecode: {
status: "1",
message: "OK",
result: [
{
SourceCode: "pragma solidity 0.6.12;",
ABI: "[]",
ContractName: "CakeToken",
CompilerVersion: "v0.6.12+commit.27d51765",
OptimizationUsed: "1",
Runs: "5000",
ConstructorArguments: "",
EVMVersion: "Default",
Library: "",
LicenseType: "None",
Proxy: "0",
Implementation: "",
SwarmSource: "ipfs://9755240809e31aec9fa5790314682233ca431b7c4f252d7e4bba347e2e742086",
},
],
},
},
post: {
verifysourcecode: {
status: "1",
message: "OK",
result: "a7xygt433w43um5fda3m6d9jk62jwtjhqdr9zcwj9fmfzcj14r",
},
verifyproxycontract: {
status: "1",
message: "OK",
result: "a7xygt433w43um5fda3m6d9jk62jwtjhqdr9zcwj9fmfzcj14r",
},
checkproxyverification: {
status: "1",
message: "OK",
result: "0xcbdcd3815b5f975e1a2c944a9b2cd1c985a1cb7f",
},
},
};
32 changes: 32 additions & 0 deletions src/__tests__/data/stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export const statsResponses: Record<string, any> = {
bnbprice: {
status: "1",
message: "OK",
result: { ethbtc: "0.009928", ethbtc_timestamp: "1639590487", ethusd: "582.6", ethusd_timestamp: "1639590487" },
},
bnbsupply: {
status: "1",
message: "OK",
result: "22479013170398300000000000",
},
validators: {
status: "1",
message: "OK",
result: [
{
validatorAddress: "0x9f8ccdafcc39f3c7d6ebf637c9151673cbc36b88",
validatorName: "",
validatorStatus: "0",
validatorVotingPower: "43379676392570",
validatorVotingPowerProportion: "0.0617",
},
{
validatorAddress: "0x2465176c461afb316ebc773c61faee85a6515daa",
validatorName: "",
validatorStatus: "0",
validatorVotingPower: "38039845465042",
validatorVotingPowerProportion: "0.0541",
},
],
},
};
29 changes: 29 additions & 0 deletions src/__tests__/modules/__snapshots__/contracts.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`contracts should checkProxyVerification 1`] = `"0xcbdcd3815b5f975e1a2c944a9b2cd1c985a1cb7f"`;

exports[`contracts should getAbi 1`] = `"[]"`;

exports[`contracts should getSourceCode 1`] = `
Array [
Object {
"ABI": "[]",
"CompilerVersion": "v0.6.12+commit.27d51765",
"ConstructorArguments": "",
"ContractName": "CakeToken",
"EVMVersion": "Default",
"Implementation": "",
"Library": "",
"LicenseType": "None",
"OptimizationUsed": "1",
"Proxy": "0",
"Runs": "5000",
"SourceCode": "pragma solidity 0.6.12;",
"SwarmSource": "ipfs://9755240809e31aec9fa5790314682233ca431b7c4f252d7e4bba347e2e742086",
},
]
`;

exports[`contracts should verifyProxyContract 1`] = `"a7xygt433w43um5fda3m6d9jk62jwtjhqdr9zcwj9fmfzcj14r"`;

exports[`contracts should verifySourceCode 1`] = `"a7xygt433w43um5fda3m6d9jk62jwtjhqdr9zcwj9fmfzcj14r"`;
31 changes: 31 additions & 0 deletions src/__tests__/modules/__snapshots__/stats.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`stats should getBNBLastPrice 1`] = `
Object {
"ethbtc": "0.009928",
"ethbtc_timestamp": "1639590487",
"ethusd": "582.6",
"ethusd_timestamp": "1639590487",
}
`;

exports[`stats should getBNBTotalSupply 1`] = `"22479013170398300000000000"`;

exports[`stats should getValidators 1`] = `
Array [
Object {
"validatorAddress": "0x9f8ccdafcc39f3c7d6ebf637c9151673cbc36b88",
"validatorName": "",
"validatorStatus": "0",
"validatorVotingPower": "43379676392570",
"validatorVotingPowerProportion": "0.0617",
},
Object {
"validatorAddress": "0x2465176c461afb316ebc773c61faee85a6515daa",
"validatorName": "",
"validatorStatus": "0",
"validatorVotingPower": "38039845465042",
"validatorVotingPowerProportion": "0.0541",
},
]
`;
5 changes: 2 additions & 3 deletions src/__tests__/modules/accounts.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import "isomorphic-fetch";
import { BscScan } from "../../client";
import { server, testAddress1, testAddress2, testAddress3 } from "../server";
import { API_KEY } from "../setup";

const addresses = [testAddress1, testAddress2];

const apikey = process.env.API_KEY;

describe("account", () => {
const client = new BscScan({ apikey });
const client = new BscScan({ apikey: API_KEY });

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
Expand Down
49 changes: 49 additions & 0 deletions src/__tests__/modules/contracts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import "isomorphic-fetch";
import { BscScan } from "../../client";
import { VerifySourceCodeRequest } from "../../typings";
import { server } from "../server";
import { API_KEY } from "../setup";

describe("contracts", () => {
const client = new BscScan({ apikey: API_KEY });

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

it("should getAbi", async () => {
const opts = { address: "0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82" };
expect(await client.contracts.getAbi(opts)).toMatchSnapshot();
});

it("should getSourceCode", async () => {
const opts = { address: "0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82" };
expect(await client.contracts.getSourceCode(opts)).toMatchSnapshot();
});

it("should verifySourceCode", async () => {
const opts: VerifySourceCodeRequest = {
contractAddress: "0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82",
contractName: "CAKE",
sourceCode: "",
compilerVersion: "v0.6.12+commit.27d51765",
optimizationUsed: "1",
runs: 5000,
licenseType: "None",
};
expect(await client.contracts.verifySourceCode(opts)).toMatchSnapshot();
});

it("should verifyProxyContract", async () => {
const opts = {
address: "0xbc46363a7669f6e12353fa95bb067aead3675c29",
expectedImplementation: "0xe45a5176bc0f2c1198e2451c4e4501d4ed9b65a6",
};
expect(await client.contracts.verifyProxyContract(opts)).toMatchSnapshot();
});

it("should checkProxyVerification", async () => {
const opts = { guid: "gwgrrnfy56zf6vc1fljuejwg6pelnc5yns6fg6y2i6zfpgzquz" };
expect(await client.contracts.checkProxyVerification(opts)).toMatchSnapshot();
});
});
24 changes: 24 additions & 0 deletions src/__tests__/modules/stats.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import "isomorphic-fetch";
import { BscScan } from "../../client";
import { server } from "../server";
import { API_KEY } from "../setup";

describe("stats", () => {
const client = new BscScan({ apikey: API_KEY });

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

it("should getBNBLastPrice", async () => {
expect(await client.stats.getBNBLastPrice()).toMatchSnapshot();
});

it("should getBNBTotalSupply", async () => {
expect(await client.stats.getBNBTotalSupply()).toMatchSnapshot();
});

it("should getValidators", async () => {
expect(await client.stats.getValidators()).toMatchSnapshot();
});
});
5 changes: 2 additions & 3 deletions src/__tests__/modules/transactions.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import "isomorphic-fetch";
import { BscScan } from "../../client";
import { server } from "../server";

const apikey = process.env.API_KEY;
import { API_KEY } from "../setup";

describe("transactions", () => {
const client = new BscScan({ apikey });
const client = new BscScan({ apikey: API_KEY });

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
Expand Down
24 changes: 24 additions & 0 deletions src/__tests__/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import dotenv from "dotenv";
dotenv.config();
import "isomorphic-fetch";
import { rest } from "msw";
import { setupServer } from "msw/node";
import { accountResponses } from "./data/accounts";
import { contractResponses } from "./data/contracts";
import { statsResponses } from "./data/stats";
import { transactionsResponses } from "./data/transactions";

export const testAddress1 = "1";
Expand All @@ -26,10 +30,30 @@ const handlers = [
}
return res(ctx.json(accountResponses[action]));
}

if (module === "contract" && contractResponses.get[action]) {
return res(ctx.json(contractResponses.get[action]));
}

if (module === "transaction" && transactionsResponses[action]) {
return res(ctx.json(transactionsResponses[action]));
}

if (module === "stats" && statsResponses[action]) {
return res(ctx.json(statsResponses[action]));
}

throw new Error("endpoint not found");
}),

rest.post(`*/api`, (req, res, ctx) => {
const module = req.url.searchParams.get("module") as string;
const action = req.url.searchParams.get("action") as string;

if (module === "contract" && contractResponses.post[action]) {
return res(ctx.json(contractResponses.post[action]));
}

throw new Error("endpoint not found");
}),
];
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const API_KEY = process.env.API_KEY;

0 comments on commit 30e2eb2

Please sign in to comment.