Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen committed Jul 2, 2024
1 parent a1a975a commit 086b9c4
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 62 deletions.
119 changes: 65 additions & 54 deletions app/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,56 +42,10 @@ func (app *App) AddCosmosEventsToEVMReceiptIfApplicable(ctx sdk.Context, tx sdk.
// check if there is a ERC20 pointer to contractAddr
pointerAddr, _, exists := app.EvmKeeper.GetERC20CW20Pointer(ctx, contractAddr)
if exists {
action, found := GetAttributeValue(wasmEvent, "action")
if !found {
continue
}
var topics []common.Hash
switch action {
case "mint", "burn", "send", "transfer", "transfer_from", "send_from", "burn_from":
topics = []common.Hash{
ERC20TransferTopic,
app.GetEvmAddressAttribute(ctx, wasmEvent, "from"),
app.GetEvmAddressAttribute(ctx, wasmEvent, "to"),
}
logs = append(logs, &ethtypes.Log{
Address: pointerAddr,
Index: uint(len(logs)),
Topics: topics,
Data: common.BigToHash(GetAmountAttribute(wasmEvent)).Bytes(),
})
case "increase_allowance", "decrease_allowance":
ownerStr, found := GetAttributeValue(wasmEvent, "owner")
if !found {
continue
}
spenderStr, found := GetAttributeValue(wasmEvent, "spender")
if !found {
continue
}
topics := []common.Hash{
ERC20ApprovalTopic,
app.GetEvmAddressAttribute(ctx, wasmEvent, "owner"),
app.GetEvmAddressAttribute(ctx, wasmEvent, "spender"),
}
res, err := app.WasmKeeper.QuerySmart(
ctx,
sdk.MustAccAddressFromBech32(contractAddr),
[]byte(fmt.Sprintf("{\"allowance\":{\"owner\":\"%s\",\"spender\":\"%s\"}}", ownerStr, spenderStr)),
)
if err != nil {
continue
}
allowanceResponse := &AllowanceResponse{}
if err := json.Unmarshal(res, allowanceResponse); err != nil {
continue
}
logs = append(logs, &ethtypes.Log{
Address: pointerAddr,
Index: uint(len(logs)),
Topics: topics,
Data: common.BigToHash(allowanceResponse.Allowance.BigInt()).Bytes(),
})
log, eligible := app.translateCW20Event(ctx, wasmEvent, pointerAddr, contractAddr)
if eligible {
log.Index = uint(len(logs))
logs = append(logs, log)
}
continue
}
Expand Down Expand Up @@ -128,13 +82,70 @@ func (app *App) AddCosmosEventsToEVMReceiptIfApplicable(ctx sdk.Context, tx sdk.
}
_ = app.EvmKeeper.SetTransientReceipt(ctx, txHash, receipt)
}
if d := app.EvmKeeper.GetEVMTxDeferredInfo(ctx); d != nil {
if d, found := app.EvmKeeper.GetEVMTxDeferredInfo(ctx); found {
app.EvmKeeper.AppendToEvmTxDeferredInfo(ctx, bloom, txHash, d.Surplus)
} else {
app.EvmKeeper.AppendToEvmTxDeferredInfo(ctx, bloom, txHash, sdk.ZeroInt())
}
}

func (app *App) translateCW20Event(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 96 in app/receipt.go

View check run for this annotation

Codecov / codecov/patch

app/receipt.go#L95-L96

Added lines #L95 - L96 were not covered by tests
var topics []common.Hash
switch action {
case "mint", "burn", "send", "transfer", "transfer_from", "send_from", "burn_from":
topics = []common.Hash{
ERC20TransferTopic,
app.GetEvmAddressAttribute(ctx, wasmEvent, "from"),
app.GetEvmAddressAttribute(ctx, wasmEvent, "to"),
}
amount, found := GetAmountAttribute(wasmEvent)
if !found {
return nil, false
}

Check warning on line 108 in app/receipt.go

View check run for this annotation

Codecov / codecov/patch

app/receipt.go#L107-L108

Added lines #L107 - L108 were not covered by tests
return &ethtypes.Log{
Address: pointerAddr,
Topics: topics,
Data: common.BigToHash(amount).Bytes(),
}, true
case "increase_allowance", "decrease_allowance":
ownerStr, found := GetAttributeValue(wasmEvent, "owner")
if !found {
return nil, false
}

Check warning on line 118 in app/receipt.go

View check run for this annotation

Codecov / codecov/patch

app/receipt.go#L117-L118

Added lines #L117 - L118 were not covered by tests
spenderStr, found := GetAttributeValue(wasmEvent, "spender")
if !found {
return nil, false
}

Check warning on line 122 in app/receipt.go

View check run for this annotation

Codecov / codecov/patch

app/receipt.go#L121-L122

Added lines #L121 - L122 were not covered by tests
topics := []common.Hash{
ERC20ApprovalTopic,
app.GetEvmAddressAttribute(ctx, wasmEvent, "owner"),
app.GetEvmAddressAttribute(ctx, wasmEvent, "spender"),
}
res, err := app.WasmKeeper.QuerySmart(
ctx,
sdk.MustAccAddressFromBech32(contractAddr),
[]byte(fmt.Sprintf("{\"allowance\":{\"owner\":\"%s\",\"spender\":\"%s\"}}", ownerStr, spenderStr)),
)
if err != nil {
return nil, false
}

Check warning on line 135 in app/receipt.go

View check run for this annotation

Codecov / codecov/patch

app/receipt.go#L134-L135

Added lines #L134 - L135 were not covered by tests
allowanceResponse := &AllowanceResponse{}
if err := json.Unmarshal(res, allowanceResponse); err != nil {
return nil, false
}

Check warning on line 139 in app/receipt.go

View check run for this annotation

Codecov / codecov/patch

app/receipt.go#L138-L139

Added lines #L138 - L139 were not covered by tests
return &ethtypes.Log{
Address: pointerAddr,
Topics: topics,
Data: common.BigToHash(allowanceResponse.Allowance.BigInt()).Bytes(),
}, true
}
return nil, false

Check warning on line 146 in app/receipt.go

View check run for this annotation

Codecov / codecov/patch

app/receipt.go#L146

Added line #L146 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 Expand Up @@ -165,13 +176,13 @@ func GetAttributeValue(event abci.Event, attribute string) (string, bool) {
return "", false

Check warning on line 176 in app/receipt.go

View check run for this annotation

Codecov / codecov/patch

app/receipt.go#L176

Added line #L176 was not covered by tests
}

func GetAmountAttribute(event abci.Event) *big.Int {
func GetAmountAttribute(event abci.Event) (*big.Int, bool) {
amount, found := GetAttributeValue(event, "amount")
if found {
amountInt, ok := sdk.NewIntFromString(amount)
if ok {
return amountInt.BigInt()
return amountInt.BigInt(), true
}
}
return big.NewInt(0)
return nil, false

Check warning on line 187 in app/receipt.go

View check run for this annotation

Codecov / codecov/patch

app/receipt.go#L187

Added line #L187 was not covered by tests
}
9 changes: 6 additions & 3 deletions app/receipt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func TestEvmEventsForCw20(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 @@ -112,7 +113,8 @@ func TestEvmEventsForCw20(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("{\"increase_allowance\":{\"spender\":\"%s\",\"amount\":\"100\"}}", recipient.String()))
Expand All @@ -136,7 +138,8 @@ func TestEvmEventsForCw20(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("0x64").Bytes(), receipt.Logs[0].Data)
}

Expand Down
8 changes: 8 additions & 0 deletions contracts/test/ERC20toCW20PointerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ describe("ERC20 to CW20 Pointer", function () {
};
const logs = await ethers.provider.getLogs(filter);
expect(logs.length).to.equal(1);
expect(logs[0]["address"]).to.equal(await pointer.getAddress());
expect(logs[0]["topics"][0]).to.equal(ethers.id("Transfer(address,address,uint256)"));
expect(logs[0]["topics"][1]).to.equal(sender.evmAddress);
expect(logs[0]["topics"][2]).to.equal(recipient.evmAddress);

const cleanupTx = await pointer.connect(recipient.signer).transfer(sender.evmAddress, 1);
await cleanupTx.wait();
Expand Down Expand Up @@ -150,6 +154,10 @@ describe("ERC20 to CW20 Pointer", function () {
};
const logs = await ethers.provider.getLogs(filter);
expect(logs.length).to.equal(1);
expect(logs[0]["address"]).to.equal(await pointer.getAddress());
expect(logs[0]["topics"][0]).to.equal(ethers.id("Approval(address,address,uint256)"));
expect(logs[0]["topics"][1]).to.equal(owner);
expect(logs[0]["topics"][2]).to.equal(spender);
});

it("should lower approval", async function () {
Expand Down
2 changes: 1 addition & 1 deletion x/evm/artifacts/README
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
The source files are under contracts/src. The artifacts should be updated whenever the source files change. To update run the following (with NativeSeiTokensERC20 as an example):
- `solc --overwrite @openzeppelin=contracts/lib/openzeppelin-contracts --bin -o x/evm/artifacts/cw20 contracts/src/CW721ERC721Pointer.sol`
- `solc --overwrite @openzeppelin=contracts/lib/openzeppelin-contracts --bin -o x/evm/artifacts/cw721 contracts/src/CW721ERC721Pointer.sol`
- `solc --overwrite @openzeppelin=contracts/lib/openzeppelin-contracts --abi -o x/evm/artifacts/cw721 contracts/src/CW721ERC721Pointer.sol`
- (clean up any artifact that is not CW721ERC721Pointer.bin/abi)
- `abigen --abi=x/evm/artifacts/cw721/CW721ERC721Pointer.abi --pkg=cw721 --out=x/evm/artifacts/cw721/cw721.go`
11 changes: 7 additions & 4 deletions x/evm/keeper/deferred.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,17 @@ func (k *Keeper) AppendToEvmTxDeferredInfo(ctx sdk.Context, bloom ethtypes.Bloom
prefix.NewStore(ctx.TransientStore(k.transientStoreKey), types.DeferredInfoPrefix).Set(key, bz)
}

func (k *Keeper) GetEVMTxDeferredInfo(ctx sdk.Context) *types.DeferredInfo {
func (k *Keeper) GetEVMTxDeferredInfo(ctx sdk.Context) (*types.DeferredInfo, bool) {
key := make([]byte, 8)
binary.BigEndian.PutUint64(key, uint64(ctx.TxIndex()))
val := &types.DeferredInfo{}
bz := prefix.NewStore(ctx.TransientStore(k.transientStoreKey), types.DeferredInfoPrefix).Get(key)
if bz == nil {
return nil
return nil, false
}
_ = val.Unmarshal(bz)
return val
if err := val.Unmarshal(bz); err != nil {
ctx.Logger().Error(fmt.Sprintf("failed to unmarshal EVM deferred info: %s", err))
return nil, false
}
return val, true

Check warning on line 79 in x/evm/keeper/deferred.go

View check run for this annotation

Codecov / codecov/patch

x/evm/keeper/deferred.go#L67-L79

Added lines #L67 - L79 were not covered by tests
}

0 comments on commit 086b9c4

Please sign in to comment.