Skip to content

Commit 94bacb0

Browse files
authored
Merge pull request #1916 from onflow/mfbz/get-system-transaction-cmd
Added get system transaction command
2 parents ae7e000 + e158277 commit 94bacb0

3 files changed

Lines changed: 119 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Flow CLI
3+
*
4+
* Copyright Flow Foundation
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package transactions
20+
21+
import (
22+
"context"
23+
24+
"github.com/spf13/cobra"
25+
26+
"github.com/onflow/flowkit/v2"
27+
"github.com/onflow/flowkit/v2/output"
28+
29+
"github.com/onflow/flow-cli/internal/command"
30+
)
31+
32+
type flagsGetSystem struct {
33+
Include []string `default:"" flag:"include" info:"Fields to include in the output. Valid values: signatures, code, payload, fee-events."`
34+
Exclude []string `default:"" flag:"exclude" info:"Fields to exclude from the output. Valid values: events."`
35+
}
36+
37+
var getSystemFlags = flagsGetSystem{}
38+
39+
var getSystemCommand = &command.Command{
40+
Cmd: &cobra.Command{
41+
Use: "get-system <block_id|latest|block_height>",
42+
Short: "Get the system transaction by block info",
43+
Example: "flow transactions get-system a1b2c3...",
44+
Args: cobra.ExactArgs(1),
45+
},
46+
Flags: &getSystemFlags,
47+
Run: getSystemTransaction,
48+
}
49+
50+
func getSystemTransaction(
51+
args []string,
52+
_ command.GlobalFlags,
53+
logger output.Logger,
54+
_ flowkit.ReaderWriter,
55+
flow flowkit.Services,
56+
) (command.Result, error) {
57+
query, err := flowkit.NewBlockQuery(args[0])
58+
if err != nil {
59+
return nil, err
60+
}
61+
62+
logger.StartProgress("Fetching Block...")
63+
defer logger.StopProgress()
64+
block, err := flow.GetBlock(context.Background(), query)
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
tx, result, err := flow.GetSystemTransaction(context.Background(), block.ID)
70+
if err != nil {
71+
return nil, err
72+
}
73+
74+
return &transactionResult{
75+
result: result,
76+
tx: tx,
77+
include: getSystemFlags.Include,
78+
exclude: getSystemFlags.Exclude,
79+
}, nil
80+
}

internal/transactions/transactions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func init() {
4949
signCommand.AddToParent(Cmd)
5050
buildCommand.AddToParent(Cmd)
5151
sendSignedCommand.AddToParent(Cmd)
52+
getSystemCommand.AddToParent(Cmd)
5253
decodeCommand.AddToParent(Cmd)
5354
}
5455

internal/transactions/transactions_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,44 @@ func Test_Get(t *testing.T) {
138138
})
139139
}
140140

141+
func Test_GetSystem(t *testing.T) {
142+
srv, _, rw := util.TestMocks(t)
143+
144+
t.Run("Success", func(t *testing.T) {
145+
inArgs := []string{"100"}
146+
147+
returnBlock := tests.NewBlock()
148+
returnBlock.Height = uint64(100)
149+
150+
srv.GetBlock.Run(func(args mock.Arguments) {
151+
assert.Equal(t, uint64(100), args.Get(1).(flowkit.BlockQuery).Height)
152+
}).Return(returnBlock, nil)
153+
154+
srv.GetSystemTransaction.Return(nil, nil, nil)
155+
156+
res, err := getSystemTransaction(inArgs, command.GlobalFlags{}, util.NoLogger, rw, srv.Mock)
157+
assert.NoError(t, err)
158+
assert.NotNil(t, res)
159+
})
160+
161+
t.Run("Fail invalid block ID", func(t *testing.T) {
162+
inArgs := []string{""}
163+
164+
returnBlock := tests.NewBlock()
165+
returnBlock.Height = uint64(100)
166+
167+
srv.GetBlock.Run(func(args mock.Arguments) {
168+
assert.Equal(t, uint64(100), args.Get(1).(flowkit.BlockQuery).Height)
169+
}).Return(returnBlock, nil)
170+
171+
srv.GetSystemTransaction.Return(nil, nil, fmt.Errorf("block not found"))
172+
173+
res, err := getSystemTransaction(inArgs, command.GlobalFlags{}, util.NoLogger, rw, srv.Mock)
174+
assert.Error(t, err)
175+
assert.Nil(t, res)
176+
})
177+
}
178+
141179
func Test_Send(t *testing.T) {
142180
srv, state, _ := util.TestMocks(t)
143181

0 commit comments

Comments
 (0)