Skip to content

Commit 70fa817

Browse files
[GMS-1103] Add erc20 contract client (#98)
* add erc20 client * upgrade to yarn * update index * udpate workflows * clean up * fix lint * add test publish --------- Co-authored-by: Allan Almeida <[email protected]>
1 parent 81dc875 commit 70fa817

File tree

13 files changed

+9919
-19610
lines changed

13 files changed

+9919
-19610
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ artifacts
33
cache
44
coverage
55
typechain
6+
dist

.github/workflows/publish.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ jobs:
4848
4949
- name: Install dependencies
5050
run: |
51-
npm ci
51+
yarn install --frozen-lockfile
5252
5353
- name: Compile contracts
5454
run: |
55-
npm run compile
55+
yarn compile
5656
5757
- name: Build dist files
5858
run: |
59-
rm -rf dist && npm run build
59+
rm -rf dist && yarn build
6060
6161
- name: Publish package
6262
uses: JS-DevTools/npm-publish@v1

.github/workflows/test.yml

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: test
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
7-
branches: [ main ]
7+
branches: [main]
88

99
jobs:
1010
hardhat-test:
@@ -17,10 +17,10 @@ jobs:
1717
uses: actions/setup-node@v3
1818
with:
1919
node-version: lts/*
20-
- name: Install NPM Dependencies
21-
run: npm ci
20+
- name: Install dependencies
21+
run: yarn install --frozen-lockfile
2222
- name: Run Tests
23-
run: npm run test
23+
run: yarn test
2424
lint:
2525
name: Run eslint
2626
runs-on: ubuntu-latest
@@ -31,7 +31,26 @@ jobs:
3131
uses: actions/setup-node@v3
3232
with:
3333
node-version: lts/*
34-
- name: Install NPM Dependencies
35-
run: npm ci
34+
- name: Install dependencies
35+
run: yarn install --frozen-lockfile
3636
- name: Run eslint
37-
run: npm run lint
37+
run: yarn lint
38+
publish:
39+
name: Publish to NPM (dry run)
40+
runs-on: ubuntu-latest
41+
steps:
42+
- name: Checkout Code
43+
uses: actions/checkout@v3
44+
- name: Setup Node
45+
uses: actions/setup-node@v3
46+
with:
47+
node-version-file: ".nvmrc"
48+
registry-url: https://registry.npmjs.org/
49+
- name: Install dependencies
50+
run: yarn install --frozen-lockfile
51+
- name: Compile contracts
52+
run: yarn compile
53+
- name: Build dist files
54+
run: rm -rf dist && yarn build
55+
- name: Test publish
56+
run: npm pack --dry-run

CONTRIBUTING.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ git checkout -b <your-branch-name>
6161
6. Be sure to run the tests and set up the relevant linters to ensure all GitHub checks pass (see GitHub issues: https://docs.github.com/en/issues/tracking-your-work-with-issues/about-issues for more information).
6262

6363
```
64-
npm test
64+
yarn test
6565
```
6666

6767
7. Add and commit your changes, including a comprehensive commit message summarising your changes, then push changes to your fork. (e.g. Fixed formatting issue with ImmutableERC721MintByID.sol)
@@ -79,4 +79,5 @@ git push origin <your-branch-name>
7979
9. We will review the pull requests and request any necessary changes. If all the checks (linting, compilation, tests) pass and everything looks good, your code will be merged into the original repository. Congratulations, and thank you for your contribution!
8080

8181
## Releasing
82-
To release the package to NPM, simply create a new GitHub release and the "Publish to NPM" GitHub action will release it to NPM.
82+
83+
To release the package to NPM, simply create a new GitHub release and the "Publish to NPM" GitHub action will release it to NPM.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ These contracts are feature-rich and are the recommended standard on Immutable z
1919
### Installation
2020

2121
```
22-
$ npm install @imtbl/zkevm-contracts
22+
$ yarn install @imtbl/zkevm-contracts
2323
```
2424

2525
### Usage

clients/erc20.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { Overrides } from "ethers";
2+
import { Provider } from "@ethersproject/providers";
3+
import { BigNumberish, BigNumber } from "@ethersproject/bignumber";
4+
import { CallOverrides, PopulatedTransaction } from "@ethersproject/contracts";
5+
6+
import { ERC20 } from "../typechain-types/@openzeppelin/contracts/token/ERC20";
7+
import { ERC20__factory } from "../typechain-types/factories/@openzeppelin/contracts/token/ERC20/ERC20__factory";
8+
import { PromiseOrValue } from "../typechain-types/common";
9+
10+
export class ERC20Client {
11+
private readonly contract: ERC20;
12+
13+
constructor(contractAddress: string) {
14+
const factory = new ERC20__factory();
15+
this.contract = factory.attach(contractAddress);
16+
}
17+
18+
/**
19+
* @returns a promise that resolves with a BigNumber that represents the amount of tokens in existence
20+
*/
21+
public async totalSupply(provider: Provider, overrides: CallOverrides = {}): Promise<BigNumber> {
22+
return this.contract.connect(provider).totalSupply(overrides);
23+
}
24+
25+
/**
26+
* @returns a promise that resolves with a BigNumber that represents the amount of tokens owned by account
27+
*/
28+
public async balanceOf(
29+
provider: Provider,
30+
account: PromiseOrValue<string>,
31+
overrides: CallOverrides = {}
32+
): Promise<BigNumber> {
33+
return this.contract.connect(provider).balanceOf(account, overrides);
34+
}
35+
36+
/**
37+
* @returns a promise that resolves with a BigNumber that represents the remaining number of tokens that spender will be allowed to spend on behalf of owner through transferFrom
38+
*/
39+
public async allowance(
40+
provider: Provider,
41+
owner: PromiseOrValue<string>,
42+
spender: PromiseOrValue<string>,
43+
overrides: CallOverrides = {}
44+
): Promise<BigNumber> {
45+
return this.contract.connect(provider).allowance(owner, spender, overrides);
46+
}
47+
48+
/**
49+
* @returns a promise that resolves with a populated transaction
50+
*/
51+
public async populateTransfer(
52+
to: PromiseOrValue<string>,
53+
amount: PromiseOrValue<BigNumberish>,
54+
overrides?: Overrides & { from?: PromiseOrValue<string> }
55+
): Promise<PopulatedTransaction> {
56+
return this.contract.populateTransaction.transfer(to, amount, overrides);
57+
}
58+
59+
/**
60+
* @returns a promise that resolves with a populated transaction
61+
*/
62+
public async populateApprove(
63+
spender: PromiseOrValue<string>,
64+
amount: PromiseOrValue<BigNumberish>,
65+
overrides: Overrides & { from?: PromiseOrValue<string> } = {}
66+
): Promise<PopulatedTransaction> {
67+
return this.contract.populateTransaction.approve(spender, amount, overrides);
68+
}
69+
70+
/**
71+
* @returns a promise that resolves with a populated transaction
72+
*/
73+
public async populateTransferFrom(
74+
from: PromiseOrValue<string>,
75+
to: PromiseOrValue<string>,
76+
amount: PromiseOrValue<BigNumberish>,
77+
overrides: Overrides & { from?: PromiseOrValue<string> } = {}
78+
): Promise<PopulatedTransaction> {
79+
return this.contract.populateTransaction.transferFrom(from, to, amount, overrides);
80+
}
81+
}

