2021-07-16 11:05:49 +00:00
# ERC20 Watcher
2023-01-10 14:21:00 +00:00
First try the [erc20 demo in stack orchestrator ](https://github.com/cerc-io/stack-orchestrator/tree/main/app/data/stacks/erc20 ) to quickly get started. Advanced users can see [here ](/docs/README.md ) for instructions on setting up a local environment by hand.
2021-07-16 11:05:49 +00:00
2023-01-04 15:08:58 +00:00
## Build
2021-07-16 11:05:49 +00:00
2023-01-04 15:08:58 +00:00
Build files:
2021-09-21 11:13:55 +00:00
2023-01-04 15:08:58 +00:00
```bash
yarn build
2021-09-21 11:13:55 +00:00
```
2023-01-04 15:08:58 +00:00
## Run
Start the job runner:
2021-07-16 11:05:49 +00:00
2023-01-04 15:08:58 +00:00
```bash
yarn job-runner
2021-07-16 11:05:49 +00:00
```
2022-05-23 13:24:14 +00:00
2023-01-04 15:08:58 +00:00
For development or to specify the config file:
```bash
yarn job-runner:dev
yarn job-runner -f environments/local.toml
2022-05-23 13:24:14 +00:00
```
2023-01-04 15:08:58 +00:00
Then, Start the server:
2021-09-21 11:13:55 +00:00
2023-01-04 15:08:58 +00:00
```bash
yarn server
2022-05-23 13:24:14 +00:00
```
2021-07-16 11:05:49 +00:00
2023-01-04 15:08:58 +00:00
For development or to specify the config file:
2023-01-10 12:39:29 +00:00
```bash
2023-01-04 15:08:58 +00:00
yarn server:dev
yarn server -f environments/local.toml
```
2021-07-16 11:05:49 +00:00
2023-01-04 15:08:58 +00:00
See the GQL console at: http://localhost:3001/graphql
Note: the port may be different depending on your configuration.
2022-11-28 13:24:09 +00:00
2023-01-04 15:08:58 +00:00
Deploy an ERC20 token:
2021-09-02 04:57:56 +00:00
```bash
2023-01-04 15:08:58 +00:00
yarn token:deploy
2021-09-02 04:57:56 +00:00
```
2023-01-04 15:08:58 +00:00
In the output you'll see:
2022-11-25 06:01:20 +00:00
2021-07-16 11:05:49 +00:00
```bash
2023-01-04 15:08:58 +00:00
GLD Token deployed to: 0xTokenAddress
2021-09-21 11:13:55 +00:00
```
2023-01-04 15:08:58 +00:00
Export the address of the deployed token to a shell variable for later use:
2021-09-21 11:13:55 +00:00
```bash
2023-01-04 15:08:58 +00:00
export TOKEN_ADDRESS=0xTokenAddress
```
2021-09-21 11:13:55 +00:00
2023-01-04 15:08:58 +00:00
Get the main account address:
```bash
yarn account
2021-07-16 11:05:49 +00:00
```
2023-01-04 15:08:58 +00:00
and export it as well:
2021-07-16 11:05:49 +00:00
2022-05-23 13:24:14 +00:00
```bash
2023-01-04 15:08:58 +00:00
export PRIMARY_ACCOUNT=0xPrimaryAccount
2022-05-23 13:24:14 +00:00
```
2023-01-04 15:08:58 +00:00
Run the following command to watch the contract:
2021-09-21 11:13:55 +00:00
```bash
2023-01-04 15:08:58 +00:00
yarn watch:contract --address $TOKEN_ADDRESS --kind ERC20 --checkpoint false
2021-09-21 11:13:55 +00:00
```
2023-01-04 15:08:58 +00:00
For specifying a config file:
2021-09-21 11:13:55 +00:00
```bash
2023-01-04 15:08:58 +00:00
yarn watch:contract -f environments/local.toml --address 0xTokenAddress --kind ERC20 --checkpoint false
2021-09-21 11:13:55 +00:00
```
To fill a block range:
```bash
yarn fill --startBlock < from-block > --endBlock < to-block >
2023-01-04 15:08:58 +00:00
```
To get the current block hash at any time, run:
2021-09-21 11:13:55 +00:00
2023-01-04 15:08:58 +00:00
```bash
yarn block:latest
2021-09-21 11:13:55 +00:00
```
2023-01-04 15:08:58 +00:00
Add a new account to Metamask and export the account address to a shell variable for later use:
2021-09-21 11:13:55 +00:00
```bash
2023-01-04 15:08:58 +00:00
export RECIPIENT_ADDRESS=0xRecipientAddress
2021-09-21 11:13:55 +00:00
```
2023-01-04 15:08:58 +00:00
Run the following GQL query against the [http://127.0.0.1:3001/graphql ](http://127.0.0.1:3001/graphql ) to get the name, symbol and total supply of the deployed token:
2021-07-16 11:05:49 +00:00
2023-01-04 15:08:58 +00:00
```graphql
query {
name(
blockHash: "LATEST_BLOCK_HASH"
token: "0xTokenAddress"
) {
2021-07-16 11:05:49 +00:00
value
proof {
data
}
}
2023-01-04 15:08:58 +00:00
symbol(
blockHash: "LATEST_BLOCK_HASH"
token: "0xTokenAddress"
) {
2021-07-16 11:05:49 +00:00
value
proof {
data
}
}
2023-01-04 15:08:58 +00:00
totalSupply(
blockHash: "LATEST_BLOCK_HASH"
token: "0xTokenAddress"
) {
2021-07-16 11:05:49 +00:00
value
proof {
data
}
}
2023-01-04 15:08:58 +00:00
}
```
2021-07-16 11:05:49 +00:00
2023-01-04 15:08:58 +00:00
Run the following GQL query to get balances for the main and the recipient account at the latest block hash:
```graphql
query {
fromBalanceOf: balanceOf(
blockHash: "LATEST_BLOCK_HASH"
token: "0xTokenAddress",
# main/primary account having all the balance initially
owner: "0xPrimaryAccount"
) {
2021-07-16 11:05:49 +00:00
value
proof {
data
}
}
2023-01-04 15:08:58 +00:00
toBalanceOf: balanceOf(
blockHash: "LATEST_BLOCK_HASH"
token: "0xTokenAddress",
owner: "0xRecipientAddress"
) {
2021-07-16 11:05:49 +00:00
value
proof {
data
}
}
2023-01-04 15:08:58 +00:00
}
```
Run the following GQL subscription at the GraphQL endpoint:
2021-07-16 11:05:49 +00:00
2023-01-04 15:08:58 +00:00
```graphql
subscription {
onEvent {
blockHash
contract
2021-07-16 11:05:49 +00:00
event {
__typename
... on TransferEvent {
from
to
value
2023-01-04 15:08:58 +00:00
},
2021-07-16 11:05:49 +00:00
... on ApprovalEvent {
owner
spender
value
}
}
proof {
data
}
}
}
```
2023-01-04 15:08:58 +00:00
Transfer tokens to the recipient account:
```bash
yarn token:transfer --token $TOKEN_ADDRESS --to $RECIPIENT_ADDRESS --amount 100
```
A Transfer event to the `RECIPIENT_ADDRESS` should be visible in the subscription.
Get the latest block hash again, then fire the GQL query above to get updated balances for the main (from) and the recipient (to) account.