Skip to content

Commit

Permalink
use strconv
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Jan 21, 2025
1 parent 7fa5c40 commit a288714
Show file tree
Hide file tree
Showing 39 changed files with 349 additions and 244 deletions.
3 changes: 2 additions & 1 deletion contract/r/gnoswap/common/halt.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"std"
"strconv"

"gno.land/p/demo/ufmt"
"gno.land/r/gnoswap/v1/consts"
Expand Down Expand Up @@ -94,6 +95,6 @@ func setHalt(halt bool) {
"setHalt",
"prevAddr", prevAddr,
"prevRealm", prevRealm,
"halt", ufmt.Sprintf("%t", halt),
"halt", strconv.FormatBool(halt),
)
}
3 changes: 2 additions & 1 deletion contract/r/gnoswap/common/limit_caller.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"std"
"strconv"

"gno.land/p/demo/ufmt"
)
Expand Down Expand Up @@ -47,6 +48,6 @@ func SetLimitCaller(v bool) {
"SetLimitCaller",
"prevAddr", prevAddr,
"prevRealm", prevPkgPath,
"limitCaller", ufmt.Sprintf("%t", v),
"limitCaller", strconv.FormatBool(v),
)
}
5 changes: 2 additions & 3 deletions contract/r/gnoswap/community_pool/community_pool.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package community_pool

import (
"std"

"gno.land/p/demo/ufmt"
"strconv"

"gno.land/r/gnoswap/v1/common"
)
Expand Down Expand Up @@ -37,7 +36,7 @@ func transferToken(tokenPath string, to std.Address, amount uint64) {
"prevRealm", prevRealm,
"tokenPath", tokenPath,
"to", to.String(),
"amount", ufmt.Sprintf("%d", amount),
"amount", strconv.FormatUint(amount, 10),
)
}

Expand Down
19 changes: 9 additions & 10 deletions contract/r/gnoswap/emission/distribution.gno
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ func changeDistributionPcts(
"ChangeDistributionPct",
"prevAddr", prevAddr,
"prevRealm", prevPkgPath,
"target01", strconv.Itoa(target01),
"pct01", strconv.FormatUint(pct01, 10),
"target02", strconv.Itoa(target02),
"pct02", strconv.FormatUint(pct02, 10),
"target03", strconv.Itoa(target03),
"pct03", strconv.FormatUint(pct03, 10),
"target04", strconv.Itoa(target04),
"pct04", strconv.FormatUint(pct04, 10),
"target01", formatInt(target01),
"pct01", formatUint(pct01),
"target02", formatInt(target02),
"pct02", formatUint(pct02),
"target03", formatInt(target03),
"pct03", formatUint(pct03),
"target04", formatInt(target04),
"pct04", formatUint(pct04),
)
}

Expand Down Expand Up @@ -179,7 +179,6 @@ func transferToTarget(target int, amount uint64) {
gns.Transfer(a2u(consts.STAKER_ADDR), amount)
distributedToStaker += amount
accuDistributedToStaker += amount
println("\t\t\t=================>[", std.GetHeight(), "] distributeToTarget : to Staker (", " : ", amount, "), accum : ", accuDistributedToStaker)

case DEVOPS:
gns.Transfer(a2u(consts.DEV_OPS), amount)
Expand Down Expand Up @@ -265,7 +264,7 @@ func setDistributionBpsPct(target int, pct uint64) {
if !exist {
panic("should not happen")
}

if oldPct.(uint64) != pct {
callbackStakerEmissionChange(calculateAmount(gns.GetEmission(), pct))
}
Expand Down
12 changes: 5 additions & 7 deletions contract/r/gnoswap/emission/emission.gno
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"std"
"time"

"gno.land/p/demo/ufmt"

"gno.land/r/gnoswap/v1/consts"
"gno.land/r/gnoswap/v1/gns"
)
Expand Down Expand Up @@ -46,11 +44,11 @@ func MintAndDistributeGns() uint64 {
"MintAndDistributeGns",
"prevAddr", prevAddr,
"prevRealm", prevPkgPath,
"internal_lastHeight", ufmt.Sprintf("%d", lastExecutedHeight),
"internal_height", ufmt.Sprintf("%d", currentHeight),
"internal_time", ufmt.Sprintf("%d", time.Now().Unix()),
"internal_amount", ufmt.Sprintf("%d", mintedEmissionRewardAmount),
"internal_totalSupply", ufmt.Sprintf("%d", gns.TotalSupply()),
"internal_lastHeight", formatInt(lastExecutedHeight),
"internal_height", formatInt(currentHeight),
"internal_time", formatInt(time.Now().Unix()),
"internal_amount", formatUint(mintedEmissionRewardAmount),
"internal_totalSupply", formatUint(gns.TotalSupply()),
)

setLastExecutedHeight(currentHeight)
Expand Down
27 changes: 27 additions & 0 deletions contract/r/gnoswap/emission/utils.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package emission

import (
"std"
"strconv"

"gno.land/p/demo/ufmt"
pusers "gno.land/p/demo/users"
Expand Down Expand Up @@ -90,3 +91,29 @@ func assertSumDistributionPct(pct01, pct02, pct03, pct04 uint64) {
))
}
}

