Skip to content

Commit

Permalink
feat: support eth_call.
Browse files Browse the repository at this point in the history
  • Loading branch information
iosguang committed May 7, 2022
1 parent 6b6dffb commit 475cc3f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 66 deletions.
25 changes: 25 additions & 0 deletions core/eth/chain_other.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package eth

import (
"context"
"math/big"
"strings"

"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/coming-chat/wallet-SDK/core/base"
)

Expand Down Expand Up @@ -56,3 +59,25 @@ func (c *Chain) BatchErc20TokenBalance(contractList []string, address string) ([
return b.Total, err
})
}

func (c *Chain) CallContract(msg *CallMsg, blockNumber string) (string, error) {
chain, err := GetConnection(c.RpcUrl)
if err != nil {
return "", err
}

ctx, cancel := context.WithTimeout(context.Background(), chain.timeout)
defer cancel()

block, _ := new(big.Int).SetString(blockNumber, 10)
hash, err := chain.RemoteRpcClient.CallContract(ctx, msg.msg, block)
if err != nil {
return "", err
}

return types.HexEncodeToString(hash), nil
}

func (c *Chain) PendingCallContract(msg *CallMsg) (string, error) {
return c.CallContract(msg, "-1")
}
38 changes: 38 additions & 0 deletions core/eth/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package eth
import (
"math/big"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
Expand Down Expand Up @@ -49,3 +50,40 @@ type Erc20TxParams struct {
Amount string `json:"amount"`
Method string `json:"method"`
}

// CallMsg contains parameters for contract calls.
type CallMsg struct {
msg ethereum.CallMsg
}

// NewCallMsg creates an empty contract call parameter list.
func NewCallMsg() *CallMsg {
return new(CallMsg)
}

func (msg *CallMsg) GetFrom() string { return msg.msg.From.String() }
func (msg *CallMsg) GetGas() int64 { return int64(msg.msg.Gas) }
func (msg *CallMsg) GetGasPrice() string { return msg.msg.GasPrice.String() }
func (msg *CallMsg) GetValue() string { return msg.msg.Value.String() }
func (msg *CallMsg) GetData() []byte { return msg.msg.Data }
func (msg *CallMsg) GetTo() string { return msg.msg.To.String() }

func (msg *CallMsg) SetFrom(address string) { msg.msg.From = common.HexToAddress(address) }
func (msg *CallMsg) SetGas(gas int64) { msg.msg.Gas = uint64(gas) }
func (msg *CallMsg) SetGasPrice(price string) {
i, _ := new(big.Int).SetString(price, 10)
msg.msg.GasPrice = i
}
func (msg *CallMsg) SetValue(value string) {
i, _ := new(big.Int).SetString(value, 10)
msg.msg.Value = i
}
func (msg *CallMsg) SetData(data []byte) { msg.msg.Data = common.CopyBytes(data) }
func (msg *CallMsg) SetTo(address string) {
if address == "" {
msg.msg.To = nil
} else {
a := common.HexToAddress(address)
msg.msg.To = &a
}
}
18 changes: 0 additions & 18 deletions core/wallet/chain.go

This file was deleted.

52 changes: 26 additions & 26 deletions core/wallet/err.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@ package wallet
import "errors"

var (
ErrNilKey = errors.New("no mnemonic or private key")
ErrNilWallet = errors.New("no mnemonic or private key or keystore")
ErrNilKeystore = errors.New("no keystore")
ErrNilMetadata = errors.New("no metadata")
ErrNotSigned = errors.New("transaction not signed")
ErrNoPublicKey = errors.New("transaction no public key")
ErrNilExtrinsic = errors.New("nil extrinsic")
ErrAddress = errors.New("err address")
ErrPublicKey = errors.New("err publicKey")
ErrSeedOrPhrase = errors.New("invalid seed length")
// ErrNilKey = errors.New("no mnemonic or private key")
// ErrNilWallet = errors.New("no mnemonic or private key or keystore")
// ErrNilKeystore = errors.New("no keystore")
// ErrNilMetadata = errors.New("no metadata")
// ErrNotSigned = errors.New("transaction not signed")
// ErrNoPublicKey = errors.New("transaction no public key")
// ErrNilExtrinsic = errors.New("nil extrinsic")
// ErrAddress = errors.New("err address")
// ErrPublicKey = errors.New("err publicKey")
// ErrSeedOrPhrase = errors.New("invalid seed length")

ErrInvalidMnemonic = errors.New("invalid mnemonic")

ErrWrongMetadata = errors.New("wrong metadata")
ErrNoEncrypted = errors.New("no encrypted data to decode")
ErrEncryptedLength = errors.New("encrypted length is less than 24")
ErrInvalidParams = errors.New("invalid injected scrypt params found")
ErrSecretLength = errors.New("secret length is not 32")
ErrEncoded = errors.New("encoded is nil")
ErrPkcs8Header = errors.New("invalid Pkcs8 header found in body")
ErrPkcs8Divider = errors.New("invalid Pkcs8 divider found in body")
// ErrWrongMetadata = errors.New("wrong metadata")
// ErrNoEncrypted = errors.New("no encrypted data to decode")
// ErrEncryptedLength = errors.New("encrypted length is less than 24")
// ErrInvalidParams = errors.New("invalid injected scrypt params found")
// ErrSecretLength = errors.New("secret length is not 32")
// ErrEncoded = errors.New("encoded is nil")
// ErrPkcs8Header = errors.New("invalid Pkcs8 header found in body")
// ErrPkcs8Divider = errors.New("invalid Pkcs8 divider found in body")

ErrNonPkcs8 = errors.New("unable to decode non-pkcs8 type")
ErrNilPassword = errors.New("password required to decode encrypted data")
ErrNoEncryptedData = errors.New("no encrypted data available to decode")
ErrKeystore = errors.New("decoded public keys are not equal")
// ErrNonPkcs8 = errors.New("unable to decode non-pkcs8 type")
// ErrNilPassword = errors.New("password required to decode encrypted data")
// ErrNoEncryptedData = errors.New("no encrypted data available to decode")
// ErrKeystore = errors.New("decoded public keys are not equal")

ErrPassword = errors.New("password err")
// ErrPassword = errors.New("password err")

ErrNumber = errors.New("illegal number")
ErrSign = errors.New("sign panic error")
// ErrNumber = errors.New("illegal number")
// ErrSign = errors.New("sign panic error")

errMethodUnusable = errors.New("TODO: Method not yet implemented")
// errMethodUnusable = errors.New("TODO: Method not yet implemented")
)
22 changes: 0 additions & 22 deletions core/wallet/transaction.go

This file was deleted.

0 comments on commit 475cc3f

Please sign in to comment.