Skip to content

Commit

Permalink
Merge branch 'pre_v2.3.0_union_logic' into pre_v2.3.0_union
Browse files Browse the repository at this point in the history
  • Loading branch information
turingczz committed Nov 24, 2022
2 parents d26e2c7 + 9387be6 commit ae7715e
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 14 deletions.
9 changes: 5 additions & 4 deletions core/commands/bttc/bttc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ var BttcCmd = &cmds.Command{
Tagline: "Interact with bttc related services.",
},
Subcommands: map[string]*cmds.Command{
"btt2wbtt": BttcBtt2WbttCmd,
"wbtt2btt": BttcWbtt2BttCmd,
"send-btt-to": BttcSendBttToCmd,
"send-wbtt-to": BttcSendWbttToCmd,
"btt2wbtt": BttcBtt2WbttCmd,
"wbtt2btt": BttcWbtt2BttCmd,
"send-btt-to": BttcSendBttToCmd,
"send-wbtt-to": BttcSendWbttToCmd,
"send-token-to": BttcSendTokenToCmd,
},
}
66 changes: 66 additions & 0 deletions core/commands/bttc/send_token_to.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package bttc

import (
"context"
"errors"
"fmt"
"github.com/bittorrent/go-btfs/chain/tokencfg"
"io"
"math/big"
"time"

cmds "github.com/bittorrent/go-btfs-cmds"
"github.com/bittorrent/go-btfs/chain"
"github.com/bittorrent/go-btfs/utils"
"github.com/ethereum/go-ethereum/common"
)

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

var BttcSendTokenToCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Transfer your WBTT to other bttc address",
},
Arguments: []cmds.Argument{
cmds.StringArg("addr", true, false, "target bttc address"),
cmds.StringArg("amount", true, false, "amount you want to send"),
},
Options: []cmds.Option{
cmds.StringOption(tokencfg.TokenTypeName, "tk", "file storage with token type,default WBTT, other TRX/USDD/USDT.").WithDefault("WBTT"),
},
RunTimeout: 5 * time.Minute,
Type: &BttcSendTokenToCmdRet{},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) (err error) {
addressStr := req.Arguments[0]
if !common.IsHexAddress(addressStr) {
return fmt.Errorf("invalid bttc address %s", addressStr)
}

tokenStr := req.Options[tokencfg.TokenTypeName].(string)
fmt.Printf("... token:%+v\n", tokenStr)
_, bl := tokencfg.MpTokenAddr[tokenStr]
if !bl {
return errors.New("your input token is none. ")
}

amount, ok := new(big.Int).SetString(utils.RemoveSpaceAndComma(req.Arguments[1]), 10)
if !ok {
return fmt.Errorf("invalid argument amount %s", req.Arguments[1])
}

trx, err := chain.SettleObject.BttcService.SendTokenTo(context.Background(), common.HexToAddress(addressStr), amount, tokenStr)
if err != nil {
return
}

return cmds.EmitOnce(res, &BttcSendTokenToCmdRet{Hash: trx.String()})
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *BttcSendTokenToCmdRet) error {
_, err := fmt.Fprintf(w, "the hash of transaction: %s\n", out.Hash)
return err
}),
},
}
File renamed without changes.
86 changes: 86 additions & 0 deletions core/commands/cheque/balance_muti_tokens.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package cheque

import (
"errors"
"fmt"
"github.com/bittorrent/go-btfs/chain/tokencfg"
"io"
"math/big"
"time"

cmds "github.com/bittorrent/go-btfs-cmds"
"github.com/bittorrent/go-btfs/chain"
"github.com/ethereum/go-ethereum/common"
"golang.org/x/net/context"
)

var ChequeAllTokenBalanceCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Get all token balance by addr.",
},
Arguments: []cmds.Argument{
cmds.StringArg("addr", true, false, "bttc account address"),
},
RunTimeout: 5 * time.Minute,
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
addr := req.Arguments[0]

mp := make(map[string]*big.Int, 0)
for k := range tokencfg.MpTokenAddr {
balance, err := chain.SettleObject.VaultService.TokenBalanceOf(context.Background(), common.HexToAddress(addr), k)
if err != nil {
return err
}

mp[k] = balance
}

