Skip to content

Commit

Permalink
test: cashout event
Browse files Browse the repository at this point in the history
  • Loading branch information
turingczz committed Nov 23, 2022
1 parent 89e7b49 commit 120398f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
7 changes: 4 additions & 3 deletions core/commands/cheque/cash_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cheque
import (
"encoding/json"
"fmt"
"github.com/bittorrent/go-btfs/chain/tokencfg"
"io"
"math/big"
"sort"
Expand Down Expand Up @@ -56,8 +55,8 @@ var ChequeCashListCmd = &cmds.Command{
return fmt.Errorf("invalid limit: %d", limit)
}

tokenStr := req.Options[tokencfg.TokenTypeName].(string)
fmt.Printf("... token:%+v\n", tokenStr)
//tokenStr := req.Options[tokencfg.TokenTypeName].(string)
//fmt.Printf("... token:%+v\n", tokenStr)
//token, bl := tokencfg.MpTokenAddr[tokenStr]
//if !bl {
// return errors.New("your input token is none. ")
Expand All @@ -67,6 +66,8 @@ var ChequeCashListCmd = &cmds.Command{
if err != nil {
return err
}
fmt.Println("get CashoutResults, ", results)

sort.Slice(results, func(i, j int) bool {
return results[i].CashTime > results[j].CashTime
})
Expand Down
7 changes: 6 additions & 1 deletion core/commands/cheque/receive_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ var ListReceiveChequeCmd = &cmds.Command{
return errors.New("your input token is none. ")
}

fmt.Println("receive list ... 1")
var listRet ListChequeRet
cheques, err := chain.SettleObject.SwapService.LastReceivedCheques(token)
fmt.Println("receive list ... 2", cheques, err)

if err != nil {
return err
}
Expand All @@ -62,6 +65,7 @@ var ListReceiveChequeCmd = &cmds.Command{
peerIds = peerIds[:limit]
}

fmt.Println("receive list ... 3")
for _, k := range peerIds {
v := cheques[k]
var record cheque
Expand All @@ -72,6 +76,7 @@ var ListReceiveChequeCmd = &cmds.Command{
record.Payout = v.CumulativePayout

cashStatus, err := chain.SettleObject.CashoutService.CashoutStatus(context.Background(), v.Vault, token)
fmt.Println("receive list ... 3.2", cashStatus, err, token)
if err != nil {
return err
}
Expand All @@ -81,7 +86,7 @@ var ListReceiveChequeCmd = &cmds.Command{

listRet.Cheques = append(listRet.Cheques, record)
}

fmt.Println("receive list ... 4")
listRet.Len = len(listRet.Cheques)
return cmds.EmitOnce(res, &listRet)
},
Expand Down
35 changes: 35 additions & 0 deletions settlement/swap/vault/cashout.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type chequeCashedEvent struct {
}

type mutiChequeCashedEvent struct {
Token common.Address
Beneficiary common.Address
Recipient common.Address
Caller common.Address
Expand Down Expand Up @@ -235,10 +236,16 @@ func (s *cashoutService) storeCashResult(ctx context.Context, vault common.Addre
CashTime: time.Now().Unix(),
Status: "fail",
}
fmt.Println("1 put CashoutResultKey ", cashResult)

_, err := s.transactionService.WaitForReceipt(ctx, txHash)
if err != nil {
fmt.Println("2 put CashoutResultKey ", cashResult)

log.Infof("storeCashResult err:%+v", err)
} else {
fmt.Println("3 put CashoutResultKey ", cashResult)

cs, err := s.CashoutStatus(ctx, vault, token)
if err != nil {
log.Infof("CashOutStats:get cashout status err:%+v", err)
Expand All @@ -256,14 +263,21 @@ func (s *cashoutService) storeCashResult(ctx context.Context, vault common.Addre
}
cashResult.Amount = totalPaidOut
totalReceivedCashed := big.NewInt(0)

fmt.Println("3.1 put CashoutResultKey ", token.String())
if err = s.store.Get(tokencfg.AddToken(statestore.TotalReceivedCashedKey, token), &totalReceivedCashed); err == nil || err == storage.ErrNotFound {
fmt.Println("3.2 put CashoutResultKey ", totalReceivedCashed.String())
totalReceivedCashed = totalReceivedCashed.Add(totalReceivedCashed, totalPaidOut)
fmt.Println("3.3 put CashoutResultKey ", totalReceivedCashed.String())
err := s.store.Put(tokencfg.AddToken(statestore.TotalReceivedCashedKey, token), totalReceivedCashed)
fmt.Println("3.4 put CashoutResultKey ", totalReceivedCashed.String())
if err != nil {
log.Infof("CashOutStats:put totalReceivedCashdKey err:%+v", err)
}
}

fmt.Println("3.5 put CashoutResultKey ", totalReceivedCashed.String())

totalDailyReceivedCashed := big.NewInt(0)
if err = s.store.Get(statestore.GetTodayTotalDailyReceivedCashedKey(token), &totalDailyReceivedCashed); err == nil || err == storage.ErrNotFound {
totalDailyReceivedCashed = totalDailyReceivedCashed.Add(totalDailyReceivedCashed, totalPaidOut)
Expand Down Expand Up @@ -296,6 +310,7 @@ func (s *cashoutService) storeCashResult(ctx context.Context, vault common.Addre
}
}
err = s.store.Put(statestore.CashoutResultKey(vault), &cashResult)
fmt.Println("4 put CashoutResultKey ", cashResult)
if err != nil {
log.Infof("CashOutStats:put cashoutResultKey err:%+v", err)
}
Expand All @@ -304,13 +319,18 @@ func (s *cashoutService) storeCashResult(ctx context.Context, vault common.Addre

// CashoutStatus gets the status of the latest cashout transaction for the vault
func (s *cashoutService) CashoutStatus(ctx context.Context, vaultAddress common.Address, token common.Address) (*CashoutStatus, error) {
fmt.Println("...1 CashoutStatus ")

cheque, err := s.chequeStore.LastReceivedCheque(vaultAddress, token)
if err != nil {
return nil, err
}

fmt.Println("...2 CashoutStatus ", cheque, err)

var action cashoutAction
err = s.store.Get(cashoutActionKey(vaultAddress, token), &action)
fmt.Println("...3 CashoutStatus ", err)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
return &CashoutStatus{
Expand All @@ -322,6 +342,7 @@ func (s *cashoutService) CashoutStatus(ctx context.Context, vaultAddress common.
}

_, pending, err := s.backend.TransactionByHash(ctx, action.TxHash)
fmt.Println("...3 CashoutStatus ", pending, err)
if err != nil {
// treat not found as pending
if !errors.Is(err, ethereum.NotFound) {
Expand All @@ -343,11 +364,15 @@ func (s *cashoutService) CashoutStatus(ctx context.Context, vaultAddress common.
}, nil
}

fmt.Println("...4 CashoutStatus ")

receipt, err := s.backend.TransactionReceipt(ctx, action.TxHash)
if err != nil {
return nil, err
}

fmt.Println("...5 CashoutStatus ", receipt)

if receipt.Status == types.ReceiptStatusFailed {
// if a tx failed (should be almost impossible in practice) we no longer have the necessary information to compute uncashed locally
// assume there are no pending transactions and that the on-chain paidOut is the last cashout action
Expand All @@ -367,11 +392,15 @@ func (s *cashoutService) CashoutStatus(ctx context.Context, vaultAddress common.
}, nil
}

fmt.Println("...6 CashoutStatus ")

result, err := s.parseCashChequeBeneficiaryReceiptMuti(vaultAddress, receipt, token)
if err != nil {
return nil, err
}

fmt.Println("...7 CashoutStatus ", result)

return &CashoutStatus{
Last: &LastCashout{
TxHash: action.TxHash,
Expand Down Expand Up @@ -415,16 +444,20 @@ func (s *cashoutService) parseCashChequeBeneficiaryReceipt(vaultAddress common.A

// parseCashChequeBeneficiaryReceiptMuti processes the receipt from a CashChequeBeneficiary transaction
func (s *cashoutService) parseCashChequeBeneficiaryReceiptMuti(vaultAddress common.Address, receipt *types.Receipt, token common.Address) (*CashChequeResult, error) {
fmt.Println("parseCashChequeBeneficiaryReceiptMuti ... 1", vaultAddress, token)
if tokencfg.IsWBTT(token) {
return s.parseCashChequeBeneficiaryReceipt(vaultAddress, receipt)
}

fmt.Println("parseCashChequeBeneficiaryReceiptMuti ... 2", vaultAddress, token)

result := &CashChequeResult{
Bounced: false,
}

var mtCashedEvent mutiChequeCashedEvent
err := transaction.FindSingleEvent(&vaultABINew, receipt, vaultAddress, mutiChequeCashedEventType, &mtCashedEvent)
fmt.Println("parseCashChequeBeneficiaryReceiptMuti ... 3", err, mtCashedEvent)
if err != nil {
return nil, err
}
Expand All @@ -438,12 +471,14 @@ func (s *cashoutService) parseCashChequeBeneficiaryReceiptMuti(vaultAddress comm

//var mtBouncedEvent mutiChequeBouncedEvent
err = transaction.FindSingleEvent(&vaultABINew, receipt, vaultAddress, mutiChequeBouncedEventType, nil)
fmt.Println("parseCashChequeBeneficiaryReceiptMuti ... 4", err)
if err == nil {
result.Bounced = true
} else if !errors.Is(err, transaction.ErrEventNotFound) {
return nil, err
}

fmt.Println("parseCashChequeBeneficiaryReceiptMuti ... 4")
return result, nil
}

Expand Down

0 comments on commit 120398f

Please sign in to comment.