Skip to content

Commit fabcfc0

Browse files
committed
fix: deposit event ERC721 tokenId topic
1 parent b62c52f commit fabcfc0

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

packages/vm/core/evm/evmimpl/impl.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ func newL1Deposit(ctx isc.Sandbox) dict.Dict {
478478
if !ok {
479479
continue
480480
}
481-
logs = append(logs, makeTransferEvent(erc20Address, addr, nt.Amount))
481+
logs = append(logs, makeTransferEventERC20(erc20Address, addr, nt.Amount))
482482
}
483483
for _, nftID := range assets.NFTs {
484484
// if the NFT belongs to a collection, emit a Transfer event from the corresponding ERC721NFTCollection contract
@@ -488,13 +488,13 @@ func newL1Deposit(ctx isc.Sandbox) dict.Dict {
488488
erc721CollectionContractAddress := iscmagic.ERC721NFTCollectionAddress(collectionID)
489489
stateDB := emulator.NewStateDB(newEmulatorContext(ctx))
490490
if stateDB.Exist(erc721CollectionContractAddress) {
491-
logs = append(logs, makeTransferEvent(erc721CollectionContractAddress, addr, iscmagic.WrapNFTID(nftID).TokenID()))
491+
logs = append(logs, makeTransferEventERC721(erc721CollectionContractAddress, addr, iscmagic.WrapNFTID(nftID).TokenID()))
492492
continue
493493
}
494494
}
495495
}
496496
// otherwise, emit a Transfer event from the ERC721NFTs contract
497-
logs = append(logs, makeTransferEvent(iscmagic.ERC721NFTsAddress, addr, iscmagic.WrapNFTID(nftID).TokenID()))
497+
logs = append(logs, makeTransferEventERC721(iscmagic.ERC721NFTsAddress, addr, iscmagic.WrapNFTID(nftID).TokenID()))
498498
}
499499

