Skip to content

Commit

Permalink
Fix balance calculation issue caused by unsigned integer values
Browse files Browse the repository at this point in the history
  • Loading branch information
yasin-ce committed Oct 15, 2024
1 parent 903d68e commit 17b46c7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mobile v0.0.0-20240909163608-642950227fb3 // indirect
golang.org/x/mobile v0.0.0-20241004191011-08a83c5af9f8 // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/tools v0.25.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/tools v0.26.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5U
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mobile v0.0.0-20240909163608-642950227fb3 h1:HOa20LMHFElnLsGI9j8/sxTIHpogkTuHZlyoIjl3kkw=
golang.org/x/mobile v0.0.0-20240909163608-642950227fb3/go.mod h1:5EJr05J3jS1A5hwVNxs4vC0pIRxtWmwM15D1ZxCj93s=
golang.org/x/mobile v0.0.0-20241004191011-08a83c5af9f8 h1:W8YtRRiKAvbIbqAh+xpWKecxEO8w1oaD6DrZhMyotF8=
golang.org/x/mobile v0.0.0-20241004191011-08a83c5af9f8/go.mod h1:snk1Mn2ZpdKCt90JPEsDh4sL3ReK520U2t0d7RHBnSU=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
Expand All @@ -44,6 +46,8 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand All @@ -54,6 +58,8 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E=
golang.org/x/tools v0.25.0 h1:oFU9pkj/iJgs+0DT+VMHrx+oBKs/LJMV+Uvg78sl+fE=
golang.org/x/tools v0.25.0/go.mod h1:/vtpO8WL1N9cQC3FN5zPqb//fRXskFHbLKk4OW1Q7rg=
golang.org/x/tools v0.26.0 h1:v/60pFQmzmT9ExmjDv2gGIfi3OqfKoEP6I5+umXlbnQ=
golang.org/x/tools v0.26.0/go.mod h1:TPVVj70c7JJ3WCazhD8OdXcZg/og+b9+tH/KxylGwH0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
30 changes: 20 additions & 10 deletions sdk/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,21 +754,26 @@ func MakeOptInAndAssetTransferTxns(
assetID int64,
params *SuggestedParams,
) (transactions *TransactionSignerArray, err error) {
var ASSET_OPT_IN_MBR uint64 = 100000
var FLAT_FEE uint64 = 1000
var ACCOUNT_MBR uint64 = 100000
var ASSET_OPT_IN_MBR int64 = 100000
var FLAT_FEE int64 = 1000
var ACCOUNT_MBR int64 = 100000

if params.Fee > 0 {
FLAT_FEE = uint64(params.Fee)
FLAT_FEE = int64(params.Fee)
}

senderExtractedAmount, _ := senderAlgoAmount.Extract()
senderExtractedMinBalance, _ := senderMinBalanceAmount.Extract()
receiverExtractedAmount, _ := receiverAlgoAmount.Extract()
receiverExtractedMinBalance, _ := receiverMinBalanceAmount.Extract()

senderExtractedAmountInt64, _ := ConvertUInt64ToInt64(senderExtractedAmount)
senderExtractedMinBalanceInt64, _ := ConvertUInt64ToInt64(senderExtractedMinBalance)
receiverExtractedAmountInt64, _ := ConvertUInt64ToInt64(receiverExtractedAmount)
receiverExtractedMinBalanceInt64, _ := ConvertUInt64ToInt64(receiverExtractedMinBalance)

// If receiver has enough algo to opt-in to the asset
if receiverExtractedAmount-receiverExtractedMinBalance >= ASSET_OPT_IN_MBR+FLAT_FEE {
if receiverExtractedAmountInt64-receiverExtractedMinBalanceInt64 >= ASSET_OPT_IN_MBR+FLAT_FEE {
optInAmount := MakeUint64(0)
receiverOptInTxn, receiverOptInTxnError := MakeAssetTransferTxn(
receiver,
Expand Down Expand Up @@ -824,22 +829,27 @@ func MakeOptInAndAssetTransferTxns(
} else {
// If receiver does not have enough algo to opt-in to the asset

var receiverExtraAlgoAmount uint64 = 0
if receiverExtractedAmount == 0 {
var receiverExtraAlgoAmount int64 = 0
if receiverExtractedAmountInt64 == 0 {
receiverExtraAlgoAmount = ACCOUNT_MBR + ASSET_OPT_IN_MBR + FLAT_FEE
} else {
algoAmount := (ASSET_OPT_IN_MBR + FLAT_FEE) - (receiverExtractedAmount - receiverExtractedMinBalance)
algoAmount := (ASSET_OPT_IN_MBR + FLAT_FEE) - (receiverExtractedAmountInt64 - receiverExtractedMinBalanceInt64)
if algoAmount > 0 {
receiverExtraAlgoAmount = algoAmount
}
}

if senderExtractedAmount-senderExtractedMinBalance < receiverExtractedAmount+FLAT_FEE+FLAT_FEE {
if senderExtractedAmountInt64-senderExtractedMinBalanceInt64 < receiverExtractedAmountInt64+FLAT_FEE+FLAT_FEE {
err = fmt.Errorf("sender does not have enough algo to cover recivers needs")
return
}

paymentAMount := MakeUint64(receiverExtraAlgoAmount)
var receiverExtraAlgoAmountUint64 uint64 = 0
if receiverExtraAlgoAmount > 0 {
receiverExtraAlgoAmountUint64 = uint64(receiverExtraAlgoAmount)
}

paymentAMount := MakeUint64(receiverExtraAlgoAmountUint64)
paymentTxn, paymentTxnError := MakePaymentTxn(
sender,
receiver,
Expand Down
22 changes: 22 additions & 0 deletions sdk/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ func (i Uint64) Extract() (value uint64, err error) {
return
}

func ConvertUInt64ToInt64(i uint64) (value int64, err error) {
if i > math.MaxInt64 {
err = fmt.Errorf("Overflow: uint64 value is too large to convert to int64.")
return
} else {
value = int64(i)
return
}
}

type TransactionSignerArray struct {
signerItems []TransactionSignerItem
transactions *BytesArray
Expand All @@ -50,6 +60,14 @@ func (tsa *TransactionSignerArray) GetSigner(index int) string {
return tsa.signerItems[index].signer
}

func (tsa *TransactionSignerArray) GetSignerItem(index int) TransactionSignerItem {
return tsa.signerItems[index]
}

func (tsa *TransactionSignerArray) GetSignerItems() []TransactionSignerItem {
return tsa.signerItems
}

func (tsa *TransactionSignerArray) GetTxnFromSigner(index int) []byte {
return tsa.signerItems[index].transaction
}
Expand All @@ -71,6 +89,10 @@ type TransactionSignerItem struct {
transaction []byte
}

func (tsa *TransactionSignerItem) GetSigner() string { return tsa.signer }

func (tsa *TransactionSignerItem) GetTransaction() []byte { return tsa.transaction }

type StringArray struct {
values []string
}
Expand Down

0 comments on commit 17b46c7

Please sign in to comment.