Skip to content

Commit

Permalink
feat: support eth.zksync fetch detail.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhangguiguang committed Jul 4, 2023
1 parent 699b6f7 commit 604f994
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 8 deletions.
4 changes: 2 additions & 2 deletions core/eth/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ func (c *Chain) FetchTransactionDetail(hash string) (*base.TransactionDetail, er
if err != nil {
return nil, err
}
detail, msg, err := chain.FetchTransactionDetail(hash)
detail, data, err := chain.FetchTransactionDetail(hash)
if err != nil {
return nil, err
}
if data := msg.Data(); len(data) > 0 {
if len(data) > 0 {
method, params, err := DecodeContractParams(Erc20AbiStr, data)
if err == nil && method == ERC20_METHOD_TRANSFER {
detail.ToAddress = params[0].(common.Address).String()
Expand Down
19 changes: 17 additions & 2 deletions core/eth/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,26 @@ import (

"github.com/coming-chat/wallet-SDK/core/base"
"github.com/coming-chat/wallet-SDK/core/testcase"
"github.com/stretchr/testify/require"
)

func TestZK(t *testing.T) {

// rpc := "https://testnet.era.zksync.dev"
// hash := "0x590fae06853d27495f1c5d4d23ea35ad910454a30de012231cb390485bc8e268"

rpc := "https://mainnet.era.zksync.io"
hash := "0xc9ac881431a8df988bdcc00693e2c286848ce12f48e6af0323588cb68d822417"

chain := NewChainWithRpc(rpc)
detail, err := chain.FetchTransactionDetail(hash)
require.Nil(t, err)
t.Log(detail.Status)
}

func TestChainDetail(t *testing.T) {
hash := "0xd9efdb33723288e16c2a552f1699d8daddfdbaf4c79cec0fc95d2dcc9025da70"
chain := rpcs.arbitrumProd.Chain()
hash := "0xb5f9b89667d854555ce1b178a025c44d869dfcdef24ae4c5a1e790c5f41bbafb"
chain := rpcs.ethereumProd.Chain()

// hash := "0xa2b05fc52748ee21845c8596263d75d85f54c522575be2e399235d144cd5a153"
// chain := rpcs.optimismProd.Chain()
Expand Down
7 changes: 5 additions & 2 deletions core/eth/ethchain_transactionDetail.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ func decodeSigner(txn *types.Transaction) (common.Address, error) {
// 获取交易的详情
// @param hashString 交易的 hash
// @return 交易详情 和 交易原文信息
func (e *EthChain) FetchTransactionDetail(hashString string) (detail *base.TransactionDetail, txn *types.Transaction, err error) {
func (e *EthChain) FetchTransactionDetail(hashString string) (detail *base.TransactionDetail, data []byte, err error) {
defer base.CatchPanicAndMapToBasicError(&err)

if e.chainId.Int64() == zksync_chainid || e.chainId.Int64() == zksync_chainid_testnet {
return e.zksync_FetchTransactionDetail(hashString)
}
ctx, cancel := context.WithTimeout(context.Background(), e.timeout)
defer cancel()
tx, isPending, err := e.RemoteRpcClient.TransactionByHash(ctx, common.HexToHash(hashString))
Expand Down Expand Up @@ -114,7 +117,7 @@ func (e *EthChain) FetchTransactionDetail(hashString string) (detail *base.Trans
detail.EstimateFees = gasFeeInt.String()
detail.FinishTimestamp = int64(blockHeader.Time)

return detail, tx, nil
return detail, tx.Data(), nil
}

// 获取交易的状态
Expand Down
114 changes: 114 additions & 0 deletions core/eth/ethchain_zksync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package eth

import (
"context"
"math/big"

"github.com/coming-chat/wallet-SDK/core/base"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
)

const zksync_chainid = 324
const zksync_chainid_testnet = 280

type zksync_Transaction struct {
BlockHash common.Hash `json:"blockHash"`
BlockNumber *hexutil.Big `json:"blockNumber"`
ChainID hexutil.Big `json:"chainId"`
From common.Address `json:"from"`
Gas hexutil.Uint64 `json:"gas"`
GasPrice hexutil.Big `json:"gasPrice"`
Hash common.Hash `json:"hash"`
Data hexutil.Bytes `json:"input"`
L1BatchNumber hexutil.Big `json:"l1BatchNumber"`
L1BatchTxIndex hexutil.Big `json:"l1BatchTxIndex"`
MaxFeePerGas hexutil.Big `json:"maxFeePerGas"`
MaxPriorityFeePerGas hexutil.Big `json:"maxPriorityFeePerGas"`
Nonce hexutil.Uint64 `json:"nonce"`
To common.Address `json:"to"`
TransactionIndex hexutil.Uint `json:"transactionIndex"`
TxType hexutil.Uint64 `json:"type"`
Value hexutil.Big `json:"value"`
V hexutil.Big `json:"v"`
R hexutil.Big `json:"r"`
S hexutil.Big `json:"s"`
}

// func (t *zksync_Transaction)

func (e *EthChain) zksync_FetchTransactionDetail(hashString string) (detail *base.TransactionDetail, data []byte, err error) {
defer base.CatchPanicAndMapToBasicError(&err)

ctx, cancel := context.WithTimeout(context.Background(), e.timeout)
defer cancel()
var tx *zksync_Transaction
err = e.RpcClient.CallContext(ctx, &tx, "eth_getTransactionByHash", hashString)
if err != nil {
return
} else if tx == nil {
return nil, nil, ethereum.NotFound
}

gasFeeInt := big.NewInt(0).Mul(tx.GasPrice.ToInt(), big.NewInt(0).SetUint64(uint64(tx.Gas)))
detail = &base.TransactionDetail{
HashString: hashString,
FromAddress: tx.From.String(),
ToAddress: tx.To.String(),
Amount: tx.Value.ToInt().String(),
EstimateFees: gasFeeInt.String(),
}

if tx.BlockNumber == nil {
detail.Status = base.TransactionStatusPending
return
}

// 交易receipt 状态信息,0表示失败,1表示成功
// 当交易没有处于pending状态时,可以查询receipt信息,即交易是否成功, err为nil时,表示查询成功进入if语句赋值
receipt, err := e.TransactionReceiptByHash(hashString)
if err != nil {
return
}
blockHeader, err := e.RemoteRpcClient.HeaderByNumber(ctx, receipt.BlockNumber)
if err != nil {
return
}

if receipt.Status == 0 {
detail.Status = base.TransactionStatusFailure
// get error message
_, err := e.RemoteRpcClient.CallContract(ctx, ethereum.CallMsg{
From: tx.From,
To: &tx.To,
Data: tx.Data,
Gas: uint64(tx.Gas),
GasPrice: tx.GasPrice.ToInt(),
GasFeeCap: tx.MaxFeePerGas.ToInt(),
GasTipCap: tx.MaxPriorityFeePerGas.ToInt(),
Value: tx.Value.ToInt(),
AccessList: nil,
}, receipt.BlockNumber)
if err != nil {
detail.FailureMessage = err.Error()
err = nil
}

} else {
detail.Status = base.TransactionStatusSuccess
}

effectiveGasPrice := tx.GasPrice.ToInt()
if receipt.EffectiveGasPrice != nil {
effectiveGasPrice = receipt.EffectiveGasPrice
}
gasFeeInt = big.NewInt(0).Mul(effectiveGasPrice, big.NewInt(0).SetUint64(receipt.GasUsed))
if receipt.L1Fee != nil {
gasFeeInt = gasFeeInt.Add(gasFeeInt, receipt.L1Fee)
}
detail.EstimateFees = gasFeeInt.String()
detail.FinishTimestamp = int64(blockHeader.Time)

return detail, tx.Data, nil
}
4 changes: 2 additions & 2 deletions core/eth/red_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,12 @@ func (c *Chain) FetchRedPacketCreationDetail(hash string) (*RedPacketDetail, err
return nil, err
}

detail, msg, err := chain.FetchTransactionDetail(hash)
detail, data, err := chain.FetchTransactionDetail(hash)
if err != nil {
return nil, err
}
redDetail := &RedPacketDetail{detail, "", 0}
if data := msg.Data(); len(data) > 0 {
if len(data) > 0 {
method, params, err_ := DecodeContractParams(RedPacketABI, data)
if err_ != nil {
return nil, err_
Expand Down

0 comments on commit 604f994

Please sign in to comment.