Skip to content

Commit 475cc3f

Browse files
committed
feat: support eth_call.
1 parent 6b6dffb commit 475cc3f

File tree

5 files changed

+89
-66
lines changed

5 files changed

+89
-66
lines changed

core/eth/chain_other.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package eth
22

33
import (
4+
"context"
5+
"math/big"
46
"strings"
57

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

@@ -56,3 +59,25 @@ func (c *Chain) BatchErc20TokenBalance(contractList []string, address string) ([
5659
return b.Total, err
5760
})
5861
}
62+
63+
func (c *Chain) CallContract(msg *CallMsg, blockNumber string) (string, error) {
64+
chain, err := GetConnection(c.RpcUrl)
65+
if err != nil {
66+
return "", err
67+
}
68+
69+
ctx, cancel := context.WithTimeout(context.Background(), chain.timeout)
70+
defer cancel()
71+
72+
block, _ := new(big.Int).SetString(blockNumber, 10)
73+
hash, err := chain.RemoteRpcClient.CallContract(ctx, msg.msg, block)
74+
if err != nil {
75+
return "", err
76+
}
77+
78+
return types.HexEncodeToString(hash), nil
79+
}
80+
81+
func (c *Chain) PendingCallContract(msg *CallMsg) (string, error) {
82+
return c.CallContract(msg, "-1")
83+
}

core/eth/types.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package eth
33
import (
44
"math/big"
55

6+
"github.com/ethereum/go-ethereum"
67
"github.com/ethereum/go-ethereum/common"
78
"github.com/ethereum/go-ethereum/core/types"
89
)
@@ -49,3 +50,40 @@ type Erc20TxParams struct {
4950
Amount string `json:"amount"`
5051
Method string `json:"method"`
5152
}
53+
54+
// CallMsg contains parameters for contract calls.
55+
type CallMsg struct {
56+
msg ethereum.CallMsg
57+
}
58+
59+
// NewCallMsg creates an empty contract call parameter list.
60+
func NewCallMsg() *CallMsg {
61+
return new(CallMsg)
62+
}
63+
64+
func (msg *CallMsg) GetFrom() string { return msg.msg.From.String() }
65+
func (msg *CallMsg) GetGas() int64 { return int64(msg.msg.Gas) }
66+
func (msg *CallMsg) GetGasPrice() string { return msg.msg.GasPrice.String() }
67+
func (msg *CallMsg) GetValue() string { return msg.msg.Value.String() }
68+
func (msg *CallMsg) GetData() []byte { return msg.msg.Data }
69+
func (msg *CallMsg) GetTo() string { return msg.msg.To.String() }
70+
71+
func (msg *CallMsg) SetFrom(address string) { msg.msg.From = common.HexToAddress(address) }
72+
func (msg *CallMsg) SetGas(gas int64) { msg.msg.Gas = uint64(gas) }
73+
func (msg *CallMsg) SetGasPrice(price string) {
74+
i, _ := new(big.Int).SetString(price, 10)
75+
msg.msg.GasPrice = i
76+
}
77+
func (msg *CallMsg) SetValue(value string) {
78+
i, _ := new(big.Int).SetString(value, 10)
79+
msg.msg.Value = i
80+
}
81+
func (msg *CallMsg) SetData(data []byte) { msg.msg.Data = common.CopyBytes(data) }
82+
func (msg *CallMsg) SetTo(address string) {
83+
if address == "" {
84+
msg.msg.To = nil
85+
} else {
86+
a := common.HexToAddress(address)
87+
msg.msg.To = &a
88+
}
89+
}

core/wallet/chain.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

core/wallet/err.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,37 @@ package wallet
33
import "errors"
44

55
var (
6-
ErrNilKey = errors.New("no mnemonic or private key")
7-
ErrNilWallet = errors.New("no mnemonic or private key or keystore")
8-
ErrNilKeystore = errors.New("no keystore")
9-
ErrNilMetadata = errors.New("no metadata")
10-
ErrNotSigned = errors.New("transaction not signed")
11-
ErrNoPublicKey = errors.New("transaction no public key")
12-
ErrNilExtrinsic = errors.New("nil extrinsic")
13-
ErrAddress = errors.New("err address")
14-
ErrPublicKey = errors.New("err publicKey")
15-
ErrSeedOrPhrase = errors.New("invalid seed length")
6+
// ErrNilKey = errors.New("no mnemonic or private key")
7+
// ErrNilWallet = errors.New("no mnemonic or private key or keystore")
8+
// ErrNilKeystore = errors.New("no keystore")
9+
// ErrNilMetadata = errors.New("no metadata")
10+
// ErrNotSigned = errors.New("transaction not signed")
11+
// ErrNoPublicKey = errors.New("transaction no public key")
12+
// ErrNilExtrinsic = errors.New("nil extrinsic")
13+
// ErrAddress = errors.New("err address")
14+
// ErrPublicKey = errors.New("err publicKey")
15+
// ErrSeedOrPhrase = errors.New("invalid seed length")
1616

1717
ErrInvalidMnemonic = errors.New("invalid mnemonic")
1818

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

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

33-
ErrPassword = errors.New("password err")
33+
// ErrPassword = errors.New("password err")
3434

35-
ErrNumber = errors.New("illegal number")
36-
ErrSign = errors.New("sign panic error")
35+
// ErrNumber = errors.New("illegal number")
36+
// ErrSign = errors.New("sign panic error")
3737

38-
errMethodUnusable = errors.New("TODO: Method not yet implemented")
38+
// errMethodUnusable = errors.New("TODO: Method not yet implemented")
3939
)

core/wallet/transaction.go

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)