Skip to content

Commit 79978b8

Browse files
notJoondongwon8247
andauthored
fix: *.Int64() -> safeConvertToInt64() (#812)
* fix: `*.Int64()` -> `safeConvertToInt64()` * fix: launchpad * fix * fix: remove abs * revert: swap_inner * fix --------- Co-authored-by: Dongwon <[email protected]>
1 parent e7d78e2 commit 79978b8

19 files changed

+181
-31
lines changed

contract/p/gnoswap/int256/conversion.gno

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (z *Int) SetInt64(x int64) *Int {
2222
u = ^u + 1 // |x| = two's complement magnitude
2323
}
2424
z.abs = z.abs.SetUint64(u)
25-
z.neg = neg && u != 0 // -0 방지
25+
z.neg = neg && u != 0 // prevent -0
2626
return z
2727
}
2828

contract/r/gnoswap/v1/gov/staker/emission_reward_state.gno

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (e *EmissionRewardState) calculateClaimableRewards(
9494
q128,
9595
)
9696

97-
return rewardAmount.Int64(), nil
97+
return safeConvertToInt64(rewardAmount), nil
9898
}
9999

100100
// addStake increases the staked amount for this address.

contract/r/gnoswap/v1/gov/staker/protocol_fee_reward_state.gno

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (p *ProtocolFeeRewardState) calculateClaimableRewards(
103103
q128,
104104
)
105105

106-
rewardAmounts[token] = rewardAmount.Int64()
106+
rewardAmounts[token] = safeConvertToInt64(rewardAmount)
107107
}
108108

109109
return rewardAmounts, nil

contract/r/gnoswap/v1/gov/staker/util.gno

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"gno.land/p/demo/avl"
88
"gno.land/p/demo/json"
99
"gno.land/p/demo/ufmt"
10+
u256 "gno.land/p/gnoswap/uint256"
1011
)
1112

1213
// marshal data to json string
@@ -92,3 +93,32 @@ func milliToSec(ms int64) int64 {
9293
var msPerSec int64 = 1000
9394
return ms / msPerSec
9495
}
96+
97+
// safeConvertToInt64 safely converts a *u256.Uint value to an int64, ensuring no overflow.
98+
//
99+
// This function attempts to convert the given *u256.Uint value to an int64. If the value exceeds
100+
// the maximum allowable range for int64 (`2^63 - 1`), it triggers a panic with a descriptive error message.
101+
//
102+
// Parameters:
103+
// - value (*u256.Uint): The unsigned 256-bit integer to be converted.
104+
//
105+
// Returns:
106+
// - int64: The converted value if it falls within the int64 range.
107+
//
108+
// Panics:
109+
// - If the `value` exceeds the range of int64, the function will panic with an error indicating
110+
// the overflow and the original value.
111+
func safeConvertToInt64(value *u256.Uint) int64 {
112+
const INT64_MAX = 9223372036854775807
113+
const MAX_INT64 = "9223372036854775807"
114+
115+
res, overflow := value.Uint64WithOverflow()
116+
if overflow || res > uint64(INT64_MAX) {
117+
panic(ufmt.Sprintf(
118+
"amount(%s) overflows int64 range (max %s)",
119+
value.ToString(),
120+
MAX_INT64,
121+
))
122+
}
123+
return int64(res)
124+
}

contract/r/gnoswap/v1/launchpad/reward_manager.gno

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ func (r *RewardManager) AccumulatedRewardPerDepositX128() *u256.Uint {
6464
}
6565

6666
func (r *RewardManager) AccumulatedReward() int64 {
67-
return u256.Zero().Rsh(r.accumulatedRewardPerDepositX128, 128).Int64()
67+
res := u256.Zero().Rsh(r.accumulatedRewardPerDepositX128, 128)
68+
return safeConvertToInt64(res)
6869
}
6970

7071
func (r *RewardManager) getDepositRewardState(depositId string) (*RewardState, error) {

contract/r/gnoswap/v1/launchpad/reward_state.gno

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (r *RewardState) calculateReward(accumRewardPerDepositX128 *u256.Uint) int6
129129
reward := u256.Zero().Mul(actualRewardPerDepositX128, u256.NewUintFromInt64(r.DepositAmount()))
130130
reward = u256.Zero().Rsh(reward, 128)
131131

132-
return reward.Int64()
132+
return safeConvertToInt64(reward)
133133
}
134134

135135
// calculateClaimableReward calculates the amount of reward that can be claimed

contract/r/gnoswap/v1/launchpad/utils.gno

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"strings"
66

77
"gno.land/p/demo/ufmt"
8+
9+
u256 "gno.land/p/gnoswap/uint256"
810
)
911

1012
// formatInt returns the string representation of the int64 value.
@@ -56,3 +58,32 @@ func parseProjectTierID(projectTierID string) (string, int64) {
5658

5759
return projectID, tierDuration
5860
}
61+
62+
// safeConvertToInt64 safely converts a *u256.Uint value to an int64, ensuring no overflow.
63+
//
64+
// This function attempts to convert the given *u256.Uint value to an int64. If the value exceeds
65+
// the maximum allowable range for int64 (`2^63 - 1`), it triggers a panic with a descriptive error message.
66+
//
67+
// Parameters:
68+
// - value (*u256.Uint): The unsigned 256-bit integer to be converted.
69+
//
70+
// Returns:
71+
// - int64: The converted value if it falls within the int64 range.
72+
//
73+
// Panics:
74+
// - If the `value` exceeds the range of int64, the function will panic with an error indicating
75+
// the overflow and the original value.
76+
func safeConvertToInt64(value *u256.Uint) int64 {
77+
const INT64_MAX = 9223372036854775807
78+
const MAX_INT64 = "9223372036854775807"
79+
80+
res, overflow := value.Uint64WithOverflow()
81+
if overflow || res > uint64(INT64_MAX) {
82+
panic(ufmt.Sprintf(
83+
"amount(%s) overflows int64 range (max %s)",
84+
value.ToString(),
85+
MAX_INT64,
86+
))
87+
}
88+
return int64(res)
89+
}

