Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ type VM interface {
GetVerifyAuth() bool
ReadState(ctx context.Context, keys [][]byte) ([][]byte, []error)
ImmutableState(ctx context.Context) (state.Immutable, error)
BalanceHandler() chain.BalanceHandler
}
14 changes: 14 additions & 0 deletions api/jsonrpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,17 @@ func (cli *JSONRPCClient) SimulateActions(ctx context.Context, actions chain.Act

return resp.ActionResults, nil
}

func (cli *JSONRPCClient) GetBalance(ctx context.Context, addr codec.Address) (uint64, error) {
args := &GetBalanceArgs{
Address: addr,
}
resp := new(GetBalanceReply)
err := cli.requester.SendRequest(
ctx,
"getBalance",
args,
resp,
)
return resp.Balance, err
}
30 changes: 30 additions & 0 deletions api/jsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,33 @@ func (j *JSONRPCServer) SimulateActions(
}
return nil
}

type GetBalanceArgs struct {
Address codec.Address `json:"address"`
}

type GetBalanceReply struct {
Balance uint64 `json:"balance"`
}

func (j *JSONRPCServer) GetBalance(
req *http.Request,
args *GetBalanceArgs,
reply *GetBalanceReply,
) error {
ctx, span := j.vm.Tracer().Start(req.Context(), "JSONRPCServer.GetBalance")
defer span.End()

im, err := j.vm.ImmutableState(ctx)
if err != nil {
return err
}

balance, err := j.vm.BalanceHandler().GetBalance(ctx, args.Address, im)
if err != nil {
return err
}

reply.Balance = balance
return nil
}
4 changes: 4 additions & 0 deletions chain/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ type BalanceHandler interface {

// AddBalance adds [amount] to [addr].
AddBalance(ctx context.Context, addr codec.Address, mu state.Mutable, amount uint64, createAccount bool) error

// GetBalance returns the balance of [addr].
// If [addr] does not exist, this should return 0 and no error.
GetBalance(ctx context.Context, addr codec.Address, im state.Immutable) (uint64, error)
}

type Object interface {
Expand Down
4 changes: 4 additions & 0 deletions examples/morpheusvm/storage/state_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ func (*BalanceHandler) AddBalance(
_, err := AddBalance(ctx, mu, addr, amount, createAccount)
return err
}

func (*BalanceHandler) GetBalance(ctx context.Context, addr codec.Address, im state.Immutable) (uint64, error) {
return GetBalance(ctx, im, addr)
}
4 changes: 4 additions & 0 deletions x/contracts/vm/storage/state_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ func (*BalanceHandler) AddBalance(
_, err := AddBalance(ctx, mu, addr, amount, createAccount)
return err
}

func (*BalanceHandler) GetBalance(ctx context.Context, addr codec.Address, im state.Immutable) (uint64, error) {
return GetBalance(ctx, im, addr)
}
Loading