Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

refund_query.gno

4.65 Kb · 134 lines
  1package game_vault
  2
  3import "strconv"
  4
  5// ListRefundScopeStates returns participation process and reward pool states for refund decisions.
  6func ListRefundScopeStates(scopeKeys ...string) []map[string]string {
  7	if len(scopeKeys)%2 != 0 {
  8		panic("refund scope key count mismatch")
  9	}
 10	if len(scopeKeys)/2 > gameVaultList {
 11		panic("count exceeds game vault list limit")
 12	}
 13
 14	result := []map[string]string{}
 15	for index := 0; index < len(scopeKeys); index += 2 {
 16		processKey := scopeKeys[index]
 17		rewardPoolKey := scopeKeys[index+1]
 18		assertKeyLength(processKey, "process key")
 19		assertKeyLength(rewardPoolKey, "pool key")
 20
 21		state := map[string]string{
 22			"requestedProcessKey":    processKey,
 23			"requestedRewardPoolKey": rewardPoolKey,
 24			"processId":              "0",
 25			"processKey":             "",
 26			"processGameWorldId":     "0",
 27			"processRewardPoolKey":   "",
 28			"participationFee":       "0",
 29			"processStatus":          "0",
 30			"rewardPoolId":           "0",
 31			"rewardPoolKey":          "",
 32			"rewardPoolGameWorldId":  "0",
 33			"rewardPoolStatus":       "0",
 34		}
 35
 36		processID := GetParticipationProcessID(processKey)
 37		if processID != 0 {
 38			process := GetParticipationProcess(processID)
 39			state["processId"] = strconv.FormatUint(process.ID, 10)
 40			state["processKey"] = process.ProcessKey
 41			state["processGameWorldId"] = strconv.FormatUint(process.GameWorldID, 10)
 42			state["processRewardPoolKey"] = GetRewardPool(process.RewardPoolID).PoolKey
 43			state["participationFee"] = strconv.FormatInt(process.ParticipationFee, 10)
 44			state["processStatus"] = strconv.Itoa(int(process.Status))
 45		}
 46
 47		rewardPoolID := GetRewardPoolID(rewardPoolKey)
 48		if rewardPoolID != 0 {
 49			pool := GetRewardPool(rewardPoolID)
 50			state["rewardPoolId"] = strconv.FormatUint(pool.ID, 10)
 51			state["rewardPoolKey"] = pool.PoolKey
 52			state["rewardPoolGameWorldId"] = strconv.FormatUint(pool.GameWorldID, 10)
 53			state["rewardPoolStatus"] = strconv.Itoa(int(pool.Status))
 54		}
 55
 56		result = append(result, state)
 57	}
 58	return result
 59}
 60
 61// ListParticipationRefundStates returns fund, processed operation, and refund evidence for each participant.
 62func ListParticipationRefundStates(values ...string) []map[string]string {
 63	if len(values)%4 != 0 {
 64		panic("participation refund value count mismatch")
 65	}
 66	if len(values)/4 > gameVaultList {
 67		panic("count exceeds game vault list limit")
 68	}
 69
 70	result := []map[string]string{}
 71	for index := 0; index < len(values); index += 4 {
 72		processID, err := strconv.ParseUint(values[index], 10, 64)
 73		if err != nil || processID == 0 {
 74			panic("invalid participation process id")
 75		}
 76		owner := address(values[index+1])
 77		paymentKey := values[index+2]
 78		operationKey := values[index+3]
 79		assertValidOwner(owner)
 80		assertKeyLength(paymentKey, "payment key")
 81		assertKeyLength(operationKey, "operation key")
 82
 83		state := map[string]string{
 84			"processId":           strconv.FormatUint(processID, 10),
 85			"owner":               owner.String(),
 86			"paymentKey":          paymentKey,
 87			"operationKey":        operationKey,
 88			"fundId":              "0",
 89			"fundProcessId":       "0",
 90			"participationKey":    "",
 91			"fundPaymentKey":      "",
 92			"fundOwner":           "",
 93			"fundAmount":          "0",
 94			"fundRefunded":        "false",
 95			"fundEffectiveStatus": "0",
 96			"operationProcessed":  strconv.FormatBool(HasProcessedOperationKey(operationKey)),
 97			"refundId":            "0",
 98			"refundPoolId":        "0",
 99			"refundOperationKey":  "",
100			"refundSourceKey":     "",
101			"refundRecipient":     "",
102			"refundAmount":        "0",
103			"refundKind":          "",
104		}
105
106		fundID := GetParticipationFundID(processID, owner, paymentKey)
107		if fundID != 0 {
108			fund := GetParticipationFund(fundID)
109			state["fundId"] = strconv.FormatUint(fund.ID, 10)
110			state["fundProcessId"] = strconv.FormatUint(fund.ProcessID, 10)
111			state["participationKey"] = fund.ParticipationKey
112			state["fundPaymentKey"] = fund.PaymentKey
113			state["fundOwner"] = fund.Owner.String()
114			state["fundAmount"] = strconv.FormatInt(fund.Amount, 10)
115			state["fundRefunded"] = strconv.FormatBool(fund.Refunded)
116			state["fundEffectiveStatus"] = strconv.Itoa(int(GetParticipationFundEffectiveStatus(fundID)))
117		}
118
119		refundIDValue, found := stringGet(vaultRefundIDs, operationKey)
120		if found {
121			refund := GetVaultRefund(refundIDValue.(uint64))
122			state["refundId"] = strconv.FormatUint(refund.ID, 10)
123			state["refundPoolId"] = strconv.FormatUint(refund.PoolID, 10)
124			state["refundOperationKey"] = refund.OperationKey
125			state["refundSourceKey"] = refund.SourceKey
126			state["refundRecipient"] = refund.Recipient.String()
127			state["refundAmount"] = strconv.FormatInt(refund.Amount, 10)
128			state["refundKind"] = refund.Kind
129		}
130
131		result = append(result, state)
132	}
133	return result
134}