-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1160 from TRON-US/feature/add-settlement-api
Add Backend api
- Loading branch information
Showing
11 changed files
with
437 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.