This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
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 #222 from irisnet/develop
R4R:merge master from develop
- Loading branch information
Showing
11 changed files
with
57 additions
and
295 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
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
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 |
---|---|---|
@@ -1,90 +1,43 @@ | ||
package handler | ||
|
||
import ( | ||
"github.com/irisnet/irishub-sync/logger" | ||
"github.com/irisnet/irishub-sync/store" | ||
"github.com/irisnet/irishub-sync/store/document" | ||
"github.com/irisnet/irishub-sync/util/constant" | ||
"github.com/irisnet/irishub-sync/logger" | ||
"github.com/irisnet/irishub-sync/types/msg" | ||
"github.com/irisnet/irishub-sync/util/helper" | ||
"encoding/json" | ||
) | ||
|
||
// TODO: sync only save account address, let app to update balance, delegation, unbondingDelegation info | ||
func SaveOrUpdateAccountBalanceInfo(accounts []string, height, timestamp int64) { | ||
var ( | ||
accountModel document.Account | ||
) | ||
if len(accounts) == 0 { | ||
//when new address not found or the tx is not success,this address will not be collected | ||
func saveNewAccount(tx *document.CommonTx) { | ||
var accountModel document.Account | ||
if document.TxStatusSuccess != tx.Status { | ||
return | ||
} | ||
|
||
for _, v := range accounts { | ||
coins, accountNumber := helper.QueryAccountInfo(v) | ||
coinIris := getCoinIrisFromCoins(coins) | ||
|
||
if err := accountModel.UpsertBalanceInfo(v, coinIris, accountNumber, height, timestamp); err != nil { | ||
logger.Error("update account balance info fail", logger.Int64("height", height), | ||
logger.String("address", v), logger.String("err", err.Error())) | ||
switch tx.Type { | ||
case constant.TxTypeTransfer: | ||
accountModel.Address = tx.To | ||
case constant.TxTypeAddTrustee: | ||
if len(tx.Msgs) > 0 { | ||
msgData := msg.DocTxMsgAddTrustee{} | ||
if err := json.Unmarshal([]byte(helper.ToJson(tx.Msgs[0].Msg)), &msgData); err == nil { | ||
accountModel.Address = msgData.Address | ||
} | ||
} | ||
} | ||
} | ||
|
||
func SaveOrUpdateAccountDelegationInfo(docTx document.CommonTx) { | ||
var ( | ||
delegator string | ||
accountModel document.Account | ||
) | ||
switch docTx.Type { | ||
case constant.TxTypeStakeDelegate, constant.TxTypeStakeBeginUnbonding, constant.TxTypeBeginRedelegate, | ||
constant.TxTypeStakeCreateValidator: | ||
delegator = docTx.From | ||
} | ||
if delegator == "" { | ||
return | ||
} | ||
delegations := helper.GetDelegations(delegator) | ||
delegation := store.Coin{ | ||
Denom: constant.IrisAttoUnit, | ||
Amount: helper.CalculateDelegatorDelegationTokens(delegations), | ||
} | ||
|
||
if err := accountModel.UpsertDelegationInfo(delegator, delegation, docTx.Height, docTx.Time.Unix()); err != nil { | ||
logger.Error("update account delegation info fail", logger.Int64("height", docTx.Height), | ||
logger.String("address", delegator), logger.String("err", err.Error())) | ||
case constant.TxTypeSetWithdrawAddress: | ||
if len(tx.Msgs) > 0 { | ||
msgData := msg.DocTxMsgSetWithdrawAddress{} | ||
if err := json.Unmarshal([]byte(helper.ToJson(tx.Msgs[0].Msg)), &msgData); err == nil { | ||
accountModel.Address = msgData.WithdrawAddr | ||
} | ||
} | ||
} | ||
} | ||
|
||
func SaveOrUpdateAccountUnbondingDelegationInfo(accounts []string, height, timestamp int64) { | ||
var ( | ||
accountModel document.Account | ||
) | ||
|
||
if len(accounts) == 0 { | ||
if accountModel.Address == "" { | ||
return | ||
} | ||
for _, v := range accounts { | ||
unbondingDelegations := helper.GetUnbondingDelegations(v) | ||
unbondingDelegation := store.Coin{ | ||
Denom: constant.IrisAttoUnit, | ||
Amount: helper.CalculateDelegatorUnbondingDelegationTokens(unbondingDelegations), | ||
} | ||
|
||
if err := accountModel.UpsertUnbondingDelegationInfo(v, unbondingDelegation, height, timestamp); err != nil { | ||
logger.Error("update account unbonding delegation info fail", logger.Int64("height", height), | ||
logger.String("address", v), logger.String("err", err.Error())) | ||
} | ||
} | ||
} | ||
|
||
func getCoinIrisFromCoins(coins store.Coins) store.Coin { | ||
if len(coins) > 0 { | ||
for _, v := range coins { | ||
if v.Denom == constant.IrisAttoUnit { | ||
return store.Coin{ | ||
Denom: v.Denom, | ||
Amount: v.Amount, | ||
} | ||
} | ||
} | ||
if err := accountModel.SaveAddress(accountModel.Address); err != nil { | ||
logger.Warn("Save new account address failed", logger.String("err", err.Error())) | ||
} | ||
return store.Coin{} | ||
} |
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
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
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.