Skip to content

Commit

Permalink
fix(eventindexer): erc1155 fix (taikoxyz#17292)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhorsey authored May 21, 2024
1 parent 859f854 commit f5aa4e6
Showing 1 changed file with 61 additions and 34 deletions.
95 changes: 61 additions & 34 deletions packages/eventindexer/indexer/index_nft_transfers.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,56 +156,36 @@ func (i *Indexer) saveERC1155Transfer(ctx context.Context, chainID *big.Int, vLo

slog.Info("erc1155 found")

type transfer struct {
ID *big.Int `abi:"id"`
Amount *big.Int `abi:"value"`
}

var transfers []transfer

erc1155ABI, err := abi.JSON(strings.NewReader(erc1155.ABI))
if err != nil {
return err
}

if vLog.Topics[0].Hex() == transferSingleSignatureHash.Hex() {
var t transfer

err = erc1155ABI.UnpackIntoInterface(&t, "TransferSingle", []byte(vLog.Data))
if err != nil {
return err
slog.Info("erc1155 transfer single")

type TransferSingleEvent struct {
Operator common.Address
From common.Address
To common.Address
Id *big.Int
Value *big.Int
}

transfers = append(transfers, t)
} else if vLog.Topics[0].Hex() == transferBatchSignatureHash.Hex() {
var t []transfer
var t TransferSingleEvent

err = erc1155ABI.UnpackIntoInterface(&t, "TransferBatch", []byte(vLog.Data))
err = erc1155ABI.UnpackIntoInterface(&t, "TransferSingle", []byte(vLog.Data))
if err != nil {
return err
}

transfers = t
}

slog.Info(
"erc1155 transfer found",
"from", from,
"to", to,
"transfers", transfers,
"contractAddress", vLog.Address.Hex(),
)

// increment To address's balance

for _, transfer := range transfers {
increaseOpts := eventindexer.UpdateNFTBalanceOpts{
ChainID: chainID.Int64(),
Address: to,
TokenID: transfer.ID.Int64(),
TokenID: t.Id.Int64(),
ContractAddress: vLog.Address.Hex(),
ContractType: "ERC1155",
Amount: transfer.Amount.Int64(),
Amount: t.Value.Int64(),
}
decreaseOpts := eventindexer.UpdateNFTBalanceOpts{}

Expand All @@ -214,18 +194,65 @@ func (i *Indexer) saveERC1155Transfer(ctx context.Context, chainID *big.Int, vLo
decreaseOpts = eventindexer.UpdateNFTBalanceOpts{
ChainID: chainID.Int64(),
Address: from,
TokenID: transfer.ID.Int64(),
TokenID: t.Id.Int64(),
ContractAddress: vLog.Address.Hex(),
ContractType: "ERC1155",
Amount: transfer.Amount.Int64(),
Amount: t.Value.Int64(),
}
}

_, _, err = i.nftBalanceRepo.IncreaseAndDecreaseBalancesInTx(ctx, increaseOpts, decreaseOpts)
if err != nil {
return err
}
} else if vLog.Topics[0].Hex() == transferBatchSignatureHash.Hex() {
slog.Info("erc1155 transfer batch")

type TransferBatchEvent struct {
Operator common.Address
From common.Address
To common.Address
Ids []*big.Int
Values []*big.Int
}

var t TransferBatchEvent

err = erc1155ABI.UnpackIntoInterface(&t, "TransferBatch", []byte(vLog.Data))
if err != nil {
return err
}

for idx, id := range t.Ids {
increaseOpts := eventindexer.UpdateNFTBalanceOpts{
ChainID: chainID.Int64(),
Address: to,
TokenID: id.Int64(),
ContractAddress: vLog.Address.Hex(),
ContractType: "ERC1155",
Amount: t.Values[idx].Int64(),
}
decreaseOpts := eventindexer.UpdateNFTBalanceOpts{}

if from != ZeroAddress.Hex() {
// decrement From address's balance
decreaseOpts = eventindexer.UpdateNFTBalanceOpts{
ChainID: chainID.Int64(),
Address: from,
TokenID: id.Int64(),
ContractAddress: vLog.Address.Hex(),
ContractType: "ERC1155",
Amount: t.Values[idx].Int64(),
}
}

_, _, err = i.nftBalanceRepo.IncreaseAndDecreaseBalancesInTx(ctx, increaseOpts, decreaseOpts)
if err != nil {
return err
}
}
}
// increment To address's balance

return nil
}

0 comments on commit f5aa4e6

Please sign in to comment.