Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Batch send command for crew3 #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions x/airdrop/client/cli/batch_send.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package cli

import (
"encoding/csv"
"fmt"
"os"
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/spf13/cobra"
)

func parseCSV(path string) [][]string {
f, err := os.Open(path)
if err != nil {
panic(err)
}
defer f.Close()

csvReader := csv.NewReader(f)
records, err := csvReader.ReadAll()
if err != nil {
panic(err)
}

return records
}

// NewBatchSendCmd returns a CLI command for multi-send
func NewBatchSendCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "batch-send [csv_file] [denom] [startIndex] [threshold]",
Short: "Execute batch-send based on csv file",
Example: `teritorid tx airdrop batch-send "./airdrop.csv" utori 0 100 --from=ACCOUNT --keyring-backend=test --gas=10000000 -y --fees=100000utori`,
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

sendMsgs := []banktypes.MsgSend{}
amountRecords := parseCSV(args[0])

for _, line := range amountRecords[1:] {
addr, amountStr := line[0], line[1]
amountDec := sdk.MustNewDecFromStr(amountStr)
decimal := sdk.NewInt(1000_000) // 10^6
amount := amountDec.Mul(decimal.ToDec()).TruncateInt()

msg := banktypes.MsgSend{
FromAddress: clientCtx.FromAddress.String(),
ToAddress: addr,
Amount: sdk.Coins{sdk.NewCoin(args[1], amount)},
}
sendMsgs = append(sendMsgs, msg)
}

startIndex, err := strconv.Atoi(args[2])
if err != nil {
return err
}
threshold, err := strconv.Atoi(args[3])
if err != nil {
return err
}

msgs := []sdk.Msg{}
for index, msg := range sendMsgs {
if index < startIndex {
continue
}
msgs = append(msgs, &banktypes.MsgSend{
FromAddress: msg.FromAddress,
ToAddress: msg.ToAddress,
Amount: msg.Amount,
})
if len(msgs) >= threshold || index+1 == len(sendMsgs) {
err := tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msgs...)
if err != nil {
return err
}
fmt.Println("executed batch ", index)
msgs = []sdk.Msg{}
}
}
fmt.Println("finalized batch execution")

return nil
},
}

flags.AddTxFlagsToCmd(cmd)
return cmd
}
1 change: 1 addition & 0 deletions x/airdrop/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func GetTxCmd() *cobra.Command {
AllocateFurtherAirdropCmd(),
FetchAndRemoveAirdropCmd(),
AllocateStarsAirdropCmd(),
NewBatchSendCmd(),
)

return txCmd
Expand Down