Skip to content

Commit

Permalink
support substrate batch calls
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioBassem committed Sep 23, 2024
1 parent de1fcda commit f363a94
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
46 changes: 46 additions & 0 deletions griddriver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,52 @@ func main() {
},
Action: rmbDecorator(rmbCall),
},
{
Name: "batch-create-contract",
Description: "makes a batch create contract call to the tfchain",
Flags: []cli.Flag{
cli.StringFlag{
Name: "mnemonics",
Value: "",
Usage: "user mnemonics",
Required: true,
},
cli.StringFlag{
Name: "substrate",
Value: "wss://tfchain.grid.tf/ws",
Usage: "substrate URL",
},
cli.StringFlag{
Name: "contracts-data",
Usage: "json encoding of list of substrate BatchCreateContractData objects",
Required: true,
},
},
Action: substrateDecorator(batchAllCreateContract),
},
{
Name: "batch-cancel-contract",
Description: "makes a batch cancel contract call to the tfchain",
Flags: []cli.Flag{
cli.StringFlag{
Name: "mnemonics",
Value: "",
Usage: "user mnemonics",
Required: true,
},
cli.StringFlag{
Name: "substrate",
Value: "wss://tfchain.grid.tf/ws",
Usage: "substrate URL",
},
cli.StringFlag{
Name: "contract-ids",
Usage: "json encoding of list of the contract ids to delete",
Required: true,
},
},
Action: substrateDecorator(batchCancelContract),
},
},
}

Expand Down
36 changes: 36 additions & 0 deletions griddriver/substrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/hex"
"encoding/json"
"fmt"

"github.com/pkg/errors"
Expand Down Expand Up @@ -149,3 +150,38 @@ func signDeployment(ctx *cli.Context, sub *substrate.Substrate, identity substra
sig := hex.EncodeToString(signatureBytes)
return sig, nil
}

func batchAllCreateContract(ctx *cli.Context, sub *substrate.Substrate, identity substrate.Identity) (interface{}, error) {
data := []byte(ctx.String("contracts-data"))

contractData := []substrate.BatchCreateContractData{}
if err := json.Unmarshal(data, &contractData); err != nil {
return nil, fmt.Errorf("failed to decode contract data: %w", err)
}

contractIds, err := sub.BatchAllCreateContract(identity, contractData)
if err != nil {
return nil, fmt.Errorf("failed to create contracts: %w", err)
}

ret, err := json.Marshal(contractIds)
if err != nil {
return nil, fmt.Errorf("failed to encode contract ids: %w", err)
}

return ret, nil
}

func batchCancelContract(ctx *cli.Context, sub *substrate.Substrate, identity substrate.Identity) (interface{}, error) {
data := []byte(ctx.String("contract-ids"))
contractIDs := []uint64{}
if err := json.Unmarshal(data, &contractIDs); err != nil {
return nil, fmt.Errorf("failed to decode contract ids: %w", err)
}

if err := sub.BatchCancelContract(identity, contractIDs); err != nil {
return nil, fmt.Errorf("failed to cancel contracts: %w", err)
}

return nil, nil
}

0 comments on commit f363a94

Please sign in to comment.