2021-07-22 07:57:11 +00:00
|
|
|
package morph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
)
|
|
|
|
|
|
|
|
// initialAlphabetNEOAmount represents total amount of GAS distributed between alphabet nodes.
|
|
|
|
const initialAlphabetNEOAmount = native.NEOTotalSupply
|
|
|
|
|
|
|
|
func (c *initializeContext) registerCandidates() error {
|
2021-07-29 13:35:57 +00:00
|
|
|
neoHash := c.nativeHash(nativenames.Neo)
|
2021-07-22 07:57:11 +00:00
|
|
|
|
|
|
|
res, err := c.Client.InvokeFunction(neoHash, "getCandidates", []smartcontract.Parameter{}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if res.State == vm.HaltState.String() && len(res.Stack) > 0 {
|
|
|
|
arr, ok := res.Stack[0].Value().([]stackitem.Item)
|
|
|
|
if ok && len(arr) > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
regPrice, err := c.Client.GetCandidateRegisterPrice()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't fetch registration price: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sysGas := regPrice + native.GASFactor // + 1 GAS
|
2021-07-30 11:26:44 +00:00
|
|
|
for _, acc := range c.Accounts {
|
2021-07-22 07:57:11 +00:00
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
emit.AppCall(w.BinWriter, neoHash, "registerCandidate", callflag.States, acc.PrivateKey().PublicKey().Bytes())
|
|
|
|
emit.Opcodes(w.BinWriter, opcode.ASSERT)
|
|
|
|
|
|
|
|
h, err := c.Client.SignAndPushInvocationTx(w.Bytes(), acc, sysGas, 0, []client.SignerAccount{{
|
|
|
|
Signer: transaction.Signer{
|
|
|
|
Account: acc.Contract.ScriptHash(),
|
|
|
|
Scopes: transaction.CalledByEntry,
|
|
|
|
},
|
|
|
|
Account: acc,
|
|
|
|
}})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Hashes = append(c.Hashes, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.awaitTx()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *initializeContext) transferNEOToAlphabetContracts() error {
|
2021-07-29 13:35:57 +00:00
|
|
|
neoHash := c.nativeHash(nativenames.Neo)
|
2021-07-22 07:57:11 +00:00
|
|
|
|
|
|
|
ok, err := c.transferNEOFinished(neoHash)
|
|
|
|
if ok || err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:19:56 +00:00
|
|
|
cs := c.getContract(alphabetContract)
|
2021-07-22 07:57:11 +00:00
|
|
|
amount := initialAlphabetNEOAmount / len(c.Wallets)
|
|
|
|
|
|
|
|
bw := io.NewBufBinWriter()
|
2021-07-30 11:26:44 +00:00
|
|
|
for _, acc := range c.Accounts {
|
2021-07-22 07:57:11 +00:00
|
|
|
h := state.CreateContractHash(acc.Contract.ScriptHash(), cs.NEF.Checksum, cs.Manifest.Name)
|
|
|
|
emit.AppCall(bw.BinWriter, neoHash, "transfer", callflag.All,
|
|
|
|
c.CommitteeAcc.Contract.ScriptHash(), h, int64(amount), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.sendCommitteeTx(bw.Bytes(), -1); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.awaitTx()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *initializeContext) transferNEOFinished(neoHash util.Uint160) (bool, error) {
|
|
|
|
bal, err := c.Client.NEP17BalanceOf(neoHash, c.CommitteeAcc.Contract.ScriptHash())
|
|
|
|
return bal < native.NEOTotalSupply, err
|
|
|
|
}
|