func formatUint(v interface{}) string {
switch v := v.(type) {
case uint8:
return strconv.FormatUint(uint64(v), 10)
case uint32:
return strconv.FormatUint(uint64(v), 10)
case uint64:
return strconv.FormatUint(v, 10)
default:
panic(ufmt.Sprintf("invalid type: %T", v))
}
}

func formatInt(v interface{}) string {
switch v := v.(type) {
case int32:
return strconv.FormatInt(int64(v), 10)
case int64:
return strconv.FormatInt(v, 10)
case int:
return strconv.Itoa(v)
default:
panic(ufmt.Sprintf("invalid type: %T", v))
}
}
15 changes: 7 additions & 8 deletions contract/r/gnoswap/gns/gns.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gns

import (
"std"
"strconv"
"strings"

"gno.land/p/demo/grc/grc20"
Expand Down Expand Up @@ -134,10 +133,10 @@ func MintGns(address pusers.AddressOrName) uint64 {
"MintGNS",
"prevAddr", prevAddr,
"prevRealm", prevPkgPath,
"mintedBlockHeight", strconv.FormatInt(currentHeight, 10),
"mintedGNSAmount", strconv.FormatUint(amountToMint, 10),
"accumMintedGNSAmount", strconv.FormatUint(GetMintedEmissionAmount(), 10),
"accumLeftMintGNSAmount", strconv.FormatUint(GetLeftEmissionAmount(), 10),
"mintedBlockHeight", formatInt(currentHeight),
"mintedGNSAmount", formatUint(amountToMint),
"accumMintedGNSAmount", formatUint(GetMintedEmissionAmount()),
"accumLeftMintGNSAmount", formatUint(GetLeftEmissionAmount()),
)

return amountToMint
Expand All @@ -155,10 +154,10 @@ func Burn(from pusers.AddressOrName, amount uint64) {
"Burn",
"prevAddr", prevAddr,
"prevRealm", prevPkgPath,
"burnedBlockHeight", strconv.FormatInt(std.GetHeight(), 10),
"burnedBlockHeight", formatInt(std.GetHeight()),
"burnFrom", fromAddr.String(),
"burnedGNSAmount", strconv.FormatUint(amount, 10),
"accumBurnedGNSAmount", strconv.FormatUint(GetBurnAmount(), 10),
"burnedGNSAmount", formatUint(amount),
"accumBurnedGNSAmount", formatUint(GetBurnAmount()),
)
}

Expand Down
10 changes: 4 additions & 6 deletions contract/r/gnoswap/gns/halving.gno
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ type HalvingData struct {
amountPerBlock []uint64
}


