|
| 1 | +package gas |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 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/interop/contract" |
| 11 | + "github.com/nspcc-dev/neo-go/pkg/core/native" |
| 12 | + "github.com/nspcc-dev/neo-go/pkg/core/native/nativeids" |
| 13 | + "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" |
| 14 | + "github.com/nspcc-dev/neo-go/pkg/smartcontract" |
| 15 | + "github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag" |
| 16 | + "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" |
| 17 | + "github.com/nspcc-dev/neo-go/pkg/util" |
| 18 | + "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" |
| 19 | +) |
| 20 | + |
| 21 | +// DefaultBalance is a balance of every account in redefined [GAS] native |
| 22 | +// contract. |
| 23 | +const DefaultBalance = 100 * native.GASFactor |
| 24 | + |
| 25 | +var _ = (native.IGAS)(&GAS{}) |
| 26 | + |
| 27 | +func (g *GAS) Metadata() *interop.ContractMD { |
| 28 | + return &g.ContractMD |
| 29 | +} |
| 30 | + |
| 31 | +// GAS represents GAS custom native contract. It always returns [DefaultBalance] as a |
| 32 | +// balance, has no-op `Burn`, `Mint`, `Transfer` operations. |
| 33 | +type GAS struct { |
| 34 | + interop.ContractMD |
| 35 | + symbol string |
| 36 | + decimals int64 |
| 37 | +} |
| 38 | + |
| 39 | +// NewGAS returns [GAS] custom native contract. |
| 40 | +func NewGAS() *GAS { |
| 41 | + g := &GAS{} |
| 42 | + defer g.BuildHFSpecificMD(g.ActiveIn()) |
| 43 | + |
| 44 | + g.ContractMD = *interop.NewContractMD(nativenames.Gas, nativeids.GasToken, func(m *manifest.Manifest, hf config.Hardfork) { |
| 45 | + m.SupportedStandards = []string{manifest.NEP17StandardName} |
| 46 | + }) |
| 47 | + g.symbol = "GAS" |
| 48 | + g.decimals = 8 |
| 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 | + g.AddMethod(md, desc) |
| 77 | + |
| 78 | + eDesc := native.NewEventDescriptor("Transfer", transferParams...) |
| 79 | + eMD := native.NewEvent(eDesc) |
| 80 | + g.AddEvent(eMD) |
| 81 | + |
| 82 | + return g |
| 83 | +} |
| 84 | + |
| 85 | +// Initialize initializes a GAS contract. |
| 86 | +func (g *GAS) Initialize(ic *interop.Context, hf *config.Hardfork, newMD *interop.HFSpecificContractMD) error { |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +// InitializeCache implements the [interop.Contract] interface. |
| 91 | +func (g *GAS) InitializeCache(_ interop.IsHardforkEnabled, blockHeight uint32, d *dao.Simple) error { |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +// OnPersist implements the [interop.Contract] interface. |
| 96 | +func (g *GAS) OnPersist(ic *interop.Context) error { |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +// PostPersist implements the [interop.Contract] interface. |
| 101 | +func (g *GAS) PostPersist(ic *interop.Context) error { |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +// ActiveIn implements the [interop.Contract] interface. |
| 106 | +func (g *GAS) ActiveIn() *config.Hardfork { |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +// BalanceOf returns native GAS token balance for the acc. |
| 111 | +func (g *GAS) BalanceOf(d *dao.Simple, acc util.Uint160) *big.Int { |
| 112 | + return big.NewInt(DefaultBalance * native.GASFactor) |
| 113 | +} |
| 114 | + |
| 115 | +func (g *GAS) Symbol(_ *interop.Context, _ []stackitem.Item) stackitem.Item { |
| 116 | + return stackitem.NewByteArray([]byte(g.symbol)) |
| 117 | +} |
| 118 | + |
| 119 | +func (g *GAS) Decimals(_ *interop.Context, _ []stackitem.Item) stackitem.Item { |
| 120 | + return stackitem.NewBigInteger(big.NewInt(g.decimals)) |
| 121 | +} |
| 122 | + |
| 123 | +func (g *GAS) TotalSupply(ic *interop.Context, _ []stackitem.Item) stackitem.Item { |
| 124 | + return stackitem.NewBigInteger(big.NewInt(DefaultBalance)) |
| 125 | +} |
| 126 | + |
| 127 | +func toUint160(s stackitem.Item) util.Uint160 { |
| 128 | + u, err := stackitem.ToUint160(s) |
| 129 | + if err != nil { |
| 130 | + panic(err) |
| 131 | + } |
| 132 | + return u |
| 133 | +} |
| 134 | + |
| 135 | +func toBigInt(s stackitem.Item) *big.Int { |
| 136 | + bi, err := s.TryInteger() |
| 137 | + if err != nil { |
| 138 | + panic(err) |
| 139 | + } |
| 140 | + return bi |
| 141 | +} |
| 142 | + |
| 143 | +func (g *GAS) Transfer(ic *interop.Context, args []stackitem.Item) stackitem.Item { |
| 144 | + from := toUint160(args[0]) |
| 145 | + to := toUint160(args[1]) |
| 146 | + amount := toBigInt(args[2]) |
| 147 | + |
| 148 | + paymentArgs := []stackitem.Item{ |
| 149 | + stackitem.NewByteArray(from.BytesBE()), |
| 150 | + stackitem.NewBigInteger(amount), |
| 151 | + args[3], |
| 152 | + } |
| 153 | + cs, err := ic.GetContract(to) |
| 154 | + if err == nil { |
| 155 | + err = contract.CallFromNative(ic, g.Hash, cs, manifest.MethodOnNEP17Payment, paymentArgs, false) |
| 156 | + if err != nil { |
| 157 | + panic(fmt.Errorf("failed to call %s: %w", manifest.MethodOnNEP17Payment, err)) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + return stackitem.NewBool(true) |
| 162 | +} |
| 163 | + |
| 164 | +// balanceOf is the only difference with default native GAS implementation: |
| 165 | +// it always returns fixed number of tokens. |
| 166 | +func (g *GAS) balanceOf(ic *interop.Context, args []stackitem.Item) stackitem.Item { |
| 167 | + return stackitem.NewBigInteger(g.BalanceOf(nil, util.Uint160{})) |
| 168 | +} |
| 169 | + |
| 170 | +func (g *GAS) Mint(ic *interop.Context, h util.Uint160, amount *big.Int, callOnPayment bool) { |
| 171 | +} |
| 172 | + |
| 173 | +func (g *GAS) Burn(ic *interop.Context, h util.Uint160, amount *big.Int) { |
| 174 | +} |
0 commit comments