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

Loading…
Cancel
Save