Skip to content

Commit

Permalink
test: muti token vault deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
turingczz committed Nov 23, 2022
1 parent 120398f commit 50e3ff4
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 16 deletions.
13 changes: 12 additions & 1 deletion chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,16 @@ func InitSettlement(
}
erc20Service := erc20.New(chaininfo.Backend, chaininfo.TransactionService, erc20Address)

fmt.Println("...InitSettlement erc20Address ", erc20Address)

// muti tokens
mpErc20Service := make(map[string]erc20.Service)
for k, tokenAddr := range tokencfg.MpTokenAddr {
mpErc20Service[k] = erc20.New(chaininfo.Backend, chaininfo.TransactionService, tokenAddr)
}

// init bttc service
bttcService := bttc.New(chaininfo.TransactionService, erc20Service)
bttcService := bttc.New(chaininfo.TransactionService, erc20Service, mpErc20Service)

//initChequeStoreCashout
chequeStore, cashoutService := initChequeStoreCashout(
Expand Down Expand Up @@ -176,6 +184,7 @@ func InitSettlement(
deployGasPrice,
chequeStore,
erc20Service,
mpErc20Service,
)

if err != nil {
Expand Down Expand Up @@ -257,6 +266,7 @@ func initVaultService(
deployGasPrice string,
chequeStore vault.ChequeStore,
erc20Service erc20.Service,
mpErc20Service map[string]erc20.Service,
) (vault.Service, error) {
chequeSigner := vault.NewChequeSigner(signer, chainID)

Expand All @@ -281,6 +291,7 @@ func initVaultService(
chequeSigner,
chequeStore,
erc20Service,
mpErc20Service,
)
if err != nil {
return nil, fmt.Errorf("vault init: %w", err)
Expand Down
1 change: 1 addition & 0 deletions core/commands/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Vault services include balance, address, withdraw, deposit operations.`,
},
Subcommands: map[string]*cmds.Command{
"balance": VaultBalanceCmd,
"balance_all": VaultBalanceAllCmd,
"address": VaultAddrCmd,
"withdraw": VaultWithdrawCmd,
"deposit": VaultDepositCmd,
Expand Down
41 changes: 41 additions & 0 deletions core/commands/vault/vault_balance_all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package vault

import (
"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"
"golang.org/x/net/context"
)

var VaultBalanceAllCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Get vault balance.",
},
RunTimeout: 5 * time.Minute,
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {

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

mp[k] = balance
}

return cmds.EmitOnce(res, &mp)
},
Type: &VaultBalanceCmdRet{},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *VaultBalanceCmdRet) error {
_, err := fmt.Fprintf(w, "the vault available balance: %v\n", out.Balance)
return err
}),
},
}
12 changes: 7 additions & 5 deletions settlement/swap/bttc/bttc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ type Service interface {
}

type service struct {
trxService transaction.Service
erc20Service erc20.Service
trxService transaction.Service
erc20Service erc20.Service
mpErc20Service map[string]erc20.Service
}

func New(trxSvc transaction.Service, erc20Svc erc20.Service) Service {
func New(trxSvc transaction.Service, erc20Svc erc20.Service, mpErc20Service map[string]erc20.Service) Service {
return &service{
trxService: trxSvc,
erc20Service: erc20Svc,
trxService: trxSvc,
erc20Service: erc20Svc,
mpErc20Service: mpErc20Service,
}
}

Expand Down
2 changes: 1 addition & 1 deletion settlement/swap/vault/cashout.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (s *cashoutService) CashCheque(ctx context.Context, vault, recipient common
return common.Hash{}, err
}

fmt.Println("_CashCheque ", vault, recipient, cheque.CumulativePayout, hexutils.BytesToHex(cheque.Signature))
fmt.Println("_CashCheque ", vault, recipient, cheque.CumulativePayout, hexutils.BytesToHex(cheque.Signature), cheque.Token)

//callData, err := vaultABI.Pack("cashChequeBeneficiary", recipient, cheque.CumulativePayout, cheque.Signature)
//if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions settlement/swap/vault/contract_muti.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ func (c *vaultContractMuti) Deposit(ctx context.Context, amount *big.Int, token
return c.contractWBTT.Deposit(ctx, amount)
}

fmt.Println("1 Deposit ", token, amount)
callData, err := vaultABINew.Pack("multiTokenDeposit", token, amount)
fmt.Println("2 Deposit err", err)
if err != nil {
return common.Hash{}, err
}
Expand All @@ -186,6 +188,7 @@ func (c *vaultContractMuti) Deposit(ctx context.Context, amount *big.Int, token
To: &c.address,
Data: callData,
})
fmt.Println("3 Deposit err", err)
if err != nil {
return hash, err
}
Expand Down
9 changes: 9 additions & 0 deletions settlement/swap/vault/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func Init(
chequeSigner ChequeSigner,
chequeStore ChequeStore,
erc20Service erc20.Service,
mpErc20Service map[string]erc20.Service,
) (vaultService Service, err error) {

// verify that the supplied factory is valid
Expand Down Expand Up @@ -94,6 +95,14 @@ func Init(
return nil, err
}

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

vaultService, err = New(transactionService, vaultAddress, overlayEthAddress, stateStore, chequeSigner, erc20Service, chequeStore)
return vaultService, err
}
Expand Down
19 changes: 10 additions & 9 deletions settlement/swap/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,16 @@ func (s *service) Address() common.Address {

// Deposit starts depositing erc20 token into the vault. This returns once the transactions has been broadcast.
func (s *service) Deposit(ctx context.Context, amount *big.Int, token common.Address) (hash common.Hash, err error) {
balance, err := s.erc20Service.BalanceOf(ctx, s.ownerAddress)
if err != nil {
return common.Hash{}, err
}

// check we can afford this so we don't waste gas
if balance.Cmp(amount) < 0 {
return common.Hash{}, ErrInsufficientFunds
}
//balance, err := s.erc20Service.BalanceOf(ctx, s.ownerAddress)
//if err != nil {
// return common.Hash{}, err
//}
//
//fmt.Println("Deposit ", balance.String(), amount.String())
//// check we can afford this so we don't waste gas
//if balance.Cmp(amount) < 0 {
// return common.Hash{}, ErrInsufficientFunds
//}

return s.contract.Deposit(ctx, amount, token)
}
Expand Down

0 comments on commit 50e3ff4

Please sign in to comment.