Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

monitor erc721 transfer event #58

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading