diff --git a/x/programs/examples/nft.go b/x/programs/examples/nft.go index e7eea29689..06130c1014 100644 --- a/x/programs/examples/nft.go +++ b/x/programs/examples/nft.go @@ -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 }