Skip to content

Commit ac9fdac

Browse files
committed
Fix prize amount config
1 parent 7aee7c4 commit ac9fdac

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

api/v1_create_reward_code.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,17 +310,21 @@ func (app *ApiServer) createRewardCode(ctx context.Context, code, mint string, a
310310
deadline := currentHeight + 100
311311
rewardID := code
312312

313+
// Convert from whole YAK (as stored in database) to smallest units for OpenAudio SDK
314+
// reward_codes.amount stores whole YAK, but OpenAudio SDK expects smallest units (9 decimals)
315+
amountInSmallestUnits := amount * 1000000000
313316
app.logger.Info("createRewardCode: Creating reward pool",
314317
zap.String("reward_id", rewardID),
315318
zap.String("name", fmt.Sprintf("%s Reward %s", rewardName, code)),
316-
zap.Uint64("amount", uint64(amount)),
319+
zap.Int64("amount_whole_yak", amount),
320+
zap.Uint64("amount_smallest_units", uint64(amountInSmallestUnits)),
317321
zap.String("claim_authority", claimAuthority),
318322
zap.Int64("deadline", deadline))
319323

320324
reward, err := oap.Rewards.CreateReward(ctx, &v1.CreateReward{
321325
RewardId: rewardID,
322326
Name: fmt.Sprintf("%s Reward %s", rewardName, code),
323-
Amount: uint64(amount),
327+
Amount: uint64(amountInSmallestUnits),
324328
ClaimAuthorities: []*v1.ClaimAuthority{
325329
{Address: claimAuthority, Name: rewardName},
326330
},

api/v1_prizes_claim.go

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import (
1616

1717
const (
1818
yakMintAddress = "ZDaUDL4XFdEct7UgeztrFQAptsvh4ZdhyZDZ1RpxYAK"
19-
yakClaimAmount = 2000000000 // 2 YAK with 9 decimals - amount required to claim a prize
20-
yakAirdropAmount = 1000000000 // 1 YAK with 9 decimals - amount awarded in coin airdrop prizes
19+
yakClaimAmount = 2000000000 // 2 YAK with 9 decimals - amount required to claim a prize (for Solana transaction)
20+
yakAirdropAmount = 1 // 1 YAK (whole YAK, no decimals) - amount awarded in coin airdrop prizes
2121
prizeReceiverAddress = "EHd892m3xNWGBuAXnafavqcFjXXUZp9bGecdSDNP2SLR"
2222
)
2323

@@ -236,7 +236,8 @@ func (app *ApiServer) v1PrizesClaim(c *fiber.Ctx) error {
236236
var metadata PrizeMetadata
237237
if err := json.Unmarshal([]byte(selectedPrize.Metadata.String), &metadata); err == nil {
238238
if metadata.Type == "coin_airdrop" && metadata.Amount == yakAirdropAmount {
239-
code, url, err := app.generateRedeemCodeForPrize(ctx, yakMintAddress, yakAirdropAmount)
239+
// yakAirdropAmount is already in whole YAK (no decimals)
240+
code, url, err := app.generateRedeemCodeForPrize(ctx, yakMintAddress, int64(yakAirdropAmount))
240241
if err != nil {
241242
app.logger.Error("Failed to generate redeem code", zap.Error(err))
242243
return fiber.NewError(fiber.StatusInternalServerError, "Failed to generate redeem code")
@@ -278,6 +279,14 @@ func (app *ApiServer) v1PrizesClaim(c *fiber.Ctx) error {
278279
RETURNING prize_id, prize_name, wallet, prize_type, action_data
279280
`
280281

282+
app.logger.Info("Inserting claimed prize into database",
283+
zap.String("wallet", req.Wallet),
284+
zap.String("signature", req.Signature),
285+
zap.String("prize_id", selectedPrize.ID),
286+
zap.String("prize_name", selectedPrize.Name),
287+
zap.Any("prize_type", prizeType),
288+
zap.Bool("has_action_data", len(actionData) > 0))
289+
281290
rows, err := app.pool.Query(ctx, sql, pgx.NamedArgs{
282291
"wallet": req.Wallet,
283292
"signature": req.Signature,
@@ -289,16 +298,35 @@ func (app *ApiServer) v1PrizesClaim(c *fiber.Ctx) error {
289298
"action_data": actionData,
290299
})
291300
if err != nil {
292-
app.logger.Error("Failed to insert claimed prize", zap.Error(err))
301+
app.logger.Error("Failed to insert claimed prize",
302+
zap.String("wallet", req.Wallet),
303+
zap.String("signature", req.Signature),
304+
zap.Error(err))
293305
return fiber.NewError(fiber.StatusInternalServerError, "Failed to save result")
294306
}
307+
app.logger.Info("Successfully inserted claimed prize, reading response")
295308

296309
response, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[PrizeClaimResponse])
297310
if err != nil {
298-
app.logger.Error("Failed to read response", zap.Error(err))
311+
app.logger.Error("Failed to read response from database",
312+
zap.String("wallet", req.Wallet),
313+
zap.String("signature", req.Signature),
314+
zap.String("prize_id", selectedPrize.ID),
315+
zap.Error(err))
316+
// Try to get more details about the error
317+
if err == pgx.ErrNoRows {
318+
app.logger.Error("No rows returned from INSERT...RETURNING query")
319+
}
299320
return fiber.NewError(fiber.StatusInternalServerError, "Failed to read response")
300321
}
301322

323+
app.logger.Info("Successfully read response",
324+
zap.String("prize_id", response.PrizeID),
325+
zap.String("prize_name", response.PrizeName),
326+
zap.String("wallet", response.Wallet),
327+
zap.Any("prize_type", response.PrizeType),
328+
zap.Bool("has_action_data", len(response.ActionData) > 0))
329+
302330
return c.JSON(response)
303331
}
304332

api/v1_prizes_claim_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func TestV1PrizesClaim(t *testing.T) {
8282
const (
8383
yakMintAddress = "ZDaUDL4XFdEct7UgeztrFQAptsvh4ZdhyZDZ1RpxYAK"
8484
yakClaimAmount = 2000000000 // 2 YAK with 9 decimals - amount required to claim a prize
85-
yakAirdropAmount = 1000000000 // 1 YAK with 9 decimals - amount awarded in coin airdrop prizes
85+
yakAirdropAmount = 1 // 1 YAK (whole YAK, no decimals) - amount awarded in coin airdrop prizes
8686
prizeReceiverAddress = "EHd892m3xNWGBuAXnafavqcFjXXUZp9bGecdSDNP2SLR"
8787
validWallet = "HLnpSz9h2S4hiLQ43rnSD9XkcUThA7B8hQMKmDaiTLcC"
8888
validSignature = "valid_signature_123"

0 commit comments

Comments
 (0)