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 every_block
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher"
|
|
|
|
"github.com/vulcanize/vulcanizedb/libraries/shared"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/core"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
|
|
|
"log"
|
|
|
|
"math/big"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Transformer struct {
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
Getter ERC20GetterInterface
|
2018-05-05 20:25:54 +00:00
|
|
|
Repository ERC20RepositoryInterface
|
|
|
|
Config erc20_watcher.ContractConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Transformer) SetConfiguration(config erc20_watcher.ContractConfig) {
|
|
|
|
t.Config = config
|
|
|
|
}
|
|
|
|
|
|
|
|
type TokenSupplyTransformerInitializer struct {
|
|
|
|
Config erc20_watcher.ContractConfig
|
|
|
|
}
|
|
|
|
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
func (i TokenSupplyTransformerInitializer) NewTokenSupplyTransformer(db *postgres.DB, blockChain core.BlockChain) shared.Transformer {
|
|
|
|
getter := NewGetter(blockChain)
|
2018-08-09 16:58:06 +00:00
|
|
|
repository := ERC20TokenRepository{DB: db}
|
2018-05-05 20:25:54 +00:00
|
|
|
transformer := Transformer{
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
Getter: &getter,
|
2018-05-05 20:25:54 +00:00
|
|
|
Repository: &repository,
|
|
|
|
Config: i.Config,
|
|
|
|
}
|
|
|
|
|
|
|
|
return transformer
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
FetchingBlocksError = "Error getting missing blocks starting at block number %d: %s"
|
|
|
|
GetSupplyError = "Error getting supply for block %d: %s"
|
2018-05-05 20:25:54 +00:00
|
|
|
CreateSupplyError = "Error inserting token_supply for block %d: %s"
|
|
|
|
)
|
|
|
|
|
|
|
|
type transformerError struct {
|
|
|
|
err string
|
|
|
|
blockNumber int64
|
|
|
|
msg string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (te *transformerError) Error() string {
|
|
|
|
return fmt.Sprintf(te.msg, te.blockNumber, te.err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTransformerError(err error, blockNumber int64, msg string) error {
|
|
|
|
e := transformerError{err.Error(), blockNumber, msg}
|
|
|
|
log.Println(e.Error())
|
|
|
|
return &e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Transformer) Execute() error {
|
|
|
|
var upperBoundBlock int64
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
blockChain := t.Getter.GetBlockChain()
|
|
|
|
lastBlock := blockChain.LastBlock().Int64()
|
2018-05-05 20:25:54 +00:00
|
|
|
|
|
|
|
if t.Config.LastBlock == -1 {
|
|
|
|
upperBoundBlock = lastBlock
|
|
|
|
} else {
|
|
|
|
upperBoundBlock = t.Config.LastBlock
|
|
|
|
}
|
|
|
|
|
2018-08-09 16:58:06 +00:00
|
|
|
// Supply transformations:
|
|
|
|
|
|
|
|
// Fetch missing supply blocks
|
|
|
|
blocks, err := t.Repository.MissingSupplyBlocks(t.Config.FirstBlock, upperBoundBlock, t.Config.Address)
|
2018-05-05 20:25:54 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return newTransformerError(err, t.Config.FirstBlock, FetchingBlocksError)
|
|
|
|
}
|
|
|
|
|
2018-08-09 16:58:06 +00:00
|
|
|
// Fetch supply for missing blocks
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
log.Printf("Gets totalSupply for %d blocks", len(blocks))
|
2018-08-09 16:58:06 +00:00
|
|
|
|
|
|
|
// For each block missing total supply, create supply model and feed the missing data into the repository
|
2018-05-05 20:25:54 +00:00
|
|
|
for _, blockNumber := range blocks {
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
totalSupply, err := t.Getter.GetTotalSupply(t.Config.Abi, t.Config.Address, blockNumber)
|
2018-05-05 20:25:54 +00:00
|
|
|
|
|
|
|
if err != nil {
|
Moved fetcher to generic directory (methods have to remain public since it is in seperate package now), added FetchHash method, created ERC20 and generic getters which call the fetcher with specific contract methods (GetTotalSupply, GetBalance, GetAllowance for ERC20 getter, and GetOwner, GetStoppedStatus, GetStringName, GetHashName, GetStringSymbol, GetHashSymbol, and GetDecimals for generic getter). Getter tests cover all but GetBalance and GetAllowance, and also cover all of the Fetcher methods- but with only nil methodArgs. GetAllowance and GetBalance tests are not working against infura and these are the only contract method calls with arguments passed in so I suspect this might be where the issue lies. Have tested GetBalance using previous version of FetchContractData without the variadic input to the Pack method and it fails with the same error so I don’t think it is due to those changes.
2018-08-15 04:17:22 +00:00
|
|
|
return newTransformerError(err, blockNumber, GetSupplyError)
|
2018-05-05 20:25:54 +00:00
|
|
|
}
|
2018-08-09 16:58:06 +00:00
|
|
|
// Create the supply model
|
2018-05-05 20:25:54 +00:00
|
|
|
model := createTokenSupplyModel(totalSupply, t.Config.Address, blockNumber)
|
2018-08-09 16:58:06 +00:00
|
|
|
// Feed it into the repository
|
|
|
|
err = t.Repository.CreateSupply(model)
|
2018-05-05 20:25:54 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return newTransformerError(err, blockNumber, CreateSupplyError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func createTokenSupplyModel(totalSupply big.Int, address string, blockNumber int64) TokenSupply {
|
|
|
|
return TokenSupply{
|
|
|
|
Value: totalSupply.String(),
|
|
|
|
TokenAddress: address,
|
|
|
|
BlockNumber: blockNumber,
|
|
|
|
}
|
|
|
|
}
|