500500
receipt := &types.Receipt{
@@ -515,7 +515,7 @@ func newL1Deposit(ctx isc.Sandbox) dict.Dict {
515515

516516
var transferEventTopic = crypto.Keccak256Hash([]byte("Transfer(address,address,uint256)"))
517517

518-
func makeTransferEvent(contractAddress, to common.Address, uint256Data *big.Int) *types.Log {
518+
func makeTransferEventERC20(contractAddress, to common.Address, uint256Data *big.Int) *types.Log {
519519
var addrTopic common.Hash
520520
copy(addrTopic[len(addrTopic)-len(to):], to[:])
521521
return &types.Log{
@@ -528,3 +528,22 @@ func makeTransferEvent(contractAddress, to common.Address, uint256Data *big.Int)
528528
Data: lo.Must((abi.Arguments{{Type: lo.Must(abi.NewType("uint256", "", nil))}}).Pack(uint256Data)),
529529
}
530530
}
531+
532+
func makeTransferEventERC721(contractAddress, to common.Address, uint256Data *big.Int) *types.Log {
533+
var addrToTopic common.Hash
534+
copy(addrToTopic[len(addrToTopic)-len(to):], to[:])
535+
536+
tokenIDPacked := lo.Must((abi.Arguments{{Type: lo.Must(abi.NewType("uint256", "", nil))}}).Pack(uint256Data))
537+
var tokenIDTopic common.Hash
538+
copy(tokenIDTopic[:], tokenIDPacked) // same len
539+
540+
return &types.Log{
541+
Address: contractAddress,
542+
Topics: []common.Hash{
543+
transferEventTopic, // event topic
544+
{}, // indexed `from` address
545+
addrToTopic, // indexed `to` address
546+
tokenIDTopic, // indexed `tokenId`
547+
},
548+
}
549+
}

packages/vm/core/evm/evmtest/evm_test.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ func TestERC721NFTs(t *testing.T) {
925925
tx := blockTxs[0]
926926
receipt := env.evmChain.TransactionReceipt(tx.Hash())
927927
require.Len(t, receipt.Logs, 1)
928-
checkTransferEvent(
928+
checkTransferEventERC721(
929929
t,
930930
receipt.Logs[0],
931931
iscmagic.ERC721NFTsAddress,
@@ -1054,7 +1054,7 @@ func TestERC721NFTCollection(t *testing.T) {
10541054
tx := blockTxs[0]
10551055
receipt := env.evmChain.TransactionReceipt(tx.Hash())
10561056
require.Len(t, receipt.Logs, 1)
1057-
checkTransferEvent(
1057+
checkTransferEventERC721(
10581058
t,
10591059
receipt.Logs[0],
10601060
iscmagic.ERC721NFTCollectionAddress(collection.ID),
@@ -1371,22 +1371,27 @@ func TestERC20BaseTokens(t *testing.T) {
13711371
}
13721372
}
13731373

1374-
func checkTransferEvent(
1374+
func checkTransferEventERC721(
13751375
t *testing.T,
13761376
log *types.Log,
13771377
contractAddress, to common.Address,
13781378
uint256Data *big.Int,
13791379
) {
13801380
require.Equal(t, contractAddress, log.Address)
13811381

1382-
require.Len(t, log.Topics, 3)
1382+
require.Len(t, log.Topics, 4)
13831383
require.Equal(t, crypto.Keccak256Hash([]byte("Transfer(address,address,uint256)")), log.Topics[0])
13841384
var addrTopic common.Hash
13851385
copy(addrTopic[len(addrTopic)-len(to):], to[:])
13861386
require.Equal(t, addrTopic, log.Topics[2])
13871387

1388-
data := lo.Must((abi.Arguments{{Type: lo.Must(abi.NewType("uint256", "", nil))}}).Unpack(log.Data))[0].(*big.Int)
1389-
require.Zero(t, uint256Data.Cmp(data))
1388+
tokenIDTopicBytes := make([]byte, common.HashLength)
1389+
copy(tokenIDTopicBytes, log.Topics[3][:])
1390+
1391+
tokenID := lo.Must((abi.Arguments{{Type: lo.Must(abi.NewType("uint256", "", nil))}}).Unpack(tokenIDTopicBytes))[0].(*big.Int)
1392+
require.Zero(t, uint256Data.Cmp(tokenID))
1393+
1394+
require.Empty(t, log.Data)
13901395
}
13911396

13921397
func TestERC20NativeTokens(t *testing.T) {
@@ -1432,7 +1437,7 @@ func TestERC20NativeTokens(t *testing.T) {
14321437
tx := blockTxs[0]
14331438
receipt := env.evmChain.TransactionReceipt(tx.Hash())
14341439
require.Len(t, receipt.Logs, 1)
1435-
checkTransferEvent(
1440+
checkTransferEventERC20(
14361441
t,
14371442
receipt.Logs[0],
14381443
iscmagic.ERC20NativeTokensAddress(foundrySN),
@@ -1461,6 +1466,24 @@ func TestERC20NativeTokens(t *testing.T) {
14611466
)
14621467
}
14631468

1469+
func checkTransferEventERC20(
1470+
t *testing.T,
1471+
log *types.Log,
1472+
contractAddress, to common.Address,
1473+
uint256Data *big.Int,
1474+
) {
1475+
require.Equal(t, contractAddress, log.Address)
1476+
1477+
require.Len(t, log.Topics, 3)
1478+
require.Equal(t, crypto.Keccak256Hash([]byte("Transfer(address,address,uint256)")), log.Topics[0])
1479+
var addrTopic common.Hash
1480+
copy(addrTopic[len(addrTopic)-len(to):], to[:])
1481+
require.Equal(t, addrTopic, log.Topics[2])
1482+
1483+
data := lo.Must((abi.Arguments{{Type: lo.Must(abi.NewType("uint256", "", nil))}}).Unpack(log.Data))[0].(*big.Int)
1484+
require.Zero(t, uint256Data.Cmp(data))
1485+
}
1486+
14641487
// helper to make sandbox calls via EVM in a more readable way
14651488
func sandboxCall(t *testing.T, wallet *ecdsa.PrivateKey, sandboxContract *IscContractInstance, contract isc.Hname, entrypoint isc.Hname, params dict.Dict, allowance uint64) {
14661489
evmParams := &iscmagic.ISCDict{}

0 commit comments

Comments
 (0)