call to get balances from native calls instead of calling smart contract

pull/651/head
Minh Doan 6 years ago committed by Minh Doan
parent 43d39bc5bd
commit 6dee372d20
  1. 95
      node/demo_contract.go

@ -8,11 +8,9 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/params"
"github.com/harmony-one/harmony/contracts" "github.com/harmony-one/harmony/contracts"
"github.com/harmony-one/harmony/contracts/structs"
"github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/core/types"
"github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/internal/utils"
contract_constants "github.com/harmony-one/harmony/internal/utils/contract" contract_constants "github.com/harmony-one/harmony/internal/utils/contract"
@ -108,52 +106,53 @@ func (node *Node) GetResultDirectly(priKey string) (players []string, balances [
// GetResult get current players and their balances. // GetResult get current players and their balances.
func (node *Node) GetResult(priKey string) (players []string, balances []*big.Int) { func (node *Node) GetResult(priKey string) (players []string, balances []*big.Int) {
// return node.GetResult2(priKey) return node.GetResultDirectly(priKey)
abi, err := abi.JSON(strings.NewReader(contracts.LotteryABI)) // TODO(minhdoan): get result from smart contract is current not working. Fix it later.
if err != nil { // abi, err := abi.JSON(strings.NewReader(contracts.LotteryABI))
utils.GetLogInstance().Error("Failed to generate staking contract's ABI", "error", err) // if err != nil {
} // utils.GetLogInstance().Error("Failed to generate staking contract's ABI", "error", err)
bytesData, err := abi.Pack("getPlayers") // }
if err != nil { // bytesData, err := abi.Pack("getPlayers")
utils.GetLogInstance().Error("Failed to generate ABI function bytes data", "error", err) // if err != nil {
} // utils.GetLogInstance().Error("Failed to generate ABI function bytes data", "error", err)
// }
demoContractAddress := node.DemoContractAddress
key, err := crypto.HexToECDSA(priKey) // demoContractAddress := node.DemoContractAddress
nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(key.PublicKey)) // key, err := crypto.HexToECDSA(priKey)
// nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(key.PublicKey))
tx := types.NewTransaction(
nonce, // tx := types.NewTransaction(
demoContractAddress, // nonce,
0, // demoContractAddress,
nil, // 0,
math.MaxUint64, // nil,
nil, // math.MaxUint64,
bytesData, // nil,
) // bytesData,
signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, key) // )
if err != nil { // signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, key)
utils.GetLogInstance().Error("Failed to sign contract call tx", "error", err) // if err != nil {
return nil, nil // utils.GetLogInstance().Error("Failed to sign contract call tx", "error", err)
} // return nil, nil
output, err := node.ContractCaller.CallContract(signedTx) // }
if err != nil { // output, err := node.ContractCaller.CallContract(signedTx)
utils.GetLogInstance().Error("Failed to call staking contract", "error", err) // if err != nil {
return nil, nil // utils.GetLogInstance().Error("Failed to call staking contract", "error", err)
} // return nil, nil
// }
ret := &structs.PlayersInfo{}
err = abi.Unpack(ret, "getPlayers", output) // ret := &structs.PlayersInfo{}
// err = abi.Unpack(ret, "getPlayers", output)
if err != nil {
utils.GetLogInstance().Error("Failed to unpack stake info", "error", err) // if err != nil {
return nil, nil // utils.GetLogInstance().Error("Failed to unpack stake info", "error", err)
} // return nil, nil
for _, player := range ret.Players { // }
players = append(players, player.String()) // for _, player := range ret.Players {
} // players = append(players, player.String())
balances = ret.Balances // }
return players, balances // balances = ret.Balances
// return players, balances
} }
// CreateTransactionForPickWinner generates transaction for enter method and add it into pending tx list. // CreateTransactionForPickWinner generates transaction for enter method and add it into pending tx list.

Loading…
Cancel
Save