Skip to content

Commit

Permalink
(unfinished) Emerald: track tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
pro-wh committed Nov 12, 2022
1 parent d231428 commit a98bcb2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
36 changes: 33 additions & 3 deletions analyzer/emerald/incoming.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/hex"
"fmt"
"math/big"

"github.com/oasisprotocol/oasis-core/go/common/cbor"
"github.com/oasisprotocol/oasis-core/go/common/crypto/address"
Expand Down Expand Up @@ -52,6 +53,11 @@ type BlockData struct {
Size int
TransactionData []*BlockTransactionData
AddressPreimages map[string]*AddressPreimageData
// TokenChanges[contractAddr][accountAddr] = +/- change
// todo: initialze
// todo: write into database
// todo: see if flat map would be okay
TokenChanges map[string]map[string]*big.Int
}

// Function naming conventions in this file:
Expand Down Expand Up @@ -171,6 +177,20 @@ func registerRelatedEthAddress(addressPreimages map[string]*AddressPreimageData,
return addr, nil
}

func findTokenChange(tokenChanges map[string]map[string]*big.Int, tokenAddr string, accountAddr string) *big.Int {
changesByAccount, ok := tokenChanges[tokenAddr]
if !ok {
changesByAccount = map[string]*big.Int{}
tokenChanges[tokenAddr] = changesByAccount
}
change, ok := changesByAccount[accountAddr]
if !ok {
change = &big.Int{}
changesByAccount[accountAddr] = change
}
return change
}

//nolint:gocyclo
func extractRound(b *block.Block, txrs []*sdkClient.TransactionWithResults, logger *log.Logger) (*BlockData, error) {
var blockData BlockData
Expand Down Expand Up @@ -301,19 +321,29 @@ func extractRound(b *block.Block, txrs []*sdkClient.TransactionWithResults, logg
return nil
},
Evm: func(event *evm.Event) error {
if err1 := common.VisitEvmEvent(event, &common.EvmEventHandler{
eventAddr, err1 := registerRelatedEthAddress(blockData.AddressPreimages, blockTransactionData.RelatedAccountAddresses, event.Address)
if err1 != nil {
return fmt.Errorf("event address: %w", err1)
}
if err1 = common.VisitEvmEvent(event, &common.EvmEventHandler{
Erc20Transfer: func(fromEthAddr []byte, toEthAddr []byte, amountU256 []byte) error {
amount := &big.Int{}
amount.SetBytes(amountU256)
if !bytes.Equal(fromEthAddr, common.ZeroEthAddr) {
_, err1 := registerRelatedEthAddress(blockData.AddressPreimages, blockTransactionData.RelatedAccountAddresses, fromEthAddr)
fromAddr, err1 := registerRelatedEthAddress(blockData.AddressPreimages, blockTransactionData.RelatedAccountAddresses, fromEthAddr)
if err1 != nil {
return fmt.Errorf("from: %w", err1)
}
change := findTokenChange(blockData.TokenChanges, eventAddr, fromAddr)
change.Sub(change, amount)
}
if !bytes.Equal(toEthAddr, common.ZeroEthAddr) {
_, err1 := registerRelatedEthAddress(blockData.AddressPreimages, blockTransactionData.RelatedAccountAddresses, toEthAddr)
toAddr, err1 := registerRelatedEthAddress(blockData.AddressPreimages, blockTransactionData.RelatedAccountAddresses, toEthAddr)
if err1 != nil {
return fmt.Errorf("to: %w", err1)
}
change := findTokenChange(blockData.TokenChanges, eventAddr, toAddr)
change.Add(change, amount)
}
return nil
},
Expand Down
8 changes: 8 additions & 0 deletions storage/migrations/02_oasis_3_emerald.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ CREATE TABLE oasis_3.address_preimages
address_data BYTEA NOT NULL
);

CREATE TABLE oasis_3.emerald_token_balances
(
token_address TEXT NOT NULL,
account_address TEXT NOT NULL,
balance BIGINT NOT NULL,
PRIMARY KEY (token_address, account_address)
);

-- Core Module Data
CREATE TABLE oasis_3.emerald_gas_used
(
Expand Down

0 comments on commit a98bcb2

Please sign in to comment.