2018-05-05 20:25:54 +00:00
|
|
|
// Copyright 2018 Vulcanize
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-07-20 16:37:46 +00:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2018-07-18 20:59:40 +00:00
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
2018-05-05 20:25:54 +00:00
|
|
|
"github.com/spf13/cobra"
|
2018-07-20 16:37:46 +00:00
|
|
|
|
2018-05-05 20:25:54 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher/every_block"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth"
|
2018-07-18 20:59:40 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth/client"
|
2018-07-20 16:37:46 +00:00
|
|
|
vRpc "github.com/vulcanize/vulcanizedb/pkg/geth/converters/rpc"
|
2018-07-18 20:59:40 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/geth/node"
|
2018-05-05 20:25:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// erc20Cmd represents the erc20 command
|
|
|
|
var erc20Cmd = &cobra.Command{
|
|
|
|
Use: "erc20",
|
|
|
|
Short: "Fetches and persists token supply",
|
|
|
|
Long: `Fetches the totalSupply for the configured token from each block and persists it in Vulcanize DB.
|
|
|
|
vulcanizedb erc20 --config environments/public
|
|
|
|
|
|
|
|
Expects an ethereum node to be running and requires a .toml config file:
|
|
|
|
|
|
|
|
[database]
|
|
|
|
name = "vulcanize_public"
|
|
|
|
hostname = "localhost"
|
|
|
|
port = 5432
|
|
|
|
|
|
|
|
[client]
|
|
|
|
ipcPath = "/Users/user/Library/Ethereum/geth.ipc"
|
|
|
|
`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
watchERC20s()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func watchERC20s() {
|
|
|
|
ticker := time.NewTicker(5 * time.Second)
|
|
|
|
defer ticker.Stop()
|
2018-07-20 16:37:46 +00:00
|
|
|
rawRpcClient, err := rpc.Dial(ipc)
|
2018-07-18 20:59:40 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2018-07-20 16:37:46 +00:00
|
|
|
rpcClient := client.NewRpcClient(rawRpcClient, ipc)
|
|
|
|
ethClient := ethclient.NewClient(rawRpcClient)
|
|
|
|
client := client.NewEthClient(ethClient)
|
|
|
|
node := node.MakeNode(rpcClient)
|
|
|
|
transactionConverter := vRpc.NewRpcTransactionConverter(ethClient)
|
2018-07-18 20:59:40 +00:00
|
|
|
blockChain := geth.NewBlockChain(client, node, transactionConverter)
|
|
|
|
db, err := postgres.NewDB(databaseConfig, blockChain.Node())
|
2018-05-05 20:25:54 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to initialize database.")
|
|
|
|
}
|
|
|
|
watcher := shared.Watcher{
|
|
|
|
DB: *db,
|
2018-07-18 20:59:40 +00:00
|
|
|
Blockchain: blockChain,
|
2018-05-05 20:25:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
watcher.AddTransformers(every_block.TransformerInitializers())
|
|
|
|
for range ticker.C {
|
|
|
|
watcher.Execute()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(erc20Cmd)
|
|
|
|
}
|