contract/r/gnoswap/v1/pool/pool.gno

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,14 @@ func Collect(
221221
if amount0.Gt(u256.Zero()) {
222222
position.tokensOwed0 = u256.Zero().Sub(position.tokensOwed0, amount0)
223223
pool.balances.token0 = u256.Zero().Sub(pool.balances.token0, amount0)
224-
if err := common.Approve(cross, pool.token0Path, positionAddr, amount0.Int64()); err != nil {
224+
if err := common.Approve(cross, pool.token0Path, positionAddr, safeConvertToInt64(amount0)); err != nil {
225225
panic(err)
226226
}
227227
}
228228
if amount1.Gt(u256.Zero()) {
229229
position.tokensOwed1 = u256.Zero().Sub(position.tokensOwed1, amount1)
230230
pool.balances.token1 = u256.Zero().Sub(pool.balances.token1, amount1)
231-
if err := common.Approve(cross, pool.token1Path, positionAddr, amount1.Int64()); err != nil {
231+
if err := common.Approve(cross, pool.token1Path, positionAddr, safeConvertToInt64(amount1)); err != nil {
232232
panic(err)
233233
}
234234
}
@@ -344,8 +344,8 @@ func collectProtocol(
344344
amount1 := u256Min(amount1Req, pool.ProtocolFeesToken1())
345345

346346
amount0, amount1 = pool.saveProtocolFees(amount0.Clone(), amount1.Clone())
347-
uAmount0 := amount0.Int64()
348-
uAmount1 := amount1.Int64()
347+
uAmount0 := safeConvertToInt64(amount0)
348+
uAmount1 := safeConvertToInt64(amount1)
349349

350350
checkTransferError(common.Transfer(cross, pool.token0Path, recipient, uAmount0))
351351
newBalanceToken0, err := updatePoolBalance(pool.BalanceToken0(), pool.BalanceToken1(), amount0, true)

contract/r/gnoswap/v1/pool/protocol_fee.gno

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ func HandleWithdrawalFee(
7171
feeAmount0, afterAmount0 := calculateAmountWithFee(u256.MustFromDecimal(amount0), u256.NewUint(fee))
7272
feeAmount1, afterAmount1 := calculateAmountWithFee(u256.MustFromDecimal(amount1), u256.NewUint(fee))
7373

74-
checkTransferError(common.TransferFrom(cross, token0Path, positionCaller, protocolFeeAddr, feeAmount0.Int64()))
75-
pf.AddToProtocolFee(cross, token0Path, feeAmount0.Int64())
76-
checkTransferError(common.TransferFrom(cross, token1Path, positionCaller, protocolFeeAddr, feeAmount1.Int64()))
77-
pf.AddToProtocolFee(cross, token1Path, feeAmount1.Int64())
74+
checkTransferError(common.TransferFrom(cross, token0Path, positionCaller, protocolFeeAddr, safeConvertToInt64(feeAmount0)))
75+
pf.AddToProtocolFee(cross, token0Path, safeConvertToInt64(feeAmount0))
76+
checkTransferError(common.TransferFrom(cross, token1Path, positionCaller, protocolFeeAddr, safeConvertToInt64(feeAmount1)))
77+
pf.AddToProtocolFee(cross, token1Path, safeConvertToInt64(feeAmount1))
7878

7979
previousRealm := std.PreviousRealm()
8080
std.Emit(

contract/r/gnoswap/v1/position/burn.gno

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,15 @@ func decreaseLiquidity(params DecreaseLiquidityParams) (uint64, string, string,
129129
poolAddr, _ := access.GetAddress(prabc.ROLE_POOL.String())
130130

131131
if isWrappedToken(pToken0) && params.unwrapResult {
132-
unwrapWithTransferFrom(poolAddr, caller, collectAmount0.Int64())
132+
unwrapWithTransferFrom(poolAddr, caller, safeConvertToInt64(collectAmount0))
133133
} else {
134-
common.TransferFrom(cross, pToken0, poolAddr, caller, collectAmount0.Int64())
134+
common.TransferFrom(cross, pToken0, poolAddr, caller, safeConvertToInt64(collectAmount0))
135135
}
136136

137137
if isWrappedToken(pToken1) && params.unwrapResult {
138-
unwrapWithTransferFrom(poolAddr, caller, collectAmount1.Int64())
138+
unwrapWithTransferFrom(poolAddr, caller, safeConvertToInt64(collectAmount1))
139139
} else {
140-
common.TransferFrom(cross, pToken1, poolAddr, caller, collectAmount1.Int64())
140+
common.TransferFrom(cross, pToken1, poolAddr, caller, safeConvertToInt64(collectAmount1))
141141
}
142142

143143
underflow := false

0 commit comments

Comments
 (0)