Skip to content

Commit

Permalink
feat: add nft of erc1155
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhongtao committed Sep 14, 2023
1 parent 21a5001 commit 811d767
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 28 deletions.
49 changes: 31 additions & 18 deletions collect/service/cmd/chain/ether/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ import (
)

type Service struct {
log *xlog.XLog
chain *config.Chain
nodeId string
transferTopic string
store collect.StoreTaskInterface
txChainClient blockchain.API
blockChainClient blockchain.API
receiptChainClient blockchain.API
log *xlog.XLog
chain *config.Chain
nodeId string
transferTopic string
nftTransferSingleTopic string
nftTransferBatchTopic string
store collect.StoreTaskInterface
txChainClient blockchain.API
blockChainClient blockchain.API
receiptChainClient blockchain.API
}

func (s *Service) GetMultiBlockByNumber(blockNumber string, log *logrus.Entry, flag bool) ([]*collect.BlockInterface, []*collect.TxInterface) {
Expand Down Expand Up @@ -364,7 +366,17 @@ func (s *Service) buildContract(receipt *collect.Receipt) (*collect.Receipt, err
}

//erc1155
//todo save contract of erc-1155 to receipt.log
// save contract of erc-1155 to receipt.log
if len(g.Topics) == 4 && g.Topics[0] == s.nftTransferSingleTopic {
//处理 普通资产和 1155 协议 资产转移
mp := make(map[string]interface{}, 3)
token, _ := s.getToken(int64(s.chain.BlockChainCode), receipt.From, g.Address, "1155")
mp["token"] = token
mp["eip"] = 1155
mp["data"] = g.Data
bs, _ := json.Marshal(mp)
g.Data = string(bs)
}

}

Expand Down Expand Up @@ -480,7 +492,7 @@ func (s *Service) CheckAddress(tx []byte, addrList map[string]int64) bool {
return has
}

func NewService(c *config.Chain, x *xlog.XLog, store collect.StoreTaskInterface, nodeId string, transferTopic string) collect.BlockChainInterface {
func NewService(c *config.Chain, x *xlog.XLog, store collect.StoreTaskInterface, nodeId string, transferTopic, nftTransferSingleTopic string) collect.BlockChainInterface {

var blockClient blockchain.API
if c.BlockTask != nil {
Expand Down Expand Up @@ -526,13 +538,14 @@ func NewService(c *config.Chain, x *xlog.XLog, store collect.StoreTaskInterface,
}

return &Service{
log: x,
chain: c,
store: store,
txChainClient: txClient,
blockChainClient: blockClient,
receiptChainClient: receiptClient,
nodeId: nodeId,
transferTopic: transferTopic,
log: x,
chain: c,
store: store,
txChainClient: txClient,
blockChainClient: blockClient,
receiptChainClient: receiptClient,
nodeId: nodeId,
transferTopic: transferTopic,
nftTransferSingleTopic: nftTransferSingleTopic,
}
}
2 changes: 1 addition & 1 deletion collect/service/cmd/chain/gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func GetBlockchain(blockchain int, c *config.Chain, store collect.StoreTaskInter
x := xlog.NewXLogger().BuildOutType(xlog.FILE).BuildFormatter(xlog.FORMAT_JSON).BuildFile(fmt.Sprintf("%v/chain_info", logConfig.Path), 24*time.Hour)
var srv collect.BlockChainInterface
if blockchain == 200 {
srv = ether.NewService(c, x, store, nodeId, collect.EthTopic)
srv = ether.NewService(c, x, store, nodeId, collect.EthTopic, collect.EthNftTransferSingleTopic)
} else if blockchain == 205 {
srv = tron2.NewService(c, x, store, nodeId, collect.TronTopic)
} else if blockchain == 201 {
Expand Down
7 changes: 4 additions & 3 deletions collect/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package collect
import "time"

const (
TronTopic = "ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
EthTopic = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
PolygonTopic = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
TronTopic = "ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
EthTopic = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
EthNftTransferSingleTopic = "0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62"
PolygonTopic = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
)

/**
Expand Down
22 changes: 21 additions & 1 deletion store/chain/ether/kafka_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func GetTxType(body []byte) (uint64, error) {
return 2, nil
}
}
func ParseTx(body []byte, transferTopic string, blockchain int64) (*store.SubTx, error) {
func ParseTx(body []byte, transferTopic, nftTransferSingleTopic string, blockchain int64) (*store.SubTx, error) {
var r store.SubTx
root := gjson.ParseBytes(body)
r.BlockChain = uint64(blockchain)
Expand Down Expand Up @@ -193,6 +193,26 @@ func ParseTx(body []byte, transferTopic string, blockchain int64) (*store.SubTx,
}
}

if eip == 1155 {
tps := v.Get("topics").Array()
v := r.Get("data").String()
var from, to string
if len(tps) == 4 && tps[0].String() == nftTransferSingleTopic {
//method = tps[0].String()
from = tps[1].String()
to = tps[2].String()
var m store.ContractTx
m.Contract = contract
m.Value = v
m.From, _ = util.Hex2Address(from)
m.To, _ = util.Hex2Address(to)
m.Method = "Transfer"
m.EIP = eip
m.Token = token
contractTx = append(contractTx, &m)
}
}

}
//else {
// 合约数据不完整,直接放弃了
Expand Down
2 changes: 1 addition & 1 deletion store/chain/ether/kafka_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestParseTx(t *testing.T) {
}
`
tx, err := ParseTx([]byte(str), store.EthTopic, 200)
tx, err := ParseTx([]byte(str), store.EthTopic, store.EthTransferSingleTopic, 200)
if err != nil {
t.Error(err)
} else {
Expand Down
2 changes: 1 addition & 1 deletion store/chain/gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func GetTxFromKafka(value []byte, blockChain int64) (*store.Tx, error) {

func ParseTx(blockchain int64, msg *kafka.Message) (*store.SubTx, error) {
if blockchain == 200 {
return ether.ParseTx(msg.Value, store.EthTopic, blockchain)
return ether.ParseTx(msg.Value, store.EthTopic, store.EthTransferSingleTopic, blockchain)
}
if blockchain == 205 {
return tron.ParseTx(msg.Value, store.TronTopic, blockchain)
Expand Down
7 changes: 4 additions & 3 deletions store/type.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package store

const (
TronTopic = "ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
EthTopic = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
PolygonTopic = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
TronTopic = "ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
EthTopic = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
EthTransferSingleTopic = "0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62"
PolygonTopic = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
)

type MonitorAddress struct {
Expand Down

0 comments on commit 811d767

Please sign in to comment.