Skip to content

Commit 7a1c804

Browse files
authored
Merge pull request #680 from onflow/improvement/flowkit-tx
Transaction API refactor
2 parents e71bb17 + ca1bc31 commit 7a1c804

18 files changed

Lines changed: 526 additions & 236 deletions

File tree

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ require (
77
github.com/go-git/go-git/v5 v5.4.2
88
github.com/manifoldco/promptui v0.9.0
99
github.com/onflow/cadence v0.28.0
10-
github.com/onflow/cadence-tools/languageserver v0.2.0
10+
github.com/onflow/cadence-tools/languageserver v0.3.1
1111
github.com/onflow/cadence-tools/test v0.2.1-0.20221012182900-f46efb551c55
1212
github.com/onflow/fcl-dev-wallet v0.5.3
13-
github.com/onflow/flow-cli/pkg/flowkit v0.0.0-20221012181819-8d43a4be0028
13+
github.com/onflow/flow-cli/pkg/flowkit v0.0.0-20221123090817-587405125c1f
1414
github.com/onflow/flow-emulator v0.38.1
1515
github.com/onflow/flow-go-sdk v0.29.1
1616
github.com/onflowser/flowser/v2 v2.0.8-beta

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,8 @@ github.com/onflow/cadence v0.15.0/go.mod h1:KMzDF6cIv6nb5PJW9aITaqazbmJX8MMeibFc
673673
github.com/onflow/cadence v0.20.1/go.mod h1:7mzUvPZUIJztIbr9eTvs+fQjWWHTF8veC+yk4ihcNIA=
674674
github.com/onflow/cadence v0.28.0 h1:18A1V9xqGewibEhuzqBNEoNvqG6OwVqHg7gKu3UliaM=
675675
github.com/onflow/cadence v0.28.0/go.mod h1:h+SbY8RNl6Q6sT6l/2cvpUZUJNCbzJya+3Bkwe/0YwY=
676-
github.com/onflow/cadence-tools/languageserver v0.2.0 h1:DwBBk2zlbjjbuftEU2Tl/2T2GKU18a2/+BiS8Gawzxo=
677-
github.com/onflow/cadence-tools/languageserver v0.2.0/go.mod h1:qCRa+S7sQ3cLIEgUu1d21Yft4oWEZAKadJ19ID4etzw=
676+
github.com/onflow/cadence-tools/languageserver v0.3.1 h1:I9au/BQgpcOL+P4dm60zbKP01la/DPt0L5mn9JO1yIE=
677+
github.com/onflow/cadence-tools/languageserver v0.3.1/go.mod h1:Jo1JI07xkrl/WlCRvuMfp+x1VyUCsXEtHXGsCnBC9/E=
678678
github.com/onflow/cadence-tools/lint v0.2.0 h1:i+Mvht3hQQNNxe4jV8BRTr97wFWlJSRGTOM03BSyloQ=
679679
github.com/onflow/cadence-tools/lint v0.2.0/go.mod h1:r4kEtmZe4XuKcx/JXEU027i6+cpMtRc0GaLCAE6EvYs=
680680
github.com/onflow/cadence-tools/test v0.2.1-0.20221012182900-f46efb551c55 h1:l/JqX4qnukAf2rCfZ0afPvK13eNPeL6gbv2GAweuVuE=

internal/accounts/contract-add.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,9 @@ func addContract(
5757
srv *services.Services,
5858
state *flowkit.State,
5959
) (command.Result, error) {
60-
filename := ""
61-
if len(args) == 1 {
62-
filename = args[0]
63-
} else {
64-
fmt.Println("⚠️Deprecation notice: using name argument in add contract " +
65-
"command will be deprecated soon.")
60+
filename := args[0]
61+
if len(args) > 1 {
62+
fmt.Println("⚠️Deprecation notice: using name argument in add contract command will be deprecated soon.")
6663
filename = args[1]
6764
}
6865

@@ -90,10 +87,12 @@ func addContract(
9087
account, err := srv.Accounts.AddContract(
9188
to,
9289
&services.Contract{
93-
Source: code,
94-
Args: contractArgs,
95-
Filename: filename,
96-
Network: globalFlags.Network,
90+
Script: &services.Script{
91+
Code: code,
92+
Args: contractArgs,
93+
Filename: filename,
94+
},
95+
Network: globalFlags.Network,
9796
},
9897
false,
9998
)

internal/accounts/contract-update.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,9 @@ func updateContract(
5757
srv *services.Services,
5858
state *flowkit.State,
5959
) (command.Result, error) {
60-
filename := ""
61-
62-
if len(args) == 1 {
63-
filename = args[0]
64-
} else {
65-
fmt.Println("⚠️Deprecation notice: using name argument in update contract " +
66-
"command will be deprecated soon.")
60+
filename := args[0]
61+
if len(args) > 1 {
62+
fmt.Println("⚠️Deprecation notice: using name argument in add contract command will be deprecated soon.")
6763
filename = args[1]
6864
}
6965

@@ -91,10 +87,12 @@ func updateContract(
9187
account, err := srv.Accounts.AddContract(
9288
to,
9389
&services.Contract{
94-
Source: code,
95-
Args: contractArgs,
96-
Filename: filename,
97-
Network: globalFlags.Network,
90+
Script: &services.Script{
91+
Code: code,
92+
Args: contractArgs,
93+
Filename: filename,
94+
},
95+
Network: globalFlags.Network,
9896
},
9997
true,
10098
)

internal/scripts/execute.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func execute(
5050
args []string,
5151
readerWriter flowkit.ReaderWriter,
5252
globalFlags command.GlobalFlags,
53-
services *services.Services,
53+
srv *services.Services,
5454
) (command.Result, error) {
5555
filename := args[0]
5656

@@ -70,10 +70,12 @@ func execute(
7070
return nil, fmt.Errorf("error parsing script arguments: %w", err)
7171
}
7272

73-
value, err := services.Scripts.Execute(
74-
code,
75-
scriptArgs,
76-
filename,
73+
value, err := srv.Scripts.Execute(
74+
&services.Script{
75+
Code: code,
76+
Args: scriptArgs,
77+
Filename: filename,
78+
},
7779
globalFlags.Network,
7880
)
7981
if err != nil {

internal/tools/flowser.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ import (
2424
"os"
2525
"runtime"
2626

27+
"github.com/onflowser/flowser/v2/pkg/flowser"
28+
"github.com/spf13/cobra"
29+
2730
"github.com/onflow/flow-cli/internal/command"
2831
"github.com/onflow/flow-cli/internal/settings"
2932
"github.com/onflow/flow-cli/pkg/flowkit"
3033
"github.com/onflow/flow-cli/pkg/flowkit/config"
3134
"github.com/onflow/flow-cli/pkg/flowkit/output"
3235
"github.com/onflow/flow-cli/pkg/flowkit/services"
33-
"github.com/onflowser/flowser/v2/pkg/flowser"
34-
"github.com/spf13/cobra"
3536
)
3637

3738
type FlagsFlowser struct{}

internal/transactions/build.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ package transactions
2121
import (
2222
"fmt"
2323

24+
"github.com/onflow/flow-cli/pkg/flowkit/output"
25+
2426
"github.com/onflow/cadence"
2527
"github.com/onflow/flow-go-sdk"
2628
"github.com/spf13/cobra"
@@ -57,7 +59,7 @@ func build(
5759
args []string,
5860
readerWriter flowkit.ReaderWriter,
5961
globalFlags command.GlobalFlags,
60-
services *services.Services,
62+
srv *services.Services,
6163
state *flowkit.State,
6264
) (command.Result, error) {
6365
proposer, err := getAddress(buildFlags.Proposer, state)
@@ -92,29 +94,31 @@ func build(
9294
} else {
9395
transactionArgs, err = flowkit.ParseArgumentsWithoutType(filename, code, args[1:])
9496
}
95-
9697
if err != nil {
9798
return nil, fmt.Errorf("error parsing transaction arguments: %w", err)
9899
}
99100

100-
build, err := services.Transactions.Build(
101-
proposer,
102-
authorizers,
103-
payer,
101+
tx, err := srv.Transactions.Build(
102+
services.NewTransactionAddresses(proposer, payer, authorizers),
104103
buildFlags.ProposerKeyIndex,
105-
code,
106-
filename,
104+
&services.Script{
105+
Code: code,
106+
Args: transactionArgs,
107+
Filename: filename,
108+
},
107109
buildFlags.GasLimit,
108-
transactionArgs,
109110
globalFlags.Network,
110-
globalFlags.Yes,
111111
)
112112
if err != nil {
113113
return nil, err
114114
}
115115

116+
if !globalFlags.Yes && !output.ApproveTransactionForBuildingPrompt(tx) {
117+
return nil, fmt.Errorf("transaction was not approved")
118+
}
119+
116120
return &TransactionResult{
117-
tx: build.FlowTransaction(),
121+
tx: tx.FlowTransaction(),
118122
include: []string{"code", "payload", "signatures"},
119123
}, nil
120124
}

internal/transactions/send-signed.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ package transactions
2121
import (
2222
"fmt"
2323

24+
"github.com/onflow/flow-cli/pkg/flowkit/output"
25+
2426
"github.com/onflow/flow-cli/pkg/flowkit"
2527

2628
"github.com/spf13/cobra"
@@ -61,14 +63,23 @@ func sendSigned(
6163
return nil, fmt.Errorf("error loading transaction payload: %w", err)
6264
}
6365

64-
tx, result, err := services.Transactions.SendSigned(code, globalFlags.Yes)
66+
tx, err := flowkit.NewTransactionFromPayload(code)
67+
if err != nil {
68+
return nil, err
69+
}
70+
71+
if !globalFlags.Yes && !output.ApproveTransactionForSendingPrompt(tx) {
72+
return nil, fmt.Errorf("transaction was not approved for sending")
73+
}
74+
75+
sentTx, result, err := services.Transactions.SendSigned(tx)
6576
if err != nil {
6677
return nil, err
6778
}
6879

6980
return &TransactionResult{
7081
result: result,
71-
tx: tx,
82+
tx: sentTx,
7283
include: sendSignedFlags.Include,
7384
exclude: sendSignedFlags.Exclude,
7485
}, nil

internal/transactions/send.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func send(
5757
args []string,
5858
readerWriter flowkit.ReaderWriter,
5959
globalFlags command.GlobalFlags,
60-
services *services.Services,
60+
srv *services.Services,
6161
state *flowkit.State,
6262
) (command.Result, error) {
6363
codeFilename := args[0]
@@ -82,17 +82,18 @@ func send(
8282
} else {
8383
transactionArgs, err = flowkit.ParseArgumentsWithoutType(codeFilename, code, args[1:])
8484
}
85-
8685
if err != nil {
8786
return nil, fmt.Errorf("error parsing transaction arguments: %w", err)
8887
}
8988

90-
tx, result, err := services.Transactions.Send(
91-
signer,
92-
code,
93-
codeFilename,
89+
tx, result, err := srv.Transactions.Send(
90+
services.NewSingleTransactionAccount(signer),
91+
&services.Script{
92+
Code: code,
93+
Args: transactionArgs,
94+
Filename: codeFilename,
95+
},
9496
sendFlags.GasLimit,
95-
transactionArgs,
9697
globalFlags.Network,
9798
)
9899

internal/transactions/sign.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,15 @@ func sign(
105105
})
106106

107107
for _, signer := range signers {
108-
signed, err = services.Transactions.Sign(signer, payload, globalFlags.Yes)
108+
if !globalFlags.Yes && !output.ApproveTransactionForSigningPrompt(tx) {
109+
return nil, fmt.Errorf("transaction was not approved for signing")
110+
}
111+
112+
signed, err = services.Transactions.Sign(signer, payload)
109113
if err != nil {
110114
return nil, err
111115
}
116+
112117
payload = []byte(hex.EncodeToString(signed.FlowTransaction().Encode()))
113118
}
114119

0 commit comments

Comments
 (0)