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

Ported project to typescript. #65

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ node_modules
#Hardhat files
cache
artifacts

typechain-types
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Then, on a new terminal, go to the repository's root folder and run this to
deploy your contract:

```sh
npx hardhat run scripts/deploy.js --network localhost
npx hardhat run scripts/deploy.ts --network localhost
```

Finally, we can run the frontend with:
Expand Down
2 changes: 1 addition & 1 deletion frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This Dapp consists of multiple React Components, which you can find in

Most of them are presentational components, have no logic, and just render HTML.

The core functionality is implemented in `src/components/Dapp.js`, which has
The core functionality is implemented in `src/components/Dapp.ts`, which has
examples of how to connect to the user's wallet, initialize your Ethereum
connection and contracts, read from the contract's state, and send transactions.

Expand Down
87 changes: 81 additions & 6 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
"bootstrap": "^4.4.1",
"ethers": "^5.4.7",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"typescript": "^4.7.4"
},
"devDependencies": {
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"react-scripts": "5.0.1"
},
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ const HARDHAT_NETWORK_ID = '1337';
// This is an error code that indicates that the user canceled a transaction
const ERROR_CODE_TX_REJECTED_BY_USER = 4001;

// Defining the Dapp components state and props for type safety.
interface TokenData {
name: string;
symbol: string;
}

interface IState {
tokenData?: TokenData;
selectedAddress?: string;
balance?: ethers.BigNumber;
txBeingSent?: string;
transactionError?: Error;
networkError?: string;
}

// Since window.ethereum is not recognized by the TypeScript compiler,
// we use this little hack
declare let window: any;

// This component is in charge of doing these things:
// 1. It connects to the user's wallet
// 2. Initializes ethers and the Token contract
Expand All @@ -37,24 +56,29 @@ const ERROR_CODE_TX_REJECTED_BY_USER = 4001;
// Note that (3) and (4) are specific of this sample application, but they show
// you how to keep your Dapp and contract's state in sync, and how to send a
// transaction.
export class Dapp extends React.Component {
export class Dapp extends React.Component<{}, IState> {
// We store multiple things in Dapp's state.
// You don't need to follow this pattern, but it's an useful example.
initialState = {
// The info of the token (i.e. It's Name and symbol)
tokenData: undefined,
// The user's address and balance
selectedAddress: undefined,
balance: undefined,
// The ID about transactions being sent, and any possible error with them
txBeingSent: undefined,
transactionError: undefined,
networkError: undefined,
};
private _provider: ethers.providers.Web3Provider;
private _pollDataInterval: NodeJS.
// We'll use ethers to interact with the Ethereum network and our contract
Timer;
private _token: any;

constructor(props) {
super(props);

// We store multiple things in Dapp's state.
// You don't need to follow this pattern, but it's an useful example.
this.initialState = {
// The info of the token (i.e. It's Name and symbol)
tokenData: undefined,
// The user's address and balance
selectedAddress: undefined,
balance: undefined,
// The ID about transactions being sent, and any possible error with them
txBeingSent: undefined,
transactionError: undefined,
networkError: undefined,
};

this.state = this.initialState;
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function Transfer({ transferTokens, tokenSymbol }) {
// form's data.
event.preventDefault();

const formData = new FormData(event.target);
const formData = new FormData(event.target as HTMLFormElement);
const to = formData.get("to");
const amount = formData.get("amount");

Expand Down
File renamed without changes.
Loading