Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2281464

Browse files
authoredAug 29, 2024··
fix: Remove go.mod replace for gno. Use new gnoclient API. (#170)
Gno PR gnolang/gno#2641 has been merged, so we don't need the go.mod replace which was introduced in PR #161 . * In go.mod, remove the replace and use the latest gnolang/gno. * The final gnoclient API which was merged in PR gnolang/gno#2641 is different. It uses `vm.MsgCall`, etc. instead of `gnoclient.MsgCall`, and uses `NewCallTx`, etc. instead of `MakeCallTx`. Therefore, this PR updates api.go to use the new API. * Change the helper functions `convertCallRequest`, etc. to maybe return an error if there is no active account, or if the `send` amount like "1000ugnot" can't be parsed. Signed-off-by: Jeff Thompson <jeff@thefirst.org>
1 parent efc2071 commit 2281464

File tree

3 files changed

+110
-49
lines changed

3 files changed

+110
-49
lines changed
 

‎go.mod

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
connectrpc.com/connect v1.16.2
99
connectrpc.com/grpchealth v1.3.0
1010
connectrpc.com/grpcreflect v1.2.0
11-
github.com/gnolang/gno v0.1.1
11+
github.com/gnolang/gno v0.1.2-0.20240826090356-651f5aac3706
1212
github.com/gnolang/gnokey-mobile v0.0.0-20240814140149-eb333b936c7c
1313
github.com/oklog/run v1.1.0
1414
github.com/peterbourgon/ff/v3 v3.4.0
@@ -69,7 +69,4 @@ require (
6969
gopkg.in/yaml.v3 v3.0.1 // indirect
7070
)
7171

72-
replace (
73-
github.com/gnolang/gno => github.com/jefft0/gno v0.2641.1 // temporary until merging https://github.com/gnolang/gno/pull/2641
74-
golang.org/x/mobile => github.com/berty/mobile v0.0.11 // temporary, see https://github.com/golang/mobile/pull/58 and https://github.com/golang/mobile/pull/82
75-
)
72+
replace golang.org/x/mobile => github.com/berty/mobile v0.0.11 // temporary, see https://github.com/golang/mobile/pull/58 and https://github.com/golang/mobile/pull/82

‎go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHqu
5757
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
5858
github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4=
5959
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
60+
github.com/gnolang/gno v0.1.2-0.20240826090356-651f5aac3706 h1:0kpoNeuErRec3JOt31mx/NbBgGrrD9BE5MNC9kKUgWk=
61+
github.com/gnolang/gno v0.1.2-0.20240826090356-651f5aac3706/go.mod h1:dBaL1Au2MNLol+3FXdCv+IKLJnMKtTmIt778zsKjVu0=
6062
github.com/gnolang/gnokey-mobile v0.0.0-20240814140149-eb333b936c7c h1:rL7dVjWOpdQxmbsh69HrgAklolhydTZmPvgo6BpgdhE=
6163
github.com/gnolang/gnokey-mobile v0.0.0-20240814140149-eb333b936c7c/go.mod h1:2NrHp15t6QXGNDruOpw6/xN6z50kquVvZs6uxvUNhsQ=
6264
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216 h1:GKvsK3oLWG9B1GL7WP/VqwM6C92j5tIvB844oggL9Lk=
@@ -92,8 +94,6 @@ github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
9294
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0=
9395
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k=
9496
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
95-
github.com/jefft0/gno v0.2641.1 h1:8kZl+QtfAWhEJxowoTT/BfwJD8UX8UNIvGprPAiG4c4=
96-
github.com/jefft0/gno v0.2641.1/go.mod h1:dBaL1Au2MNLol+3FXdCv+IKLJnMKtTmIt778zsKjVu0=
9797
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
9898
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
9999
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=

‎service/api.go

Lines changed: 106 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/gnolang/gno/tm2/pkg/crypto/bip39"
1515
crypto_keys "github.com/gnolang/gno/tm2/pkg/crypto/keys"
1616
"github.com/gnolang/gno/tm2/pkg/crypto/keys/keyerror"
17+
"github.com/gnolang/gno/tm2/pkg/sdk/bank"
1718
"github.com/gnolang/gno/tm2/pkg/std"
1819
"go.uber.org/zap"
1920

@@ -419,14 +420,17 @@ func (s *gnoNativeService) Call(ctx context.Context, req *connect.Request[api_ge
419420
s.logger.Debug("Call", zap.String("package", msg.PackagePath), zap.String("function", msg.Fnc), zap.Any("args", msg.Args))
420421
}
421422

422-
cfg, msgs := convertCallRequest(req.Msg)
423+
cfg, msgs, err := s.convertCallRequest(req.Msg)
424+
if err != nil {
425+
return err
426+
}
423427

424428
if s.useGnokeyMobile {
425429
c, err := s.getClient()
426430
if err != nil {
427431
return getGrpcError(err)
428432
}
429-
tx, err := c.MakeCallTx(*cfg, msgs...)
433+
tx, err := gnoclient.NewCallTx(*cfg, msgs...)
430434
if err != nil {
431435
return err
432436
}
@@ -490,30 +494,45 @@ func (s *gnoNativeService) Call(ctx context.Context, req *connect.Request[api_ge
490494
return nil
491495
}
492496

493-
func convertCallRequest(req *api_gen.CallRequest) (*gnoclient.BaseTxCfg, []gnoclient.MsgCall) {
497+
func (s *gnoNativeService) convertCallRequest(req *api_gen.CallRequest) (*gnoclient.BaseTxCfg, []vm.MsgCall, error) {
494498
var callerAddress crypto.Address
495499
if req.CallerAddress != nil {
496500
callerAddress = crypto.AddressFromBytes(req.CallerAddress)
501+
} else {
502+
// Get the caller address from the active account
503+
s.lock.RLock()
504+
account := s.activeAccount
505+
s.lock.RUnlock()
506+
if account == nil {
507+
return nil, nil, api_gen.ErrCode_ErrNoActiveAccount
508+
}
509+
510+
callerAddress = account.keyInfo.GetAddress()
497511
}
498512
cfg := &gnoclient.BaseTxCfg{
499-
GasFee: req.GasFee,
500-
GasWanted: req.GasWanted,
501-
Memo: req.Memo,
502-
CallerAddress: callerAddress,
513+
GasFee: req.GasFee,
514+
GasWanted: req.GasWanted,
515+
Memo: req.Memo,
503516
}
504517

505-
msgs := make([]gnoclient.MsgCall, 0)
518+
msgs := make([]vm.MsgCall, 0)
506519

507520
for _, msg := range req.Msgs {
508-
msgs = append(msgs, gnoclient.MsgCall{
509-
PkgPath: msg.PackagePath,
510-
FuncName: msg.Fnc,
511-
Args: msg.Args,
512-
Send: msg.Send,
521+
send, err := std.ParseCoins(msg.Send)
522+
if err != nil {
523+
return nil, nil, getGrpcError(err)
524+
}
525+
526+
msgs = append(msgs, vm.MsgCall{
527+
Caller: callerAddress,
528+
PkgPath: msg.PackagePath,
529+
Func: msg.Fnc,
530+
Args: msg.Args,
531+
Send: send,
513532
})
514533
}
515534

516-
return cfg, msgs
535+
return cfg, msgs, nil
517536
}
518537

519538
func (s *gnoNativeService) Send(ctx context.Context, req *connect.Request[api_gen.SendRequest], stream *connect.ServerStream[api_gen.SendResponse]) error {
@@ -528,9 +547,12 @@ func (s *gnoNativeService) Send(ctx context.Context, req *connect.Request[api_ge
528547
}
529548
s.lock.RUnlock()
530549

531-
cfg, msgs := convertSendRequest(req.Msg)
550+
cfg, msgs, err := s.convertSendRequest(req.Msg)
551+
if err != nil {
552+
return err
553+
}
532554

533-
_, err := s.client.Send(*cfg, msgs...)
555+
_, err = s.client.Send(*cfg, msgs...)
534556
if err != nil {
535557
return getGrpcError(err)
536558
}
@@ -543,28 +565,43 @@ func (s *gnoNativeService) Send(ctx context.Context, req *connect.Request[api_ge
543565
return nil
544566
}
545567

546-
func convertSendRequest(req *api_gen.SendRequest) (*gnoclient.BaseTxCfg, []gnoclient.MsgSend) {
568+
func (s *gnoNativeService) convertSendRequest(req *api_gen.SendRequest) (*gnoclient.BaseTxCfg, []bank.MsgSend, error) {
547569
var callerAddress crypto.Address
548570
if req.CallerAddress != nil {
549571
callerAddress = crypto.AddressFromBytes(req.CallerAddress)
572+
} else {
573+
// Get the caller address from the active account
574+
s.lock.RLock()
575+
account := s.activeAccount
576+
s.lock.RUnlock()
577+
if account == nil {
578+
return nil, nil, api_gen.ErrCode_ErrNoActiveAccount
579+
}
580+
581+
callerAddress = account.keyInfo.GetAddress()
550582
}
551583
cfg := &gnoclient.BaseTxCfg{
552-
GasFee: req.GasFee,
553-
GasWanted: req.GasWanted,
554-
Memo: req.Memo,
555-
CallerAddress: callerAddress,
584+
GasFee: req.GasFee,
585+
GasWanted: req.GasWanted,
586+
Memo: req.Memo,
556587
}
557588

558-
msgs := make([]gnoclient.MsgSend, 0)
589+
msgs := make([]bank.MsgSend, 0)
559590

560591
for _, msg := range req.Msgs {
561-
msgs = append(msgs, gnoclient.MsgSend{
562-
ToAddress: crypto.AddressFromBytes(msg.ToAddress),
563-
Send: msg.Send,
592+
send, err := std.ParseCoins(msg.Send)
593+
if err != nil {
594+
return nil, nil, getGrpcError(err)
595+
}
596+
597+
msgs = append(msgs, bank.MsgSend{
598+
FromAddress: callerAddress,
599+
ToAddress: crypto.AddressFromBytes(msg.ToAddress),
600+
Amount: send,
564601
})
565602
}
566603

567-
return cfg, msgs
604+
return cfg, msgs, nil
568605
}
569606

570607
func (s *gnoNativeService) Run(ctx context.Context, req *connect.Request[api_gen.RunRequest], stream *connect.ServerStream[api_gen.RunResponse]) error {
@@ -575,7 +612,10 @@ func (s *gnoNativeService) Run(ctx context.Context, req *connect.Request[api_gen
575612
}
576613
s.lock.RUnlock()
577614

578-
cfg, msgs := convertRunRequest(req.Msg)
615+
cfg, msgs, err := s.convertRunRequest(req.Msg)
616+
if err != nil {
617+
return err
618+
}
579619

580620
bres, err := s.client.Run(*cfg, msgs...)
581621
if err != nil {
@@ -592,21 +632,35 @@ func (s *gnoNativeService) Run(ctx context.Context, req *connect.Request[api_gen
592632
return nil
593633
}
594634

595-
func convertRunRequest(req *api_gen.RunRequest) (*gnoclient.BaseTxCfg, []gnoclient.MsgRun) {
635+
func (s *gnoNativeService) convertRunRequest(req *api_gen.RunRequest) (*gnoclient.BaseTxCfg, []vm.MsgRun, error) {
596636
var callerAddress crypto.Address
597637
if req.CallerAddress != nil {
598638
callerAddress = crypto.AddressFromBytes(req.CallerAddress)
639+
} else {
640+
// Get the caller address from the active account
641+
s.lock.RLock()
642+
account := s.activeAccount
643+
s.lock.RUnlock()
644+
if account == nil {
645+
return nil, nil, api_gen.ErrCode_ErrNoActiveAccount
646+
}
647+
648+
callerAddress = account.keyInfo.GetAddress()
599649
}
600650
cfg := &gnoclient.BaseTxCfg{
601-
GasFee: req.GasFee,
602-
GasWanted: req.GasWanted,
603-
Memo: req.Memo,
604-
CallerAddress: callerAddress,
651+
GasFee: req.GasFee,
652+
GasWanted: req.GasWanted,
653+
Memo: req.Memo,
605654
}
606655

607-
msgs := make([]gnoclient.MsgRun, 0)
656+
msgs := make([]vm.MsgRun, 0)
608657

609658
for _, msg := range req.Msgs {
659+
send, err := std.ParseCoins(msg.Send)
660+
if err != nil {
661+
return nil, nil, getGrpcError(err)
662+
}
663+
610664
memPkg := &std.MemPackage{
611665
Files: []*std.MemFile{
612666
{
@@ -615,22 +669,26 @@ func convertRunRequest(req *api_gen.RunRequest) (*gnoclient.BaseTxCfg, []gnoclie
615669
},
616670
},
617671
}
618-
msgs = append(msgs, gnoclient.MsgRun{
672+
msgs = append(msgs, vm.MsgRun{
673+
Caller: callerAddress,
619674
Package: memPkg,
620-
Send: msg.Send,
675+
Send: send,
621676
})
622677
}
623678

624-
return cfg, msgs
679+
return cfg, msgs, nil
625680
}
626681

627682
func (s *gnoNativeService) MakeCallTx(ctx context.Context, req *connect.Request[api_gen.CallRequest]) (*connect.Response[api_gen.MakeTxResponse], error) {
628683
for _, msg := range req.Msg.Msgs {
629684
s.logger.Debug("MakeCallTx", zap.String("package", msg.PackagePath), zap.String("function", msg.Fnc), zap.Any("args", msg.Args))
630685
}
631686

632-
cfg, msgs := convertCallRequest(req.Msg)
633-
tx, err := s.client.MakeCallTx(*cfg, msgs...)
687+
cfg, msgs, err := s.convertCallRequest(req.Msg)
688+
if err != nil {
689+
return nil, err
690+
}
691+
tx, err := gnoclient.NewCallTx(*cfg, msgs...)
634692
if err != nil {
635693
return nil, getGrpcError(err)
636694
}
@@ -643,8 +701,11 @@ func (s *gnoNativeService) MakeCallTx(ctx context.Context, req *connect.Request[
643701
}
644702

645703
func (s *gnoNativeService) MakeSendTx(ctx context.Context, req *connect.Request[api_gen.SendRequest]) (*connect.Response[api_gen.MakeTxResponse], error) {
646-
cfg, msgs := convertSendRequest(req.Msg)
647-
tx, err := s.client.MakeSendTx(*cfg, msgs...)
704+
cfg, msgs, err := s.convertSendRequest(req.Msg)
705+
if err != nil {
706+
return nil, err
707+
}
708+
tx, err := gnoclient.NewSendTx(*cfg, msgs...)
648709
if err != nil {
649710
return nil, getGrpcError(err)
650711
}
@@ -657,8 +718,11 @@ func (s *gnoNativeService) MakeSendTx(ctx context.Context, req *connect.Request[
657718
}
658719

659720
func (s *gnoNativeService) MakeRunTx(ctx context.Context, req *connect.Request[api_gen.RunRequest]) (*connect.Response[api_gen.MakeTxResponse], error) {
660-
cfg, msgs := convertRunRequest(req.Msg)
661-
tx, err := s.client.MakeRunTx(*cfg, msgs...)
721+
cfg, msgs, err := s.convertRunRequest(req.Msg)
722+
if err != nil {
723+
return nil, err
724+
}
725+
tx, err := gnoclient.NewRunTx(*cfg, msgs...)
662726
if err != nil {
663727
return nil, getGrpcError(err)
664728
}

0 commit comments

Comments
 (0)
Please sign in to comment.