clients/erc721-mint-by-id.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type TransferRequest = ImmutableERC721Base.TransferRequestStruct;
1414
// Struct for safe burning multiple tokens from a single address.
1515
export type IDBurn = ImmutableERC721Base.IDBurnStruct;
1616

17-
export class ERC721MintByID {
17+
export class ERC721MintByIDClient {
1818
private readonly contract: ImmutableERC721MintByID;
1919

2020
constructor(contractAddress: string) {

clients/erc721.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type TransferRequest = ERC721Hybrid.TransferRequestStruct;
1717
// Struct for burning multiple tokens from the same address
1818
export type IDBurn = ERC721Hybrid.IDBurnStruct;
1919

20-
export class ERC721 {
20+
export class ERC721Client {
2121
private readonly contract: ImmutableERC721;
2222

2323
constructor(contractAddress: string) {

clients/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
export { ERC721 as ERC721Client } from "./erc721";
2-
export { ERC721MintByID as ERC721MintByIDClient } from "./erc721-mint-by-id";
1+
export { ERC20Client } from "./erc20";
2+
export { ERC721Client } from "./erc721";
3+
export { ERC721MintByIDClient } from "./erc721-mint-by-id";
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
5+
6+
abstract contract ImmutableERC20 is ERC20 {}

0 commit comments

Comments
 (0)