return cmds.EmitOnce(res, &mp)
},
Type: &ChequeBttBalanceCmdRet{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *ChequeBttBalanceCmdRet) error {
_, err := fmt.Fprintf(w, "the balance is: %v\n", out.Balance)
return err
}),
},
}

var ChequeTokenBalanceCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Get one token balance by addr.",
},
Arguments: []cmds.Argument{
cmds.StringArg("addr", true, false, "bttc account address"),
},
Options: []cmds.Option{
cmds.StringOption(tokencfg.TokenTypeName, "tk", "file storage with token type,default WBTT, other TRX/USDD/USDT.").WithDefault("WBTT"),
},
RunTimeout: 5 * time.Minute,
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
addr := req.Arguments[0]

tokenStr := req.Options[tokencfg.TokenTypeName].(string)
fmt.Printf("... token:%+v\n", tokenStr)
_, bl := tokencfg.MpTokenAddr[tokenStr]
if !bl {
return errors.New("your input token is none. ")
}

balance, err := chain.SettleObject.VaultService.TokenBalanceOf(context.Background(), common.HexToAddress(addr), tokenStr)
if err != nil {
return err
}

return cmds.EmitOnce(res, &ChequeBttBalanceCmdRet{
Balance: balance,
})
},
Type: &ChequeBttBalanceCmdRet{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *ChequeBttBalanceCmdRet) error {
_, err := fmt.Fprintf(w, "the balance is: %v\n", out.Balance)
return err
}),
},
}
6 changes: 4 additions & 2 deletions core/commands/cheque/cheque.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ Vault services include issue cheque to peer, receive cheque and store operations
"stats": ChequeStatsCmd,
"stats-all": ChequeStatsAllCmd,

"chaininfo": ChequeChainInfoCmd,
"bttbalance": ChequeBttBalanceCmd,
"chaininfo": ChequeChainInfoCmd,
"bttbalance": ChequeBttBalanceCmd,
"token_balance": ChequeTokenBalanceCmd,
"all_token_balance": ChequeAllTokenBalanceCmd,
},
}

Expand Down
20 changes: 20 additions & 0 deletions settlement/swap/bttc/bttc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type Service interface {
SendBttTo(ctx context.Context, to common.Address, amount *big.Int) (trx common.Hash, err error)
// SendWbttTo transfers `amount` of WBTT to the given address `to`
SendWbttTo(ctx context.Context, to common.Address, amount *big.Int) (trx common.Hash, err error)
// SendTokenTo transfers `amount` of WBTT to the given address `to`
SendTokenTo(ctx context.Context, to common.Address, amount *big.Int, tokenStr string) (trx common.Hash, err error)
}

