Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GMS-1103] Add erc20 contract client #98

Merged
merged 7 commits into from
Sep 27, 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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ artifacts
cache
coverage
typechain
dist
6 changes: 3 additions & 3 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ jobs:

- name: Install dependencies
run: |
npm ci
yarn install --frozen-lockfile

- name: Compile contracts
run: |
npm run compile
yarn compile

- name: Build dist files
run: |
rm -rf dist && npm run build
rm -rf dist && yarn build

- name: Publish package
uses: JS-DevTools/npm-publish@v1
Expand Down
35 changes: 27 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: test

on:
push:
branches: [ main ]
branches: [main]
pull_request:
branches: [ main ]
branches: [main]

jobs:
hardhat-test:
Expand All @@ -17,10 +17,10 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: lts/*
- name: Install NPM Dependencies
run: npm ci
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Run Tests
run: npm run test
run: yarn test
lint:
name: Run eslint
runs-on: ubuntu-latest
Expand All @@ -31,7 +31,26 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: lts/*
- name: Install NPM Dependencies
run: npm ci
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Run eslint
run: npm run lint
run: yarn lint
publish:
name: Publish to NPM (dry run)
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version-file: ".nvmrc"
registry-url: https://registry.npmjs.org/
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Compile contracts
run: yarn compile
- name: Build dist files
run: rm -rf dist && yarn build
- name: Test publish
run: npm pack --dry-run
5 changes: 3 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ git checkout -b <your-branch-name>
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).

```
npm test
yarn test
```

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)
Expand All @@ -79,4 +79,5 @@ git push origin <your-branch-name>
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!

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

To release the package to NPM, simply create a new GitHub release and the "Publish to NPM" GitHub action will release it to NPM.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ These contracts are feature-rich and are the recommended standard on Immutable z
### Installation

```
$ npm install @imtbl/zkevm-contracts
$ yarn install @imtbl/zkevm-contracts
```

### Usage
Expand Down
81 changes: 81 additions & 0 deletions clients/erc20.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Overrides } from "ethers";
import { Provider } from "@ethersproject/providers";
import { BigNumberish, BigNumber } from "@ethersproject/bignumber";
import { CallOverrides, PopulatedTransaction } from "@ethersproject/contracts";

import { ERC20 } from "../typechain-types/@openzeppelin/contracts/token/ERC20";
import { ERC20__factory } from "../typechain-types/factories/@openzeppelin/contracts/token/ERC20/ERC20__factory";
import { PromiseOrValue } from "../typechain-types/common";

export class ERC20Client {
private readonly contract: ERC20;

constructor(contractAddress: string) {
const factory = new ERC20__factory();
this.contract = factory.attach(contractAddress);
}

/**
* @returns a promise that resolves with a BigNumber that represents the amount of tokens in existence
*/
public async totalSupply(provider: Provider, overrides: CallOverrides = {}): Promise<BigNumber> {
return this.contract.connect(provider).totalSupply(overrides);
}

/**
* @returns a promise that resolves with a BigNumber that represents the amount of tokens owned by account
*/
public async balanceOf(
provider: Provider,
account: PromiseOrValue<string>,
overrides: CallOverrides = {}
): Promise<BigNumber> {
return this.contract.connect(provider).balanceOf(account, overrides);
}

/**
* @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
*/
public async allowance(
provider: Provider,
owner: PromiseOrValue<string>,
spender: PromiseOrValue<string>,
overrides: CallOverrides = {}
): Promise<BigNumber> {
return this.contract.connect(provider).allowance(owner, spender, overrides);
}

/**
* @returns a promise that resolves with a populated transaction
*/
public async populateTransfer(
to: PromiseOrValue<string>,
amount: PromiseOrValue<BigNumberish>,
overrides?: Overrides & { from?: PromiseOrValue<string> }
): Promise<PopulatedTransaction> {
return this.contract.populateTransaction.transfer(to, amount, overrides);
}

/**
* @returns a promise that resolves with a populated transaction
*/
public async populateApprove(
spender: PromiseOrValue<string>,
amount: PromiseOrValue<BigNumberish>,
overrides: Overrides & { from?: PromiseOrValue<string> } = {}
): Promise<PopulatedTransaction> {
return this.contract.populateTransaction.approve(spender, amount, overrides);
}

/**
* @returns a promise that resolves with a populated transaction
*/
public async populateTransferFrom(
from: PromiseOrValue<string>,
to: PromiseOrValue<string>,
amount: PromiseOrValue<BigNumberish>,
overrides: Overrides & { from?: PromiseOrValue<string> } = {}
): Promise<PopulatedTransaction> {
return this.contract.populateTransaction.transferFrom(from, to, amount, overrides);
}
}
2 changes: 1 addition & 1 deletion clients/erc721-mint-by-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type TransferRequest = ImmutableERC721Base.TransferRequestStruct;
// Struct for safe burning multiple tokens from a single address.
export type IDBurn = ImmutableERC721Base.IDBurnStruct;

export class ERC721MintByID {
export class ERC721MintByIDClient {
private readonly contract: ImmutableERC721MintByID;

constructor(contractAddress: string) {
Expand Down
2 changes: 1 addition & 1 deletion clients/erc721.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type TransferRequest = ERC721Hybrid.TransferRequestStruct;
// Struct for burning multiple tokens from the same address
export type IDBurn = ERC721Hybrid.IDBurnStruct;

export class ERC721 {
export class ERC721Client {
private readonly contract: ImmutableERC721;

constructor(contractAddress: string) {
Expand Down
5 changes: 3 additions & 2 deletions clients/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { ERC721 as ERC721Client } from "./erc721";
export { ERC721MintByID as ERC721MintByIDClient } from "./erc721-mint-by-id";
export { ERC20Client } from "./erc20";
export { ERC721Client } from "./erc721";
export { ERC721MintByIDClient } from "./erc721-mint-by-id";
6 changes: 6 additions & 0 deletions contracts/token/erc20/ImmutableERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

abstract contract ImmutableERC20 is ERC20 {}
Loading
Loading