Skip to content

Instantly share code, notes, and snippets.

@bas-vk
Created January 20, 2017 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bas-vk/ace0436bb9de203fb6573e098c8f56b3 to your computer and use it in GitHub Desktop.
Save bas-vk/ace0436bb9de203fb6573e098c8f56b3 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"log"
"math/big"
"strings"
"time"
ethereum "github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
var (
ContractABI = `[{"constant":false,"inputs":[],"name":"divest","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"id","type":"uint256"}],"name":"getBet","outputs":[{"name":"","type":"address"},{"name":"","type":"uint256"},{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"getBankroll","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"bet","outputs":[],"payable":true,"type":"function"},{"constant":true,"inputs":[{"name":"currentInvestor","type":"address"}],"name":"getProfitShare","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"stopContract","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"numInvestors","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"currentInvestor","type":"address"}],"name":"getLossesShare","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"vote","type":"bool"}],"name":"voteEmergencyWithdrawal","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"newHouse","type":"address"}],"name":"changeHouseAddress","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"forceDivestOfAllInvestors","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"myid","type":"bytes32"},{"name":"result","type":"string"}],"name":"__callback","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"myid","type":"bytes32"},{"name":"result","type":"string"},{"name":"proof","type":"bytes"}],"name":"__callback","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"isStopped","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"investors","outputs":[{"name":"investorAddress","type":"address"},{"name":"amountInvested","type":"uint256"},{"name":"votedForEmergencyWithdrawal","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"searchSmallestInvestor","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"withdrawalAddress","type":"address"}],"name":"proposeEmergencyWithdrawal","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"getStatus","outputs":[{"name":"","type":"uint256"},{"name":"","type":"uint256"},{"name":"","type":"uint256"},{"name":"","type":"uint256"},{"name":"","type":"uint256"},{"name":"","type":"uint256"},{"name":"","type":"uint256"},{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_proofType","type":"bytes1"}],"name":"changeOraclizeProofType","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"currentInvestor","type":"address"}],"name":"forceDivestOfOneInvestor","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"proposedWithdrawal","outputs":[{"name":"toAddress","type":"address"},{"name":"atTime","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"getMaxBetAmount","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"investorsLosses","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"investorsProfit","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"investorIDs","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"betsKeys","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"getMinInvestment","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"getMinBetAmount","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"changeOwnerAddress","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"increaseInvestment","outputs":[],"payable":true,"type":"function"},{"constant":false,"inputs":[],"name":"resumeContract","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"bets","outputs":[{"name":"playerAddress","type":"address"},{"name":"amountBet","type":"uint256"},{"name":"numberRolled","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"invested","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"newGasLimit","type":"uint256"}],"name":"changeGasLimitOfSafeSend","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"houseAddress","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_config","type":"bytes32"}],"name":"changeOraclizeConfig","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"numBets","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"executeEmergencyWithdrawal","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"currentInvestor","type":"address"}],"name":"getBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"newInvestor","outputs":[],"payable":true,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"},{"payable":true,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"playerAddress","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LOG_NewBet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"playerAddress","type":"address"},{"indexed":false,"name":"numberRolled","type":"uint256"},{"indexed":false,"name":"amountWon","type":"uint256"}],"name":"LOG_BetWon","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"playerAddress","type":"address"},{"indexed":false,"name":"numberRolled","type":"uint256"}],"name":"LOG_BetLost","type":"event"},{"anonymous":false,"inputs":[],"name":"LOG_EmergencyWithdrawalProposed","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"withdrawalAddress","type":"address"}],"name":"LOG_EmergencyWithdrawalFailed","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"withdrawalAddress","type":"address"},{"indexed":false,"name":"amountWithdrawn","type":"uint256"}],"name":"LOG_EmergencyWithdrawalSucceeded","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"receiver","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LOG_FailedSend","type":"event"},{"anonymous":false,"inputs":[],"name":"LOG_ZeroSend","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"investor","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LOG_InvestorEntrance","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"investor","type":"address"},{"indexed":false,"name":"amount","type":"int256"}],"name":"LOG_InvestorCapitalUpdate","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"investor","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LOG_InvestorExit","type":"event"},{"anonymous":false,"inputs":[],"name":"LOG_ContractStopped","type":"event"},{"anonymous":false,"inputs":[],"name":"LOG_ContractResumed","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"oldAddr","type":"address"},{"indexed":false,"name":"newOwnerAddress","type":"address"}],"name":"LOG_OwnerAddressChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"oldAddr","type":"address"},{"indexed":false,"name":"newHouseAddress","type":"address"}],"name":"LOG_HouseAddressChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"oldGasLimit","type":"uint256"},{"indexed":false,"name":"newGasLimit","type":"uint256"}],"name":"LOG_GasLimitChanged","type":"event"},{"anonymous":false,"inputs":[],"name":"LOG_EmergencyAutoStop","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"investor","type":"address"},{"indexed":false,"name":"vote","type":"bool"}],"name":"LOG_EmergencyWithdrawalVote","type":"event"},{"anonymous":false,"inputs":[],"name":"LOG_ValueIsTooBig","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"addr","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"LOG_SuccessfulSend","type":"event"}]`
ContractAddress = common.HexToAddress("0xdd98b423dc61A756e1070De151b1485425505954")
)
// Test example shows how to use the block logsBloom field to if a
// block contains logs that are of interest.
func main() {
contractABI, _ := abi.JSON(strings.NewReader(ContractABI))
client, err := ethclient.Dial("http://localhost:8545")
if err != nil {
panic(err)
}
// search for logs generated from the contract deployed on `ContractAddress` or logs
// with the `LOG_NewBet` event as defined in the ABI.
// https://etherscan.io/address/0xdd98b423dc61A756e1070De151b1485425505954
event := contractABI.Events["LOG_NewBet"]
start, end := int64(2933800), int64(3031150)
for blockNum := start; blockNum < end; blockNum++ {
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
block, err := client.BlockByNumber(ctx, big.NewInt(blockNum))
if err == ethereum.NotFound {
log.Printf("Sync your node")
return
}
if err != nil {
continue
}
if types.BloomLookup(block.Bloom(), ContractAddress) {
log.Printf("block #%d contains logs from 0x%x (matched by address)\n", block.NumberU64(), ContractAddress)
}
if types.BloomLookup(block.Bloom(), event.Id()) {
log.Printf("block #%d contains logs matching event `%s` (matched by topic)\n", block.NumberU64(), event.Name)
for _, tx := range block.Transactions() {
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
receipt, err := client.TransactionReceipt(ctx, tx.Hash())
if err != nil {
log.Fatalf("could not retrieve tx receipt 0x%x: %v", tx.Hash(), err)
}
for _, l := range receipt.Logs {
if l.Topics[0] == event.Id() {
log.Printf("\tlog from 0x%x matches", l.Address)
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment