Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed Jul 3, 2024
1 parent 552903c commit 2431dae
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 108 deletions.
192 changes: 97 additions & 95 deletions app/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,101 +56,10 @@ func (app *App) AddCosmosEventsToEVMReceiptIfApplicable(ctx sdk.Context, tx sdk.
// check if there is a ERC721 pointer to contract Addr
pointerAddr, _, exists = app.EvmKeeper.GetERC721CW721Pointer(ctx, contractAddr)
if exists {
action, found := GetAttributeValue(wasmEvent, "action")
if !found {
continue
}
var topics []common.Hash
switch action {
case "transfer_nft", "send_nft", "burn":
topics = []common.Hash{
ERC721TransferTopic,
app.GetEvmAddressAttribute(ctx, wasmEvent, "sender"),
app.GetEvmAddressAttribute(ctx, wasmEvent, "recipient"),
}
tokenID := GetTokenIDAttribute(wasmEvent)
if tokenID == nil {
continue
}
logs = append(logs, &ethtypes.Log{
Address: pointerAddr,
Index: uint(len(logs)),
Topics: topics,
Data: common.BigToHash(tokenID).Bytes(),
})
continue
case "mint":
topics = []common.Hash{
ERC721TransferTopic,
EmptyHash,
app.GetEvmAddressAttribute(ctx, wasmEvent, "owner"),
}
tokenID := GetTokenIDAttribute(wasmEvent)
if tokenID == nil {
continue
}
logs = append(logs, &ethtypes.Log{
Address: pointerAddr,
Index: uint(len(logs)),
Topics: topics,
Data: common.BigToHash(tokenID).Bytes(),
})
case "approve":
topics = []common.Hash{
ERC721ApprovalTopic,
app.GetEvmAddressAttribute(ctx, wasmEvent, "sender"),
app.GetEvmAddressAttribute(ctx, wasmEvent, "spender"),
}
tokenID := GetTokenIDAttribute(wasmEvent)
if tokenID == nil {
continue
}
logs = append(logs, &ethtypes.Log{
Address: pointerAddr,
Index: uint(len(logs)),
Topics: topics,
Data: common.BigToHash(tokenID).Bytes(),
})
case "revoke":
topics = []common.Hash{
ERC721ApprovalTopic,
app.GetEvmAddressAttribute(ctx, wasmEvent, "sender"),
EmptyHash,
}
tokenID := GetTokenIDAttribute(wasmEvent)
if tokenID == nil {
continue
}
logs = append(logs, &ethtypes.Log{
Address: pointerAddr,
Index: uint(len(logs)),
Topics: topics,
Data: common.BigToHash(tokenID).Bytes(),
})
case "approve_all":
topics = []common.Hash{
ERC721ApproveAllTopic,
app.GetEvmAddressAttribute(ctx, wasmEvent, "sender"),
app.GetEvmAddressAttribute(ctx, wasmEvent, "operator"),
}
logs = append(logs, &ethtypes.Log{
Address: pointerAddr,
Index: uint(len(logs)),
Topics: topics,
Data: TrueHash.Bytes(),
})
case "revoke_all":
topics = []common.Hash{
ERC721ApproveAllTopic,
app.GetEvmAddressAttribute(ctx, wasmEvent, "sender"),
app.GetEvmAddressAttribute(ctx, wasmEvent, "operator"),
}
logs = append(logs, &ethtypes.Log{
Address: pointerAddr,
Index: uint(len(logs)),
Topics: topics,
Data: EmptyHash.Bytes(),
})
log, eligible := app.translateCW721Event(ctx, wasmEvent, pointerAddr, contractAddr)
if eligible {
log.Index = uint(len(logs))
logs = append(logs, log)
}
continue
}
Expand Down Expand Up @@ -251,6 +160,99 @@ func (app *App) translateCW20Event(ctx sdk.Context, wasmEvent abci.Event, pointe
return nil, false
}

func (app *App) translateCW721Event(ctx sdk.Context, wasmEvent abci.Event, pointerAddr common.Address, contractAddr string) (*ethtypes.Log, bool) {
action, found := GetAttributeValue(wasmEvent, "action")
if !found {
return nil, false
}

Check warning on line 167 in app/receipt.go

View check run for this annotation

Codecov / codecov/patch

app/receipt.go#L166-L167

Added lines #L166 - L167 were not covered by tests
var topics []common.Hash
switch action {
case "transfer_nft", "send_nft", "burn":
topics = []common.Hash{
ERC721TransferTopic,
app.GetEvmAddressAttribute(ctx, wasmEvent, "sender"),
app.GetEvmAddressAttribute(ctx, wasmEvent, "recipient"),
}
tokenID := GetTokenIDAttribute(wasmEvent)
if tokenID == nil {
return nil, false
}

Check warning on line 179 in app/receipt.go

View check run for this annotation

Codecov / codecov/patch

app/receipt.go#L178-L179

Added lines #L178 - L179 were not covered by tests
return &ethtypes.Log{
Address: pointerAddr,
Topics: topics,
Data: common.BigToHash(tokenID).Bytes(),
}, true
case "mint":
topics = []common.Hash{
ERC721TransferTopic,
EmptyHash,
app.GetEvmAddressAttribute(ctx, wasmEvent, "owner"),
}
tokenID := GetTokenIDAttribute(wasmEvent)
if tokenID == nil {
return nil, false
}

Check warning on line 194 in app/receipt.go

View check run for this annotation

Codecov / codecov/patch

app/receipt.go#L193-L194

Added lines #L193 - L194 were not covered by tests
return &ethtypes.Log{
Address: pointerAddr,
Topics: topics,
Data: common.BigToHash(tokenID).Bytes(),
}, true
case "approve":
topics = []common.Hash{
ERC721ApprovalTopic,
app.GetEvmAddressAttribute(ctx, wasmEvent, "sender"),
app.GetEvmAddressAttribute(ctx, wasmEvent, "spender"),
}
tokenID := GetTokenIDAttribute(wasmEvent)
if tokenID == nil {
return nil, false
}

Check warning on line 209 in app/receipt.go

View check run for this annotation

Codecov / codecov/patch

app/receipt.go#L208-L209

Added lines #L208 - L209 were not covered by tests
return &ethtypes.Log{
Address: pointerAddr,
Topics: topics,
Data: common.BigToHash(tokenID).Bytes(),
}, true
case "revoke":
topics = []common.Hash{
ERC721ApprovalTopic,
app.GetEvmAddressAttribute(ctx, wasmEvent, "sender"),
EmptyHash,
}
tokenID := GetTokenIDAttribute(wasmEvent)
if tokenID == nil {
return nil, false
}

Check warning on line 224 in app/receipt.go

View check run for this annotation

Codecov / codecov/patch

app/receipt.go#L223-L224

Added lines #L223 - L224 were not covered by tests
return &ethtypes.Log{
Address: pointerAddr,
Topics: topics,
Data: common.BigToHash(tokenID).Bytes(),
}, true
case "approve_all":
topics = []common.Hash{
ERC721ApproveAllTopic,
app.GetEvmAddressAttribute(ctx, wasmEvent, "sender"),
app.GetEvmAddressAttribute(ctx, wasmEvent, "operator"),
}
return &ethtypes.Log{
Address: pointerAddr,
Topics: topics,
Data: TrueHash.Bytes(),
}, true
case "revoke_all":
topics = []common.Hash{
ERC721ApproveAllTopic,
app.GetEvmAddressAttribute(ctx, wasmEvent, "sender"),
app.GetEvmAddressAttribute(ctx, wasmEvent, "operator"),
}
return &ethtypes.Log{
Address: pointerAddr,
Topics: topics,
Data: EmptyHash.Bytes(),
}, true
}
return nil, false

Check warning on line 253 in app/receipt.go

View check run for this annotation

Codecov / codecov/patch

app/receipt.go#L253

Added line #L253 was not covered by tests
}

func (app *App) GetEvmAddressAttribute(ctx sdk.Context, event abci.Event, attribute string) common.Hash {
addrStr, found := GetAttributeValue(event, attribute)
if found {
Expand Down
21 changes: 14 additions & 7 deletions app/receipt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ func TestEvmEventsForCw721(t *testing.T) {
require.Equal(t, 1, len(receipt.Logs))
require.NotEmpty(t, receipt.LogsBloom)
require.Equal(t, mockPointerAddr.Hex(), receipt.Logs[0].Address)
require.NotNil(t, testkeeper.EVMTestApp.EvmKeeper.GetEVMTxDeferredInfo(ctx))
_, found := testkeeper.EVMTestApp.EvmKeeper.GetEVMTxDeferredInfo(ctx)
require.True(t, found)

// calling from wasmd precompile
abi, err := wasmd.GetABI()
Expand Down Expand Up @@ -228,7 +229,8 @@ func TestEvmEventsForCw721(t *testing.T) {
require.Equal(t, 1, len(receipt.Logs))
require.NotEmpty(t, receipt.LogsBloom)
require.Equal(t, mockPointerAddr.Hex(), receipt.Logs[0].Address)
require.NotNil(t, testkeeper.EVMTestApp.EvmKeeper.GetEVMTxDeferredInfo(ctx))
_, found = testkeeper.EVMTestApp.EvmKeeper.GetEVMTxDeferredInfo(ctx)
require.True(t, found)

// test approval message
payload = []byte(fmt.Sprintf("{\"approve\":{\"spender\":\"%s\",\"token_id\":\"2\"}}", recipient.String()))
Expand All @@ -252,7 +254,8 @@ func TestEvmEventsForCw721(t *testing.T) {
require.Equal(t, 1, len(receipt.Logs))
require.NotEmpty(t, receipt.LogsBloom)
require.Equal(t, mockPointerAddr.Hex(), receipt.Logs[0].Address)
require.NotNil(t, testkeeper.EVMTestApp.EvmKeeper.GetEVMTxDeferredInfo(ctx))
_, found = testkeeper.EVMTestApp.EvmKeeper.GetEVMTxDeferredInfo(ctx)
require.True(t, found)
require.Equal(t, common.HexToHash("0x2").Bytes(), receipt.Logs[0].Data)

// revoke
Expand All @@ -277,7 +280,8 @@ func TestEvmEventsForCw721(t *testing.T) {
require.Equal(t, 1, len(receipt.Logs))
require.NotEmpty(t, receipt.LogsBloom)
require.Equal(t, mockPointerAddr.Hex(), receipt.Logs[0].Address)
require.NotNil(t, testkeeper.EVMTestApp.EvmKeeper.GetEVMTxDeferredInfo(ctx))
_, found = testkeeper.EVMTestApp.EvmKeeper.GetEVMTxDeferredInfo(ctx)
require.True(t, found)
require.Equal(t, common.HexToHash("0x2").Bytes(), receipt.Logs[0].Data)

// approve all
Expand All @@ -302,7 +306,8 @@ func TestEvmEventsForCw721(t *testing.T) {
require.Equal(t, 1, len(receipt.Logs))
require.NotEmpty(t, receipt.LogsBloom)
require.Equal(t, mockPointerAddr.Hex(), receipt.Logs[0].Address)
require.NotNil(t, testkeeper.EVMTestApp.EvmKeeper.GetEVMTxDeferredInfo(ctx))
_, found = testkeeper.EVMTestApp.EvmKeeper.GetEVMTxDeferredInfo(ctx)
require.True(t, found)
require.Equal(t, common.HexToHash("0x1").Bytes(), receipt.Logs[0].Data)

// revoke all
Expand All @@ -327,7 +332,8 @@ func TestEvmEventsForCw721(t *testing.T) {
require.Equal(t, 1, len(receipt.Logs))
require.NotEmpty(t, receipt.LogsBloom)
require.Equal(t, mockPointerAddr.Hex(), receipt.Logs[0].Address)
require.NotNil(t, testkeeper.EVMTestApp.EvmKeeper.GetEVMTxDeferredInfo(ctx))
_, found = testkeeper.EVMTestApp.EvmKeeper.GetEVMTxDeferredInfo(ctx)
require.True(t, found)
require.Equal(t, common.HexToHash("0x0").Bytes(), receipt.Logs[0].Data)

// burn
Expand All @@ -352,7 +358,8 @@ func TestEvmEventsForCw721(t *testing.T) {
require.Equal(t, 1, len(receipt.Logs))
require.NotEmpty(t, receipt.LogsBloom)
require.Equal(t, mockPointerAddr.Hex(), receipt.Logs[0].Address)
require.NotNil(t, testkeeper.EVMTestApp.EvmKeeper.GetEVMTxDeferredInfo(ctx))
_, found = testkeeper.EVMTestApp.EvmKeeper.GetEVMTxDeferredInfo(ctx)
require.True(t, found)
require.Equal(t, common.HexToHash("0x2").Bytes(), receipt.Logs[0].Data)
}

Expand Down
32 changes: 26 additions & 6 deletions contracts/test/ERC721toCW721PointerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,24 @@ describe("ERC721 to CW721 Pointer", function () {

describe("write", function(){
it("approve", async function () {
const blockNumber = await ethers.provider.getBlockNumber();
const approvedTxResp = await pointerAcc0.approve(accounts[1].evmAddress, 2)
await approvedTxResp.wait()
const approved = await pointerAcc0.getApproved(2);
expect(approved).to.equal(accounts[1].evmAddress);

await expect(approvedTxResp)
.to.emit(pointerAcc0, 'Approval')
.withArgs(accounts[0].evmAddress, accounts[1].evmAddress, 2);
const filter = {
fromBlock: blockNumber,
toBlock: 'latest',
address: await pointerAcc1.getAddress(),
topics: [ethers.id("Approval(address,address,uint256)")]
};
const logs = await ethers.provider.getLogs(filter);
expect(logs.length).to.equal(1);
expect(logs[0]["address"]).to.equal(await pointerAcc1.getAddress());
expect(logs[0]["topics"][0]).to.equal(ethers.id("Approval(address,address,uint256)"));
expect(logs[0]["topics"][1].substring(26)).to.equal(accounts[0].evmAddress.substring(2).toLowerCase());
expect(logs[0]["topics"][2].substring(26)).to.equal(accounts[1].evmAddress.substring(2).toLowerCase());
});

it("cannot approve token you don't own", async function () {
Expand All @@ -99,11 +109,21 @@ describe("ERC721 to CW721 Pointer", function () {
it("transfer from", async function () {
// accounts[0] should transfer token id 2 to accounts[1]
await mine(pointerAcc0.approve(accounts[1].evmAddress, 2));
const blockNumber = await ethers.provider.getBlockNumber();
transferTxResp = await pointerAcc1.transferFrom(accounts[0].evmAddress, accounts[1].evmAddress, 2);
await transferTxResp.wait();
await expect(transferTxResp)
.to.emit(pointerAcc0, 'Transfer')
.withArgs(accounts[0].evmAddress, accounts[1].evmAddress, 2);
const filter = {
fromBlock: blockNumber,
toBlock: 'latest',
address: await pointerAcc1.getAddress(),
topics: [ethers.id("Transfer(address,address,uint256)")]
};
const logs = await ethers.provider.getLogs(filter);
expect(logs.length).to.equal(1);
expect(logs[0]["address"]).to.equal(await pointerAcc1.getAddress());
expect(logs[0]["topics"][0]).to.equal(ethers.id("Transfer(address,address,uint256)"));
expect(logs[0]["topics"][1].substring(26)).to.equal(accounts[1].evmAddress.substring(2).toLowerCase());
expect(logs[0]["topics"][2].substring(26)).to.equal(accounts[1].evmAddress.substring(2).toLowerCase());
const balance0 = await pointerAcc0.balanceOf(accounts[0].evmAddress);
expect(balance0).to.equal(0);
const balance1 = await pointerAcc0.balanceOf(accounts[1].evmAddress);
Expand Down

0 comments on commit 2431dae

Please sign in to comment.