func (h *HalvingData) getStartBlockHeight(year int64) int64 {
if year == 0 {
return 0
Expand Down Expand Up @@ -339,8 +338,8 @@ func SetAvgBlockTimeInMsByAdmin(ms int64) {
"SetAvgBlockTimeInMsByAdmin",
"prevAddr", prevAddr,
"prevRealm", prevPkgPath,
"prevAvgBlockTimeInMs", strconv.FormatInt(prevAvgBlockTimeInMs, 10),
"avgBlockTimeInMs", strconv.FormatInt(ms, 10),
"prevAvgBlockTimeInMs", formatInt(prevAvgBlockTimeInMs),
"avgBlockTimeInMs", formatInt(ms),
)
}

Expand All @@ -357,8 +356,8 @@ func SetAvgBlockTimeInMs(ms int64) {
"SetAvgBlockTimeInMs",
"prevAddr", prevAddr,
"prevRealm", prevPkgPath,
"prevAvgBlockTimeInMs", strconv.FormatInt(prevAvgBlockTimeInMs, 10),
"avgBlockTimeInMs", strconv.FormatInt(ms, 10),
"prevAvgBlockTimeInMs", formatInt(prevAvgBlockTimeInMs),
"avgBlockTimeInMs", formatInt(ms),
)
}

Expand Down Expand Up @@ -421,7 +420,6 @@ func setAvgBlockTimeInMs(ms int64) {

avgBlockTimeMs = ms


}

// GetAmountByHeight returns the amount of gns to mint by height
Expand Down
27 changes: 27 additions & 0 deletions contract/r/gnoswap/gns/utils.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gns

