Skip to content

Commit 69aa454

Browse files
committed
Fix create reward code
1 parent 90901ed commit 69aa454

File tree

1 file changed

+47
-117
lines changed

1 file changed

+47
-117
lines changed

api/v1_create_reward_code.go

Lines changed: 47 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -227,125 +227,55 @@ func (app *ApiServer) createRewardCode(ctx context.Context, code, mint string, a
227227

228228
// Only create reward pool if deterministic secret is configured
229229
if app.config.LaunchpadDeterministicSecret != "" {
230-
app.logger.Info("createRewardCode: Deterministic secret configured, checking for existing reward pool",
231-
zap.String("mint", mint))
232-
// Check for existing reward address for this mint (reuse pattern)
233-
var existingRewardAddress string
234-
err := app.pool.QueryRow(ctx, `
235-
SELECT reward_address FROM reward_codes
236-
WHERE mint = $1 AND reward_address IS NOT NULL AND reward_address != ''
237-
LIMIT 1
238-
`, mint).Scan(&existingRewardAddress)
239-
240-
if err == nil && existingRewardAddress != "" {
241-
// Reuse existing reward pool
242-
app.logger.Info("createRewardCode: Reusing existing reward pool",
243-
zap.String("mint", mint),
244-
zap.String("reward_address", existingRewardAddress))
245-
rewardAddress = existingRewardAddress
246-
} else {
247-
if err != nil && err != pgx.ErrNoRows {
248-
app.logger.Warn("createRewardCode: Error checking for existing reward pool, will create new",
249-
zap.String("mint", mint),
250-
zap.Error(err))
251-
} else {
252-
app.logger.Info("createRewardCode: No existing reward pool found, creating new",
253-
zap.String("mint", mint))
254-
}
255-
256-
// Create new reward pool
257-
app.logger.Info("createRewardCode: Parsing mint public key",
258-
zap.String("mint", mint))
259-
mintPubKey, err := solana.PublicKeyFromBase58(mint)
260-
if err != nil {
261-
app.logger.Error("createRewardCode: Invalid mint address",
262-
zap.String("mint", mint),
263-
zap.Error(err))
264-
return "", fmt.Errorf("invalid mint address: %w", err)
265-
}
266-
267-
app.logger.Info("createRewardCode: Deriving Ethereum address for mint",
268-
zap.String("mint", mint))
269-
claimAuthority, claimAuthorityPrivateKey, err := utils.DeriveEthAddressForMint(
270-
[]byte("claimAuthority"),
271-
app.config.LaunchpadDeterministicSecret,
272-
mintPubKey,
273-
)
274-
if err != nil {
275-
app.logger.Error("createRewardCode: Failed to derive Ethereum key",
276-
zap.String("mint", mint),
277-
zap.Error(err))
278-
return "", fmt.Errorf("failed to derive Ethereum key: %w", err)
279-
}
280-
app.logger.Info("createRewardCode: Ethereum address derived",
281-
zap.String("claim_authority", claimAuthority),
282-
zap.String("mint", mint))
283-
284-
// Convert the private key to the format expected by the SDK
285-
app.logger.Info("createRewardCode: Converting private key format")
286-
privateKey, err := common.EthToEthKey(claimAuthorityPrivateKey)
287-
if err != nil {
288-
app.logger.Error("createRewardCode: Failed to convert private key",
289-
zap.Error(err))
290-
return "", fmt.Errorf("failed to convert private key: %w", err)
291-
}
292-
293-
// Create OpenAudio SDK instance and set the private key
294-
app.logger.Info("createRewardCode: Creating OpenAudio SDK instance",
295-
zap.String("audiusd_url", app.config.AudiusdURL))
296-
oap := sdk.NewOpenAudioSDK(app.config.AudiusdURL)
297-
oap.SetPrivKey(privateKey)
298-
299-
// Get current chain status to calculate deadline
300-
app.logger.Info("createRewardCode: Getting chain status")
301-
statusResp, err := oap.Core.GetStatus(ctx, connect.NewRequest(&v1.GetStatusRequest{}))
302-
if err != nil {
303-
app.logger.Error("createRewardCode: Failed to get chain status",
304-
zap.String("audiusd_url", app.config.AudiusdURL),
305-
zap.Error(err))
306-
return "", fmt.Errorf("failed to get chain status: %w", err)
307-
}
308-
309-
currentHeight := statusResp.Msg.ChainInfo.CurrentHeight
310-
deadline := currentHeight + 100
311-
rewardID := code
312-
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
316-
app.logger.Info("createRewardCode: Creating reward pool",
317-
zap.String("reward_id", rewardID),
318-
zap.String("name", fmt.Sprintf("%s Reward %s", rewardName, code)),
319-
zap.Int64("amount_whole_yak", amount),
320-
zap.Uint64("amount_smallest_units", uint64(amountInSmallestUnits)),
321-
zap.String("claim_authority", claimAuthority),
322-
zap.Int64("deadline", deadline))
323-
324-
reward, err := oap.Rewards.CreateReward(ctx, &v1.CreateReward{
325-
RewardId: rewardID,
326-
Name: fmt.Sprintf("%s Reward %s", rewardName, code),
327-
Amount: uint64(amountInSmallestUnits),
328-
ClaimAuthorities: []*v1.ClaimAuthority{
329-
{Address: claimAuthority, Name: rewardName},
330-
},
331-
DeadlineBlockHeight: deadline,
332-
})
333-
if err != nil {
334-
app.logger.Error("createRewardCode: Failed to create reward pool via OpenAudio SDK",
335-
zap.String("reward_id", rewardID),
336-
zap.String("audiusd_url", app.config.AudiusdURL),
337-
zap.Error(err))
338-
return "", fmt.Errorf("failed to create reward pool: %w", err)
339-
}
340-
341-
rewardAddress = reward.Address
342-
app.logger.Info("createRewardCode: Reward pool created successfully",
343-
zap.String("reward_address", rewardAddress),
344-
zap.String("reward_id", rewardID))
230+
mintPubKey, err := solana.PublicKeyFromBase58(mint)
231+
if err != nil {
232+
return "", fmt.Errorf("invalid mint address: %w", err)
233+
}
234+
235+
claimAuthority, claimAuthorityPrivateKey, err := utils.DeriveEthAddressForMint(
236+
[]byte("claimAuthority"),
237+
app.config.LaunchpadDeterministicSecret,
238+
mintPubKey,
239+
)
240+
if err != nil {
241+
return "", fmt.Errorf("failed to derive Ethereum key: %w", err)
345242
}
243+
244+
// Convert the private key to the format expected by the SDK
245+
privateKey, err := common.EthToEthKey(claimAuthorityPrivateKey)
246+
if err != nil {
247+
return "", fmt.Errorf("failed to convert private key: %w", err)
248+
}
249+
250+
// Create OpenAudio SDK instance and set the private key
251+
oap := sdk.NewOpenAudioSDK(app.config.AudiusdURL)
252+
oap.SetPrivKey(privateKey)
253+
254+
// Get current chain status to calculate deadline
255+
statusResp, err := oap.Core.GetStatus(context.Background(), connect.NewRequest(&v1.GetStatusRequest{}))
256+
if err != nil {
257+
return "", fmt.Errorf("failed to get chain status: %w", err)
258+
}
259+
260+
currentHeight := statusResp.Msg.ChainInfo.CurrentHeight
261+
deadline := currentHeight + 100
262+
rewardID := fmt.Sprintf("%s", code)
263+
264+
reward, err := oap.Rewards.CreateReward(context.Background(), &v1.CreateReward{
265+
RewardId: rewardID,
266+
Name: fmt.Sprintf("Launchpad Reward %s", code),
267+
Amount: uint64(amount),
268+
ClaimAuthorities: []*v1.ClaimAuthority{
269+
{Address: claimAuthority, Name: "Launchpad"},
270+
},
271+
DeadlineBlockHeight: deadline,
272+
})
273+
if err != nil {
274+
return "", fmt.Errorf("failed to create reward pool: %w", err)
275+
}
276+
277+
rewardAddress = reward.Address
346278
} else {
347-
app.logger.Info("createRewardCode: No deterministic secret configured, skipping reward pool creation",
348-
zap.String("mint", mint))
349279
rewardAddress = ""
350280
}
351281

0 commit comments

Comments
 (0)