Skip to content
Merged
52 changes: 44 additions & 8 deletions examples/rewards/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"
"crypto/ed25519"
"encoding/hex"
"fmt"
"log"
"os"
Expand All @@ -10,6 +12,7 @@ import (
v1 "github.com/OpenAudio/go-openaudio/pkg/api/core/v1"
"github.com/OpenAudio/go-openaudio/pkg/common"
"github.com/OpenAudio/go-openaudio/pkg/sdk"
"github.com/mr-tron/base58/base58"
)

func main() {
Expand Down Expand Up @@ -38,15 +41,48 @@ func main() {
currentHeight := resp.Msg.ChainInfo.CurrentHeight
deadline := currentHeight + 100

// CreateRewardPool requires possession of the RM's ed25519 keypair —
// the same keypair that signed the InitRewardManager instruction on
// Solana. The launchpad relay derives this from
// (launchpadDeterministicSecret, mint) and so always has it.
//
// REWARDS_MANAGER_SECRET_HEX is the 64-byte ed25519 secret key, hex-
// encoded. We derive the public key (which IS the rewards_manager_pubkey,
// base58-encoded) from it.
secretHex := os.Getenv("REWARDS_MANAGER_SECRET_HEX")
if secretHex == "" {
log.Fatalf("REWARDS_MANAGER_SECRET_HEX environment variable is not set (64-byte ed25519 secret, hex-encoded)")
}
rmSecret, err := hex.DecodeString(secretHex)
if err != nil {
log.Fatalf("Failed to decode REWARDS_MANAGER_SECRET_HEX: %v", err)
}
if len(rmSecret) != ed25519.PrivateKeySize {
log.Fatalf("REWARDS_MANAGER_SECRET_HEX must decode to %d bytes; got %d", ed25519.PrivateKeySize, len(rmSecret))
}
rmPrivKey := ed25519.PrivateKey(rmSecret)
rewardsManagerPubkey := base58.Encode(rmPrivKey.Public().(ed25519.PublicKey))
Comment thread
rickyrombo marked this conversation as resolved.
Outdated

// First-class CreateReward requires an existing pool keyed by the
// reward manager pubkey. Create one — fail loudly on any error so the
// next call doesn't proceed against a broken setup. If the pool was
// created in a previous run, this will surface as a "pool already
// exists" error and the example needs to be rerun against a fresh RM
// pubkey (or the existing pool's tx hash recorded for the reuse path).
authorities := []string{oap.Address()}
if _, err := oap.Rewards.CreateRewardPool(context.Background(), &v1.CreateRewardPool{
RewardsManagerPubkey: rewardsManagerPubkey,
Authorities: authorities,
}, rmPrivKey, deadline); err != nil {
log.Fatalf("Failed to create reward pool: %v", err)
}

reward, err := oap.Rewards.CreateReward(context.Background(), &v1.CreateReward{
RewardId: "reward1",
Name: "Test Reward 1",
Amount: 1000,
ClaimAuthorities: []*v1.ClaimAuthority{
{Address: oap.Address(), Name: "Alec"},
},
DeadlineBlockHeight: deadline,
})
RewardId: "reward1",
Name: "Test Reward 1",
Amount: 1000,
RewardsManagerPubkey: rewardsManagerPubkey,
}, deadline)
if err != nil {
log.Fatalf("Failed to create reward: %v", err)
}
Expand Down
221 changes: 115 additions & 106 deletions pkg/api/core/v1/service.pb.go

Large diffs are not rendered by default.

Loading
Loading