import (
"std"
"strconv"

"gno.land/p/demo/ufmt"
pusers "gno.land/p/demo/users"
Expand Down Expand Up @@ -64,3 +65,29 @@ func i64Min(x, y int64) int64 {
func secToMs(sec int64) int64 {
return sec * consts.SECOND_IN_MILLISECOND
}

func formatUint(v interface{}) string {
switch v := v.(type) {
case uint8:
return strconv.FormatUint(uint64(v), 10)
case uint32:
return strconv.FormatUint(uint64(v), 10)
case uint64:
return strconv.FormatUint(v, 10)
default:
panic(ufmt.Sprintf("invalid type: %T", v))
}
}

func formatInt(v interface{}) string {
switch v := v.(type) {
case int32:
return strconv.FormatInt(int64(v), 10)
case int64:
return strconv.FormatInt(v, 10)
case int:
return strconv.Itoa(v)
default:
panic(ufmt.Sprintf("invalid type: %T", v))
}
}
44 changes: 22 additions & 22 deletions contract/r/gnoswap/gov/governance/api.gno
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

func createProposalJsonNode(id uint64, proposal ProposalInfo) *json.Node {
return json.Builder().
WriteString("id", formatUint64(id)).
WriteString("configVersion", formatUint64(proposal.ConfigVersion)).
WriteString("id", formatUint(id)).
WriteString("configVersion", formatUint(proposal.ConfigVersion)).
WriteString("proposer", proposal.Proposer.String()).
WriteString("status", b64Encode(getProposalStatus(id))).
WriteString("type", proposal.ProposalType.String()).
Expand Down Expand Up @@ -53,7 +53,7 @@ func GetProposalById(id uint64) string {
en.MintAndDistributeGns()
updateProposalsState()

_, exists := proposals.Get(formatUint64(id))
_, exists := proposals.Get(formatUint(id))
if !exists {
return ""
}
Expand All @@ -78,13 +78,13 @@ func GetVoteStatusFromProposalById(id uint64) string {
en.MintAndDistributeGns()
updateProposalsState()

_, exists := proposals.Get(formatUint64(id))
_, exists := proposals.Get(formatUint(id))
if !exists {
return ""
}

votesObj := metaNode()
votesObj.AppendObject("proposalId", json.StringNode("proposalId", formatUint64(id)))
votesObj.AppendObject("proposalId", json.StringNode("proposalId", formatUint(id)))
votesObj.AppendObject("votes", json.StringNode("votes", b64Encode(getProposalVotes(id)))) // max, yes, no

return marshal(votesObj)
Expand Down Expand Up @@ -139,17 +139,17 @@ func GetVoteByAddressFromProposalById(addr std.Address, id uint64) string {

func createVoteJsonNode(addr std.Address, id uint64, vote voteWithWeight) *json.Node {
return json.Builder().
WriteString("proposalId", formatUint64(id)).
WriteString("proposalId", formatUint(id)).
WriteString("voteYes", formatBool(vote.Yes)).
WriteString("voteWeight", formatUint64(vote.Weight)).
WriteString("voteHeight", formatUint64(vote.VotedHeight)).
WriteString("voteTimestamp", formatUint64(vote.VotedAt)).
WriteString("voteWeight", formatUint(vote.Weight)).
WriteString("voteHeight", formatUint(vote.VotedHeight)).
WriteString("voteTimestamp", formatUint(vote.VotedAt)).
Node()
}

// getProposalExtraData returns the extra data of a proposal based on its type.
func getProposalExtraData(proposalId uint64) string {
proposal, exist := proposals.Get(formatUint64(proposalId))
proposal, exist := proposals.Get(formatUint(proposalId))
if !exist {
return ""
}
Expand All @@ -176,7 +176,7 @@ func getCommunityPoolSpendProposalData(proposalId uint64) string {
proposalObj := json.Builder().
WriteString("to", proposal.CommunityPoolSpend.To.String()).
WriteString("tokenPath", proposal.CommunityPoolSpend.TokenPath).
WriteString("amount", formatUint64(proposal.CommunityPoolSpend.Amount)).
WriteString("amount", formatUint(proposal.CommunityPoolSpend.Amount)).
Node()

return marshal(proposalObj)
Expand All @@ -194,7 +194,7 @@ func getParameterChangeProposalData(proposalId uint64) string {

// getProposalStatus returns the status of a proposal.
func getProposalStatus(id uint64) string {
prop, exist := proposals.Get(formatUint64(id))
prop, exist := proposals.Get(formatUint(id))
if !exist {
return ""
}
Expand All @@ -211,27 +211,27 @@ func getProposalStatus(id uint64) string {

func createProposalStateNode(state ProposalState, votingStart, votingEnd uint64) *json.Node {
return json.Builder().
WriteString("createdAt", formatUint64(state.CreatedAt)).
WriteString("createdAt", formatUint(state.CreatedAt)).
WriteString("upcoming", formatBool(state.Upcoming)).
WriteString("active", formatBool(state.Active)).
WriteString("votingStart", formatUint64(votingStart)).
WriteString("votingEnd", formatUint64(votingEnd)).
WriteString("votingStart", formatUint(votingStart)).
WriteString("votingEnd", formatUint(votingEnd)).
WriteString("passed", formatBool(state.Passed)).
WriteString("passedAt", formatUint64(state.PassedAt)).
WriteString("passedAt", formatUint(state.PassedAt)).
WriteString("rejected", formatBool(state.Rejected)).
WriteString("rejectedAt", formatUint64(state.RejectedAt)).
WriteString("rejectedAt", formatUint(state.RejectedAt)).
WriteString("canceled", formatBool(state.Canceled)).
WriteString("canceledAt", formatUint64(state.CanceledAt)).
WriteString("canceledAt", formatUint(state.CanceledAt)).
WriteString("executed", formatBool(state.Executed)).
WriteString("executedAt", formatUint64(state.ExecutedAt)).
WriteString("executedAt", formatUint(state.ExecutedAt)).
WriteString("expired", formatBool(state.Expired)).
WriteString("expiredAt", formatUint64(state.ExpiredAt)).
WriteString("expiredAt", formatUint(state.ExpiredAt)).
Node()
}

// getProposalVotes returns the votes of a proposal.
func getProposalVotes(id uint64) string {
prop, exist := proposals.Get(formatUint64(id))
prop, exist := proposals.Get(formatUint(id))
if !exist {
return ""
}
Expand All @@ -240,7 +240,7 @@ func getProposalVotes(id uint64) string {
maxVoting := proposal.MaxVotingWeight.ToString()

proposalObj := json.Builder().
WriteString("quorum", formatUint64(proposal.QuorumAmount)).
WriteString("quorum", formatUint(proposal.QuorumAmount)).
WriteString("max", maxVoting).
WriteString("yes", proposal.Yea.ToString()).
WriteString("no", proposal.Nay.ToString()).
Expand Down
Loading

0 comments on commit a288714

Please sign in to comment.