Skip to content

Commit

Permalink
increase ONT/ONG decimals (#1376)
Browse files Browse the repository at this point in the history
* increase ONT/ONG decimals

* update ontio-std version

* fix license

* add v2 method

* add ong v2 method

* update gas price for eip155 tx and api

* update test case

* fix testcase

* fix test

* fix test

* fmt

* fix test

* fix test

* fix test

* fix unboud ong test

* fix eip155tx test

* fix deserialize bug

* update

* add api v2

* fix v2

* fmt code

* fmt code

Co-authored-by: laizy <[email protected]>

Co-authored-by: lucas <[email protected]>
  • Loading branch information
laizy and lucas7788 authored Oct 26, 2021
1 parent a18419d commit 3b35579
Show file tree
Hide file tree
Showing 41 changed files with 1,149 additions and 311 deletions.
13 changes: 4 additions & 9 deletions cmd/utils/ont.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func ApproveTx(gasPrice, gasLimit uint64, asset string, from, to string, amount
if err != nil {
return nil, fmt.Errorf("To address:%s invalid:%s", to, err)
}
var state = &ont.State{
var state = &ont.TransferState{
From: fromAddr,
To: toAddr,
Value: amount,
Expand Down Expand Up @@ -217,8 +217,8 @@ func TransferTx(gasPrice, gasLimit uint64, asset, from, to string, amount uint64
if err != nil {
return nil, fmt.Errorf("to address:%s invalid:%s", to, err)
}
var sts []*ont.State
sts = append(sts, &ont.State{
var sts []*ont.TransferState
sts = append(sts, &ont.TransferState{
From: fromAddr,
To: toAddr,
Value: amount,
Expand Down Expand Up @@ -256,12 +256,7 @@ func TransferFromTx(gasPrice, gasLimit uint64, asset, sender, from, to string, a
if err != nil {
return nil, fmt.Errorf("to address:%s invalid:%s", to, err)
}
transferFrom := &ont.TransferFrom{
Sender: senderAddr,
From: fromAddr,
To: toAddr,
Value: amount,
}
transferFrom := ont.NewTransferFromState(senderAddr, fromAddr, toAddr, amount)
var version byte
var contractAddr common.Address
switch strings.ToLower(asset) {
Expand Down
21 changes: 16 additions & 5 deletions common/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package constants

import (
"time"

"github.com/laizy/bigint"
)

// genesis constants
Expand All @@ -32,19 +34,28 @@ var (
)

// ont constants
const GWei = 1000000000

const (
ONT_NAME = "ONT Token"
ONT_SYMBOL = "ONT"
ONT_DECIMALS = 0
ONT_TOTAL_SUPPLY = uint64(1000000000)
ONT_NAME = "ONT Token"
ONT_SYMBOL = "ONT"
ONT_DECIMALS = 0
ONT_DECIMALS_V2 = 9
ONT_TOTAL_SUPPLY = 1000000000
ONT_TOTAL_SUPPLY_V2 = 1000000000000000000
)

// ong constants
const (
ONG_NAME = "ONG Token"
ONG_SYMBOL = "ONG"
ONG_DECIMALS = 9
ONG_TOTAL_SUPPLY = uint64(1000000000000000000)
ONG_DECIMALS_V2 = 18
ONG_TOTAL_SUPPLY = 1000000000000000000
)

var (
ONG_TOTAL_SUPPLY_V2 = bigint.New(10).ExpUint8(27)
)

// ont/ong unbound model constants
Expand Down
3 changes: 2 additions & 1 deletion common/zero_copy_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ func (self *ZeroCopySink) WriteUint32(data uint32) {
binary.LittleEndian.PutUint32(buf, data)
}

func (self *ZeroCopySink) WriteUint64(data uint64) {
func (self *ZeroCopySink) WriteUint64(data uint64) *ZeroCopySink {
buf := self.NextBytes(8)
binary.LittleEndian.PutUint64(buf, data)
return self
}

func (self *ZeroCopySink) WriteInt64(data int64) {
Expand Down
120 changes: 120 additions & 0 deletions core/states/native_token_balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/

package states

import (
"errors"
"fmt"
"math/big"

"github.com/laizy/bigint"
"github.com/ontio/ontology/common"
)

const ScaleFactor = 1000000000
const ScaleDecimal9Version = 1
const DefaultVersion = 0

// ont balance with decimal 9
type NativeTokenBalance struct {
Balance bigint.Int
}

func (self *NativeTokenBalance) String() string {
return self.Balance.String()
}

func (self NativeTokenBalance) MustToStorageItemBytes() []byte {
return self.MustToStorageItem().ToArray()
}

func (self NativeTokenBalance) Add(rhs NativeTokenBalance) NativeTokenBalance {
return NativeTokenBalance{Balance: self.Balance.Add(rhs.Balance)}
}

func (self NativeTokenBalance) IsZero() bool {
return self.Balance.IsZero()
}

func (self NativeTokenBalance) Sub(rhs NativeTokenBalance) (result NativeTokenBalance, err error) {
if self.Balance.LessThan(rhs.Balance) {
return result, fmt.Errorf("balance sub underflow: a: %s, b: %s", self.Balance, rhs.Balance)
}
return NativeTokenBalance{Balance: self.Balance.Sub(rhs.Balance)}, nil
}

func (self NativeTokenBalance) MustToStorageItem() *StorageItem {
if self.IsFloat() {
return &StorageItem{
StateBase: StateBase{StateVersion: ScaleDecimal9Version},
Value: common.BigIntToNeoBytes(self.Balance.BigInt()),
}
}

return &StorageItem{
StateBase: StateBase{StateVersion: DefaultVersion},
Value: common.NewZeroCopySink(nil).WriteUint64(self.MustToInteger64()).Bytes(),
}
}

func NativeTokenBalanceFromStorageItem(val *StorageItem) (NativeTokenBalance, error) {
if val.StateVersion == DefaultVersion {
balance, err := common.NewZeroCopySource(val.Value).ReadUint64()
if err != nil {
return NativeTokenBalance{}, err
}
return NativeTokenBalance{Balance: bigint.Mul(balance, ScaleFactor)}, nil
}
balance := bigint.New(common.BigIntFromNeoBytes(val.Value))
if balance.IsNegative() {
return NativeTokenBalance{}, errors.New("negative balance")
}

return NativeTokenBalance{Balance: bigint.New(common.BigIntFromNeoBytes(val.Value))}, nil
}

func (self NativeTokenBalance) IsFloat() bool {
return !self.Balance.Mod(ScaleFactor).IsZero()
}

func (self NativeTokenBalance) ToInteger() bigint.Int {
return self.Balance.Div(ScaleFactor)
}

func (self NativeTokenBalance) FloatPart() uint64 {
val := self.Balance.Mod(ScaleFactor).BigInt()
return val.Uint64()
}

func (self NativeTokenBalance) MustToInteger64() uint64 {
val := self.Balance.Div(ScaleFactor).BigInt()
if !val.IsUint64() {
panic("too large token balance")
}

return val.Uint64()
}

func NativeTokenBalanceFromInteger(val uint64) NativeTokenBalance {
return NativeTokenBalance{Balance: bigint.Mul(val, ScaleFactor)}
}

func (self NativeTokenBalance) ToBigInt() *big.Int {
return self.Balance.BigInt()
}
4 changes: 2 additions & 2 deletions core/store/ledgerstore/block_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@ func TestBlock(t *testing.T) {
}

func transferTx(from, to common.Address, amount uint64) (*types.Transaction, error) {
var sts []ont.State
sts = append(sts, ont.State{
var sts []ont.TransferState
sts = append(sts, ont.TransferState{
From: from,
To: to,
Value: amount,
Expand Down
2 changes: 1 addition & 1 deletion core/store/ledgerstore/tx_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func SaveNotify(eventStore scommon.EventStore, txHash common.Uint256, notify *ev
}

func genNativeTransferCode(from, to common.Address, value uint64) []byte {
transfer := &ont.Transfers{States: []ont.State{{From: from, To: to, Value: value}}}
transfer := &ont.TransferStates{States: []ont.TransferState{{From: from, To: to, Value: value}}}
return common.SerializeToBytes(transfer)
}

Expand Down
7 changes: 6 additions & 1 deletion core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,17 @@ func TransactionFromEIP155(eiptx *types.Transaction) (*Transaction, error) {
if eiptx.Nonce() > uint64(math.MaxUint32) || !eiptx.GasPrice().IsUint64() {
return nil, fmt.Errorf("nonce :%d or GasPrice :%d is too big", eiptx.Nonce(), eiptx.GasPrice())
}
gasPrice := eiptx.GasPrice().Uint64()
if gasPrice%constants.GWei != 0 {
return nil, fmt.Errorf("gasprice %d is not multiple of GWei", gasPrice)
}
gasPriceInGwei := gasPrice / constants.GWei

retTx := &Transaction{
Version: byte(0),
TxType: EIP155,
Nonce: uint32(eiptx.Nonce()),
GasPrice: eiptx.GasPrice().Uint64(),
GasPrice: gasPriceInGwei,
GasLimit: eiptx.Gas(),
Payer: addr,
Payload: &payload.EIP155Code{EIPTx: eiptx},
Expand Down
5 changes: 4 additions & 1 deletion core/types/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import (
"math/big"
"testing"

"github.com/laizy/bigint"
"github.com/ontio/ontology/common/constants"

ethcomm "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -67,7 +70,7 @@ func genTx(nonce uint64) *Transaction {
privateKey, _ := crypto.HexToECDSA("fad9c8855b740a0b7ed4c221dbad0f33a83a49cad6b3fe8d5817ac83d38b6a19")
value := big.NewInt(1000000000)
gaslimit := uint64(21000)
gasPrice := big.NewInt(2500)
gasPrice := bigint.Mul(2500, constants.GWei).BigInt()
toAddress := ethcomm.HexToAddress("0x4592d8f8d7b001e72cb26a73e4fa1806a51ac79d")
tx := types.NewTransaction(nonce, toAddress, value, gaslimit, gasPrice, nil)

Expand Down
8 changes: 2 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ go 1.17

require (
github.com/JohnCGriffin/overflow v0.0.0-20170615021017-4d914c927216
github.com/OneOfOne/xxhash v1.2.5 // indirect
github.com/Workiva/go-datastructures v1.0.50
github.com/Workiva/go-datastructures v1.0.50 // indirect
github.com/blang/semver v3.5.1+incompatible
github.com/elastic/gosigar v0.8.1-0.20180330100440-37f05ff46ffa // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/ethereum/go-ethereum v1.9.25
github.com/gammazero/workerpool v1.1.2
Expand All @@ -20,6 +18,7 @@ require (
github.com/howeyc/gopass v0.0.0-20210920133722-c8aef6fb66ef
github.com/itchyny/base58-go v0.1.0
github.com/json-iterator/go v1.1.10
github.com/laizy/bigint v0.1.3
github.com/mattn/go-isatty v0.0.10 // indirect
github.com/ontio/ontology-crypto v1.2.1
github.com/ontio/ontology-eventbus v0.9.1
Expand All @@ -28,14 +27,11 @@ require (
github.com/pborman/uuid v1.2.0
github.com/prometheus/client_golang v0.9.1
github.com/scylladb/go-set v1.0.2
github.com/spaolacci/murmur3 v1.0.1-0.20190317074736-539464a789e9 // indirect
github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4
github.com/stretchr/testify v1.4.0
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca
github.com/urfave/cli v1.22.1
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d
gotest.tools v2.2.0+incompatible
)

replace (
Expand Down
Loading

0 comments on commit 3b35579

Please sign in to comment.