2018-11-07 21:50:43 +00:00
// VulcanizeDB
// Copyright © 2018 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package poller_test
import (
2018-11-20 16:38:23 +00:00
"fmt"
2018-11-07 21:50:43 +00:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
2018-11-20 16:38:23 +00:00
"github.com/vulcanize/vulcanizedb/pkg/core"
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
2018-11-07 21:50:43 +00:00
"github.com/vulcanize/vulcanizedb/pkg/omni/constants"
"github.com/vulcanize/vulcanizedb/pkg/omni/contract"
"github.com/vulcanize/vulcanizedb/pkg/omni/helpers/test_helpers"
"github.com/vulcanize/vulcanizedb/pkg/omni/poller"
)
var _ = Describe ( "Poller" , func ( ) {
var p poller . Poller
var con * contract . Contract
2018-11-20 16:38:23 +00:00
var db * postgres . DB
var bc core . BlockChain
2018-11-07 21:50:43 +00:00
BeforeEach ( func ( ) {
2018-11-20 16:38:23 +00:00
db , bc = test_helpers . SetupDBandBC ( )
p = poller . NewPoller ( bc , db )
} )
AfterEach ( func ( ) {
test_helpers . TearDown ( db )
2018-11-07 21:50:43 +00:00
} )
Describe ( "PollContract" , func ( ) {
2018-11-20 16:38:23 +00:00
It ( "Polls specified contract methods using contract's token holder address list" , func ( ) {
2018-11-07 21:50:43 +00:00
con = test_helpers . SetupTusdContract ( nil , [ ] string { "balanceOf" } )
Expect ( con . Abi ) . To ( Equal ( constants . TusdAbiString ) )
con . StartingBlock = 6707322
con . LastBlock = 6707323
con . TknHolderAddrs = map [ string ] bool {
"0xfE9e8709d3215310075d67E3ed32A380CCf451C8" : true ,
"0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE" : true ,
}
2018-11-20 16:38:23 +00:00
err := p . PollContract ( * con )
Expect ( err ) . ToNot ( HaveOccurred ( ) )
scanStruct := test_helpers . BalanceOf { }
err = db . QueryRowx ( fmt . Sprintf ( "SELECT * FROM c%s.balanceof_method WHERE who_ = '0xfE9e8709d3215310075d67E3ed32A380CCf451C8' AND block = '6707322'" , constants . TusdContractAddress ) ) . StructScan ( & scanStruct )
2018-11-07 21:50:43 +00:00
Expect ( err ) . ToNot ( HaveOccurred ( ) )
2018-11-20 16:38:23 +00:00
Expect ( scanStruct . Balance ) . To ( Equal ( "66386309548896882859581786" ) )
Expect ( scanStruct . TokenName ) . To ( Equal ( "TrueUSD" ) )
2018-11-07 21:50:43 +00:00
2018-11-20 16:38:23 +00:00
err = db . QueryRowx ( fmt . Sprintf ( "SELECT * FROM c%s.balanceof_method WHERE who_ = '0xfE9e8709d3215310075d67E3ed32A380CCf451C8' AND block = '6707323'" , constants . TusdContractAddress ) ) . StructScan ( & scanStruct )
Expect ( err ) . ToNot ( HaveOccurred ( ) )
Expect ( scanStruct . Balance ) . To ( Equal ( "66386309548896882859581786" ) )
Expect ( scanStruct . TokenName ) . To ( Equal ( "TrueUSD" ) )
err = db . QueryRowx ( fmt . Sprintf ( "SELECT * FROM c%s.balanceof_method WHERE who_ = '0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE' AND block = '6707322'" , constants . TusdContractAddress ) ) . StructScan ( & scanStruct )
Expect ( err ) . ToNot ( HaveOccurred ( ) )
Expect ( scanStruct . Balance ) . To ( Equal ( "17982350181394112023885864" ) )
Expect ( scanStruct . TokenName ) . To ( Equal ( "TrueUSD" ) )
err = db . QueryRowx ( fmt . Sprintf ( "SELECT * FROM c%s.balanceof_method WHERE who_ = '0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE' AND block = '6707323'" , constants . TusdContractAddress ) ) . StructScan ( & scanStruct )
Expect ( err ) . ToNot ( HaveOccurred ( ) )
Expect ( scanStruct . Balance ) . To ( Equal ( "17982350181394112023885864" ) )
Expect ( scanStruct . TokenName ) . To ( Equal ( "TrueUSD" ) )
} )
It ( "Does not poll and persist any methods if none are specified" , func ( ) {
con = test_helpers . SetupTusdContract ( nil , nil )
Expect ( con . Abi ) . To ( Equal ( constants . TusdAbiString ) )
con . StartingBlock = 6707322
con . LastBlock = 6707323
con . TknHolderAddrs = map [ string ] bool {
"0xfE9e8709d3215310075d67E3ed32A380CCf451C8" : true ,
"0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE" : true ,
2018-11-07 21:50:43 +00:00
}
2018-11-20 16:38:23 +00:00
err := p . PollContract ( * con )
Expect ( err ) . ToNot ( HaveOccurred ( ) )
scanStruct := test_helpers . BalanceOf { }
err = db . QueryRowx ( fmt . Sprintf ( "SELECT * FROM c%s.balanceof_method WHERE who_ = '0xfE9e8709d3215310075d67E3ed32A380CCf451C8' AND block = '6707322'" , constants . TusdContractAddress ) ) . StructScan ( & scanStruct )
Expect ( err ) . To ( HaveOccurred ( ) )
2018-11-07 21:50:43 +00:00
} )
} )
Describe ( "PollMethod" , func ( ) {
It ( "Polls a single contract method" , func ( ) {
var name = new ( string )
2018-11-20 16:38:23 +00:00
err := p . FetchContractData ( constants . TusdAbiString , constants . TusdContractAddress , "name" , nil , & name , 6197514 )
2018-11-07 21:50:43 +00:00
Expect ( err ) . ToNot ( HaveOccurred ( ) )
Expect ( * name ) . To ( Equal ( "TrueUSD" ) )
} )
} )
} )