Skip to content

Commit

Permalink
fix: Rebase nft.go off latest runtime changes
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Sover <dan.sover@avalabs.org>
  • Loading branch information
exdx committed Oct 17, 2023
1 parent f2d0d0b commit d832d3f
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions x/programs/examples/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,84 +5,84 @@ package examples

import (
"context"
"fmt"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/hypersdk/state"
"github.com/ava-labs/hypersdk/x/programs/examples/storage"

"github.com/ava-labs/hypersdk/crypto/ed25519"
"github.com/ava-labs/hypersdk/x/programs/runtime"
"github.com/ava-labs/hypersdk/x/programs/utils"

"go.uber.org/zap"
)

func NewNFT(log logging.Logger, programBytes []byte, maxFee uint64, costMap map[string]uint64, m Metadata) *NFT {
func NewNFT(log logging.Logger, programBytes []byte, cfg *runtime.Config, imports runtime.SupportedImports, db state.Mutable) *NFT {
return &NFT{
log: log,
programBytes: programBytes,
metadata: m,
maxFee: maxFee,
costMap: costMap,
cfg: cfg,
imports: imports,
db: db,
}
}

type NFT struct {
log logging.Logger
programBytes []byte
metadata Metadata
// metering
maxFee uint64
costMap map[string]uint64
}

type Metadata struct {
Name string
Symbol string
URI string
cfg *runtime.Config
imports runtime.SupportedImports
db state.Mutable
}

func (t *NFT) Run(ctx context.Context) error {
meter := runtime.NewMeter(t.log, t.maxFee, t.costMap)
db := utils.NewTestDB()
store := newProgramStorage(db)

runtime := runtime.New(t.log, meter, store)
contractId, err := runtime.Create(ctx, t.programBytes)
rt := runtime.New(t.log, t.cfg, t.imports)
err := rt.Initialize(ctx, t.programBytes)
if err != nil {
return err
}

t.log.Debug("initial cost",
zap.Int("gas", 0),
t.log.Debug("initial meter",
zap.Uint64("balance", rt.Meter().GetBalance()),
)

// Generate keys for Alice
alicePtr, _, err := nftKeyPtr(ctx, runtime)
// simulate create program transaction
programID := ids.GenerateTestID()
err = storage.SetProgram(ctx, t.db, programID, t.programBytes)
if err != nil {
return err
}

_, err = runtime.Call(ctx, "init", contractId)
programIDPtr, err := runtime.WriteBytes(rt.Memory(), programID[:])
if err != nil {
return err
}
t.log.Debug("init called")

// mint 1 token, send to alice
_, err = runtime.Call(ctx, "mint", contractId, alicePtr)
// initialize program
resp, err := rt.Call(ctx, "init", programIDPtr)
if err != nil {
return fmt.Errorf("failed to initialize program: %w", err)
}

t.log.Debug("init response",
zap.Uint64("init", resp[0]),
)

_, aliceKey, err := newKey()
if err != nil {
return err
}
t.log.Debug("minted")

return nil
}
// write alice's key to stack and get pointer
alicePtr, err := newKeyPtr(ctx, aliceKey, rt)
if err != nil {
return err
}

func nftKeyPtr(ctx context.Context, runtime runtime.Runtime) (uint64, ed25519.PublicKey, error) {
priv, err := ed25519.GeneratePrivateKey()
// mint 1 token, send to alice
_, err = rt.Call(ctx, "mint", programIDPtr, alicePtr)
if err != nil {
return 0, ed25519.EmptyPublicKey, err
return err
}
t.log.Debug("minted")

pk := priv.PublicKey()
ptr, err := runtime.WriteGuestBuffer(ctx, pk[:])
return ptr, pk, err
return nil
}

0 comments on commit d832d3f

Please sign in to comment.