Skip to content

Commit

Permalink
Merge pull request #1160 from TRON-US/feature/add-settlement-api
Browse files Browse the repository at this point in the history
Add Backend  api
  • Loading branch information
sunbenxin committed Nov 1, 2021
2 parents 55c51e9 + 2b115f7 commit 5497189
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 45 deletions.
44 changes: 44 additions & 0 deletions bigint/bigint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2021 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package bigint

import (
"encoding/json"
"fmt"
"math/big"
)

type BigInt struct {
*big.Int
}

func (i *BigInt) MarshalJSON() ([]byte, error) {
if i.Int == nil {
return []byte("null"), nil
}

return []byte(fmt.Sprintf(`"%s"`, i.String())), nil
}

func (i *BigInt) UnmarshalJSON(b []byte) error {
var val string
err := json.Unmarshal(b, &val)
if err != nil {
return err
}

if i.Int == nil {
i.Int = new(big.Int)
}

i.SetString(val, 10)

return nil
}

//Wrap wraps big.Int pointer into BigInt struct.
func Wrap(i *big.Int) *BigInt {
return &BigInt{Int: i}
}
24 changes: 24 additions & 0 deletions core/commands/cheque/cheque.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ import (
cmds "github.com/TRON-US/go-btfs-cmds"
"github.com/TRON-US/go-btfs/chain"
"github.com/TRON-US/go-btfs/settlement/swap/chequebook"
"golang.org/x/net/context"
)

type StorePriceRet struct {
Price string `json:"price"`
}

type CashChequeRet struct {
TxHash string
}
Expand Down Expand Up @@ -42,10 +47,29 @@ Chequebook services include issue cheque to peer, receive cheque and store opera
"cash": CashChequeCmd,
"list": ListChequeCmd,
"history": ChequeHistoryCmd,
"price": StorePriceCmd,
//"info": ChequeInfo,
},
}

var StorePriceCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Get btfs store price.",
},
RunTimeout: 5 * time.Minute,
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
price, err := chain.SettleObject.OracleService.GetPrice(context.Background())
if err != nil {
return err
}

return cmds.EmitOnce(res, &StorePriceRet{
Price: price.String(),
})
},
Type: StorePriceRet{},
}

var ChequeHistoryCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Display the cheque records.",
Expand Down
19 changes: 19 additions & 0 deletions core/commands/chequebook/chequebook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package chequebook

import (
cmds "github.com/TRON-US/go-btfs-cmds"
)

var ChequeBookCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Interact with chequebook services on BTFS.",
ShortDescription: `
Chequebook services include balance, address, withdraw, deposit operations.`,
},
Subcommands: map[string]*cmds.Command{
"balance": ChequeBookBalanceCmd,
"address": ChequeBookAddrCmd,
"withdraw": ChequeBookWithdrawCmd,
"deposit": ChequeBookDepositCmd,
},
}
27 changes: 27 additions & 0 deletions core/commands/chequebook/chequebook_address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package chequebook

import (
"time"

cmds "github.com/TRON-US/go-btfs-cmds"
"github.com/TRON-US/go-btfs/chain"
)

type ChequeBookAddrCmdRet struct {
Addr string `json:"addr"`
}

var ChequeBookAddrCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Get chequebook address.",
},
RunTimeout: 5 * time.Minute,
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
addr := chain.SettleObject.ChequebookService.Address()

return cmds.EmitOnce(res, &ChequeBookAddrCmdRet{
Addr: addr.String(),
})
},
Type: &ChequeBookAddrCmdRet{},
}
31 changes: 31 additions & 0 deletions core/commands/chequebook/chequebook_balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package chequebook

import (
"time"

cmds "github.com/TRON-US/go-btfs-cmds"
"github.com/TRON-US/go-btfs/chain"
"golang.org/x/net/context"
)

type ChequeBookBalanceCmdRet struct {
Balance string `json:"balance"`
}

var ChequeBookBalanceCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Get chequebook balance.",
},
RunTimeout: 5 * time.Minute,
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
balance, err := chain.SettleObject.ChequebookService.AvailableBalance(context.Background())
if err != nil {
return err
}

return cmds.EmitOnce(res, &ChequeBookBalanceCmdRet{
Balance: balance.String(),
})
},
Type: &ChequeBookBalanceCmdRet{},
}
40 changes: 40 additions & 0 deletions core/commands/chequebook/chequebook_deposit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package chequebook

import (
"fmt"
"math/big"
"time"

cmds "github.com/TRON-US/go-btfs-cmds"
"github.com/TRON-US/go-btfs/chain"
"golang.org/x/net/context"
)

type ChequeBookDepositCmdRet struct {
Hash string `json:"hash"`
}

var ChequeBookDepositCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Deposit from beneficiary to chequebook contract account.",
},
Arguments: []cmds.Argument{
cmds.StringArg("amount", true, false, "deposit amount."),
},
RunTimeout: 5 * time.Minute,
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
amount, ok := new(big.Int).SetString(req.Arguments[0], 10)
if !ok {
return fmt.Errorf("amount:%s cannot be parsed", req.Arguments[0])
}
hash, err := chain.SettleObject.ChequebookService.Deposit(context.Background(), amount)
if err != nil {
return err
}

