Skip to content

Commit 5ddb9ab

Browse files
dvli2007dirtyshabmr-meatscottirvine
authored
Pointers contracts: support for ERC1155 and CW1155 contracts (#1755)
* wip cw1155 pointer * cleanup todos * sync events with current standard * fix query BalanceOfBatch * fix import * Fix up cwerc1155 compiled files * Add 1155 requests into bindings * queries * follow same coding style * Add cwerc1155.wasm binary to `/contracts` * Add go code to support new pointer contract * Fix typo * Add updated protobuf files * Fix up tests * Deprecate pointer proposals * queries * follow same coding style * safetransfer functions, royalyinfo, set approval * adding in transfer and approval tests * solc and abigen * clean up unused artifacts * Add all go bindings * return unsupported for methods not supported in erc standard * fix query BalanceOfBatch response * Add new test files * fix Erc1155BalanceOfBatchResponse * More testing * Update cwerc1155.wasm * More tests * return true/false instead of erroring in query IsApprovedForAll * use correct response for query BalanceOfBatch * pass optional token id field to evm query TotalSupply * remove todos * remove AllTokenInfo query * Fix balances query struct * Update and rebuild CW1155 pointer * Add new cw1155 wasms * merge * Update cwerc1155.wasm * Fix up ERC1155 pointer tests * balanceOfBatch * adding in expect calls * remove unused import * Round of updates to cw1155 contracts * Update cw1155 pointer tests * Fix: missing ERC1155TransferPayload * removing unnecessary return * Update binary * Fix balance of batch * add minor checks * Update integration test with latest main updates * Fix up erc721-payload and erc1155-payload cli commands * Lint code * Update integration test with new pointer contract code id * Map CW1155 events to EVM * updates from cw1155 base * update cw1155 base * Test events emissions for cw1155 events * Update wasm build for cwerc1155 * Fix integration test 1 * update cw1155 cargo, add optimize script to cargo * Update wasm builds * Add migrations for 1155s * Code quality * Remove unnecessary lines * updates from audit on cw1155 standard * [P1-I-01] Validate parameters in the "balanceOfBatch" function * build pointer wasm from audit changes * update pointer wasms in other places * [P1-H-01] Fix "safeBatchTransferFrom" - change to "onERC1155BatchReceived" * [P1-I-02] Consider adding "ERC1155Burnable" to "CW1155ERC1155Pointer" * Fix CW1155ERC1155Pointer.sol styles * Update ERC1155 pointer artifacts * send funds if empty msg in send and send_batch * rebuild cw1155 pointer/standard * fix version issue * cw1155_base wasm fix * Update and fix tests * Fix tests * Remove events from 1155 pointer so as not to double-emit * Linting --------- Co-authored-by: shab <dirtyshab@protonmail.com> Co-authored-by: Mr Meat <77750450+mr-meat@users.noreply.github.com> Co-authored-by: scottirvine <136702693+scottirvine@users.noreply.github.com>
1 parent 15333e5 commit 5ddb9ab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+9586
-152
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ coverage.out
4949
example/cosmwasm/echo/target
5050
example/cosmwasm/cw20/target
5151
example/cosmwasm/cw721/target
52+
example/cosmwasm/cw1155/target
5253

5354
# Solidity contracts artifacts
5455
contracts/artifacts

app/receipt.go

+157
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"math"
77
"math/big"
8+
"strings"
89

910
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
1011
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -24,6 +25,10 @@ var ERC20TransferTopic = common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952
2425
var ERC721TransferTopic = common.HexToHash("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef")
2526
var ERC721ApprovalTopic = common.HexToHash("0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925")
2627
var ERC721ApproveAllTopic = common.HexToHash("0x17307eab39ab6107e8899845ad3d59bd9653f200f220920489ca2b5937696c31")
28+
var ERC1155TransferSingleTopic = common.HexToHash("0xc3d58168c5ae7397731d063d5bbf3d657854427343f4c083240f7aacaa2d0f62")
29+
var ERC1155TransferBatchTopic = common.HexToHash("0x4a39dc06d4c0dbc64b70af90fd698a233a518aa5d07e595d983b8c0526c8f7fb")
30+
var ERC1155ApprovalForAllTopic = common.HexToHash("0x17307eab39ab6107e8899845ad3d59bd9653f200f220920489ca2b5937696c31")
31+
var ERC1155URITopic = common.HexToHash("0x6bb7ff708619ba0610cba295a58592e0451dee2622938c8755667688daf3529b")
2732
var EmptyHash = common.HexToHash("0x0")
2833
var TrueHash = common.HexToHash("0x1")
2934

@@ -91,6 +96,16 @@ func (app *App) AddCosmosEventsToEVMReceiptIfApplicable(ctx sdk.Context, tx sdk.
9196
}
9297
continue
9398
}
99+
// check if there is a ERC1155 pointer to contract Addr
100+
pointerAddr, _, exists = app.EvmKeeper.GetERC1155CW1155Pointer(ctx, contractAddr)
101+
if exists {
102+
log, eligible := app.translateCW1155Event(ctx, wasmEvent, pointerAddr, contractAddr)
103+
if eligible {
104+
log.Index = uint(len(logs))
105+
logs = append(logs, log)
106+
}
107+
continue
108+
}
94109
}
95110
if len(logs) == 0 {
96111
return
@@ -319,6 +334,99 @@ func (app *App) translateCW721Event(ctx sdk.Context, wasmEvent abci.Event, point
319334
return nil, false
320335
}
321336

337+
func (app *App) translateCW1155Event(ctx sdk.Context, wasmEvent abci.Event, pointerAddr common.Address, contractAddr string) (*ethtypes.Log, bool) {
338+
action, found := GetAttributeValue(wasmEvent, "action")
339+
if !found {
340+
return nil, false
341+
}
342+
var topics []common.Hash
343+
switch action {
344+
case "transfer_single", "mint_single", "burn_single":
345+
fromHash := EmptyHash
346+
toHash := EmptyHash
347+
if action != "mint_single" {
348+
fromHash = app.GetEvmAddressAttribute(ctx, wasmEvent, "owner")
349+
}
350+
if action != "burn_single" {
351+
toHash = app.GetEvmAddressAttribute(ctx, wasmEvent, "recipient")
352+
}
353+
topics = []common.Hash{
354+
ERC1155TransferSingleTopic,
355+
app.GetEvmAddressAttribute(ctx, wasmEvent, "sender"),
356+
fromHash,
357+
toHash,
358+
}
359+
tokenID := GetTokenIDAttribute(wasmEvent)
360+
if tokenID == nil {
361+
return nil, false
362+
}
363+
tokenAmount, found := GetAmountAttribute(wasmEvent)
364+
if !found {
365+
return nil, false
366+
}
367+
dataHash1 := common.BigToHash(tokenID).Bytes()
368+
dataHash2 := common.BigToHash(tokenAmount).Bytes()
369+
return &ethtypes.Log{
370+
Address: pointerAddr,
371+
Topics: topics,
372+
Data: append(dataHash1, dataHash2...),
373+
}, true
374+
case "transfer_batch", "mint_batch", "burn_batch":
375+
fromHash := EmptyHash
376+
toHash := EmptyHash
377+
if action != "mint_batch" {
378+
fromHash = app.GetEvmAddressAttribute(ctx, wasmEvent, "owner")
379+
}
380+
if action != "burn_batch" {
381+
toHash = app.GetEvmAddressAttribute(ctx, wasmEvent, "recipient")
382+
}
383+
topics = []common.Hash{
384+
ERC1155TransferBatchTopic,
385+
app.GetEvmAddressAttribute(ctx, wasmEvent, "sender"),
386+
fromHash,
387+
toHash,
388+
}
389+
tokenIDs, found := GetTokenIDsAttribute(wasmEvent)
390+
if !found {
391+
return nil, false
392+
}
393+
tokenAmounts, found := GetAmountsAttribute(wasmEvent)
394+
if !found {
395+
return nil, false
396+
}
397+
value := EncodeBigIntArray(tokenIDs)
398+
value = append(value, EncodeBigIntArray(tokenAmounts)...)
399+
return &ethtypes.Log{
400+
Address: pointerAddr,
401+
Topics: topics,
402+
Data: value,
403+
}, true
404+
case "approve_all":
405+
topics = []common.Hash{
406+
ERC1155ApprovalForAllTopic,
407+
app.GetEvmAddressAttribute(ctx, wasmEvent, "sender"),
408+
app.GetEvmAddressAttribute(ctx, wasmEvent, "operator"),
409+
}
410+
return &ethtypes.Log{
411+
Address: pointerAddr,
412+
Topics: topics,
413+
Data: TrueHash.Bytes(),
414+
}, true
415+
case "revoke_all":
416+
topics = []common.Hash{
417+
ERC1155ApprovalForAllTopic,
418+
app.GetEvmAddressAttribute(ctx, wasmEvent, "sender"),
419+
app.GetEvmAddressAttribute(ctx, wasmEvent, "operator"),
420+
}
421+
return &ethtypes.Log{
422+
Address: pointerAddr,
423+
Topics: topics,
424+
Data: EmptyHash.Bytes(),
425+
}, true
426+
}
427+
return nil, false
428+
}
429+
322430
func (app *App) GetEvmAddressAttribute(ctx sdk.Context, event abci.Event, attribute string) common.Hash {
323431
addrStr, found := GetAttributeValue(event, attribute)
324432
if found {
@@ -360,6 +468,22 @@ func GetAmountAttribute(event abci.Event) (*big.Int, bool) {
360468
return nil, false
361469
}
362470

471+
func GetAmountsAttribute(event abci.Event) ([]*big.Int, bool) {
472+
results := []*big.Int{}
473+
amounts, found := GetAttributeValue(event, "amounts")
474+
if !found {
475+
return results, false
476+
}
477+
for _, amt := range strings.Split(amounts, ",") {
478+
amtInt, ok := sdk.NewIntFromString(amt)
479+
if !ok {
480+
return results, false
481+
}
482+
results = append(results, amtInt.BigInt())
483+
}
484+
return results, true
485+
}
486+
363487
func GetTokenIDAttribute(event abci.Event) *big.Int {
364488
tokenID, found := GetAttributeValue(event, "token_id")
365489
if !found {
@@ -371,3 +495,36 @@ func GetTokenIDAttribute(event abci.Event) *big.Int {
371495
}
372496
return tokenIDInt.BigInt()
373497
}
498+
499+
func GetTokenIDsAttribute(event abci.Event) ([]*big.Int, bool) {
500+
results := []*big.Int{}
501+
tokenIDs, found := GetAttributeValue(event, "token_ids")
502+
if !found {
503+
return results, false
504+
}
505+
for _, tid := range strings.Split(tokenIDs, ",") {
506+
tidInt, ok := sdk.NewIntFromString(tid)
507+
if !ok {
508+
return results, false
509+
}
510+
results = append(results, tidInt.BigInt())
511+
}
512+
return results, true
513+
}
514+
515+
func EncodeBigIntArray(inputs []*big.Int) []byte {
516+
// Arrays are broken up into components:
517+
// - offset byte (always 32)
518+
// - length of array
519+
// - ...array values
520+
offset := big.NewInt(32)
521+
length := big.NewInt(int64(len(inputs)))
522+
value := append(
523+
common.BigToHash(offset).Bytes(),
524+
common.BigToHash(length).Bytes()...,
525+
)
526+
for _, i := range inputs {
527+
value = append(value, common.BigToHash(i).Bytes()...)
528+
}
529+
return value
530+
}

0 commit comments

Comments
 (0)