Skip to content

Commit 0459bd4

Browse files
Add Getter to BalanceHandler (#1685)
1 parent 960b790 commit 0459bd4

File tree

6 files changed

+57
-0
lines changed

6 files changed

+57
-0
lines changed

api/dependencies.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ type VM interface {
4242
GetVerifyAuth() bool
4343
ReadState(ctx context.Context, keys [][]byte) ([][]byte, []error)
4444
ImmutableState(ctx context.Context) (state.Immutable, error)
45+
BalanceHandler() chain.BalanceHandler
4546
}

api/jsonrpc/client.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,17 @@ func (cli *JSONRPCClient) SimulateActions(ctx context.Context, actions chain.Act
257257

258258
return resp.ActionResults, nil
259259
}
260+
261+
func (cli *JSONRPCClient) GetBalance(ctx context.Context, addr codec.Address) (uint64, error) {
262+
args := &GetBalanceArgs{
263+
Address: addr,
264+
}
265+
resp := new(GetBalanceReply)
266+
err := cli.requester.SendRequest(
267+
ctx,
268+
"getBalance",
269+
args,
270+
resp,
271+
)
272+
return resp.Balance, err
273+
}

api/jsonrpc/server.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,33 @@ func (j *JSONRPCServer) SimulateActions(
318318
}
319319
return nil
320320
}
321+
322+
type GetBalanceArgs struct {
323+
Address codec.Address `json:"address"`
324+
}
325+
326+
type GetBalanceReply struct {
327+
Balance uint64 `json:"balance"`
328+
}
329+
330+
func (j *JSONRPCServer) GetBalance(
331+
req *http.Request,
332+
args *GetBalanceArgs,
333+
reply *GetBalanceReply,
334+
) error {
335+
ctx, span := j.vm.Tracer().Start(req.Context(), "JSONRPCServer.GetBalance")
336+
defer span.End()
337+
338+
im, err := j.vm.ImmutableState(ctx)
339+
if err != nil {
340+
return err
341+
}
342+
343+
balance, err := j.vm.BalanceHandler().GetBalance(ctx, args.Address, im)
344+
if err != nil {
345+
return err
346+
}
347+
348+
reply.Balance = balance
349+
return nil
350+
}

chain/dependencies.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ type BalanceHandler interface {
172172

173173
// AddBalance adds [amount] to [addr].
174174
AddBalance(ctx context.Context, addr codec.Address, mu state.Mutable, amount uint64, createAccount bool) error
175+
176+
// GetBalance returns the balance of [addr].
177+
// If [addr] does not exist, this should return 0 and no error.
178+
GetBalance(ctx context.Context, addr codec.Address, im state.Immutable) (uint64, error)
175179
}
176180

177181
type Object interface {

examples/morpheusvm/storage/state_manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ func (*BalanceHandler) AddBalance(
5757
_, err := AddBalance(ctx, mu, addr, amount, createAccount)
5858
return err
5959
}
60+
61+
func (*BalanceHandler) GetBalance(ctx context.Context, addr codec.Address, im state.Immutable) (uint64, error) {
62+
return GetBalance(ctx, im, addr)
63+
}

x/contracts/vm/storage/state_manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ func (*BalanceHandler) AddBalance(
5757
_, err := AddBalance(ctx, mu, addr, amount, createAccount)
5858
return err
5959
}
60+
61+
func (*BalanceHandler) GetBalance(ctx context.Context, addr codec.Address, im state.Immutable) (uint64, error) {
62+
return GetBalance(ctx, im, addr)
63+
}

0 commit comments

Comments
 (0)