|
| 1 | +package gas |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/big" |
| 5 | + |
| 6 | + "github.com/nspcc-dev/neo-go/pkg/config" |
| 7 | + "github.com/nspcc-dev/neo-go/pkg/core/dao" |
| 8 | + "github.com/nspcc-dev/neo-go/pkg/core/interop" |
| 9 | + "github.com/nspcc-dev/neo-go/pkg/core/native" |
| 10 | + "github.com/nspcc-dev/neo-go/pkg/core/native/nativeids" |
| 11 | + "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" |
| 12 | + "github.com/nspcc-dev/neo-go/pkg/smartcontract" |
| 13 | + "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" |
| 14 | + "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" |
| 15 | + "github.com/nspcc-dev/neo-go/pkg/util" |
| 16 | + "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" |
| 17 | +) |
| 18 | + |
| 19 | +// DefaultBalance is a balance of every account in redefined [GAS] native |
| 20 | +// contract. |
| 21 | +const DefaultBalance = 100 |
| 22 | + |
| 23 | +var _ = (native.IGAS)(&GAS{}) |
| 24 | + |
| 25 | +func (g *GAS) Metadata() *interop.ContractMD { |
| 26 | + return &g.ContractMD |
| 27 | +} |
| 28 | + |
| 29 | +// GAS represents GAS custom native contract. It always returns [DefaultBalance] as a |
| 30 | +// balance, has no-op `Burn`, `Mint`, `Transfer` operations. |
| 31 | +type GAS struct { |
| 32 | + interop.ContractMD |
| 33 | + symbol string |
| 34 | + decimals int64 |
| 35 | + factor int64 |
| 36 | +} |
| 37 | + |
| 38 | +// NewGAS returns [GAS] custom native contract. |
| 39 | +func NewGAS() *GAS { |
| 40 | + g := &GAS{} |
| 41 | + defer g.BuildHFSpecificMD(g.ActiveIn()) |
| 42 | + |
| 43 | + g.ContractMD = *interop.NewContractMD(nativenames.Gas, nativeids.GasToken, func(m *manifest.Manifest, hf config.Hardfork) { |
| 44 | + m.SupportedStandards = []string{manifest.NEP17StandardName} |
| 45 | + }) |
| 46 | + g.symbol = "GAS" |
| 47 | + g.decimals = 8 |
| 48 | + g.factor = native.GASFactor |
| 49 | + |
| 50 | + desc := native.NewDescriptor("symbol", smartcontract.StringType) |
| 51 | + md := native.NewMethodAndPrice(g.Symbol, 0, callflag.NoneFlag) |
| 52 | + g.AddMethod(md, desc) |
| 53 | + |
| 54 | + desc = native.NewDescriptor("decimals", smartcontract.IntegerType) |
| 55 | + md = native.NewMethodAndPrice(g.Decimals, 0, callflag.NoneFlag) |
| 56 | + g.AddMethod(md, desc) |
| 57 | + |
| 58 | + desc = native.NewDescriptor("totalSupply", smartcontract.IntegerType) |
| 59 | + md = native.NewMethodAndPrice(g.TotalSupply, 1<<15, callflag.ReadStates) |
| 60 | + g.AddMethod(md, desc) |
| 61 | + |
| 62 | + desc = native.NewDescriptor("balanceOf", smartcontract.IntegerType, |
| 63 | + manifest.NewParameter("account", smartcontract.Hash160Type)) |
| 64 | + md = native.NewMethodAndPrice(g.balanceOf, 1<<15, callflag.ReadStates) |
| 65 | + g.AddMethod(md, desc) |
| 66 | + |
| 67 | + transferParams := []manifest.Parameter{ |
| 68 | + manifest.NewParameter("from", smartcontract.Hash160Type), |
| 69 | + manifest.NewParameter("to", smartcontract.Hash160Type), |
| 70 | + manifest.NewParameter("amount", smartcontract.IntegerType), |
| 71 | + } |
| 72 | + desc = native.NewDescriptor("transfer", smartcontract.BoolType, |
| 73 | + append(transferParams, manifest.NewParameter("data", smartcontract.AnyType))..., |
| 74 | + ) |
| 75 | + md = native.NewMethodAndPrice(g.Transfer, 1<<17, callflag.States|callflag.AllowCall|callflag.AllowNotify) |
| 76 | + md.StorageFee = 50 |
| 77 | + g.AddMethod(md, desc) |
| 78 | + |
| 79 | + eDesc := native.NewEventDescriptor("Transfer", transferParams...) |
| 80 | + eMD := native.NewEvent(eDesc) |
| 81 | + g.AddEvent(eMD) |
| 82 | + |
| 83 | + return g |
| 84 | +} |
| 85 | + |
| 86 | +// Initialize initializes a GAS contract. |
| 87 | +func (g *GAS) Initialize(ic *interop.Context, hf *config.Hardfork, newMD *interop.HFSpecificContractMD) error { |
| 88 | + return nil |
| 89 | +} |
| 90 | + |
| 91 | +// InitializeCache implements the [interop.Contract] interface. |
| 92 | +func (g *GAS) InitializeCache(_ interop.IsHardforkEnabled, blockHeight uint32, d *dao.Simple) error { |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +// OnPersist implements the [interop.Contract] interface. |
| 97 | +func (g *GAS) OnPersist(ic *interop.Context) error { |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +// PostPersist implements the [interop.Contract] interface. |
| 102 | +func (g *GAS) PostPersist(ic *interop.Context) error { |
| 103 | + return nil |
| 104 | +} |
| 105 | + |
| 106 | +// ActiveIn implements the [interop.Contract] interface. |
| 107 | +func (g *GAS) ActiveIn() *config.Hardfork { |
| 108 | + return nil |
| 109 | +} |
| 110 | + |
| 111 | +// BalanceOf returns native GAS token balance for the acc. |
| 112 | +func (g *GAS) BalanceOf(d *dao.Simple, acc util.Uint160) *big.Int { |
| 113 | + return big.NewInt(DefaultBalance * native.GASFactor) |
| 114 | +} |
| 115 | + |
| 116 | +func (g *GAS) Symbol(_ *interop.Context, _ []stackitem.Item) stackitem.Item { |
| 117 | + return stackitem.NewByteArray([]byte(g.symbol)) |
| 118 | +} |
| 119 | + |
| 120 | +func (g *GAS) Decimals(_ *interop.Context, _ []stackitem.Item) stackitem.Item { |
| 121 | + return stackitem.NewBigInteger(big.NewInt(g.decimals)) |
| 122 | +} |
| 123 | + |
| 124 | +func (g *GAS) TotalSupply(ic *interop.Context, _ []stackitem.Item) stackitem.Item { |
| 125 | + return stackitem.NewBigInteger(big.NewInt(DefaultBalance * native.GASFactor)) |
| 126 | +} |
| 127 | + |
| 128 | +func (g *GAS) Transfer(ic *interop.Context, args []stackitem.Item) stackitem.Item { |
| 129 | + return stackitem.NewBool(true) |
| 130 | +} |
| 131 | + |
| 132 | +// balanceOf is the only difference with default native GAS implementation: |
| 133 | +// it always returns fixed number of tokens. |
| 134 | +func (g *GAS) balanceOf(ic *interop.Context, args []stackitem.Item) stackitem.Item { |
| 135 | + return stackitem.NewBigInteger(big.NewInt(DefaultBalance * native.GASFactor)) |
| 136 | +} |
| 137 | + |
| 138 | +func (g *GAS) Mint(ic *interop.Context, h util.Uint160, amount *big.Int, callOnPayment bool) { |
| 139 | +} |
| 140 | + |
| 141 | +func (g *GAS) Burn(ic *interop.Context, h util.Uint160, amount *big.Int) { |
| 142 | +} |
0 commit comments