Skip to content

Commit

Permalink
Merge pull request #3921 from TrueBlocks/feature/send
Browse files Browse the repository at this point in the history
First working version of tx send highly suspect
  • Loading branch information
tjayrush authored Dec 14, 2024
2 parents 6241f5a + 96f333a commit cc1102e
Show file tree
Hide file tree
Showing 41 changed files with 195 additions and 132 deletions.
2 changes: 1 addition & 1 deletion docs
2 changes: 1 addition & 1 deletion examples
2 changes: 1 addition & 1 deletion sdk
18 changes: 12 additions & 6 deletions src/apps/chifra/cmd/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ Notes:
- If the queried node does not store historical state, the results are undefined.
- Special blocks are detailed under chifra when --list.
- Balance is the default mode. To select a single mode use none first, followed by that mode.
- Valid parameters for --call include Solidity-like syntax: balanceOf(0x316b...183d), a four-byte followed by parameters: 0x70a08231(0x316b...183d), or encoded input data.
- Valid parameters for --calldata include Solidity-like syntax: balanceOf(0x316b...), a four-byte followed by parameters: 0x70a08231(0x316b...), or encoded input data.
- You may specify multiple parts on a single line.
- In the --call string, you may separate multiple calls with a colon.
- Your use of the unaudited --send option legally absolves, in any jurisdiction, TrueBlocks, LLC or any associated parties from liability or loss related to such use.
- The --send option does not validate its input before sending your transaction to the network. If you provide invalid data, you may lose your funds. Be warned.`
- The --send option does not validate its input before sending your transaction to the network. If you provide invalid data, you may lose your funds. Be warned.
- As of version 4.0.0, use --call --calldata <cmd> to provide your command.
- --calldata may be one or more solidity calls, four-byte plus parameters, or encoded call data strings.`

