From f363a9406d0af5635a016129a89da556eed94a13 Mon Sep 17 00:00:00 2001 From: mariobassem Date: Mon, 23 Sep 2024 11:33:24 +0300 Subject: [PATCH] support substrate batch calls --- griddriver/main.go | 46 +++++++++++++++++++++++++++++++++++++++++ griddriver/substrate.go | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/griddriver/main.go b/griddriver/main.go index 4823047b0..606e40daa 100644 --- a/griddriver/main.go +++ b/griddriver/main.go @@ -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), + }, }, } diff --git a/griddriver/substrate.go b/griddriver/substrate.go index d39fb5aa7..b17c8d854 100644 --- a/griddriver/substrate.go +++ b/griddriver/substrate.go @@ -2,6 +2,7 @@ package main import ( "encoding/hex" + "encoding/json" "fmt" "github.com/pkg/errors" @@ -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 +}