type service struct {
Expand Down Expand Up @@ -95,6 +97,24 @@ func (svc *service) SendWbttTo(ctx context.Context, to common.Address, amount *b
return
}

func (svc *service) SendTokenTo(ctx context.Context, to common.Address, amount *big.Int, tokenStr string) (trx common.Hash, err error) {
myAddr := svc.trxService.OverlayEthAddress(ctx)
err = validateBeforeTransfer(ctx, myAddr, to, amount)
if err != nil {
return zeroHash, err
}
balance, err := svc.mpErc20Service[tokenStr].BalanceOf(ctx, myAddr)
if err != nil {
return
}
fmt.Printf("your %s balance is %d, will transfer %d wbtt to address %s\n", tokenStr, balance, amount, to)
if balance.Cmp(amount) < 0 {
return zeroHash, ErrInsufficientWBTT
}
trx, err = svc.mpErc20Service[tokenStr].Transfer(ctx, to, amount)
return
}

func (svc *service) SendBttTo(ctx context.Context, to common.Address, amount *big.Int) (trx common.Hash, err error) {
myAddr := svc.trxService.OverlayEthAddress(ctx)
err = validateBeforeTransfer(ctx, myAddr, to, amount)
Expand Down
12 changes: 6 additions & 6 deletions settlement/swap/vault/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,20 @@ func Init(
}

// approve to vaultAddress
err = erc20tokenApprove(ctx, erc20Service, overlayEthAddress, vaultAddress)
err = erc20tokenApprove(ctx, "WBTT", erc20Service, overlayEthAddress, vaultAddress)
if err != nil {
return nil, err
}

// muti tokens
for _, erc20Svr := range mpErc20Service {
err = erc20tokenApprove(ctx, erc20Svr, overlayEthAddress, vaultAddress)
for tokenStr, erc20Svr := range mpErc20Service {
err = erc20tokenApprove(ctx, tokenStr, erc20Svr, overlayEthAddress, vaultAddress)
if err != nil {
return nil, err
}
}

vaultService, err = New(transactionService, vaultAddress, overlayEthAddress, stateStore, chequeSigner, erc20Service, chequeStore)
vaultService, err = New(transactionService, vaultAddress, overlayEthAddress, stateStore, chequeSigner, erc20Service, mpErc20Service, chequeStore)
return vaultService, err
}

Expand Down Expand Up @@ -179,7 +179,7 @@ func deployVaultIfNotExist(
return vaultAddress, err
}

func erc20tokenApprove(ctx context.Context, erc20Service erc20.Service, issuer, vault common.Address) error {
func erc20tokenApprove(ctx context.Context, tokenStr string, erc20Service erc20.Service, issuer, vault common.Address) error {
allowance, err := erc20Service.Allowance(ctx, issuer, vault)
if err != nil {
return err
Expand All @@ -192,7 +192,7 @@ func erc20tokenApprove(ctx context.Context, erc20Service erc20.Service, issuer,
if err != nil {
return err
}
log.Infof("approve WBTT to vault [0x%x] at tx [0x%x] \n", vault, hash)
log.Infof("approve %s to vault [0x%x] at tx [0x%x] \n", tokenStr, vault, hash)
}
return nil
}
12 changes: 10 additions & 2 deletions settlement/swap/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ type Service interface {
LastCheques(token common.Address) (map[common.Address]*SignedCheque, error)
// WbttBalanceOf retrieve the addr balance
WBTTBalanceOf(ctx context.Context, addr common.Address) (*big.Int, error)
// TokenBalanceOf retrieve the addr balance
TokenBalanceOf(ctx context.Context, addr common.Address, tokenStr string) (*big.Int, error)
// BTTBalanceOf retrieve the btt balance of addr
BTTBalanceOf(ctx context.Context, address common.Address, block *big.Int) (*big.Int, error)
// TotalPaidOut return total pay out of the vault
Expand All @@ -91,7 +93,8 @@ type service struct {
contract *vaultContractMuti
ownerAddress common.Address

erc20Service erc20.Service
erc20Service erc20.Service
mpErc20Service map[string]erc20.Service

store storage.StateStorer
chequeSigner ChequeSigner
Expand All @@ -102,13 +105,14 @@ type service struct {

// New creates a new vault service for the provided vault contract.
func New(transactionService transaction.Service, address, ownerAddress common.Address, store storage.StateStorer,
chequeSigner ChequeSigner, erc20Service erc20.Service, chequeStore ChequeStore) (Service, error) {
chequeSigner ChequeSigner, erc20Service erc20.Service, mpErc20Service map[string]erc20.Service, chequeStore ChequeStore) (Service, error) {
return &service{
transactionService: transactionService,
address: address,
contract: newVaultContractMuti(address, transactionService),
ownerAddress: ownerAddress,
erc20Service: erc20Service,
mpErc20Service: mpErc20Service,
store: store,
chequeSigner: chequeSigner,
//totalIssuedReserved: big.NewInt(0),
Expand Down Expand Up @@ -561,6 +565,10 @@ func (s *service) WBTTBalanceOf(ctx context.Context, addr common.Address) (*big.
return s.erc20Service.BalanceOf(ctx, addr)
}

func (s *service) TokenBalanceOf(ctx context.Context, addr common.Address, tokenStr string) (*big.Int, error) {
return s.mpErc20Service[tokenStr].BalanceOf(ctx, addr)
}

func (s *service) BTTBalanceOf(ctx context.Context, address common.Address, block *big.Int) (*big.Int, error) {
return s.transactionService.BttBalanceAt(ctx, address, block)
}
Expand Down

0 comments on commit ae7715e

Please sign in to comment.