Skip to content

Commit

Permalink
complete inc and dec asset value
Browse files Browse the repository at this point in the history
  • Loading branch information
Bin0J committed Oct 29, 2018
1 parent 4c6b2e6 commit d30a8df
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 22 deletions.
47 changes: 33 additions & 14 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,10 @@ const (
SendAssetFunc
// TimeLockFunc wacom
TimeLockFunc
// BuyTicket wacom
// BuyTicketFunc wacom
BuyTicketFunc
// AssetValueChangeFunc wacom
AssetValueChangeFunc
)

// FSNCallParam wacom
Expand All @@ -381,10 +383,11 @@ type FSNCallParam struct {

// GenAssetParam wacom
type GenAssetParam struct {
Name string
Symbol string
Decimals uint8
Total *big.Int
Name string
Symbol string
Decimals uint8
Total *big.Int
CanChange bool
}

// SendAssetParam wacom
Expand All @@ -394,6 +397,14 @@ type SendAssetParam struct {
Value *big.Int
}

// AssetValueChangeParam wacom
type AssetValueChangeParam struct {
AssetID Hash
To Address
Value *big.Int
IsInc bool
}

// TimeLockParam wacom
type TimeLockParam struct {
Type TimeLockType
Expand Down Expand Up @@ -424,23 +435,31 @@ func (p *TimeLockParam) ToBytes() ([]byte, error) {
return rlp.EncodeToBytes(p)
}

// ToBytes wacom
func (p *AssetValueChangeParam) ToBytes() ([]byte, error) {
return rlp.EncodeToBytes(p)
}

// ToAsset wacom
func (p *GenAssetParam) ToAsset() Asset {
return Asset{
Name: p.Name,
Symbol: p.Symbol,
Decimals: p.Decimals,
Total: p.Total,
Name: p.Name,
Symbol: p.Symbol,
Decimals: p.Decimals,
Total: p.Total,
CanChange: p.CanChange,
}
}

// Asset wacom
type Asset struct {
ID Hash
Name string
Symbol string
Decimals uint8
Total *big.Int
ID Hash
Owner Address
Name string
Symbol string
Decimals uint8
Total *big.Int
CanChange bool
}

// SystemAsset wacom
Expand Down
30 changes: 30 additions & 0 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ func (st *StateTransition) handleFsnCall() error {
rlp.DecodeBytes(param.Data, &genAssetParam)
asset := genAssetParam.ToAsset()
asset.ID = st.msg.AsTransaction().Hash()
asset.Owner = st.msg.From()
if err := st.state.GenAsset(asset); err != nil {
return err
}
Expand Down Expand Up @@ -361,6 +362,35 @@ func (st *StateTransition) handleFsnCall() error {
return err
}
return nil
case common.AssetValueChangeFunc:
assetValueChangeParam := common.AssetValueChangeParam{}
rlp.DecodeBytes(param.Data, &assetValueChangeParam)
assets := st.state.AllAssets()

asset, ok := assets[assetValueChangeParam.AssetID]
if !ok {
return fmt.Errorf("asset not found")
}

if !asset.CanChange {
return fmt.Errorf("asset can't inc or dec")
}

if asset.Owner != st.msg.From() {
return fmt.Errorf("must be change by onwer")
}

if assetValueChangeParam.IsInc {
st.state.AddBalance(assetValueChangeParam.To, assetValueChangeParam.AssetID, assetValueChangeParam.Value)
asset.Total = asset.Total.Add(asset.Total, assetValueChangeParam.Value)
} else {
if st.state.GetBalance(assetValueChangeParam.AssetID, assetValueChangeParam.To).Cmp(assetValueChangeParam.Value) < 0 {
return fmt.Errorf("not enough asset")
}
st.state.SubBalance(assetValueChangeParam.To, assetValueChangeParam.AssetID, assetValueChangeParam.Value)
asset.Total = asset.Total.Sub(asset.Total, assetValueChangeParam.Value)
}
return st.state.UpdateAsset(asset)
}
return fmt.Errorf("Unsupport")
}
3 changes: 3 additions & 0 deletions core/vm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ type StateDB interface {
GetNotation(common.Address) uint64

GenAsset(common.Asset) error
UpdateAsset(common.Asset) error

AddTicket(common.Ticket) error

AllAssets() map[common.Hash]common.Asset
}

// CallContext provides a basic interface for the EVM calling conventions. The EVM EVM
Expand Down
90 changes: 82 additions & 8 deletions internal/ethapi/api_fsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ type FusionBaseArgs struct {
// GenAssetArgs wacom
type GenAssetArgs struct {
FusionBaseArgs
Name string `json:"name"`
Symbol string `json:"symbol"`
Decimals uint8 `json:"decimals"`
Total *hexutil.Big `json:"total"`
Name string `json:"name"`
Symbol string `json:"symbol"`
Decimals uint8 `json:"decimals"`
Total *hexutil.Big `json:"total"`
CanChange bool `json:"canChange"`
}

// SendAssetArgs wacom
Expand All @@ -44,6 +45,12 @@ type TimeLockArgs struct {
EndTime *hexutil.Uint64 `json:"end"`
}

// AssetValueChangeArgs wacom
type AssetValueChangeArgs struct {
SendAssetArgs
IsInc bool `json:"isInc"`
}

func (args *FusionBaseArgs) toSendArgs() SendTxArgs {
return SendTxArgs{
From: args.From,
Expand Down Expand Up @@ -76,10 +83,21 @@ func (args *TimeLockArgs) toData(typ common.TimeLockType) ([]byte, error) {

func (args *GenAssetArgs) toData() ([]byte, error) {
param := common.GenAssetParam{
Name: args.Name,
Symbol: args.Symbol,
Decimals: args.Decimals,
Total: args.Total.ToInt(),
Name: args.Name,
Symbol: args.Symbol,
Decimals: args.Decimals,
Total: args.Total.ToInt(),
CanChange: args.CanChange,
}
return param.ToBytes()
}

func (args *AssetValueChangeArgs) toData() ([]byte, error) {
param := common.AssetValueChangeParam{
AssetID: args.AssetID,
To: args.To,
Value: args.Value.ToInt(),
IsInc: args.IsInc,
}
return param.ToBytes()
}
Expand Down Expand Up @@ -427,6 +445,62 @@ func (s *PrivateFusionAPI) BuyTicket(ctx context.Context, args FusionBaseArgs, p
return s.papi.SendTransaction(ctx, sendArgs, passwd)
}

// IncAsset ss
func (s *PrivateFusionAPI) IncAsset(ctx context.Context, args AssetValueChangeArgs, passwd string) (common.Hash, error) {
args.IsInc = true
return s.checkAssetValueChange(ctx, args, passwd)
}

// DecAsset ss
func (s *PrivateFusionAPI) DecAsset(ctx context.Context, args AssetValueChangeArgs, passwd string) (common.Hash, error) {
args.IsInc = false
return s.checkAssetValueChange(ctx, args, passwd)
}

func (s *PrivateFusionAPI) checkAssetValueChange(ctx context.Context, args AssetValueChangeArgs, passwd string) (common.Hash, error) {
state, _, err := s.b.StateAndHeaderByNumber(ctx, rpc.LatestBlockNumber)
if state == nil || err != nil {
return common.Hash{}, err
}

assets := state.AllAssets()

asset, ok := assets[args.AssetID]

if !ok {
return common.Hash{}, fmt.Errorf("asset not found")
}

if !asset.CanChange {
return common.Hash{}, fmt.Errorf("asset can't inc or dec")
}

if asset.Owner != args.From {
return common.Hash{}, fmt.Errorf("must be change by onwer")
}

if !args.IsInc {
if state.GetBalance(args.AssetID, args.To).Cmp(args.Value.ToInt()) < 0 {
return common.Hash{}, fmt.Errorf("not enough asset")
}
}

funcData, err := args.toData()
if err != nil {
return common.Hash{}, err
}
var param = common.FSNCallParam{Func: common.AssetValueChangeFunc, Data: funcData}
data, err := param.ToBytes()
if err != nil {
return common.Hash{}, err
}
var argsData = hexutil.Bytes(data)
sendArgs := args.toSendArgs()
sendArgs.To = &common.FSNCallAddress
sendArgs.Data = &argsData
return s.papi.SendTransaction(ctx, sendArgs, passwd)
}

func calcNotationDisplay(notation uint64) uint64 {
if notation == 0 {
return notation
Expand Down
18 changes: 18 additions & 0 deletions internal/web3ext/web3ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,24 @@ web3._extend({
null
]
}),
new web3._extend.Method({
name: 'incAsset',
call: 'fsn_incAsset',
params: 2,
inputFormatter: [
web3._extend.formatters.inputTransactionFormatter,
null
]
}),
new web3._extend.Method({
name: 'decAsset',
call: 'fsn_decAsset',
params: 2,
inputFormatter: [
web3._extend.formatters.inputTransactionFormatter,
null
]
}),
],
properties:[
new web3._extend.Property({
Expand Down

0 comments on commit d30a8df

Please sign in to comment.