-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Dan Sover <dan.sover@avalabs.org>
- Loading branch information
Showing
3 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package examples | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ava-labs/avalanchego/utils/logging" | ||
|
||
"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 { | ||
return &NFT{ | ||
log: log, | ||
programBytes: programBytes, | ||
metadata: m, | ||
maxFee: maxFee, | ||
costMap: costMap, | ||
} | ||
} | ||
|
||
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 | ||
} | ||
|
||
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) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
t.log.Debug("initial cost", | ||
zap.Int("gas", 0), | ||
) | ||
|
||
// Generate keys for Alice | ||
// She deploys the NFT contract and is the original owner | ||
alicePtr, _, err := nftKeyPtr(ctx, runtime) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = runtime.Call(ctx, "init", contractId, alicePtr) | ||
if err != nil { | ||
return err | ||
} | ||
t.log.Debug("init called", | ||
zap.String("alicePtr", fmt.Sprint(alicePtr)), | ||
) | ||
|
||
// mint 1 token, send to alice | ||
mintAlice := uint64(1) | ||
_, err = runtime.Call(ctx, "mint", contractId, alicePtr, mintAlice) | ||
if err != nil { | ||
return err | ||
} | ||
t.log.Debug("minted", | ||
zap.Uint64("alice", mintAlice), | ||
) | ||
|
||
// Generate keys for Bob | ||
bobPtr, _, err := nftKeyPtr(ctx, runtime) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// transfer NFT from Alice to Bob | ||
_, err = runtime.Call(ctx, "transfer", contractId, alicePtr, bobPtr) | ||
if err != nil { | ||
return err | ||
} | ||
t.log.Debug("transfer", | ||
zap.Uint64("alice", mintAlice), | ||
) | ||
|
||
return nil | ||
} | ||
|
||
func nftKeyPtr(ctx context.Context, runtime runtime.Runtime) (uint64, ed25519.PublicKey, error) { | ||
priv, err := ed25519.GeneratePrivateKey() | ||
if err != nil { | ||
return 0, ed25519.EmptyPublicKey, err | ||
} | ||
|
||
pk := priv.PublicKey() | ||
ptr, err := runtime.WriteGuestBuffer(ctx, pk[:]) | ||
return ptr, pk, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package examples | ||
|
||
import ( | ||
"context" | ||
_ "embed" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/avalanchego/utils/logging" | ||
) | ||
|
||
var ( | ||
//go:embed testdata/token.wasm | ||
nftProgramBytes []byte | ||
|
||
// example cost map | ||
nftCostMap = map[string]uint64{ | ||
"ConstI32 0x0": 1, | ||
"ConstI64 0x0": 2, | ||
} | ||
nftMaxGas uint64 = 13000 | ||
nftLog = logging.NewLogger( | ||
"", | ||
logging.NewWrappedCore( | ||
logging.Debug, | ||
os.Stderr, | ||
logging.Plain.ConsoleEncoder(), | ||
)) | ||
|
||
metadata = Metadata{ | ||
Name: "My NFT", | ||
Symbol: "MNFT", | ||
URI: "ipfs://my-nft.jpg", | ||
} | ||
) | ||
|
||
// go test -v -timeout 30s -run ^TestNFTProgram$ github.com/ava-labs/hypersdk/x/programs/examples | ||
func TestNFTProgram(t *testing.T) { | ||
require := require.New(t) | ||
|
||
program := NewNFT(nftLog, nftProgramBytes, nftMaxGas, nftCostMap, metadata) | ||
err := program.Run(context.Background()) | ||
require.NoError(err) | ||
} | ||
|
||
// go test -v -benchmark -run=^$ -bench ^BenchmarkNFTProgram$ github.com/ava-labs/hypersdk/x/programs/examples -memprofile benchvset.mem -cpuprofile benchvset.cpu | ||
func BenchmarkNFTProgram(b *testing.B) { | ||
require := require.New(b) | ||
program := NewNFT(nftLog, nftProgramBytes, nftMaxGas, nftCostMap, metadata) | ||
b.ResetTimer() | ||
b.Run("benchmark_nft_program", func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
err := program.Run(context.Background()) | ||
require.NoError(err) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters