Skip to content
This repository was archived by the owner on Dec 26, 2023. It is now read-only.

Commit 05b8886

Browse files
committed
Add tests & typechain type generation
1 parent c63b8bc commit 05b8886

File tree

5 files changed

+176
-23
lines changed

5 files changed

+176
-23
lines changed

frontend/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ npm-debug.log*
2222
yarn-debug.log*
2323
yarn-error.log*
2424

25-
.next
25+
.next
26+
27+
/types/ethers-contracts
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { renderHook } from '@testing-library/react-hooks';
2+
3+
import useDevNFTSupply from '../../src/hooks/useDevNFTSupply';
4+
import { Ddao__factory } from '../../types/ethers-contracts/factories/Ddao__factory';
5+
6+
jest.mock('../../types/ethers-contracts/factories/Ddao__factory');
7+
const mockedDdaoFactory = Ddao__factory as jest.Mocked<typeof Ddao__factory>;
8+
9+
test('should return correct supply values & transition loading state', async () => {
10+
mockedDdaoFactory.connect.mockImplementationOnce((address, provider) => {
11+
return {
12+
totalSupply: async () => ({ toNumber: () => 2345 }),
13+
ownerOf: async (tokenId: number) => {
14+
if (tokenId === 8000)
15+
return '0x91d7a9e7c09477392290fe16c1b243e4a36d279a';
16+
17+
throw Error();
18+
},
19+
} as any;
20+
});
21+
22+
const { result, waitForValueToChange } = renderHook(() => useDevNFTSupply());
23+
24+
expect(result.current.loading).toBe(true);
25+
await waitForValueToChange(() => result.current.loading);
26+
expect(result.current.loading).toBe(false);
27+
28+
expect(result.current.maxSupply).toBe(8000);
29+
expect(result.current.totalSupply).toBe(2345);
30+
expect(result.current.lockedSupply).toBe(1);
31+
expect(result.current.publicSupply).toBe(2344);
32+
33+
expect(result.current.remainingTotalSupply).toBe(8000 - 2345);
34+
expect(result.current.remainingPublicSupply).toBe(7777 - 2344);
35+
expect(result.current.remainingLockedSupply).toBe(8000 - 7777 - 1);
36+
});
37+
38+
test('should correctly handle "all minted" state', async () => {
39+
mockedDdaoFactory.connect.mockImplementationOnce((address, provider) => {
40+
return {
41+
totalSupply: async () => ({ toNumber: () => 8000 }),
42+
ownerOf: async (tokenId: number) =>
43+
'0x91d7a9e7c09477392290fe16c1b243e4a36d279a',
44+
} as any;
45+
});
46+
47+
const { result, waitForValueToChange } = renderHook(() => useDevNFTSupply());
48+
49+
expect(result.current.loading).toBe(true);
50+
await waitForValueToChange(() => result.current.loading);
51+
expect(result.current.loading).toBe(false);
52+
53+
expect(result.current.maxSupply).toBe(8000);
54+
expect(result.current.totalSupply).toBe(8000);
55+
expect(result.current.lockedSupply).toBe(8000 - 7777);
56+
expect(result.current.publicSupply).toBe(7777);
57+
58+
expect(result.current.remainingTotalSupply).toBe(0);
59+
expect(result.current.remainingPublicSupply).toBe(0);
60+
expect(result.current.remainingLockedSupply).toBe(0);
61+
});

