-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add cosmos tx hash <> tx hash conversion in json rpc with new namespa…
…ce "cosmos"
- Loading branch information
Showing
7 changed files
with
127 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package backend | ||
|
||
import ( | ||
"errors" | ||
|
||
"cosmossdk.io/collections" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
) | ||
|
||
// CosmosTxHashByTxHash returns the Cosmos transaction hash by the Ethereum transaction hash. | ||
func (b *JSONRPCBackend) CosmosTxHashByTxHash(hash common.Hash) (hexutil.Bytes, error) { | ||
queryCtx, err := b.getQueryCtx() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cosmosTxHash, err := b.app.EVMIndexer().CosmosTxHashByTxHash(queryCtx, hash) | ||
if err != nil && errors.Is(err, collections.ErrNotFound) { | ||
return nil, nil | ||
} | ||
|
||
return cosmosTxHash, nil | ||
} | ||
|
||
// TxHashByCosmosTxHash returns the Ethereum transaction hash by the Cosmos transaction hash. | ||
func (b *JSONRPCBackend) TxHashByCosmosTxHash(hash []byte) (common.Hash, error) { | ||
queryCtx, err := b.getQueryCtx() | ||
if err != nil { | ||
return common.Hash{}, err | ||
} | ||
|
||
txHash, err := b.app.EVMIndexer().TxHashByCosmosTxHash(queryCtx, hash) | ||
if err != nil && errors.Is(err, collections.ErrNotFound) { | ||
return common.Hash{}, nil | ||
} | ||
|
||
return txHash, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cosmos | ||
|
||
import ( | ||
"context" | ||
|
||
"cosmossdk.io/log" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/initia-labs/minievm/jsonrpc/backend" | ||
) | ||
|
||
// CosmosAPI is the cosmos namespace for the Ethereum JSON-RPC APIs. | ||
type CosmosAPI struct { | ||
ctx context.Context | ||
logger log.Logger | ||
backend *backend.JSONRPCBackend | ||
} | ||
|
||
// NewCosmosAPI creates an instance of the public ETH Web3 API. | ||
func NewCosmosAPI(logger log.Logger, backend *backend.JSONRPCBackend) *CosmosAPI { | ||
api := &CosmosAPI{ | ||
ctx: context.TODO(), | ||
logger: logger.With("client", "json-rpc"), | ||
backend: backend, | ||
} | ||
|
||
return api | ||
} | ||
|
||
func (api *CosmosAPI) CosmosTxHashByTxHash(hash common.Hash) (hexutil.Bytes, error) { | ||
return api.backend.CosmosTxHashByTxHash(hash) | ||
} | ||
|
||
func (api *CosmosAPI) TxHashByCosmosTxHash(hash hexutil.Bytes) (common.Hash, error) { | ||
return api.backend.TxHashByCosmosTxHash(hash) | ||
} |