forked from qtumproject/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheth_getBalance.go
70 lines (58 loc) · 2.08 KB
/
eth_getBalance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package transformer
import (
"math/big"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/labstack/echo"
"github.com/qtumproject/janus/pkg/eth"
"github.com/qtumproject/janus/pkg/qtum"
"github.com/qtumproject/janus/pkg/utils"
)
// ProxyETHGetBalance implements ETHProxy
type ProxyETHGetBalance struct {
*qtum.Qtum
}
func (p *ProxyETHGetBalance) Method() string {
return "eth_getBalance"
}
func (p *ProxyETHGetBalance) Request(rawreq *eth.JSONRPCRequest, c echo.Context) (interface{}, eth.JSONRPCError) {
var req eth.GetBalanceRequest
if err := unmarshalRequest(rawreq.Params, &req); err != nil {
// TODO: Correct error code?
return nil, eth.NewInvalidParamsError(err.Error())
}
addr := utils.RemoveHexPrefix(req.Address)
{
// is address a contract or an account?
qtumreq := qtum.GetAccountInfoRequest(addr)
qtumresp, err := p.GetAccountInfo(c.Request().Context(), &qtumreq)
// the address is a contract
if err == nil {
// the unit of the balance Satoshi
p.GetDebugLogger().Log("method", p.Method(), "address", req.Address, "msg", "is a contract")
return hexutil.EncodeUint64(uint64(qtumresp.Balance)), nil
}
}
{
// try account
base58Addr, err := p.FromHexAddress(addr)
if err != nil {
p.GetDebugLogger().Log("method", p.Method(), "address", req.Address, "msg", "error parsing address", "error", err)
return nil, eth.NewCallbackError(err.Error())
}
qtumreq := qtum.GetAddressBalanceRequest{Address: base58Addr}
qtumresp, err := p.GetAddressBalance(c.Request().Context(), &qtumreq)
if err != nil {
if err == qtum.ErrInvalidAddress {
// invalid address should return 0x0
return "0x0", nil
}
p.GetDebugLogger().Log("method", p.Method(), "address", req.Address, "msg", "error getting address balance", "error", err)
return nil, eth.NewCallbackError(err.Error())
}
// 1 QTUM = 10 ^ 8 Satoshi
balance := new(big.Int).SetUint64(qtumresp.Balance)
//Balance for ETH response is represented in Weis (1 QTUM Satoshi = 10 ^ 10 Wei)
balance = balance.Mul(balance, big.NewInt(10000000000))
return hexutil.EncodeBig(balance), nil
}
}