func init() {
var capabilities caps.Capability // capabilities for chifra state
Expand All @@ -71,10 +73,14 @@ func init() {
One or more of [ balance | nonce | code | proxy | deployed | accttype | some | all ]`)
stateCmd.Flags().BoolVarP(&statePkg.GetOptions().Changes, "changes", "c", false, `only report a balance when it changes from one block to the next`)
stateCmd.Flags().BoolVarP(&statePkg.GetOptions().NoZero, "no_zero", "z", false, `suppress the display of zero balance accounts`)
stateCmd.Flags().StringVarP(&statePkg.GetOptions().Call, "call", "l", "", `write-only call to a smart contract with one or more solidity calls, four-byte plus parameters, or encoded call data strings`)
stateCmd.Flags().StringVarP(&statePkg.GetOptions().Send, "send", "s", "", `send a transaction to a smart contract using a solidity function, a four-byte plus parameters, or an encoded call data string`)
stateCmd.Flags().BoolVarP(&statePkg.GetOptions().Articulate, "articulate", "a", false, `for the --call option only, articulate the retrieved data if ABIs can be found`)
stateCmd.Flags().StringVarP(&statePkg.GetOptions().ProxyFor, "proxy_for", "r", "", `for the --call option only, redirects calls to this implementation`)
stateCmd.Flags().BoolVarP(&statePkg.GetOptions().Call, "call", "l", false, `write-only call (a query) to a smart contract`)
stateCmd.Flags().BoolVarP(&statePkg.GetOptions().Send, "send", "s", false, `writes a transaction to an address (see docs for more information) (hidden)`)
stateCmd.Flags().StringVarP(&statePkg.GetOptions().Calldata, "calldata", "d", "", `for commands (--call or --send), provides the call data (in various forms) for the command (may be empty for --send)`)
stateCmd.Flags().BoolVarP(&statePkg.GetOptions().Articulate, "articulate", "a", false, `for commands only, articulate the retrieved data if ABIs can be found`)
stateCmd.Flags().StringVarP(&statePkg.GetOptions().ProxyFor, "proxy_for", "r", "", `for commands only, redirects calls to this implementation`)
if os.Getenv("TEST_MODE") != "true" {
_ = stateCmd.Flags().MarkHidden("send")
}
globals.InitGlobals("state", stateCmd, &statePkg.GetOptions().Globals, capabilities)

stateCmd.SetUsageTemplate(UsageWithNotes(notesState))
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/abis/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func abisFinishParse(args []string) *AbisOptions {
if len(args) > 0 {
tmp := []string{}
for _, arg := range args {
if value := dedup[arg]; value == 0 {
if cnt := dedup[arg]; cnt == 0 {
tmp = append(tmp, arg)
}
dedup[arg]++
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/blocks/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func blocksFinishParse(args []string) *BlocksOptions {
if len(args) > 0 {
tmp := []string{}
for _, arg := range args {
if value := dedup[arg]; value == 0 {
if cnt := dedup[arg]; cnt == 0 {
tmp = append(tmp, arg)
}
dedup[arg]++
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/chunks/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func chunksFinishParse(args []string) *ChunksOptions {
if len(args) > 0 {
tmp := []string{}
for _, arg := range args {
if value := dedup[arg]; value == 0 {
if cnt := dedup[arg]; cnt == 0 {
tmp = append(tmp, arg)
}
dedup[arg]++
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func configFinishParse(args []string) *ConfigOptions {
if len(args) > 0 {
tmp := []string{}
for _, arg := range args {
if value := dedup[arg]; value == 0 {
if cnt := dedup[arg]; cnt == 0 {
tmp = append(tmp, arg)
}
dedup[arg]++
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/daemon/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func daemonFinishParse(args []string) *DaemonOptions {
if len(args) > 0 {
tmp := []string{}
for _, arg := range args {
if value := dedup[arg]; value == 0 {
if cnt := dedup[arg]; cnt == 0 {
tmp = append(tmp, arg)
}
dedup[arg]++
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/explore/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func exploreFinishParse(args []string) *ExploreOptions {
if len(args) > 0 {
tmp := []string{}
for _, arg := range args {
if value := dedup[arg]; value == 0 {
if cnt := dedup[arg]; cnt == 0 {
tmp = append(tmp, arg)
}
dedup[arg]++
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/export/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func exportFinishParse(args []string) *ExportOptions {
if len(args) > 0 {
tmp := []string{}
for _, arg := range args {
if value := dedup[arg]; value == 0 {
if cnt := dedup[arg]; cnt == 0 {
tmp = append(tmp, arg)
}
dedup[arg]++
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/init/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func initFinishParse(args []string) *InitOptions {
if len(args) > 0 {
tmp := []string{}
for _, arg := range args {
if value := dedup[arg]; value == 0 {
if cnt := dedup[arg]; cnt == 0 {
tmp = append(tmp, arg)
}
dedup[arg]++
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/list/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func listFinishParse(args []string) *ListOptions {
if len(args) > 0 {
tmp := []string{}
for _, arg := range args {
if value := dedup[arg]; value == 0 {
if cnt := dedup[arg]; cnt == 0 {
tmp = append(tmp, arg)
}
dedup[arg]++
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/logs/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func logsFinishParse(args []string) *LogsOptions {
if len(args) > 0 {
tmp := []string{}
for _, arg := range args {
if value := dedup[arg]; value == 0 {
if cnt := dedup[arg]; cnt == 0 {
tmp = append(tmp, arg)
}
dedup[arg]++
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/monitors/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func monitorsFinishParse(args []string) *MonitorsOptions {
if len(args) > 0 {
tmp := []string{}
for _, arg := range args {
if value := dedup[arg]; value == 0 {
if cnt := dedup[arg]; cnt == 0 {
tmp = append(tmp, arg)
}
dedup[arg]++
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/names/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func namesFinishParse(args []string) *NamesOptions {
if len(args) > 0 {
tmp := []string{}
for _, arg := range args {
if value := dedup[arg]; value == 0 {
if cnt := dedup[arg]; cnt == 0 {
tmp = append(tmp, arg)
}
dedup[arg]++
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/receipts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func receiptsFinishParse(args []string) *ReceiptsOptions {
if len(args) > 0 {
tmp := []string{}
for _, arg := range args {
if value := dedup[arg]; value == 0 {
if cnt := dedup[arg]; cnt == 0 {
tmp = append(tmp, arg)
}
dedup[arg]++
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/scrape/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func scrapeFinishParse(args []string) *ScrapeOptions {
if len(args) > 0 {
tmp := []string{}
for _, arg := range args {
if value := dedup[arg]; value == 0 {
if cnt := dedup[arg]; cnt == 0 {
tmp = append(tmp, arg)
}
dedup[arg]++
Expand Down
2 changes: 1 addition & 1 deletion src/apps/chifra/internal/slurp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func slurpFinishParse(args []string) *SlurpOptions {
if len(args) > 0 {
tmp := []string{}
for _, arg := range args {
if value := dedup[arg]; value == 0 {
if cnt := dedup[arg]; cnt == 0 {
tmp = append(tmp, arg)
}
dedup[arg]++
Expand Down
12 changes: 7 additions & 5 deletions src/apps/chifra/internal/state/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ Flags:
One or more of [ balance | nonce | code | proxy | deployed | accttype | some | all ]
-c, --changes only report a balance when it changes from one block to the next
-z, --no_zero suppress the display of zero balance accounts
-l, --call string write-only call to a smart contract with one or more solidity calls, four-byte plus parameters, or encoded call data strings
-s, --send string send a transaction to a smart contract using a solidity function, a four-byte plus parameters, or an encoded call data string
-a, --articulate for the --call option only, articulate the retrieved data if ABIs can be found
-r, --proxy_for string for the --call option only, redirects calls to this implementation
-l, --call write-only call (a query) to a smart contract
-d, --calldata string for commands (--call or --send), provides the call data (in various forms) for the command (may be empty for --send)
-a, --articulate for commands only, articulate the retrieved data if ABIs can be found
-r, --proxy_for string for commands only, redirects calls to this implementation
-H, --ether specify value in ether
-o, --cache force the results of the query into the cache
-D, --decache removes related items from the cache
Expand All @@ -40,11 +40,13 @@ Notes:
- If the queried node does not store historical state, the results are undefined.
- Special blocks are detailed under chifra when --list.
- Balance is the default mode. To select a single mode use none first, followed by that mode.
- Valid parameters for --call include Solidity-like syntax: balanceOf(0x316b...183d), a four-byte followed by parameters: 0x70a08231(0x316b...183d), or encoded input data.
- Valid parameters for --calldata include Solidity-like syntax: balanceOf(0x316b...), a four-byte followed by parameters: 0x70a08231(0x316b...), or encoded input data.
- You may specify multiple parts on a single line.
- In the --call string, you may separate multiple calls with a colon.
- Your use of the unaudited --send option legally absolves, in any jurisdiction, TrueBlocks, LLC or any associated parties from liability or loss related to such use.
- The --send option does not validate its input before sending your transaction to the network. If you provide invalid data, you may lose your funds. Be warned.
- As of version 4.0.0, use --call --calldata <cmd> to provide your command.
- --calldata may be one or more solidity calls, four-byte plus parameters, or encoded call data strings.
```

Data models produced by this tool:
Expand Down
9 changes: 4 additions & 5 deletions src/apps/chifra/internal/state/handle_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (opts *StateOptions) HandleCall(rCtx *output.RenderCtx) error {
return articulate.ArticulateFunction(function, "", str[2:])
}

callAddress := opts.GetCallAddress()
callAddress := opts.GetAddressOrProxy()
fetchData := func(modelChan chan types.Modeler, errorChan chan error) {
apps, _, err := identifiers.IdsToApps(chain, opts.BlockIds)
if err != nil {
Expand Down Expand Up @@ -65,7 +65,7 @@ func (opts *StateOptions) HandleCall(rCtx *output.RenderCtx) error {
for _, c := range opts.Calls {
if contractCall, _, err := call.NewContractCall(opts.Conn, callAddress, c); err != nil {
delete(thisMap, app)
return fmt.Errorf("the --call value provided (%s) was not found: %s", c, err)
return fmt.Errorf("the --calldata value provided (%s) was not found: %s", c, err)

} else {
contractCall.BlockNumber = bn
Expand Down Expand Up @@ -117,9 +117,8 @@ func (opts *StateOptions) HandleCall(rCtx *output.RenderCtx) error {
return output.StreamMany(rCtx, fetchData, opts.Globals.OutputOptsWithExtra(extraOpts))
}

func (opts *StateOptions) GetCallAddress() base.Address {
// Note that the validator precludes the possibility of having more than one address
// if the call option is present.
func (opts *StateOptions) GetAddressOrProxy() base.Address {
// Note that validtor makes sure there's exactly one address for --call or --send.
callAddress := base.HexToAddress(opts.Addrs[0])
proxy := base.HexToAddress(opts.ProxyFor)
if !proxy.IsZero() {
Expand Down
4 changes: 2 additions & 2 deletions src/apps/chifra/internal/state/handle_decache.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ func (opts *StateOptions) getItemsToRemove() ([]cache.Locator, error) {

for _, c := range opts.Calls {
if len(c) > 0 {
callAddress := opts.GetCallAddress()
callAddress := opts.GetAddressOrProxy()
if contractCall, _, err := call.NewContractCall(opts.Conn, callAddress, c); err != nil {
wrapped := fmt.Errorf("the --call value provided (%s) was not found: %s", c, err)
wrapped := fmt.Errorf("the --calldata value provided (%s) was not found: %s", c, err)
return []cache.Locator{}, wrapped

} else {
Expand Down
11 changes: 8 additions & 3 deletions src/apps/chifra/internal/state/handle_send.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
package statePkg

import (
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
)

func (opts *StateOptions) HandleSend(rCtx *output.RenderCtx) error {
logger.Info("HandleSend was called.")
return nil
fetchData := func(modelChan chan types.Modeler, errorChan chan error) {
modelChan <- &types.Result{
Name: "I am not right",
}
}

return output.StreamMany(rCtx, fetchData, opts.Globals.OutputOpts())
}
32 changes: 18 additions & 14 deletions src/apps/chifra/internal/state/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ type StateOptions struct {
Parts []string `json:"parts,omitempty"` // Control which state to export
Changes bool `json:"changes,omitempty"` // Only report a balance when it changes from one block to the next
NoZero bool `json:"noZero,omitempty"` // Suppress the display of zero balance accounts
Call string `json:"call,omitempty"` // Write-only call to a smart contract with one or more solidity calls, four-byte plus parameters, or encoded call data strings
Send string `json:"send,omitempty"` // Send a transaction to a smart contract using a solidity function, a four-byte plus parameters, or an encoded call data string
Articulate bool `json:"articulate,omitempty"` // For the --call option only, articulate the retrieved data if ABIs can be found
ProxyFor string `json:"proxyFor,omitempty"` // For the --call option only, redirects calls to this implementation
Call bool `json:"call,omitempty"` // Write-only call (a query) to a smart contract
Send bool `json:"send,omitempty"` // Writes a transaction to an address (see docs for more information)
Calldata string `json:"calldata,omitempty"` // For commands (--call or --send), provides the call data (in various forms) for the command (may be empty for --send)
Articulate bool `json:"articulate,omitempty"` // For commands only, articulate the retrieved data if ABIs can be found
ProxyFor string `json:"proxyFor,omitempty"` // For commands only, redirects calls to this implementation
Globals globals.GlobalOptions `json:"globals,omitempty"` // The global options
Conn *rpc.Connection `json:"conn,omitempty"` // The connection to the RPC server
BadFlag error `json:"badFlag,omitempty"` // An error flag if needed
Expand All @@ -56,8 +57,9 @@ func (opts *StateOptions) testLog() {
logger.TestLog(len(opts.Parts) > 0, "Parts: ", opts.Parts)
logger.TestLog(opts.Changes, "Changes: ", opts.Changes)
logger.TestLog(opts.NoZero, "NoZero: ", opts.NoZero)
logger.TestLog(len(opts.Call) > 0, "Call: ", opts.Call)
logger.TestLog(len(opts.Send) > 0, "Send: ", opts.Send)
logger.TestLog(opts.Call, "Call: ", opts.Call)
logger.TestLog(opts.Send, "Send: ", opts.Send)
logger.TestLog(len(opts.Calldata) > 0, "Calldata: ", opts.Calldata)
logger.TestLog(opts.Articulate, "Articulate: ", opts.Articulate)
logger.TestLog(len(opts.ProxyFor) > 0, "ProxyFor: ", opts.ProxyFor)
opts.Conn.TestLog(opts.getCaches())
Expand Down Expand Up @@ -105,9 +107,11 @@ func StateFinishParseInternal(w io.Writer, values url.Values) *StateOptions {
case "noZero":
opts.NoZero = true
case "call":
opts.Call = value[0]
opts.Call = true
case "send":
opts.Send = value[0]
opts.Send = true
case "calldata":
opts.Calldata = value[0]
case "articulate":
opts.Articulate = true
case "proxyFor":
Expand All @@ -124,8 +128,8 @@ func StateFinishParseInternal(w io.Writer, values url.Values) *StateOptions {
opts.Conn = opts.Globals.FinishParseApi(w, values, opts.getCaches())

// EXISTING_CODE
opts.Call = strings.Replace(strings.Trim(opts.Call, "'"), "'", "\"", -1)
opts.Calls = strings.Split(opts.Call, ":")
opts.Calldata = strings.Replace(strings.Trim(opts.Calldata, "'"), "'", "\"", -1)
opts.Calls = strings.Split(opts.Calldata, ":")
if len(opts.Blocks) == 0 {
if opts.Globals.TestMode {
opts.Blocks = []string{"17000000"}
Expand All @@ -147,7 +151,7 @@ func stateFinishParse(args []string) *StateOptions {
if len(args) > 0 {
tmp := []string{}
for _, arg := range args {
if value := dedup[arg]; value == 0 {
if cnt := dedup[arg]; cnt == 0 {
tmp = append(tmp, arg)
}
dedup[arg]++
Expand All @@ -167,8 +171,8 @@ func stateFinishParse(args []string) *StateOptions {
opts.Blocks = append(opts.Blocks, arg)
}
}
opts.Call = strings.Replace(strings.Trim(opts.Call, "'"), "'", "\"", -1)
opts.Calls = strings.Split(opts.Call, ":")
opts.Calldata = strings.Replace(strings.Trim(opts.Calldata, "'"), "'", "\"", -1)
opts.Calls = strings.Split(opts.Calldata, ":")
if len(opts.Blocks) == 0 {
if opts.Globals.TestMode {
opts.Blocks = []string{"17000000"}
Expand Down Expand Up @@ -218,7 +222,7 @@ func (opts *StateOptions) getCaches() (caches map[walk.CacheType]bool) {
// EXISTING_CODE
caches = map[walk.CacheType]bool{
walk.Cache_State: true,
walk.Cache_Results: len(opts.Call) > 0,
walk.Cache_Results: opts.Call || opts.Send,
}
// EXISTING_CODE
return
Expand Down
4 changes: 2 additions & 2 deletions src/apps/chifra/internal/state/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ func (opts *StateOptions) StateInternal(rCtx *output.RenderCtx) error {
// EXISTING_CODE
if opts.Globals.Decache {
err = opts.HandleDecache(rCtx)
} else if len(opts.Call) > 0 {
} else if opts.Call {
err = opts.HandleCall(rCtx)
} else if len(opts.Send) > 0 {
} else if opts.Send {
err = opts.HandleSend(rCtx)
} else {
err = opts.HandleShow(rCtx)
Expand Down
Loading

0 comments on commit cc1102e

Please sign in to comment.