adjust retriever to pull token holder addresses from Transfer and Approval events (iterating over Approvals might be redundant); edit Makefile to import new missing dependencies of go-ethereum/accounts/keystore, organizing mocks and adding event related mocks and filters

This commit is contained in:
Ian Norden 2018-08-28 12:50:53 -05:00
parent ada872404e
commit aa2068bd08
11 changed files with 597 additions and 396 deletions

View File

@ -42,6 +42,7 @@ lint:
.PHONY: test .PHONY: test
test: | $(GINKGO) $(LINT) test: | $(GINKGO) $(LINT)
go get -t ./...
go vet ./... go vet ./...
go fmt ./... go fmt ./...
$(GINKGO) -r $(GINKGO) -r

View File

@ -17,6 +17,8 @@ package constants
import ( import (
"github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi"
"github.com/vulcanize/vulcanizedb/examples/generic/helpers" "github.com/vulcanize/vulcanizedb/examples/generic/helpers"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/filters"
"github.com/vulcanize/vulcanizedb/pkg/geth" "github.com/vulcanize/vulcanizedb/pkg/geth"
"log" "log"
) )
@ -53,3 +55,21 @@ func init() {
// Hashed event signatures // Hashed event signatures
var TransferEventSignature = helpers.GenerateSignature("Transfer(address,address,uint)") var TransferEventSignature = helpers.GenerateSignature("Transfer(address,address,uint)")
var ApprovalEventSignature = helpers.GenerateSignature("Approval(address,address,uint)") var ApprovalEventSignature = helpers.GenerateSignature("Approval(address,address,uint)")
// Filters
var DaiFilters = []filters.LogFilter{
{
Name: "Transfers",
FromBlock: 0,
ToBlock: -1,
Address: DaiContractAddress,
Topics: core.Topics{TransferEventSignature},
},
{
Name: "Approvals",
FromBlock: 0,
ToBlock: -1,
Address: DaiContractAddress,
Topics: core.Topics{ApprovalEventSignature},
},
}

View File

@ -88,7 +88,7 @@ var approvalEvent = core.WatchedEvent{
Address: constants.DaiContractAddress, Address: constants.DaiContractAddress,
TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae", TxHash: "0x135391a0962a63944e5908e6fedfff90fb4be3e3290a21017861099bad6546ae",
Index: 110, Index: 110,
Topic0: constants.TransferEventSignature, Topic0: constants.ApprovalEventSignature,
Topic1: "0x000000000000000000000000000000000000000000000000000000000000af21", Topic1: "0x000000000000000000000000000000000000000000000000000000000000af21",
Topic2: "0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba391", Topic2: "0x9dd48110dcc444fdc242510c09bbbbe21a5975cac061d82f7b843bce061ba391",
Topic3: "", Topic3: "",

View File

@ -125,7 +125,7 @@ func (t Transformer) Execute() error {
// Retrieve all token holder addresses for the given contract configuration // Retrieve all token holder addresses for the given contract configuration
tokenHolderAddresses, err := t.Retriever.RetrieveContractAssociatedAddresses() tokenHolderAddresses, err := t.Retriever.RetrieveTokenHolderAddresses()
if err != nil { if err != nil {
return newTransformerError(err, t.Config.FirstBlock, FetchingTokenAddressesError) return newTransformerError(err, t.Config.FirstBlock, FetchingTokenAddressesError)
} }

View File

@ -25,8 +25,8 @@ import (
// address in an attempt to generate a list of token holder addresses // address in an attempt to generate a list of token holder addresses
type RetrieverInterface interface { type RetrieverInterface interface {
RetrieveSendingAddresses() ([]string, error) retrieveTransferEventAddresses() ([][2]string, error)
RetrieveReceivingAddresses() ([]string, error) retrieveApprovalEventAddresses() ([][2]string, error)
RetrieveContractAssociatedAddresses() (map[common.Address]bool, error) RetrieveContractAssociatedAddresses() (map[common.Address]bool, error)
} }
@ -55,8 +55,10 @@ func newRetrieverError(err error, msg string, address string) error {
// Constant error definitions // Constant error definitions
const ( const (
GetSenderError = "Error fetching addresses receiving from contract %s: %s" GetSendersError = "Error fetching token senders from contract %s: %s"
GetReceiverError = "Error fetching addresses sending to contract %s: %s" GetReceiversError = "Error fetching token receivers from contract %s: %s"
GetOwnersError = "Error fetching token owners from contract %s: %s"
GetSpendersError = "Error fetching token spenders from contract %s: %s"
) )
func NewRetriever(db *postgres.DB, address string) Retriever { func NewRetriever(db *postgres.DB, address string) Retriever {
@ -66,59 +68,107 @@ func NewRetriever(db *postgres.DB, address string) Retriever {
} }
} }
func (rt Retriever) RetrieveReceivingAddresses() ([]string, error) { func (rt Retriever) retrieveTokenSenders() ([]string, error) {
receiversFromContract := make([]string, 0) senders := make([]string, 0)
err := rt.Database.DB.Select( err := rt.Database.DB.Select(
&receiversFromContract, &senders,
`SELECT tx_to FROM TRANSACTIONS `SELECT from_address FROM token_transfers
WHERE tx_from = $1 WHERE token_address = $1`,
LIMIT 20`,
rt.ContractAddress, rt.ContractAddress,
) )
if err != nil { if err != nil {
return []string{}, newRetrieverError(err, GetReceiverError, rt.ContractAddress) return []string{}, newRetrieverError(err, GetSendersError, rt.ContractAddress)
} }
return receiversFromContract, err return senders, err
} }
func (rt Retriever) RetrieveSendingAddresses() ([]string, error) { func (rt Retriever) retrieveTokenReceivers() ([]string, error) {
sendersToContract := make([]string, 0) receivers := make([]string, 0)
err := rt.Database.DB.Select( err := rt.Database.DB.Select(
&sendersToContract, &receivers,
`SELECT tx_from FROM TRANSACTIONS `SELECT to_address FROM token_transfers
WHERE tx_to = $1 WHERE token_address = $1`,
LIMIT 20`,
rt.ContractAddress, rt.ContractAddress,
) )
if err != nil { if err != nil {
return []string{}, newRetrieverError(err, GetSenderError, rt.ContractAddress) return []string{}, newRetrieverError(err, GetReceiversError, rt.ContractAddress)
} }
return sendersToContract, err return receivers, err
} }
func (rt Retriever) RetrieveContractAssociatedAddresses() (map[common.Address]bool, error) { func (rt Retriever) retrieveTokenOwners() ([]string, error) {
sending, err := rt.RetrieveSendingAddresses() owners := make([]string, 0)
err := rt.Database.DB.Select(
&owners,
`SELECT owner FROM token_approvals
WHERE token_address = $1`,
rt.ContractAddress,
)
if err != nil {
return []string{}, newRetrieverError(err, GetOwnersError, rt.ContractAddress)
}
return owners, err
}
func (rt Retriever) retrieveTokenSpenders() ([]string, error) {
spenders := make([]string, 0)
err := rt.Database.DB.Select(
&spenders,
`SELECT spender FROM token_approvals
WHERE token_address = $1`,
rt.ContractAddress,
)
if err != nil {
return []string{}, newRetrieverError(err, GetSpendersError, rt.ContractAddress)
}
return spenders, err
}
func (rt Retriever) RetrieveTokenHolderAddresses() (map[common.Address]bool, error) {
senders, err := rt.retrieveTokenSenders()
if err != nil { if err != nil {
return nil, err return nil, err
} }
receiving, err := rt.RetrieveReceivingAddresses() receivers, err := rt.retrieveTokenReceivers()
if err != nil {
return nil, err
}
owners, err := rt.retrieveTokenOwners()
if err != nil {
return nil, err
}
spenders, err := rt.retrieveTokenSenders()
if err != nil { if err != nil {
return nil, err return nil, err
} }
contractAddresses := make(map[common.Address]bool) contractAddresses := make(map[common.Address]bool)
for _, addr := range sending { for _, addr := range senders {
contractAddresses[common.HexToAddress(addr)] = true contractAddresses[common.HexToAddress(addr)] = true
} }
for _, addr := range receiving { for _, addr := range receivers {
contractAddresses[common.HexToAddress(addr)] = true
}
for _, addr := range owners {
contractAddresses[common.HexToAddress(addr)] = true
}
for _, addr := range spenders {
contractAddresses[common.HexToAddress(addr)] = true contractAddresses[common.HexToAddress(addr)] = true
} }

View File

@ -0,0 +1,71 @@
// 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 mocks
import (
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher/event_triggered"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/filters"
)
type MockWatchedEventsRepository struct {
watchedEvents []*core.WatchedEvent
Names []string
}
func (mwer *MockWatchedEventsRepository) SetWatchedEvents(watchedEvents []*core.WatchedEvent) {
mwer.watchedEvents = watchedEvents
}
func (mwer *MockWatchedEventsRepository) GetWatchedEvents(name string) ([]*core.WatchedEvent, error) {
mwer.Names = append(mwer.Names, name)
result := mwer.watchedEvents
// clear watched events once returned so same events are returned for every filter while testing
mwer.watchedEvents = []*core.WatchedEvent{}
return result, nil
}
type MockTransferRepo struct {
LogMakes []event_triggered.TransferModel
VulcanizeLogIDs []int64
}
func (molr *MockTransferRepo) Create(offerModel event_triggered.TransferModel, vulcanizeLogId int64) error {
molr.LogMakes = append(molr.LogMakes, offerModel)
molr.VulcanizeLogIDs = append(molr.VulcanizeLogIDs, vulcanizeLogId)
return nil
}
type MockApprovalRepo struct {
LogKills []event_triggered.ApprovalModel
VulcanizeLogIDs []int64
}
func (molk *MockApprovalRepo) Create(model event_triggered.ApprovalModel, vulcanizeLogID int64) error {
molk.LogKills = append(molk.LogKills, model)
molk.VulcanizeLogIDs = append(molk.VulcanizeLogIDs, vulcanizeLogID)
return nil
}
type MockFilterRepository struct {
}
func (MockFilterRepository) CreateFilter(filter filters.LogFilter) error {
panic("implement me")
}
func (MockFilterRepository) GetFilter(name string) (filters.LogFilter, error) {
panic("implement me")
}

View File

@ -0,0 +1,75 @@
// 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 mocks
import (
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher/every_block"
)
type ERC20TokenRepository struct {
TotalSuppliesCreated []every_block.TokenSupply
MissingSupplyBlockNumbers []int64
TotalBalancesCreated []every_block.TokenBalance
MissingBalanceBlockNumbers []int64
TotalAllowancesCreated []every_block.TokenAllowance
MissingAllowanceBlockNumbers []int64
StartingBlock int64
EndingBlock int64
}
func (fr *ERC20TokenRepository) CreateSupply(supply every_block.TokenSupply) error {
fr.TotalSuppliesCreated = append(fr.TotalSuppliesCreated, supply)
return nil
}
func (fr *ERC20TokenRepository) CreateBalance(balance every_block.TokenBalance) error {
fr.TotalBalancesCreated = append(fr.TotalBalancesCreated, balance)
return nil
}
func (fr *ERC20TokenRepository) CreateAllowance(allowance every_block.TokenAllowance) error {
fr.TotalAllowancesCreated = append(fr.TotalAllowancesCreated, allowance)
return nil
}
func (fr *ERC20TokenRepository) MissingSupplyBlocks(startingBlock, highestBlock int64, tokenAddress string) ([]int64, error) {
fr.StartingBlock = startingBlock
fr.EndingBlock = highestBlock
return fr.MissingSupplyBlockNumbers, nil
}
func (fr *ERC20TokenRepository) MissingBalanceBlocks(startingBlock, highestBlock int64, tokenAddress, holderAddress string) ([]int64, error) {
fr.StartingBlock = startingBlock
fr.EndingBlock = highestBlock
return fr.MissingBalanceBlockNumbers, nil
}
func (fr *ERC20TokenRepository) MissingAllowanceBlocks(startingBlock, highestBlock int64, tokenAddress, holderAddress, spenderAddress string) ([]int64, error) {
fr.StartingBlock = startingBlock
fr.EndingBlock = highestBlock
return fr.MissingAllowanceBlockNumbers, nil
}
func (fr *ERC20TokenRepository) SetMissingSupplyBlocks(missingBlocks []int64) {
fr.MissingSupplyBlockNumbers = missingBlocks
}
func (fr *ERC20TokenRepository) SetMissingBalanceBlocks(missingBlocks []int64) {
fr.MissingBalanceBlockNumbers = missingBlocks
}
func (fr *ERC20TokenRepository) SetMissingAllowanceBlocks(missingBlocks []int64) {
fr.MissingAllowanceBlockNumbers = missingBlocks
}

View File

@ -0,0 +1,116 @@
// 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 mocks
import (
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher/every_block"
"github.com/vulcanize/vulcanizedb/pkg/fakes"
)
type FailureRepository struct {
createSupplyFail bool
createBalanceFail bool
createAllowanceFail bool
missingSupplyBlocksFail bool
missingBalanceBlocksFail bool
missingAllowanceBlocksFail bool
missingSupplyBlocksNumbers []int64
missingBalanceBlocksNumbers []int64
missingAllowanceBlocksNumbers []int64
}
func (fr *FailureRepository) CreateSupply(supply every_block.TokenSupply) error {
if fr.createSupplyFail {
return fakes.FakeError
} else {
return nil
}
}
func (fr *FailureRepository) CreateBalance(balance every_block.TokenBalance) error {
if fr.createBalanceFail {
return fakes.FakeError
} else {
return nil
}
}
func (fr *FailureRepository) CreateAllowance(allowance every_block.TokenAllowance) error {
if fr.createAllowanceFail {
return fakes.FakeError
} else {
return nil
}
}
func (fr *FailureRepository) MissingSupplyBlocks(startingBlock, highestBlock int64, tokenAddress string) ([]int64, error) {
if fr.missingSupplyBlocksFail {
return []int64{}, fakes.FakeError
} else {
return fr.missingSupplyBlocksNumbers, nil
}
}
func (fr *FailureRepository) MissingBalanceBlocks(startingBlock, highestBlock int64, tokenAddress, holderAddress string) ([]int64, error) {
if fr.missingBalanceBlocksFail {
return []int64{}, fakes.FakeError
} else {
return fr.missingBalanceBlocksNumbers, nil
}
}
func (fr *FailureRepository) MissingAllowanceBlocks(startingBlock, highestBlock int64, tokenAddress, holderAddress, spenderAddress string) ([]int64, error) {
if fr.missingAllowanceBlocksFail {
return []int64{}, fakes.FakeError
} else {
return fr.missingAllowanceBlocksNumbers, nil
}
}
func (fr *FailureRepository) SetCreateSupplyFail(fail bool) {
fr.createSupplyFail = fail
}
func (fr *FailureRepository) SetCreateBalanceFail(fail bool) {
fr.createBalanceFail = fail
}
func (fr *FailureRepository) SetCreateAllowanceFail(fail bool) {
fr.createAllowanceFail = fail
}
func (fr *FailureRepository) SetMissingSupplyBlocksFail(fail bool) {
fr.missingSupplyBlocksFail = fail
}
func (fr *FailureRepository) SetMissingBalanceBlocksFail(fail bool) {
fr.missingBalanceBlocksFail = fail
}
func (fr *FailureRepository) SetMissingAllowanceBlocksFail(fail bool) {
fr.missingAllowanceBlocksFail = fail
}
func (fr *FailureRepository) SetMissingSupplyBlocks(missingBlocks []int64) {
fr.missingSupplyBlocksNumbers = missingBlocks
}
func (fr *FailureRepository) SetMissingBalanceBlocks(missingBlocks []int64) {
fr.missingBalanceBlocksNumbers = missingBlocks
}
func (fr *FailureRepository) SetMissingAllowanceBlocks(missingBlocks []int64) {
fr.missingAllowanceBlocksNumbers = missingBlocks
}

159
examples/mocks/fetcher.go Normal file
View File

@ -0,0 +1,159 @@
// 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 mocks
import (
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/vulcanize/vulcanizedb/pkg/core"
"reflect"
)
type Fetcher struct {
ContractAddress string
Abi string
FetchedBlocks []int64
BlockChain core.BlockChain
supply big.Int
balance map[string]*big.Int
allowance map[string]map[string]*big.Int
owner common.Address
stopped bool
stringName string
hashName common.Hash
stringSymbol string
hashSymbol common.Hash
}
func (f *Fetcher) SetSupply(supply string) {
f.supply.SetString(supply, 10)
}
func (f Fetcher) GetBlockChain() core.BlockChain {
return f.BlockChain
}
func (f *Fetcher) FetchBigInt(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (big.Int, error) {
f.Abi = contractAbi
f.ContractAddress = contractAddress
f.FetchedBlocks = append(f.FetchedBlocks, blockNumber)
accumulator := big.NewInt(1)
if method == "totalSupply" {
f.supply.Add(&f.supply, accumulator)
return f.supply, nil
}
if method == "balanceOf" {
rfl := reflect.ValueOf(methodArgs[0])
tokenHolderAddr := rfl.Interface().(string)
pnt := f.balance[tokenHolderAddr]
f.balance[tokenHolderAddr].Add(pnt, accumulator)
return *f.balance[tokenHolderAddr], nil
}
if method == "allowance" {
rfl1 := reflect.ValueOf(methodArgs[0])
rfl2 := reflect.ValueOf(methodArgs[1])
tokenHolderAddr := rfl1.Interface().(string)
spenderAddr := rfl2.Interface().(string)
pnt := f.allowance[tokenHolderAddr][spenderAddr]
f.allowance[tokenHolderAddr][spenderAddr].Add(pnt, accumulator)
return *f.allowance[tokenHolderAddr][spenderAddr], nil
}
return *big.NewInt(0), errors.New("invalid method argument")
}
func (f *Fetcher) FetchBool(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (bool, error) {
f.Abi = contractAbi
f.ContractAddress = contractAddress
f.FetchedBlocks = append(f.FetchedBlocks, blockNumber)
b := true
if method == "stopped" {
f.stopped = b
return f.stopped, nil
}
return false, errors.New("invalid method argument")
}
func (f *Fetcher) FetchAddress(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (common.Address, error) {
f.Abi = contractAbi
f.ContractAddress = contractAddress
f.FetchedBlocks = append(f.FetchedBlocks, blockNumber)
adr := common.StringToAddress("test_address")
if method == "owner" {
f.owner = adr
return f.owner, nil
}
return common.StringToAddress(""), errors.New("invalid method argument")
}
func (f *Fetcher) FetchString(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (string, error) {
f.Abi = contractAbi
f.ContractAddress = contractAddress
f.FetchedBlocks = append(f.FetchedBlocks, blockNumber)
if method == "name" {
f.stringName = "test_name"
return f.stringName, nil
}
if method == "symbol" {
f.stringSymbol = "test_symbol"
return f.stringSymbol, nil
}
return "", errors.New("invalid method argument")
}
func (f *Fetcher) FetchHash(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (common.Hash, error) {
f.Abi = contractAbi
f.ContractAddress = contractAddress
f.FetchedBlocks = append(f.FetchedBlocks, blockNumber)
if method == "name" {
f.hashName = common.StringToHash("test_name")
return f.hashName, nil
}
if method == "symbol" {
f.hashSymbol = common.StringToHash("test_symbol")
return f.hashSymbol, nil
}
return common.StringToHash(""), errors.New("invalid method argument")
}

78
examples/mocks/getter.go Normal file
View File

@ -0,0 +1,78 @@
// 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 mocks
import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/vulcanize/vulcanizedb/pkg/core"
)
type Getter struct {
Fetcher Fetcher
}
func NewGetter(blockChain core.BlockChain) Getter {
return Getter{
Fetcher: Fetcher{
BlockChain: blockChain,
},
}
}
func (g *Getter) GetTotalSupply(contractAbi, contractAddress string, blockNumber int64) (big.Int, error) {
return g.Fetcher.FetchBigInt("totalSupply", contractAbi, contractAddress, blockNumber, nil)
}
func (g *Getter) GetBalance(contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (big.Int, error) {
return g.Fetcher.FetchBigInt("balanceOf", contractAbi, contractAddress, blockNumber, methodArgs)
}
func (g *Getter) GetAllowance(contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (big.Int, error) {
return g.Fetcher.FetchBigInt("allowance", contractAbi, contractAddress, blockNumber, methodArgs)
}
func (g *Getter) GetOwner(contractAbi, contractAddress string, blockNumber int64) (common.Address, error) {
return g.Fetcher.FetchAddress("owner", contractAbi, contractAddress, blockNumber, nil)
}
func (g *Getter) GetStoppedStatus(contractAbi, contractAddress string, blockNumber int64) (bool, error) {
return g.Fetcher.FetchBool("stopped", contractAbi, contractAddress, blockNumber, nil)
}
func (g *Getter) GetStringName(contractAbi, contractAddress string, blockNumber int64) (string, error) {
return g.Fetcher.FetchString("name", contractAbi, contractAddress, blockNumber, nil)
}
func (g *Getter) GetHashName(contractAbi, contractAddress string, blockNumber int64) (common.Hash, error) {
return g.Fetcher.FetchHash("name", contractAbi, contractAddress, blockNumber, nil)
}
func (g *Getter) GetStringSymbol(contractAbi, contractAddress string, blockNumber int64) (string, error) {
return g.Fetcher.FetchString("symbol", contractAbi, contractAddress, blockNumber, nil)
}
func (g *Getter) GetHashSymbol(contractAbi, contractAddress string, blockNumber int64) (common.Hash, error) {
return g.Fetcher.FetchHash("symbol", contractAbi, contractAddress, blockNumber, nil)
}
func (g *Getter) GetDecimals(contractAbi, contractAddress string, blockNumber int64) (big.Int, error) {
return g.Fetcher.FetchBigInt("decimals", contractAbi, contractAddress, blockNumber, nil)
}
func (g *Getter) GetBlockChain() core.BlockChain {
return g.Fetcher.BlockChain
}

View File

@ -1,369 +0,0 @@
// 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 mocks
import (
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/vulcanize/vulcanizedb/examples/erc20_watcher/every_block"
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/fakes"
"reflect"
)
type Fetcher struct {
ContractAddress string
Abi string
FetchedBlocks []int64
BlockChain core.BlockChain
supply big.Int
balance map[string]*big.Int
allowance map[string]map[string]*big.Int
owner common.Address
stopped bool
stringName string
hashName common.Hash
stringSymbol string
hashSymbol common.Hash
}
func (f *Fetcher) SetSupply(supply string) {
f.supply.SetString(supply, 10)
}
func (f Fetcher) GetBlockChain() core.BlockChain {
return f.BlockChain
}
func (f *Fetcher) FetchBigInt(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (big.Int, error) {
f.Abi = contractAbi
f.ContractAddress = contractAddress
f.FetchedBlocks = append(f.FetchedBlocks, blockNumber)
accumulator := big.NewInt(1)
if method == "totalSupply" {
f.supply.Add(&f.supply, accumulator)
return f.supply, nil
}
if method == "balanceOf" {
rfl := reflect.ValueOf(methodArgs[0])
tokenHolderAddr := rfl.Interface().(string)
pnt := f.balance[tokenHolderAddr]
f.balance[tokenHolderAddr].Add(pnt, accumulator)
return *f.balance[tokenHolderAddr], nil
}
if method == "allowance" {
rfl1 := reflect.ValueOf(methodArgs[0])
rfl2 := reflect.ValueOf(methodArgs[1])
tokenHolderAddr := rfl1.Interface().(string)
spenderAddr := rfl2.Interface().(string)
pnt := f.allowance[tokenHolderAddr][spenderAddr]
f.allowance[tokenHolderAddr][spenderAddr].Add(pnt, accumulator)
return *f.allowance[tokenHolderAddr][spenderAddr], nil
}
return *big.NewInt(0), errors.New("invalid method argument")
}
func (f *Fetcher) FetchBool(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (bool, error) {
f.Abi = contractAbi
f.ContractAddress = contractAddress
f.FetchedBlocks = append(f.FetchedBlocks, blockNumber)
b := true
if method == "stopped" {
f.stopped = b
return f.stopped, nil
}
return false, errors.New("invalid method argument")
}
func (f *Fetcher) FetchAddress(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (common.Address, error) {
f.Abi = contractAbi
f.ContractAddress = contractAddress
f.FetchedBlocks = append(f.FetchedBlocks, blockNumber)
adr := common.StringToAddress("test_address")
if method == "owner" {
f.owner = adr
return f.owner, nil
}
return common.StringToAddress(""), errors.New("invalid method argument")
}
func (f *Fetcher) FetchString(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (string, error) {
f.Abi = contractAbi
f.ContractAddress = contractAddress
f.FetchedBlocks = append(f.FetchedBlocks, blockNumber)
if method == "name" {
f.stringName = "test_name"
return f.stringName, nil
}
if method == "symbol" {
f.stringSymbol = "test_symbol"
return f.stringSymbol, nil
}
return "", errors.New("invalid method argument")
}
func (f *Fetcher) FetchHash(method, contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (common.Hash, error) {
f.Abi = contractAbi
f.ContractAddress = contractAddress
f.FetchedBlocks = append(f.FetchedBlocks, blockNumber)
if method == "name" {
f.hashName = common.StringToHash("test_name")
return f.hashName, nil
}
if method == "symbol" {
f.hashSymbol = common.StringToHash("test_symbol")
return f.hashSymbol, nil
}
return common.StringToHash(""), errors.New("invalid method argument")
}
type Getter struct {
Fetcher Fetcher
}
func NewGetter(blockChain core.BlockChain) Getter {
return Getter{
Fetcher: Fetcher{
BlockChain: blockChain,
},
}
}
func (g *Getter) GetTotalSupply(contractAbi, contractAddress string, blockNumber int64) (big.Int, error) {
return g.Fetcher.FetchBigInt("totalSupply", contractAbi, contractAddress, blockNumber, nil)
}
func (g *Getter) GetBalance(contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (big.Int, error) {
return g.Fetcher.FetchBigInt("balanceOf", contractAbi, contractAddress, blockNumber, methodArgs)
}
func (g *Getter) GetAllowance(contractAbi, contractAddress string, blockNumber int64, methodArgs []interface{}) (big.Int, error) {
return g.Fetcher.FetchBigInt("allowance", contractAbi, contractAddress, blockNumber, methodArgs)
}
func (g *Getter) GetOwner(contractAbi, contractAddress string, blockNumber int64) (common.Address, error) {
return g.Fetcher.FetchAddress("owner", contractAbi, contractAddress, blockNumber, nil)
}
func (g *Getter) GetStoppedStatus(contractAbi, contractAddress string, blockNumber int64) (bool, error) {
return g.Fetcher.FetchBool("stopped", contractAbi, contractAddress, blockNumber, nil)
}
func (g *Getter) GetStringName(contractAbi, contractAddress string, blockNumber int64) (string, error) {
return g.Fetcher.FetchString("name", contractAbi, contractAddress, blockNumber, nil)
}
func (g *Getter) GetHashName(contractAbi, contractAddress string, blockNumber int64) (common.Hash, error) {
return g.Fetcher.FetchHash("name", contractAbi, contractAddress, blockNumber, nil)
}
func (g *Getter) GetStringSymbol(contractAbi, contractAddress string, blockNumber int64) (string, error) {
return g.Fetcher.FetchString("symbol", contractAbi, contractAddress, blockNumber, nil)
}
func (g *Getter) GetHashSymbol(contractAbi, contractAddress string, blockNumber int64) (common.Hash, error) {
return g.Fetcher.FetchHash("symbol", contractAbi, contractAddress, blockNumber, nil)
}
func (g *Getter) GetDecimals(contractAbi, contractAddress string, blockNumber int64) (big.Int, error) {
return g.Fetcher.FetchBigInt("decimals", contractAbi, contractAddress, blockNumber, nil)
}
func (g *Getter) GetBlockChain() core.BlockChain {
return g.Fetcher.BlockChain
}
type ERC20TokenRepository struct {
TotalSuppliesCreated []every_block.TokenSupply
MissingSupplyBlockNumbers []int64
TotalBalancesCreated []every_block.TokenBalance
MissingBalanceBlockNumbers []int64
TotalAllowancesCreated []every_block.TokenAllowance
MissingAllowanceBlockNumbers []int64
StartingBlock int64
EndingBlock int64
}
func (fr *ERC20TokenRepository) CreateSupply(supply every_block.TokenSupply) error {
fr.TotalSuppliesCreated = append(fr.TotalSuppliesCreated, supply)
return nil
}
func (fr *ERC20TokenRepository) CreateBalance(balance every_block.TokenBalance) error {
fr.TotalBalancesCreated = append(fr.TotalBalancesCreated, balance)
return nil
}
func (fr *ERC20TokenRepository) CreateAllowance(allowance every_block.TokenAllowance) error {
fr.TotalAllowancesCreated = append(fr.TotalAllowancesCreated, allowance)
return nil
}
func (fr *ERC20TokenRepository) MissingSupplyBlocks(startingBlock, highestBlock int64, tokenAddress string) ([]int64, error) {
fr.StartingBlock = startingBlock
fr.EndingBlock = highestBlock
return fr.MissingSupplyBlockNumbers, nil
}
func (fr *ERC20TokenRepository) MissingBalanceBlocks(startingBlock, highestBlock int64, tokenAddress, holderAddress string) ([]int64, error) {
fr.StartingBlock = startingBlock
fr.EndingBlock = highestBlock
return fr.MissingBalanceBlockNumbers, nil
}
func (fr *ERC20TokenRepository) MissingAllowanceBlocks(startingBlock, highestBlock int64, tokenAddress, holderAddress, spenderAddress string) ([]int64, error) {
fr.StartingBlock = startingBlock
fr.EndingBlock = highestBlock
return fr.MissingAllowanceBlockNumbers, nil
}
func (fr *ERC20TokenRepository) SetMissingSupplyBlocks(missingBlocks []int64) {
fr.MissingSupplyBlockNumbers = missingBlocks
}
func (fr *ERC20TokenRepository) SetMissingBalanceBlocks(missingBlocks []int64) {
fr.MissingBalanceBlockNumbers = missingBlocks
}
func (fr *ERC20TokenRepository) SetMissingAllowanceBlocks(missingBlocks []int64) {
fr.MissingAllowanceBlockNumbers = missingBlocks
}
type FailureRepository struct {
createSupplyFail bool
createBalanceFail bool
createAllowanceFail bool
missingSupplyBlocksFail bool
missingBalanceBlocksFail bool
missingAllowanceBlocksFail bool
missingSupplyBlocksNumbers []int64
missingBalanceBlocksNumbers []int64
missingAllowanceBlocksNumbers []int64
}
func (fr *FailureRepository) CreateSupply(supply every_block.TokenSupply) error {
if fr.createSupplyFail {
return fakes.FakeError
} else {
return nil
}
}
func (fr *FailureRepository) CreateBalance(balance every_block.TokenBalance) error {
if fr.createBalanceFail {
return fakes.FakeError
} else {
return nil
}
}
func (fr *FailureRepository) CreateAllowance(allowance every_block.TokenAllowance) error {
if fr.createAllowanceFail {
return fakes.FakeError
} else {
return nil
}
}
func (fr *FailureRepository) MissingSupplyBlocks(startingBlock, highestBlock int64, tokenAddress string) ([]int64, error) {
if fr.missingSupplyBlocksFail {
return []int64{}, fakes.FakeError
} else {
return fr.missingSupplyBlocksNumbers, nil
}
}
func (fr *FailureRepository) MissingBalanceBlocks(startingBlock, highestBlock int64, tokenAddress, holderAddress string) ([]int64, error) {
if fr.missingBalanceBlocksFail {
return []int64{}, fakes.FakeError
} else {
return fr.missingBalanceBlocksNumbers, nil
}
}
func (fr *FailureRepository) MissingAllowanceBlocks(startingBlock, highestBlock int64, tokenAddress, holderAddress, spenderAddress string) ([]int64, error) {
if fr.missingAllowanceBlocksFail {
return []int64{}, fakes.FakeError
} else {
return fr.missingAllowanceBlocksNumbers, nil
}
}
func (fr *FailureRepository) SetCreateSupplyFail(fail bool) {
fr.createSupplyFail = fail
}
func (fr *FailureRepository) SetCreateBalanceFail(fail bool) {
fr.createBalanceFail = fail
}
func (fr *FailureRepository) SetCreateAllowanceFail(fail bool) {
fr.createAllowanceFail = fail
}
func (fr *FailureRepository) SetMissingSupplyBlocksFail(fail bool) {
fr.missingSupplyBlocksFail = fail
}
func (fr *FailureRepository) SetMissingBalanceBlocksFail(fail bool) {
fr.missingBalanceBlocksFail = fail
}
func (fr *FailureRepository) SetMissingAllowanceBlocksFail(fail bool) {
fr.missingAllowanceBlocksFail = fail
}
func (fr *FailureRepository) SetMissingSupplyBlocks(missingBlocks []int64) {
fr.missingSupplyBlocksNumbers = missingBlocks
}
func (fr *FailureRepository) SetMissingBalanceBlocks(missingBlocks []int64) {
fr.missingBalanceBlocksNumbers = missingBlocks
}
func (fr *FailureRepository) SetMissingAllowanceBlocks(missingBlocks []int64) {
fr.missingAllowanceBlocksNumbers = missingBlocks
}