Skip to content

Commit

Permalink
monitor erc721 transfer event (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
huangzhiran authored Dec 26, 2024
1 parent 0aa7504 commit 9c167ff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func main() {
UpsertScannedBlockNumber: db.UpsertScannedBlockNumber,
UpsertProjectMetadata: db.UpsertApp,
UpsertDevice: db.UpsertDevice,
UpdateDeviceOwner: db.UpdateOwner,
},
&monitor.ContractAddr{
Project: common.HexToAddress(cfg.ProjectContractAddr),
Expand Down
11 changes: 10 additions & 1 deletion db/device.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package db

import (
"math/big"
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"gorm.io/gorm"
"gorm.io/gorm/clause"
Expand All @@ -16,6 +18,7 @@ const (

type Device struct {
ID string `gorm:"primary_key"`
NFTID string `gorm:"uniqueIndex:device_nft_id,not null"`
Name string `gorm:"not null;default:''"`
Owner string `gorm:"not null;default:''"`
Address string `gorm:"not null;default:''"`
Expand Down Expand Up @@ -56,11 +59,17 @@ func (d *DB) Device(id string) (*Device, error) {
func (d *DB) UpsertDevice(t *Device) error {
err := d.db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "id"}},
DoUpdates: clause.AssignmentColumns([]string{"owner", "address", "status", "proposer", "updated_at"}),
DoUpdates: clause.AssignmentColumns([]string{"nftid", "owner", "address", "status", "proposer", "updated_at"}),
}).Create(t).Error
return errors.Wrap(err, "failed to upsert device")
}

func (d *DB) UpdateOwner(nftid *big.Int, owner common.Address) error {
values := map[string]any{"owner": owner.String()}
err := d.db.Model(&Device{}).Where("nftid = ?", nftid.String()).Updates(values).Error
return errors.Wrap(err, "failed to update device owner")
}

func (d *DB) UpdateByID(id string, values map[string]any) error {
err := d.db.Model(&Device{}).Where("id = ?", id).Updates(values).Error
return errors.Wrap(err, "failed to update device")
Expand Down
17 changes: 17 additions & 0 deletions monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/pkg/errors"
"gorm.io/gorm"

"github.com/iotexproject/pebble-server/contract/ioid"
"github.com/iotexproject/pebble-server/contract/project"
Expand All @@ -25,13 +26,15 @@ type (
UpsertScannedBlockNumber func(uint64) error
UpsertProjectMetadata func(projectID uint64, key [32]byte, value []byte) error
UpsertDevice func(t *db.Device) error
UpdateDeviceOwner func(*big.Int, common.Address) error
)

type Handler struct {
ScannedBlockNumber
UpsertScannedBlockNumber
UpsertProjectMetadata
UpsertDevice
UpdateDeviceOwner
}

type ContractAddr struct {
Expand All @@ -54,11 +57,13 @@ type contract struct {
var (
projectAddMetadataTopic = crypto.Keccak256Hash([]byte("AddMetadata(uint256,string,bytes32,bytes)"))
createIoIDTopic = crypto.Keccak256Hash([]byte("CreateIoID(address,uint256,address,string)"))
erc721TransferTopic = crypto.Keccak256Hash([]byte("Transfer(address,address,uint256)"))
)

var allTopic = []common.Hash{
projectAddMetadataTopic,
createIoIDTopic,
erc721TransferTopic,
}

func (c *contract) processLogs(logs []types.Log) error {
Expand Down Expand Up @@ -98,6 +103,7 @@ func (c *contract) processLogs(logs []types.Log) error {

if err := c.h.UpsertDevice(&db.Device{
ID: e.Did,
NFTID: e.Id.String(),
Owner: e.Owner.String(),
Address: address.String(),
Status: db.CONFIRM,
Expand All @@ -106,6 +112,17 @@ func (c *contract) processLogs(logs []types.Log) error {
}); err != nil {
return err
}
case erc721TransferTopic:
e, err := c.ioidInstance.ParseTransfer(l)
if err != nil {
return errors.Wrap(err, "failed to parse erc721 transfer event")
}
if err := c.h.UpdateDeviceOwner(e.TokenId, e.To); err != nil {
if err == gorm.ErrRecordNotFound {
continue
}
return errors.Wrap(err, "failed to update device owner")
}
}
}
return nil
Expand Down

0 comments on commit 9c167ff

Please sign in to comment.