Skip to content

Commit

Permalink
Added contracts examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kpachhai committed Oct 22, 2024
1 parent a89ed82 commit 15d9da1
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 136 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,10 @@ Refer to [Datasets Demo](./docs/demos/datasets.md) to learn how to create a data
Refer to [Marketplace Demo](./docs/demos/marketplace.md) to learn how to create a publish your dataset up for sale on the Nuklai Marketplace and how to subscribe to a dataset.
### WASM smart contracts
Refer to [WASM Smart Contracts Demo](./docs/demos/wasm_smart_contracts.md) to learn how to deploy smart contracts and interact with them on NuklaiVM.
## Faucet
You can run the faucet by doing:
Expand Down
15 changes: 8 additions & 7 deletions actions/contract_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ type StateKeyPermission struct {

type ContractCall struct {
// contract is the address of the contract to be called
ContractAddress codec.Address `json:"contractAddress"`
ContractAddress codec.Address `serialize:"true" json:"contractAddress"`

// Amount are transferred to [To].
Value uint64 `json:"value"`
Value uint64 `serialize:"true" json:"value"`

// Function is the name of the function to call on the contract.
Function string `json:"function"`
Function string `serialize:"true" json:"function"`

// CallData are the serialized parameters to be passed to the contract.
CallData []byte `json:"calldata"`
CallData []byte `serialize:"true" json:"calldata"`

SpecifiedStateKeys []StateKeyPermission `json:"statekeys"`
SpecifiedStateKeys []StateKeyPermission `serialize:"true" json:"statekeys"`

Fuel uint64 `json:"fuel"`
Fuel uint64 `serialize:"true" json:"fuel"`

r *runtime.WasmRuntime
}
Expand All @@ -71,6 +71,7 @@ func (t *ContractCall) Execute(
actor codec.Address,
_ ids.ID,
) (codec.Typed, error) {

callInfo := &runtime.CallInfo{
Contract: t.ContractAddress,
Actor: actor,
Expand Down Expand Up @@ -101,7 +102,7 @@ func (*ContractCall) ValidRange(chain.Rules) (int64, int64) {
var _ chain.Marshaler = (*ContractCall)(nil)

func (t *ContractCall) Size() int {
return codec.AddressLen + 2*consts.Uint64Len + len(t.CallData) + len(t.Function) + len(t.SpecifiedStateKeys)
return codec.AddressLen + 2*consts.Uint64Len + codec.BytesLen(t.CallData) + codec.StringLen(t.Function) + len(t.SpecifiedStateKeys)
}

func (t *ContractCall) Marshal(p *codec.Packer) {
Expand Down
8 changes: 4 additions & 4 deletions actions/contract_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ var _ chain.Action = (*ContractDeploy)(nil)
const MAXCREATIONSIZE = units.MiB

type ContractDeploy struct {
ContractID runtime.ContractID `json:"contractID"`
CreationInfo []byte `json:"creationInfo"`
ContractID runtime.ContractID `serialize:"true" json:"contractID"`
CreationInfo []byte `serialize:"true" json:"creationInfo"`
address codec.Address
}

Expand Down Expand Up @@ -68,7 +68,7 @@ func (*ContractDeploy) ValidRange(chain.Rules) (int64, int64) {
var _ chain.Marshaler = (*ContractDeploy)(nil)

func (d *ContractDeploy) Size() int {
return len(d.CreationInfo) + len(d.ContractID)
return codec.BytesLen(d.CreationInfo) + codec.BytesLen(d.ContractID)
}

func (d *ContractDeploy) Marshal(p *codec.Packer) {
Expand All @@ -78,7 +78,7 @@ func (d *ContractDeploy) Marshal(p *codec.Packer) {

func UnmarshalDeployContract(p *codec.Packer) (chain.Action, error) {
var deployContract ContractDeploy
p.UnpackBytes(36, true, (*[]byte)(&deployContract.ContractID))
p.UnpackBytes(40, true, (*[]byte)(&deployContract.ContractID))
p.UnpackBytes(MAXCREATIONSIZE, false, &deployContract.CreationInfo)
deployContract.address = storage.GetAddressForDeploy(0, deployContract.CreationInfo)
if err := p.Err(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion actions/contract_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (*ContractPublish) ValidRange(chain.Rules) (int64, int64) {
var _ chain.Marshaler = (*ContractPublish)(nil)

func (t *ContractPublish) Size() int {
return 4 + len(t.ContractBytes)
return codec.BytesLen(t.ContractBytes)
}

func (t *ContractPublish) Marshal(p *codec.Packer) {
Expand Down
76 changes: 38 additions & 38 deletions cmd/nuklai-cli/cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,43 @@ var publishFileCmd = &cobra.Command{
},
}

var deployCmd = &cobra.Command{
Use: "deploy",
RunE: func(*cobra.Command, []string) error {
ctx := context.Background()
_, _, factory, cli, bcli, ws, err := handler.DefaultActor()
if err != nil {
return err
}

contractID, err := prompt.Bytes("contract id")
if err != nil {
return err
}

creationInfo, err := prompt.Bytes("creation info")
if err != nil {
return err
}

// Confirm action
cont, err := prompt.Continue()
if !cont || err != nil {
return err
}

// Generate transaction
result, _, err := sendAndWait(ctx, []chain.Action{&actions.ContractDeploy{
ContractID: contractID,
CreationInfo: creationInfo,
}}, cli, bcli, ws, factory)
if err != nil {
return err
}
return processResult(result)
},
}

var callCmd = &cobra.Command{
Use: "call",
RunE: func(*cobra.Command, []string) error {
Expand All @@ -144,7 +181,7 @@ var callCmd = &cobra.Command{
}

// Get balance info
balance, err := handler.GetBalance(ctx, bcli, priv.Address, ids.Empty)
balance, err := handler.GetBalance(ctx, bcli, priv.Address)
if balance == 0 || err != nil {
return err
}
Expand Down Expand Up @@ -236,43 +273,6 @@ var callCmd = &cobra.Command{
},
}

var deployCmd = &cobra.Command{
Use: "deploy",
RunE: func(*cobra.Command, []string) error {
ctx := context.Background()
_, _, factory, cli, bcli, ws, err := handler.DefaultActor()
if err != nil {
return err
}

contractID, err := prompt.Bytes("contract id")
if err != nil {
return err
}

creationInfo, err := prompt.Bytes("creation info")
if err != nil {
return err
}

// Confirm action
cont, err := prompt.Continue()
if !cont || err != nil {
return err
}

// Generate transaction
result, _, err := sendAndWait(ctx, []chain.Action{&actions.ContractDeploy{
ContractID: contractID,
CreationInfo: creationInfo,
}}, cli, bcli, ws, factory)
if err != nil {
return err
}
return processResult(result)
},
}

// Define the layout that matches the provided date string
// Note: the reference time is "Mon Jan 2 15:04:05 MST 2006" in Go
const (
Expand Down
5 changes: 2 additions & 3 deletions cmd/nuklai-cli/cmd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,8 @@ func (*Handler) GetBalance(
ctx context.Context,
cli *vm.JSONRPCClient,
addr codec.Address,
asset ids.ID,
) (uint64, error) {
balance, err := cli.Balance(ctx, addr.String(), asset.String())
balance, err := cli.Balance(ctx, addr.String(), storage.NAIAddress.String())
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -603,6 +602,6 @@ func (*Controller) HandleTx(tx *chain.Transaction, result *chain.Result) {

func (*Controller) LookupBalance(address codec.Address, uri string) (uint64, error) {
cli := vm.NewJSONRPCClient(uri)
balance, err := cli.Balance(context.TODO(), address.String(), ids.Empty.String())
balance, err := cli.Balance(context.TODO(), address.String(), storage.NAIAddress.String())
return balance, err
}
18 changes: 7 additions & 11 deletions cmd/nuklai-cli/cmd/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"runtime"
"sync"

"github.com/nuklai/nuklaivm/storage"
"github.com/nuklai/nuklaivm/vm"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -136,10 +135,12 @@ var setKeyCmd = &cobra.Command{
var balanceKeyCmd = &cobra.Command{
Use: "balance [address]",
RunE: func(_ *cobra.Command, args []string) error {
var (
addr codec.Address
err error
)
ctx := context.Background()
_, _, _, _, bcli, _, err := handler.DefaultActor()
if err != nil {
return err
}
var addr codec.Address
if len(args) != 1 {
addr, _, err = handler.h.GetDefaultKey(true)
if err != nil {
Expand All @@ -151,15 +152,10 @@ var balanceKeyCmd = &cobra.Command{
return err
}
}
nclients, err := handler.DefaultNuklaiVMJSONRPCClient(checkAllChains)
_, err = handler.GetBalance(ctx, bcli, addr)
if err != nil {
return err
}
for _, ncli := range nclients {
if _, _, _, _, _, _, _, _, _, _, _, _, _, err := handler.GetAssetInfo(context.TODO(), ncli, addr, storage.NAIAddress, true, false, -1); err != nil {
return err
}
}
return nil
},
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.5
require (
github.com/ava-labs/avalanchego v1.11.12-rc.2.0.20241001202925-f03745d187d0
github.com/ava-labs/hypersdk v0.0.18-0.20241018181853-22241f53b9ff
github.com/bytecodealliance/wasmtime-go/v14 v14.0.0
github.com/fatih/color v1.13.0
github.com/gorilla/mux v1.8.0
github.com/manifoldco/promptui v0.9.0
Expand All @@ -15,6 +16,7 @@ require (
github.com/status-im/keycard-go v0.2.0
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.26.0
golang.org/x/exp v0.0.0-20231127185646-65229373498e
golang.org/x/time v0.3.0
gopkg.in/yaml.v2 v2.4.0
)
Expand All @@ -29,7 +31,6 @@ require (
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
github.com/bytecodealliance/wasmtime-go/v14 v14.0.0 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chzyer/readline v1.5.1 // indirect
Expand Down Expand Up @@ -137,7 +138,6 @@ require (
go.uber.org/mock v0.4.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20231127185646-65229373498e // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
Expand Down
70 changes: 0 additions & 70 deletions storage/recorder.go

This file was deleted.

0 comments on commit 15d9da1

Please sign in to comment.