|
|
@ -2,6 +2,8 @@ package node |
|
|
|
|
|
|
|
|
|
|
|
// CreateTransactionForEnterMethod creates transaction to call enter method of lottery contract.
|
|
|
|
// CreateTransactionForEnterMethod creates transaction to call enter method of lottery contract.
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
|
|
|
|
"fmt" |
|
|
|
|
|
|
|
"math" |
|
|
|
"math/big" |
|
|
|
"math/big" |
|
|
|
"os" |
|
|
|
"os" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
@ -104,55 +106,62 @@ func (node *Node) GetResultDirectly(priKey string) (players []string, balances [ |
|
|
|
return players, balances |
|
|
|
return players, balances |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GenerateResultDirectly get current players and their balances, not from smart contract.
|
|
|
|
|
|
|
|
func (node *Node) GenerateResultDirectly(addresses []common.Address) (players []string, balances []*big.Int) { |
|
|
|
|
|
|
|
for _, address := range addresses { |
|
|
|
|
|
|
|
players = append(players, address.String()) |
|
|
|
|
|
|
|
balances = append(balances, node.GetBalanceOfAddress(address)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
fmt.Println("generate result", players, balances) |
|
|
|
|
|
|
|
return players, 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.GetResultDirectly(priKey) |
|
|
|
|
|
|
|
// TODO(minhdoan): get result from smart contract is current not working. Fix it later.
|
|
|
|
// TODO(minhdoan): get result from smart contract is current not working. Fix it later.
|
|
|
|
// abi, err := abi.JSON(strings.NewReader(contracts.LotteryABI))
|
|
|
|
abi, err := abi.JSON(strings.NewReader(contracts.LotteryABI)) |
|
|
|
// if err != nil {
|
|
|
|
if err != nil { |
|
|
|
// utils.GetLogInstance().Error("Failed to generate staking contract's ABI", "error", err)
|
|
|
|
utils.GetLogInstance().Error("Failed to generate staking contract's ABI", "error", err) |
|
|
|
// }
|
|
|
|
} |
|
|
|
// bytesData, err := abi.Pack("getPlayers")
|
|
|
|
bytesData, err := abi.Pack("getPlayers") |
|
|
|
// if err != nil {
|
|
|
|
if err != nil { |
|
|
|
// utils.GetLogInstance().Error("Failed to generate ABI function bytes data", "error", err)
|
|
|
|
utils.GetLogInstance().Error("Failed to generate ABI function bytes data", "error", err) |
|
|
|
// }
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// demoContractAddress := node.DemoContractAddress
|
|
|
|
demoContractAddress := node.DemoContractAddress |
|
|
|
// key, err := crypto.HexToECDSA(priKey)
|
|
|
|
key, err := crypto.HexToECDSA(priKey) |
|
|
|
// nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(key.PublicKey))
|
|
|
|
nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(key.PublicKey)) |
|
|
|
|
|
|
|
|
|
|
|
// tx := types.NewTransaction(
|
|
|
|
tx := types.NewTransaction( |
|
|
|
// nonce,
|
|
|
|
nonce, |
|
|
|
// demoContractAddress,
|
|
|
|
demoContractAddress, |
|
|
|
// 0,
|
|
|
|
0, |
|
|
|
// nil,
|
|
|
|
nil, |
|
|
|
// math.MaxUint64,
|
|
|
|
math.MaxUint64, |
|
|
|
// nil,
|
|
|
|
nil, |
|
|
|
// bytesData,
|
|
|
|
bytesData, |
|
|
|
// )
|
|
|
|
) |
|
|
|
// signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, key)
|
|
|
|
signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, key) |
|
|
|
// if err != nil {
|
|
|
|
if err != nil { |
|
|
|
// utils.GetLogInstance().Error("Failed to sign contract call tx", "error", err)
|
|
|
|
utils.GetLogInstance().Error("Failed to sign contract call tx", "error", err) |
|
|
|
// return nil, nil
|
|
|
|
return nil, nil |
|
|
|
// }
|
|
|
|
} |
|
|
|
// output, err := node.ContractCaller.CallContract(signedTx)
|
|
|
|
output, err := node.ContractCaller.CallContract(signedTx) |
|
|
|
// if err != nil {
|
|
|
|
if err != nil { |
|
|
|
// utils.GetLogInstance().Error("Failed to call staking contract", "error", err)
|
|
|
|
utils.GetLogInstance().Error("Failed to call staking contract", "error", err) |
|
|
|
// return nil, nil
|
|
|
|
return nil, nil |
|
|
|
// }
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ret := &structs.PlayersInfo{}
|
|
|
|
ret := []common.Address{} |
|
|
|
// err = abi.Unpack(ret, "getPlayers", output)
|
|
|
|
err = abi.Unpack(&ret, "getPlayers", output) |
|
|
|
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
if err != nil { |
|
|
|
// utils.GetLogInstance().Error("Failed to unpack stake info", "error", err)
|
|
|
|
utils.GetLogInstance().Error("Failed to unpack getPlayers", "error", err) |
|
|
|
// return nil, nil
|
|
|
|
return nil, nil |
|
|
|
// }
|
|
|
|
} |
|
|
|
// for _, player := range ret.Players {
|
|
|
|
utils.GetLogInstance().Info("get result: ", ret) |
|
|
|
// players = append(players, player.String())
|
|
|
|
fmt.Println("get result called:", ret) |
|
|
|
// }
|
|
|
|
return node.GenerateResultDirectly(ret) |
|
|
|
// 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.
|
|
|
|