return cmds.EmitOnce(res, &ChequeBookDepositCmdRet{
Hash: hash.String(),
})
},
Type: &ChequeBookDepositCmdRet{},
}
40 changes: 40 additions & 0 deletions core/commands/chequebook/chequebook_withdraw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package chequebook

import (
"fmt"
"math/big"
"time"

cmds "github.com/TRON-US/go-btfs-cmds"
"github.com/TRON-US/go-btfs/chain"
"golang.org/x/net/context"
)

type ChequeBookWithdrawCmdRet struct {
Hash string `json:"hash"`
}

var ChequeBookWithdrawCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Withdraw from chequebook contract account to beneficiary.",
},
Arguments: []cmds.Argument{
cmds.StringArg("amount", true, false, "withdraw amount."),
},
RunTimeout: 5 * time.Minute,
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
amount, ok := new(big.Int).SetString(req.Arguments[0], 10)
if !ok {
return fmt.Errorf("amount:%s cannot be parsed", req.Arguments[0])
}
hash, err := chain.SettleObject.ChequebookService.Withdraw(context.Background(), amount)
if err != nil {
return err
}

return cmds.EmitOnce(res, &ChequeBookWithdrawCmdRet{
Hash: hash.String(),
})
},
Type: &ChequeBookWithdrawCmdRet{},
}
94 changes: 49 additions & 45 deletions core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"errors"

"github.com/TRON-US/go-btfs/core/commands/cheque"
"github.com/TRON-US/go-btfs/core/commands/chequebook"
cmdenv "github.com/TRON-US/go-btfs/core/commands/cmdenv"
dag "github.com/TRON-US/go-btfs/core/commands/dag"
name "github.com/TRON-US/go-btfs/core/commands/name"
ocmd "github.com/TRON-US/go-btfs/core/commands/object"
settlement "github.com/TRON-US/go-btfs/core/commands/settlements"
"github.com/TRON-US/go-btfs/core/commands/storage"
"github.com/TRON-US/go-btfs/core/commands/storage/challenge"
"github.com/TRON-US/go-btfs/core/commands/storage/upload/upload"
Expand Down Expand Up @@ -120,51 +122,53 @@ The CLI will exit with one of the following values:
var CommandsDaemonCmd = CommandsCmd(Root)

var rootSubcommands = map[string]*cmds.Command{
"add": AddCmd,
"bitswap": BitswapCmd,
"block": BlockCmd,
"cat": CatCmd,
"commands": CommandsDaemonCmd,
"files": FilesCmd,
"filestore": FileStoreCmd,
"get": GetCmd,
"pubsub": PubsubCmd,
"repo": RepoCmd,
"stats": StatsCmd,
"bootstrap": BootstrapCmd,
"test": TestCmd,
"config": ConfigCmd,
"dag": dag.DagCmd,
"dht": DhtCmd,
"diag": DiagCmd,
"dns": DNSCmd,
"id": IDCmd,
"key": KeyCmd,
"log": LogCmd,
"ls": LsCmd,
"mount": MountCmd,
"name": name.NameCmd,
"object": ocmd.ObjectCmd,
"pin": PinCmd,
"ping": PingCmd,
"p2p": P2PCmd,
"refs": RefsCmd,
"resolve": ResolveCmd,
"swarm": SwarmCmd,
"tar": TarCmd,
"file": unixfs.UnixFSCmd,
"urlstore": urlStoreCmd,
"version": VersionCmd,
"shutdown": daemonShutdownCmd,
"restart": restartCmd,
"cid": CidCmd,
"rm": RmCmd,
"storage": storage.StorageCmd,
"metadata": MetadataCmd,
"guard": GuardCmd,
"wallet": WalletCmd,
"tron": TronCmd,
"cheque": cheque.ChequeCmd,
"add": AddCmd,
"bitswap": BitswapCmd,
"block": BlockCmd,
"cat": CatCmd,
"commands": CommandsDaemonCmd,
"files": FilesCmd,
"filestore": FileStoreCmd,
"get": GetCmd,
"pubsub": PubsubCmd,
"repo": RepoCmd,
"stats": StatsCmd,
"bootstrap": BootstrapCmd,
"test": TestCmd,
"config": ConfigCmd,
"dag": dag.DagCmd,
"dht": DhtCmd,
"diag": DiagCmd,
"dns": DNSCmd,
"id": IDCmd,
"key": KeyCmd,
"log": LogCmd,
"ls": LsCmd,
"mount": MountCmd,
"name": name.NameCmd,
"object": ocmd.ObjectCmd,
"pin": PinCmd,
"ping": PingCmd,
"p2p": P2PCmd,
"refs": RefsCmd,
"resolve": ResolveCmd,
"swarm": SwarmCmd,
"tar": TarCmd,
"file": unixfs.UnixFSCmd,
"urlstore": urlStoreCmd,
"version": VersionCmd,
"shutdown": daemonShutdownCmd,
"restart": restartCmd,
"cid": CidCmd,
"rm": RmCmd,
"storage": storage.StorageCmd,
"metadata": MetadataCmd,
"guard": GuardCmd,
"wallet": WalletCmd,
"tron": TronCmd,
"cheque": cheque.ChequeCmd,
"chequebook": chequebook.ChequeBookCmd,
"settlement": settlement.SettlementCmd,
//"update": ExternalBinary(),
}

Expand Down
Loading

0 comments on commit 5497189

Please sign in to comment.