Compare commits
2 Commits
master
...
update-ali
Author | SHA1 | Date | |
---|---|---|---|
b497dde1e8 | |||
cedd55fd31 |
2
go.mod
2
go.mod
@ -5,7 +5,7 @@ go 1.23.4
|
|||||||
require (
|
require (
|
||||||
git.defalsify.org/vise.git v0.3.2-0.20250528124150-03bf7bfc1b66
|
git.defalsify.org/vise.git v0.3.2-0.20250528124150-03bf7bfc1b66
|
||||||
git.grassecon.net/grassrootseconomics/common v0.9.0-beta.1.0.20250417111317-2953f4c2f32e
|
git.grassecon.net/grassrootseconomics/common v0.9.0-beta.1.0.20250417111317-2953f4c2f32e
|
||||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250624090744-339ba854c997
|
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250626065419-57ee409f9629
|
||||||
git.grassecon.net/grassrootseconomics/visedriver v0.9.0-beta.2.0.20250408094335-e2d1f65bb306
|
git.grassecon.net/grassrootseconomics/visedriver v0.9.0-beta.2.0.20250408094335-e2d1f65bb306
|
||||||
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250129070628-5a539172c694
|
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250129070628-5a539172c694
|
||||||
github.com/alecthomas/assert/v2 v2.2.2
|
github.com/alecthomas/assert/v2 v2.2.2
|
||||||
|
2
go.sum
2
go.sum
@ -12,6 +12,8 @@ git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250624074830-
|
|||||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250624074830-5aa032400c12/go.mod h1:y/vsN8UO0wSxZk3gv0y5df4RPKMJI6TIxjVcVCPF8T8=
|
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250624074830-5aa032400c12/go.mod h1:y/vsN8UO0wSxZk3gv0y5df4RPKMJI6TIxjVcVCPF8T8=
|
||||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250624090744-339ba854c997 h1:8bCKyYoV4YiVBvCZlRclc3aQlBYpWhgtM35mvniDFD8=
|
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250624090744-339ba854c997 h1:8bCKyYoV4YiVBvCZlRclc3aQlBYpWhgtM35mvniDFD8=
|
||||||
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250624090744-339ba854c997/go.mod h1:y/vsN8UO0wSxZk3gv0y5df4RPKMJI6TIxjVcVCPF8T8=
|
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250624090744-339ba854c997/go.mod h1:y/vsN8UO0wSxZk3gv0y5df4RPKMJI6TIxjVcVCPF8T8=
|
||||||
|
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250626065419-57ee409f9629 h1:ew3vCFrLS/7/8uULTTPCbsHzFntQ6X68SScnBEy3pl0=
|
||||||
|
git.grassecon.net/grassrootseconomics/sarafu-api v0.9.0-beta.1.0.20250626065419-57ee409f9629/go.mod h1:y/vsN8UO0wSxZk3gv0y5df4RPKMJI6TIxjVcVCPF8T8=
|
||||||
git.grassecon.net/grassrootseconomics/visedriver v0.9.0-beta.2.0.20250408094335-e2d1f65bb306 h1:Jo+yWysWw/N5BJQtAyEMN8ePVvAyPHv+JG4lQti+1N4=
|
git.grassecon.net/grassrootseconomics/visedriver v0.9.0-beta.2.0.20250408094335-e2d1f65bb306 h1:Jo+yWysWw/N5BJQtAyEMN8ePVvAyPHv+JG4lQti+1N4=
|
||||||
git.grassecon.net/grassrootseconomics/visedriver v0.9.0-beta.2.0.20250408094335-e2d1f65bb306/go.mod h1:FdLwYtzsjOIcDiW4uDgDYnB4Wdzq12uJUe0QHSSPbSo=
|
git.grassecon.net/grassrootseconomics/visedriver v0.9.0-beta.2.0.20250408094335-e2d1f65bb306/go.mod h1:FdLwYtzsjOIcDiW4uDgDYnB4Wdzq12uJUe0QHSSPbSo=
|
||||||
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250129070628-5a539172c694 h1:DjJlBSz0S13acft5XZDWk7ZYnzElym0xLMYEVgyNJ+E=
|
git.grassecon.net/grassrootseconomics/visedriver-africastalking v0.0.0-20250129070628-5a539172c694 h1:DjJlBSz0S13acft5XZDWk7ZYnzElym0xLMYEVgyNJ+E=
|
||||||
|
@ -1830,12 +1830,12 @@ func (h *MenuHandlers) ValidateAmount(ctx context.Context, sym string, input []b
|
|||||||
return res, fmt.Errorf("missing session")
|
return res, fmt.Errorf("missing session")
|
||||||
}
|
}
|
||||||
flag_invalid_amount, _ := h.flagManager.GetFlag("flag_invalid_amount")
|
flag_invalid_amount, _ := h.flagManager.GetFlag("flag_invalid_amount")
|
||||||
userStore := h.userdataStore
|
store := h.userdataStore
|
||||||
|
|
||||||
var balanceValue float64
|
var balanceValue float64
|
||||||
|
|
||||||
// retrieve the active balance
|
// retrieve the active balance
|
||||||
activeBal, err := userStore.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL)
|
activeBal, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACTIVE_BAL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logg.ErrorCtxf(ctx, "failed to read activeBal entry with", "key", storedb.DATA_ACTIVE_BAL, "error", err)
|
logg.ErrorCtxf(ctx, "failed to read activeBal entry with", "key", storedb.DATA_ACTIVE_BAL, "error", err)
|
||||||
return res, err
|
return res, err
|
||||||
@ -1861,15 +1861,9 @@ func (h *MenuHandlers) ValidateAmount(ctx context.Context, sym string, input []b
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format the amount to 2 decimal places before saving (truncated)
|
// Format the amount with 2 decimal places before saving
|
||||||
formattedAmount, err := store.TruncateDecimalString(amountStr, 2)
|
formattedAmount := fmt.Sprintf("%.2f", inputAmount)
|
||||||
if err != nil {
|
err = store.WriteEntry(ctx, sessionId, storedb.DATA_AMOUNT, []byte(formattedAmount))
|
||||||
res.FlagSet = append(res.FlagSet, flag_invalid_amount)
|
|
||||||
res.Content = amountStr
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
err = userStore.WriteEntry(ctx, sessionId, storedb.DATA_AMOUNT, []byte(formattedAmount))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logg.ErrorCtxf(ctx, "failed to write amount entry with", "key", storedb.DATA_AMOUNT, "value", formattedAmount, "error", err)
|
logg.ErrorCtxf(ctx, "failed to write amount entry with", "key", storedb.DATA_AMOUNT, "value", formattedAmount, "error", err)
|
||||||
return res, err
|
return res, err
|
||||||
@ -2567,52 +2561,6 @@ func (h *MenuHandlers) persistLanguageCode(ctx context.Context, code string) err
|
|||||||
return h.persistInitialLanguageCode(ctx, sessionId, code)
|
return h.persistInitialLanguageCode(ctx, sessionId, code)
|
||||||
}
|
}
|
||||||
|
|
||||||
// constructAccountAlias retrieves and alias based on the first and family name
|
|
||||||
// and writes the result in DATA_ACCOUNT_ALIAS
|
|
||||||
func (h *MenuHandlers) constructAccountAlias(ctx context.Context) error {
|
|
||||||
var alias string
|
|
||||||
store := h.userdataStore
|
|
||||||
sessionId, ok := ctx.Value("SessionId").(string)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("missing session")
|
|
||||||
}
|
|
||||||
firstName, err := store.ReadEntry(ctx, sessionId, storedb.DATA_FIRST_NAME)
|
|
||||||
if err != nil {
|
|
||||||
if db.IsNotFound(err) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
familyName, err := store.ReadEntry(ctx, sessionId, storedb.DATA_FAMILY_NAME)
|
|
||||||
if err != nil {
|
|
||||||
if db.IsNotFound(err) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
pubKey, err := store.ReadEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY)
|
|
||||||
if err != nil {
|
|
||||||
if db.IsNotFound(err) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
aliasInput := fmt.Sprintf("%s%s", firstName, familyName)
|
|
||||||
aliasResult, err := h.accountService.RequestAlias(ctx, string(pubKey), aliasInput)
|
|
||||||
if err != nil {
|
|
||||||
logg.ErrorCtxf(ctx, "failed to retrieve alias", "alias", aliasInput, "error_alias_request", err)
|
|
||||||
return fmt.Errorf("Failed to retrieve alias: %s", err.Error())
|
|
||||||
}
|
|
||||||
alias = aliasResult.Alias
|
|
||||||
//Store the alias
|
|
||||||
err = store.WriteEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS, []byte(alias))
|
|
||||||
if err != nil {
|
|
||||||
logg.ErrorCtxf(ctx, "failed to write account alias", "key", storedb.DATA_ACCOUNT_ALIAS, "value", alias, "error", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// RequestCustomAlias requests an ENS based alias name based on a user's input,then saves it as temporary value
|
// RequestCustomAlias requests an ENS based alias name based on a user's input,then saves it as temporary value
|
||||||
func (h *MenuHandlers) RequestCustomAlias(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
func (h *MenuHandlers) RequestCustomAlias(ctx context.Context, sym string, input []byte) (resource.Result, error) {
|
||||||
var res resource.Result
|
var res resource.Result
|
||||||
@ -3052,15 +3000,7 @@ func (h *MenuHandlers) SwapPreview(ctx context.Context, sym string, input []byte
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format the amount to 2 decimal places
|
finalAmountStr, err := store.ParseAndScaleAmount(inputStr, swapData.ActiveSwapFromDecimal)
|
||||||
formattedAmount, err := store.TruncateDecimalString(inputStr, 2)
|
|
||||||
if err != nil {
|
|
||||||
res.FlagSet = append(res.FlagSet, flag_invalid_amount)
|
|
||||||
res.Content = inputStr
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
finalAmountStr, err := store.ParseAndScaleAmount(formattedAmount, swapData.ActiveSwapFromDecimal)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
|
@ -1678,22 +1678,6 @@ func TestValidateAmount(t *testing.T) {
|
|||||||
Content: "0.02ms",
|
Content: "0.02ms",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "Test with valid decimal amount",
|
|
||||||
input: []byte("0.149"),
|
|
||||||
activeBal: []byte("5"),
|
|
||||||
expectedResult: resource.Result{
|
|
||||||
Content: "0.14",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Test with valid large decimal amount",
|
|
||||||
input: []byte("1.8599999999"),
|
|
||||||
activeBal: []byte("5"),
|
|
||||||
expectedResult: resource.Result{
|
|
||||||
Content: "1.85",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
@ -2545,11 +2529,11 @@ func TestCheckTransactions(t *testing.T) {
|
|||||||
|
|
||||||
mockTXResponse := []dataserviceapi.Last10TxResponse{
|
mockTXResponse := []dataserviceapi.Last10TxResponse{
|
||||||
{
|
{
|
||||||
Sender: "0X13242618721", Recipient: "0x41c188d63Qa", TransferValue: "100", ContractAddress: "0X1324262343rfdGW23",
|
Sender: "0X13242618721", Recipient: "0x41c188d63Qa", TransferValue: "100", TokenAddress: "0X1324262343rfdGW23",
|
||||||
TxHash: "0x123wefsf34rf", DateBlock: time.Now(), TokenSymbol: "SRF", TokenDecimals: "6",
|
TxHash: "0x123wefsf34rf", DateBlock: time.Now(), TokenSymbol: "SRF", TokenDecimals: "6",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Sender: "0x41c188d63Qa", Recipient: "0X13242618721", TransferValue: "200", ContractAddress: "0X1324262343rfdGW23",
|
Sender: "0x41c188d63Qa", Recipient: "0X13242618721", TransferValue: "200", TokenAddress: "0X1324262343rfdGW23",
|
||||||
TxHash: "0xq34wresfdb44", DateBlock: time.Now(), TokenSymbol: "SRF", TokenDecimals: "6",
|
TxHash: "0xq34wresfdb44", DateBlock: time.Now(), TokenSymbol: "SRF", TokenDecimals: "6",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -2601,11 +2585,11 @@ func TestGetTransactionsList(t *testing.T) {
|
|||||||
|
|
||||||
mockTXResponse := []dataserviceapi.Last10TxResponse{
|
mockTXResponse := []dataserviceapi.Last10TxResponse{
|
||||||
{
|
{
|
||||||
Sender: "0X13242618721", Recipient: "0x41c188d63Qa", TransferValue: "1000", ContractAddress: "0X1324262343rfdGW23",
|
Sender: "0X13242618721", Recipient: "0x41c188d63Qa", TransferValue: "1000", TokenAddress: "0X1324262343rfdGW23",
|
||||||
TxHash: "0x123wefsf34rf", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
|
TxHash: "0x123wefsf34rf", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Sender: "0x41c188d63Qa", Recipient: "0X13242618721", TransferValue: "2000", ContractAddress: "0X1324262343rfdGW23",
|
Sender: "0x41c188d63Qa", Recipient: "0X13242618721", TransferValue: "2000", TokenAddress: "0X1324262343rfdGW23",
|
||||||
TxHash: "0xq34wresfdb44", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
|
TxHash: "0xq34wresfdb44", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -2670,11 +2654,11 @@ func TestViewTransactionStatement(t *testing.T) {
|
|||||||
|
|
||||||
mockTXResponse := []dataserviceapi.Last10TxResponse{
|
mockTXResponse := []dataserviceapi.Last10TxResponse{
|
||||||
{
|
{
|
||||||
Sender: "0X13242618721", Recipient: "0x41c188d63Qa", TransferValue: "1000", ContractAddress: "0X1324262343rfdGW23",
|
Sender: "0X13242618721", Recipient: "0x41c188d63Qa", TransferValue: "1000", TokenAddress: "0X1324262343rfdGW23",
|
||||||
TxHash: "0x123wefsf34rf", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
|
TxHash: "0x123wefsf34rf", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Sender: "0x41c188d63Qa", Recipient: "0X13242618721", TransferValue: "2000", ContractAddress: "0X1324262343rfdGW23",
|
Sender: "0x41c188d63Qa", Recipient: "0X13242618721", TransferValue: "2000", TokenAddress: "0X1324262343rfdGW23",
|
||||||
TxHash: "0xq34wresfdb44", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
|
TxHash: "0xq34wresfdb44", DateBlock: dateBlock, TokenSymbol: "SRF", TokenDecimals: "2",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -3170,97 +3154,6 @@ func TestResetUnregisteredNumber(t *testing.T) {
|
|||||||
assert.Equal(t, expectedResult, res)
|
assert.Equal(t, expectedResult, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConstructAccountAlias(t *testing.T) {
|
|
||||||
ctx, store := InitializeTestStore(t)
|
|
||||||
sessionId := "session123"
|
|
||||||
mockAccountService := new(mocks.MockAccountService)
|
|
||||||
|
|
||||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
|
||||||
|
|
||||||
h := &MenuHandlers{
|
|
||||||
userdataStore: store,
|
|
||||||
accountService: mockAccountService,
|
|
||||||
}
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
firstName string
|
|
||||||
familyName string
|
|
||||||
publicKey string
|
|
||||||
expectedAlias string
|
|
||||||
aliasResponse *models.RequestAliasResult
|
|
||||||
aliasError error
|
|
||||||
expectedError error
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Valid alias construction",
|
|
||||||
firstName: "John",
|
|
||||||
familyName: "Doe",
|
|
||||||
publicKey: "pubkey123",
|
|
||||||
expectedAlias: "JohnDoeAlias",
|
|
||||||
aliasResponse: &models.RequestAliasResult{Alias: "JohnDoeAlias"},
|
|
||||||
aliasError: nil,
|
|
||||||
expectedError: nil,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Account service fails to return alias",
|
|
||||||
firstName: "Jane",
|
|
||||||
familyName: "Smith",
|
|
||||||
publicKey: "pubkey456",
|
|
||||||
expectedAlias: "",
|
|
||||||
aliasResponse: nil,
|
|
||||||
aliasError: fmt.Errorf("service unavailable"),
|
|
||||||
expectedError: fmt.Errorf("Failed to retrieve alias: service unavailable"),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
if tt.firstName != "" {
|
|
||||||
err := store.WriteEntry(ctx, sessionId, storedb.DATA_FIRST_NAME, []byte(tt.firstName))
|
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if tt.familyName != "" {
|
|
||||||
err := store.WriteEntry(ctx, sessionId, storedb.DATA_FAMILY_NAME, []byte(tt.familyName))
|
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if tt.publicKey != "" {
|
|
||||||
err := store.WriteEntry(ctx, sessionId, storedb.DATA_PUBLIC_KEY, []byte(tt.publicKey))
|
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
aliasInput := fmt.Sprintf("%s%s", tt.firstName, tt.familyName)
|
|
||||||
|
|
||||||
// Mock service behavior
|
|
||||||
mockAccountService.On(
|
|
||||||
"RequestAlias",
|
|
||||||
tt.publicKey,
|
|
||||||
aliasInput,
|
|
||||||
).Return(tt.aliasResponse, tt.aliasError)
|
|
||||||
|
|
||||||
// Call the function under test
|
|
||||||
err := h.constructAccountAlias(ctx)
|
|
||||||
|
|
||||||
// Assertions
|
|
||||||
if tt.expectedError != nil {
|
|
||||||
assert.EqualError(t, err, tt.expectedError.Error())
|
|
||||||
} else {
|
|
||||||
assert.NoError(t, err)
|
|
||||||
if tt.expectedAlias != "" {
|
|
||||||
storedAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS)
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, tt.expectedAlias, string(storedAlias))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure mock expectations were met
|
|
||||||
mockAccountService.AssertExpectations(t)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestInsertProfileItems(t *testing.T) {
|
func TestInsertProfileItems(t *testing.T) {
|
||||||
ctx, store := InitializeTestStore(t)
|
ctx, store := InitializeTestStore(t)
|
||||||
sessionId := "session123"
|
sessionId := "session123"
|
||||||
|
@ -3,7 +3,6 @@ package store
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"math/big"
|
"math/big"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -21,27 +20,6 @@ type TransactionData struct {
|
|||||||
ActiveAddress string
|
ActiveAddress string
|
||||||
}
|
}
|
||||||
|
|
||||||
// TruncateDecimalString safely truncates the input amount to the specified decimal places
|
|
||||||
func TruncateDecimalString(input string, decimalPlaces int) (string, error) {
|
|
||||||
num, ok := new(big.Float).SetString(input)
|
|
||||||
if !ok {
|
|
||||||
return "", fmt.Errorf("invalid input")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Multiply by 10^decimalPlaces
|
|
||||||
scale := new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(decimalPlaces)), nil))
|
|
||||||
scaled := new(big.Float).Mul(num, scale)
|
|
||||||
|
|
||||||
// Truncate by converting to int (chops off decimals)
|
|
||||||
intPart, _ := scaled.Int(nil)
|
|
||||||
|
|
||||||
// Divide back to get truncated float
|
|
||||||
truncated := new(big.Float).Quo(new(big.Float).SetInt(intPart), scale)
|
|
||||||
|
|
||||||
// Format with fixed decimals
|
|
||||||
return truncated.Text('f', decimalPlaces), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func ParseAndScaleAmount(storedAmount, activeDecimal string) (string, error) {
|
func ParseAndScaleAmount(storedAmount, activeDecimal string) (string, error) {
|
||||||
// Parse token decimal
|
// Parse token decimal
|
||||||
tokenDecimal, err := strconv.Atoi(activeDecimal)
|
tokenDecimal, err := strconv.Atoi(activeDecimal)
|
||||||
@ -60,8 +38,11 @@ func ParseAndScaleAmount(storedAmount, activeDecimal string) (string, error) {
|
|||||||
multiplier := new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(tokenDecimal)), nil))
|
multiplier := new(big.Float).SetInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(tokenDecimal)), nil))
|
||||||
finalAmount := new(big.Float).Mul(amount, multiplier)
|
finalAmount := new(big.Float).Mul(amount, multiplier)
|
||||||
|
|
||||||
// Return finalAmount as a string with 0 decimal places (rounded)
|
// Convert finalAmount to a string
|
||||||
return finalAmount.Text('f', 0), nil
|
finalAmountStr := new(big.Int)
|
||||||
|
finalAmount.Int(finalAmountStr)
|
||||||
|
|
||||||
|
return finalAmountStr.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadTransactionData(ctx context.Context, store DataStore, sessionId string) (TransactionData, error) {
|
func ReadTransactionData(ctx context.Context, store DataStore, sessionId string) (TransactionData, error) {
|
||||||
|
@ -7,109 +7,6 @@ import (
|
|||||||
"github.com/alecthomas/assert/v2"
|
"github.com/alecthomas/assert/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTruncateDecimalString(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
input string
|
|
||||||
decimalPlaces int
|
|
||||||
want string
|
|
||||||
expectError bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "whole number",
|
|
||||||
input: "4",
|
|
||||||
decimalPlaces: 2,
|
|
||||||
want: "4.00",
|
|
||||||
expectError: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "single decimal",
|
|
||||||
input: "4.1",
|
|
||||||
decimalPlaces: 2,
|
|
||||||
want: "4.10",
|
|
||||||
expectError: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "one decimal place",
|
|
||||||
input: "4.19",
|
|
||||||
decimalPlaces: 1,
|
|
||||||
want: "4.1",
|
|
||||||
expectError: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "truncates to 2 dp",
|
|
||||||
input: "0.149",
|
|
||||||
decimalPlaces: 2,
|
|
||||||
want: "0.14",
|
|
||||||
expectError: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "does not round",
|
|
||||||
input: "1.8599999999",
|
|
||||||
decimalPlaces: 2,
|
|
||||||
want: "1.85",
|
|
||||||
expectError: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "high precision input",
|
|
||||||
input: "123.456789",
|
|
||||||
decimalPlaces: 4,
|
|
||||||
want: "123.4567",
|
|
||||||
expectError: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "zero",
|
|
||||||
input: "0",
|
|
||||||
decimalPlaces: 2,
|
|
||||||
want: "0.00",
|
|
||||||
expectError: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "invalid input string",
|
|
||||||
input: "abc",
|
|
||||||
decimalPlaces: 2,
|
|
||||||
want: "",
|
|
||||||
expectError: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "edge rounding case",
|
|
||||||
input: "4.99999999",
|
|
||||||
decimalPlaces: 2,
|
|
||||||
want: "4.99",
|
|
||||||
expectError: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "small value",
|
|
||||||
input: "0.0001",
|
|
||||||
decimalPlaces: 2,
|
|
||||||
want: "0.00",
|
|
||||||
expectError: false,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
got, err := TruncateDecimalString(tt.input, tt.decimalPlaces)
|
|
||||||
|
|
||||||
if tt.expectError {
|
|
||||||
if err == nil {
|
|
||||||
t.Errorf("TruncateDecimalString(%q, %d) expected error, got nil", tt.input, tt.decimalPlaces)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("TruncateDecimalString(%q, %d) unexpected error: %v", tt.input, tt.decimalPlaces, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if got != tt.want {
|
|
||||||
t.Errorf("TruncateDecimalString(%q, %d) = %q, want %q", tt.input, tt.decimalPlaces, got, tt.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseAndScaleAmount(t *testing.T) {
|
func TestParseAndScaleAmount(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
@ -167,20 +64,6 @@ func TestParseAndScaleAmount(t *testing.T) {
|
|||||||
want: "0",
|
want: "0",
|
||||||
expectError: false,
|
expectError: false,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "high decimals",
|
|
||||||
amount: "1.85",
|
|
||||||
decimals: "18",
|
|
||||||
want: "1850000000000000000",
|
|
||||||
expectError: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "6 d.p",
|
|
||||||
amount: "2.32",
|
|
||||||
decimals: "6",
|
|
||||||
want: "2320000",
|
|
||||||
expectError: false,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
Loading…
Reference in New Issue
Block a user