frontend/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939
"contributors:generate": "all-contributors generate",
4040
"type-check": "tsc --noEmit",
4141
"test": "jest",
42-
"test:watch": "jest --watch --detectOpenHandles"
42+
"test:watch": "jest --watch --detectOpenHandles",
43+
"generate-types": "typechain --target ethers-v5 src/artifacts/*.json",
44+
"postinstall": "yarn generate-types"
4345
},
4446
"eslintConfig": {
4547
"extends": [
@@ -63,6 +65,7 @@
6365
"devDependencies": {
6466
"@testing-library/cypress": "^8.0.1",
6567
"@testing-library/react-hooks": "^7.0.2",
68+
"@typechain/ethers-v5": "^8.0.1",
6669
"all-contributors-cli": "^6.20.0",
6770
"cypress": "^8.4.1",
6871
"eslint": "^7.32.0",
@@ -73,6 +76,7 @@
7376
"husky": "^7.0.2",
7477
"lint-staged": "^11.1.2",
7578
"prettier": "2.3.2",
79+
"typechain": "^6.0.2",
7680
"typescript": "^4.4.3"
7781
},
7882
"lint-staged": {

frontend/src/hooks/useDevNFTSupply.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,34 @@
11
import { useEffect, useState } from 'react';
22
import { ethers } from 'ethers';
3-
4-
import {
5-
DEVELOPER_DAO_CONTRACT,
6-
DEVELOPER_DAO_CONTRACT_ABI,
7-
} from '../utils/DeveloperDaoConstants';
83
import {
94
AlchemyProvider,
105
FallbackProvider,
116
InfuraProvider,
127
} from '@ethersproject/providers';
138

9+
import { DEVELOPER_DAO_CONTRACT } from '../utils/DeveloperDaoConstants';
10+
import { Ddao__factory } from '../../types/ethers-contracts/factories/Ddao__factory';
11+
1412
// TokenIDs start at 1. Tokens 1-7777 (supply of 7777) are open to mint by anyone. 7778-8000 are locked to mint by contract owner
1513
const MAX_SUPPLY = 8000;
1614
const PUBLIC_MAX_SUPPLY = 7777;
1715

18-
const contract = new ethers.Contract(
19-
DEVELOPER_DAO_CONTRACT,
20-
DEVELOPER_DAO_CONTRACT_ABI,
21-
new FallbackProvider([
22-
{ provider: new InfuraProvider() },
23-
{ provider: new AlchemyProvider() },
24-
]),
25-
);
26-
2716
export default function useDevNFTSupply() {
2817
const [totalSupply, setTotalSupply] = useState<number>(-1);
2918
const [lockedSupply, setLockedSupply] = useState<number>(-1);
3019

3120
useEffect(() => {
21+
const contract = Ddao__factory.connect(
22+
DEVELOPER_DAO_CONTRACT,
23+
new FallbackProvider([
24+
{ provider: new InfuraProvider() },
25+
{ provider: new AlchemyProvider() },
26+
]),
27+
);
28+
3229
// Fetches the count of minted DEV NFTs
3330
const fetchTotalSupply = async () => {
34-
const totalSupply: number = (
35-
await contract.functions.totalSupply()
36-
)[0].toNumber();
37-
31+
const totalSupply: number = (await contract.totalSupply()).toNumber();
3832
setTotalSupply(totalSupply);
3933
};
4034

@@ -48,9 +42,9 @@ export default function useDevNFTSupply() {
4842
lockedTokenId++
4943
) {
5044
requests.push(
51-
contract.functions.ownerOf(lockedTokenId).then(
45+
contract.ownerOf(lockedTokenId).then(
5246
// Return the owner's address
53-
(addressResponse) => addressResponse[0],
47+
(addressResponse) => addressResponse,
5448
// Return the zero-address if no owner was found
5549
() => ethers.constants.AddressZero,
5650
),

frontend/yarn.lock

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1956,6 +1956,14 @@
19561956
resolved "https://registry.npmjs.org/@tootallnate/once/-/once-1.1.2.tgz"
19571957
integrity sha512-RbzJvlNzmRq5c3O09UipeuXno4tA1FE6ikOjxZK0tuxVv3412l64l5t1W5pj4+rJq9vpkm/kwiR07aZXnsKPxw==
19581958

1959+
"@typechain/ethers-v5@^8.0.1":
1960+
version "8.0.1"
1961+
resolved "https://registry.yarnpkg.com/@typechain/ethers-v5/-/ethers-v5-8.0.1.tgz#26d8a8aa53524865aff62cd68a77329664a390a4"
1962+
integrity sha512-HFYlrdZWkKtuRjkKAZFY8WU7hcW0HPlDaQEX1W0Qo2VvpE7eCzhM4BcEGycjisKcBeb03fXMoBtHVcO1rZ9e9A==
1963+
dependencies:
1964+
lodash "^4.17.15"
1965+
ts-essentials "^7.0.1"
1966+
19591967
"@types/aria-query@^4.2.0":
19601968
version "4.2.2"
19611969
resolved "https://registry.npmjs.org/@types/aria-query/-/aria-query-4.2.2.tgz"
@@ -2101,6 +2109,11 @@
21012109
dependencies:
21022110
"@types/node" "*"
21032111

2112+
"@types/prettier@^2.1.1":
2113+
version "2.4.1"
2114+
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-2.4.1.tgz#e1303048d5389563e130f5bdd89d37a99acb75eb"
2115+
integrity sha512-Fo79ojj3vdEZOHg3wR9ksAMRz4P3S5fDB5e/YWZiFnyFQI1WY2Vftu9XoXVVtJfxB7Bpce/QTqWSSntkz2Znrw==
2116+
21042117
"@types/prettier@^2.1.5":
21052118
version "2.3.2"
21062119
resolved "https://registry.npmjs.org/@types/prettier/-/prettier-2.3.2.tgz"
@@ -2633,6 +2646,20 @@ aria-query@^4.2.2:
26332646
"@babel/runtime" "^7.10.2"
26342647
"@babel/runtime-corejs3" "^7.10.2"
26352648

2649+
array-back@^1.0.3, array-back@^1.0.4:
2650+
version "1.0.4"
2651+
resolved "https://registry.yarnpkg.com/array-back/-/array-back-1.0.4.tgz#644ba7f095f7ffcf7c43b5f0dc39d3c1f03c063b"
2652+
integrity sha1-ZEun8JX3/898Q7Xw3DnTwfA8Bjs=
2653+
dependencies:
2654+
typical "^2.6.0"
2655+
2656+
array-back@^2.0.0:
2657+
version "2.0.0"
2658+
resolved "https://registry.yarnpkg.com/array-back/-/array-back-2.0.0.tgz#6877471d51ecc9c9bfa6136fb6c7d5fe69748022"
2659+
integrity sha512-eJv4pLLufP3g5kcZry0j6WXpIbzYw9GUB4mVJZno9wfwiBxbizTnHCw3VJb07cBihbFX48Y7oSrW9y+gt4glyw==
2660+
dependencies:
2661+
typical "^2.6.1"
2662+
26362663
26372664
version "1.1.1"
26382665
resolved "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz"
@@ -3564,6 +3591,15 @@ combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6:
35643591
dependencies:
35653592
delayed-stream "~1.0.0"
35663593

3594+
command-line-args@^4.0.7:
3595+
version "4.0.7"
3596+
resolved "https://registry.yarnpkg.com/command-line-args/-/command-line-args-4.0.7.tgz#f8d1916ecb90e9e121eda6428e41300bfb64cc46"
3597+
integrity sha512-aUdPvQRAyBvQd2n7jXcsMDz68ckBJELXNzBybCHOibUWEg0mWTnaYCSRU8h9R+aNRSvDihJtssSRCiDRpLaezA==
3598+
dependencies:
3599+
array-back "^2.0.0"
3600+
find-replace "^1.0.3"
3601+
typical "^2.6.1"
3602+
35673603
commander@^5.1.0:
35683604
version "5.1.0"
35693605
resolved "https://registry.yarnpkg.com/commander/-/commander-5.1.0.tgz#46abbd1652f8e059bddaef99bbdcb2ad9cf179ae"
@@ -5180,6 +5216,14 @@ [email protected]:
51805216
make-dir "^3.0.2"
51815217
pkg-dir "^4.1.0"
51825218

5219+
find-replace@^1.0.3:
5220+
version "1.0.3"
5221+
resolved "https://registry.yarnpkg.com/find-replace/-/find-replace-1.0.3.tgz#b88e7364d2d9c959559f388c66670d6130441fa0"
5222+
integrity sha1-uI5zZNLZyVlVnziMZmcNYTBEH6A=
5223+
dependencies:
5224+
array-back "^1.0.4"
5225+
test-value "^2.1.0"
5226+
51835227
find-root@^1.1.0:
51845228
version "1.1.0"
51855229
resolved "https://registry.npmjs.org/find-root/-/find-root-1.1.0.tgz"
@@ -5294,6 +5338,15 @@ fs-extra@^4.0.2:
52945338
jsonfile "^4.0.0"
52955339
universalify "^0.1.0"
52965340

5341+
fs-extra@^7.0.0:
5342+
version "7.0.1"
5343+
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-7.0.1.tgz#4f189c44aa123b895f722804f55ea23eadc348e9"
5344+
integrity sha512-YJDaCJZEnBmcbw13fvdAM9AwNOJwOzrE4pqMqBq5nFiEqXUqHwlK4B+3pUw6JNvfSPtX05xFHtYy/1ni01eGCw==
5345+
dependencies:
5346+
graceful-fs "^4.1.2"
5347+
jsonfile "^4.0.0"
5348+
universalify "^0.1.0"
5349+
52975350
fs-extra@^9.1.0:
52985351
version "9.1.0"
52995352
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.1.0.tgz#5954460c764a8da2094ba3554bf839e6b9a7c86d"
@@ -7285,7 +7338,7 @@ mkdirp-promise@^5.0.1:
72857338
dependencies:
72867339
mkdirp "*"
72877340

7288-
mkdirp@*:
7341+
mkdirp@*, mkdirp@^1.0.4:
72897342
version "1.0.4"
72907343
resolved "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz"
72917344
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==
@@ -8093,6 +8146,11 @@ [email protected]:
80938146
resolved "https://registry.npmjs.org/prettier/-/prettier-2.3.2.tgz"
80948147
integrity sha512-lnJzDfJ66zkMy58OL5/NY5zp70S7Nz6KqcKkXYzn2tMVrNxvbqaBpg7H3qHaLxCJ5lNMsGuM8+ohS7cZrthdLQ==
80958148

8149+
prettier@^2.1.2:
8150+
version "2.4.1"
8151+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.4.1.tgz#671e11c89c14a4cfc876ce564106c4a6726c9f5c"
8152+
integrity sha512-9fbDAXSBcc6Bs1mZrDYb3XKzDLm4EXXL9sC1LqKP5rZkT6KRr/rf9amVUcODVXgguK/isJz0d0hP72WeaKWsvA==
8153+
80968154
pretty-bytes@^5.6.0:
80978155
version "5.6.0"
80988156
resolved "https://registry.yarnpkg.com/pretty-bytes/-/pretty-bytes-5.6.0.tgz#356256f643804773c82f64723fe78c92c62beaeb"
@@ -9390,6 +9448,14 @@ test-exclude@^6.0.0:
93909448
glob "^7.1.4"
93919449
minimatch "^3.0.4"
93929450

9451+
test-value@^2.1.0:
9452+
version "2.1.0"
9453+
resolved "https://registry.yarnpkg.com/test-value/-/test-value-2.1.0.tgz#11da6ff670f3471a73b625ca4f3fdcf7bb748291"
9454+
integrity sha1-Edpv9nDzRxpztiXKTz/c97t0gpE=
9455+
dependencies:
9456+
array-back "^1.0.3"
9457+
typical "^2.6.0"
9458+
93939459
text-table@^0.2.0:
93949460
version "0.2.0"
93959461
resolved "https://registry.npmjs.org/text-table/-/text-table-0.2.0.tgz"
@@ -9519,6 +9585,11 @@ tr46@~0.0.3:
95199585
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
95209586
integrity sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=
95219587

9588+
ts-essentials@^7.0.1:
9589+
version "7.0.3"
9590+
resolved "https://registry.yarnpkg.com/ts-essentials/-/ts-essentials-7.0.3.tgz#686fd155a02133eedcc5362dc8b5056cde3e5a38"
9591+
integrity sha512-8+gr5+lqO3G84KdiTSMRLtuyJ+nTBVRKuCrK4lidMPdVeEp0uqC875uE5NMcaA7YYMN7XsNiFQuMvasF8HT/xQ==
9592+
95229593
ts-pnp@^1.1.6:
95239594
version "1.2.0"
95249595
resolved "https://registry.npmjs.org/ts-pnp/-/ts-pnp-1.2.0.tgz"
@@ -9625,6 +9696,22 @@ type@^2.0.0:
96259696
resolved "https://registry.npmjs.org/type/-/type-2.3.0.tgz"
96269697
integrity sha512-rgPIqOdfK/4J9FhiVrZ3cveAjRRo5rsQBAIhnylX874y1DX/kEKSVdLsnuHB6l1KTjHyU01VjiMBHgU2adejyg==
96279698

9699+
typechain@^6.0.2:
9700+
version "6.0.2"
9701+
resolved "https://registry.yarnpkg.com/typechain/-/typechain-6.0.2.tgz#0a1d6328aa934dfd66c8941cd94c1d3491cc34f0"
9702+
integrity sha512-fphYQ2+r5z38eq02qr4KjgU9/Xdda8Cj+eV2QHSRXhDPyhEVv+ln9123iGjjDLEhGyROb3DQkbvjm3Dl0GtUeQ==
9703+
dependencies:
9704+
"@types/prettier" "^2.1.1"
9705+
command-line-args "^4.0.7"
9706+
debug "^4.1.1"
9707+
fs-extra "^7.0.0"
9708+
glob "^7.1.6"
9709+
js-sha3 "^0.8.0"
9710+
lodash "^4.17.15"
9711+
mkdirp "^1.0.4"
9712+
prettier "^2.1.2"
9713+
ts-essentials "^7.0.1"
9714+
96289715
[email protected], typedarray-to-buffer@^3.1.5:
96299716
version "3.1.5"
96309717
resolved "https://registry.npmjs.org/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz"
@@ -9637,6 +9724,11 @@ typescript@^4.4.3:
96379724
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.4.3.tgz#bdc5407caa2b109efd4f82fe130656f977a29324"
96389725
integrity sha512-4xfscpisVgqqDfPaJo5vkd+Qd/ItkoagnHpufr+i2QCHBsNYp+G7UAoyFl8aPtx879u38wPV65rZ8qbGZijalA==
96399726

9727+
typical@^2.6.0, typical@^2.6.1:
9728+
version "2.6.1"
9729+
resolved "https://registry.yarnpkg.com/typical/-/typical-2.6.1.tgz#5c080e5d661cbbe38259d2e70a3c7253e873881d"
9730+
integrity sha1-XAgOXWYcu+OCWdLnCjxyU+hziB0=
9731+
96409732
ultron@~1.1.0:
96419733
version "1.1.1"
96429734
resolved "https://registry.npmjs.org/ultron/-/ultron-1.1.1.tgz"

0 commit comments

Comments
 (0)