|
| 1 | +package gas |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "math/big" |
| 6 | + |
| 7 | + "github.com/nspcc-dev/neo-go/pkg/config" |
| 8 | + "github.com/nspcc-dev/neo-go/pkg/core/dao" |
| 9 | + "github.com/nspcc-dev/neo-go/pkg/core/interop" |
| 10 | + "github.com/nspcc-dev/neo-go/pkg/core/native" |
| 11 | + "github.com/nspcc-dev/neo-go/pkg/core/native/nativeids" |
| 12 | + "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" |
| 13 | + "github.com/nspcc-dev/neo-go/pkg/crypto/hash" |
| 14 | + "github.com/nspcc-dev/neo-go/pkg/crypto/keys" |
| 15 | + "github.com/nspcc-dev/neo-go/pkg/smartcontract" |
| 16 | + "github.com/nspcc-dev/neo-go/pkg/util" |
| 17 | +) |
| 18 | + |
| 19 | +// DefaultBalance is a balance of every account in redefined [GAS] native |
| 20 | +// contract |
| 21 | +const DefaultBalance = 100 |
| 22 | + |
| 23 | +// GAS represents GAS custom native contract. It always returns [DefaultBalance] as a |
| 24 | +// balance, has no-op `Burn`, `Mint`, `Transfer` operations. |
| 25 | +type GAS struct { |
| 26 | + nep17TokenNative |
| 27 | + initialSupply int64 |
| 28 | +} |
| 29 | + |
| 30 | +// NewGAS returns [GAS] custom native contract. |
| 31 | +func NewGAS(init int64) *GAS { |
| 32 | + g := &GAS{ |
| 33 | + initialSupply: init, |
| 34 | + } |
| 35 | + defer g.BuildHFSpecificMD(g.ActiveIn()) |
| 36 | + |
| 37 | + nep17 := newNEP17Native(nativenames.Gas, nativeids.GasToken, nil) |
| 38 | + nep17.symbol = "GAS" |
| 39 | + nep17.decimals = 8 |
| 40 | + nep17.factor = native.GASFactor |
| 41 | + |
| 42 | + g.nep17TokenNative = *nep17 |
| 43 | + |
| 44 | + return g |
| 45 | +} |
| 46 | + |
| 47 | +// Initialize initializes a GAS contract. |
| 48 | +func (g *GAS) Initialize(ic *interop.Context, hf *config.Hardfork, newMD *interop.HFSpecificContractMD) error { |
| 49 | + if hf != g.ActiveIn() { |
| 50 | + return nil |
| 51 | + } |
| 52 | + |
| 53 | + if err := g.nep17TokenNative.Initialize(ic); err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + _, totalSupply := g.getTotalSupply(ic.DAO) |
| 57 | + if totalSupply.Sign() != 0 { |
| 58 | + return errors.New("already initialized") |
| 59 | + } |
| 60 | + h, err := getStandbyValidatorsHash(ic) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + g.Mint(ic, h, big.NewInt(g.initialSupply), false) |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +// InitializeCache implements the [interop.Contract] interface. |
| 69 | +func (g *GAS) InitializeCache(_ interop.IsHardforkEnabled, blockHeight uint32, d *dao.Simple) error { |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +// OnPersist implements the [interop.Contract] interface. |
| 74 | +func (g *GAS) OnPersist(ic *interop.Context) error { |
| 75 | + return nil |
| 76 | +} |
| 77 | + |
| 78 | +// PostPersist implements the [interop.Contract] interface. |
| 79 | +func (g *GAS) PostPersist(ic *interop.Context) error { |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +// ActiveIn implements the [interop.Contract] interface. |
| 84 | +func (g *GAS) ActiveIn() *config.Hardfork { |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +// BalanceOf returns native GAS token balance for the acc. |
| 89 | +func (g *GAS) BalanceOf(d *dao.Simple, acc util.Uint160) *big.Int { |
| 90 | + return g.balanceOfInternal(d, acc) |
| 91 | +} |
| 92 | + |
| 93 | +func getStandbyValidatorsHash(ic *interop.Context) (util.Uint160, error) { |
| 94 | + cfg := ic.Chain.GetConfig() |
| 95 | + committee, err := keys.NewPublicKeysFromStrings(cfg.StandbyCommittee) |
| 96 | + if err != nil { |
| 97 | + return util.Uint160{}, err |
| 98 | + } |
| 99 | + s, err := smartcontract.CreateDefaultMultiSigRedeemScript(committee[:cfg.GetNumOfCNs(0)]) |
| 100 | + if err != nil { |
| 101 | + return util.Uint160{}, err |
| 102 | + } |
| 103 | + return hash.Hash160(s), nil |
| 104 | +} |
0 commit comments