-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
6,250 additions
and
418 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package cli | ||
|
||
import ( | ||
"bufio" | ||
"encoding/csv" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
sdkmath "cosmossdk.io/math" | ||
|
||
"github.com/Stride-Labs/stride/v22/x/airdrop/types" | ||
) | ||
|
||
// Parses an allocations CSV file consisting of allocations for various addresses | ||
// | ||
// Example Schema: | ||
// | ||
// strideXXX,10,10,20 | ||
// strideYYY,0,10,0 | ||
func ParseMultipleUserAllocations(fileName string) ([]types.RawAllocation, error) { | ||
file, err := os.Open(fileName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
|
||
reader := csv.NewReader(file) | ||
rows, err := reader.ReadAll() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
allAllocations := []types.RawAllocation{} | ||
for _, row := range rows { | ||
if len(row) < 2 { | ||
return nil, errors.New("invalid csv row") | ||
} | ||
|
||
userAddress := row[0] | ||
|
||
allocations := []sdkmath.Int{} | ||
for _, allocationString := range row[1:] { | ||
allocation, ok := sdkmath.NewIntFromString(allocationString) | ||
if !ok { | ||
return nil, fmt.Errorf("unable to parse allocation %s into sdk.Int", allocationString) | ||
} | ||
allocations = append(allocations, allocation) | ||
} | ||
|
||
allocation := types.RawAllocation{ | ||
UserAddress: userAddress, | ||
Allocations: allocations, | ||
} | ||
allAllocations = append(allAllocations, allocation) | ||
} | ||
|
||
return allAllocations, nil | ||
} | ||
|
||
// Parses a single user's allocations from a single line file with comma separate reward amounts | ||
// Ex: 10,10,20 | ||
func ParseSingleUserAllocations(fileName string) (allocations []sdkmath.Int, err error) { | ||
file, err := os.Open(fileName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
var content string | ||
|
||
if scanner.Scan() { | ||
content = scanner.Text() | ||
} | ||
|
||
if scanner.Scan() { | ||
return nil, fmt.Errorf("file %s has more than one line", fileName) | ||
} | ||
if err := scanner.Err(); err != nil { | ||
return nil, err | ||
} | ||
|
||
allocationsSplit := strings.Split(content, ",") | ||
for _, allocationString := range allocationsSplit { | ||
allocation, ok := sdkmath.NewIntFromString(allocationString) | ||
if !ok { | ||
return nil, errors.New("unable to parse reward") | ||
} | ||
allocations = append(allocations, allocation) | ||
} | ||
|
||
return allocations, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package cli_test | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
sdkmath "cosmossdk.io/math" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/Stride-Labs/stride/v22/x/airdrop/client/cli" | ||
"github.com/Stride-Labs/stride/v22/x/airdrop/types" | ||
) | ||
|
||
func ParseMultipleUserAllocations(t *testing.T) { | ||
inputCSVContents := `strideXXX,10,10,20 | ||
strideYYY,0,10,0 | ||
strideZZZ,5,100,6` | ||
|
||
expectedAllocations := []types.RawAllocation{ | ||
{ | ||
UserAddress: "strideXXX", | ||
Allocations: []sdkmath.Int{sdkmath.NewInt(10), sdkmath.NewInt(10), sdkmath.NewInt(20)}, | ||
}, | ||
{ | ||
UserAddress: "strideYYY", | ||
Allocations: []sdkmath.Int{sdkmath.NewInt(0), sdkmath.NewInt(10), sdkmath.NewInt(0)}, | ||
}, | ||
{ | ||
UserAddress: "strideZZZ", | ||
Allocations: []sdkmath.Int{sdkmath.NewInt(5), sdkmath.NewInt(100), sdkmath.NewInt(6)}, | ||
}, | ||
} | ||
|
||
// Create a temporary file | ||
tmpfile, err := os.CreateTemp("", "allocations*.csv") | ||
require.NoError(t, err) | ||
defer os.Remove(tmpfile.Name()) | ||
|
||
// Write the CSV string to the temp file | ||
_, err = tmpfile.WriteString(inputCSVContents) | ||
require.NoError(t, err) | ||
err = tmpfile.Close() | ||
require.NoError(t, err) | ||
|
||
// Call the function with the temporary file name | ||
actualAllocations, err := cli.ParseMultipleUserAllocations(tmpfile.Name()) | ||
require.NoError(t, err) | ||
|
||
// Validate the allocations match expectations | ||
require.Equal(t, expectedAllocations, actualAllocations) | ||
} | ||
|
||
func TestParseSingleUserAllocations(t *testing.T) { | ||
inputCSVContents := "10,20,30" | ||
|
||
expectedAllocations := []sdkmath.Int{sdkmath.NewInt(10), sdkmath.NewInt(20), sdkmath.NewInt(30)} | ||
|
||
// Create a temporary file | ||
tmpfile, err := os.CreateTemp("", "allocations*.csv") | ||
require.NoError(t, err) | ||
defer os.Remove(tmpfile.Name()) | ||
|
||
// Write the CSV string to the temp file | ||
_, err = tmpfile.WriteString(inputCSVContents) | ||
require.NoError(t, err) | ||
err = tmpfile.Close() | ||
require.NoError(t, err) | ||
|
||
// Call the function with the temporary file name | ||
actualAllocations, err := cli.ParseSingleUserAllocations(tmpfile.Name()) | ||
require.NoError(t, err) | ||
|
||
// Validate the allocations match expectations | ||
require.Equal(t, expectedAllocations, actualAllocations) | ||
} |
Oops, something went wrong.