Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix examples #793

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion access/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func (h *httpHandler) getExecutionResults(
u := h.mustBuildURL("/execution_results", opts...)

q := u.Query()
q.Add("block_ids", strings.Join(blockIDs, ","))
q.Add("block_id", strings.Join(blockIDs, ","))
u.RawQuery = q.Encode()

var results []models.ExecutionResult
Expand Down
2 changes: 1 addition & 1 deletion access/http/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ func TestHandler_GetExecResult(t *testing.T) {

u, _ := url.Parse("/execution_results")
q := u.Query()
q.Add("block_ids", strings.Join(ids, ","))
q.Add("block_id", strings.Join(ids, ","))
u.RawQuery = q.Encode()

req.SetData(*u, fixture)
Expand Down
24 changes: 20 additions & 4 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.PHONY: all
all: get-blocks get-accounts get-events get-collection get-network-parameters get-transactions execute-script send-transaction create-account add-account-key deploy-contract storage-usage transaction-arguments single-party single-party-multisig multi-party multi-party-multisig user-signature user-signature-validate-all user-signature-validate-any http-grpc-clients modify-account
all: get-blocks get-accounts get-events get-collection get-network-parameters get-transactions execute-script send-transactions create-account add-account-key deploy-contract storage-usage transaction-arguments single-party single-party-multisig multi-party multi-party-multisig user-signature user-signature-validate-all user-signature-validate-any http-grpc-clients modify-account get-execution-data

.PHONY: create-account
create-account:
Expand Down Expand Up @@ -47,7 +47,7 @@ multi-party-multisig:

.PHONY: user-signature
user-signature:
go run ./user_signature/main.go
go run ./verify_signature/user_signature/main.go

.PHONY: get-blocks
get-blocks:
Expand Down Expand Up @@ -77,8 +77,8 @@ get-transactions:
execute-script:
go run ./execute_script/main.go

.PHONY: send-transaction
send-transaction:
.PHONY: send-transactions
send-transactions:
go run ./send_transactions/main.go

.PHONY: user-signature-validate-all
Expand All @@ -96,3 +96,19 @@ http-grpc-clients:
.PHONY: modify-account
modify-account:
go run ./modify_account/main.go

.PHONY: get-execution-data
get-execution-data:
go run ./get_execution_data/main.go

.PHONY: stream-events
stream-events:
go run ./stream_events/main.go

.PHONY: stream-events-reconnect
stream-events-reconnect:
go run ./stream_events_reconnect/main.go

.PHONY: stream-execution-data
stream-execution-data:
go run ./stream_execution_data/main.go
4 changes: 2 additions & 2 deletions examples/deploy_contract/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func GenerateMintScript(nftCodeAddr flow.Address) []byte {
import GreatToken from 0x%s

transaction {
prepare(acct: auth(Storage) &Account) {
prepare(acct: auth(Storage, Capabilities) &Account) {
let minter = acct.storage.borrow<&GreatToken.GreatNFTMinter>(from: /storage/GreatNFTMinter)!
if let nft <- acct.storage.load<@GreatToken.GreatNFT>(from: /storage/GreatNFT) {
destroy nft
Expand All @@ -236,7 +236,7 @@ func GenerateGetNFTIDScript(nftCodeAddr, userAddr flow.Address) []byte {
template := `
import GreatToken from 0x%s

pub fun main(): Int {
access(all) fun main(): Int {
let acct = getAccount(0x%s)
let nft = acct.capabilities.borrow<&GreatToken.GreatNFT>(/public/GreatNFT)!
return nft.id()
Expand Down
6 changes: 3 additions & 3 deletions examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"time"

jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/flow-cli/flowkit/config"
"github.com/onflow/flowkit/config"
"github.com/spf13/afero"

"github.com/onflow/flow-go-sdk"
Expand All @@ -38,7 +38,7 @@ import (
"github.com/onflow/flow-go-sdk/templates"

"github.com/onflow/cadence"
"github.com/onflow/flow-cli/flowkit/config/json"
"github.com/onflow/flowkit/config/json"

"github.com/onflow/cadence/sema"
)
Expand Down Expand Up @@ -110,7 +110,7 @@ func RandomTransaction(flowClient access.Client) *flow.Transaction {
tx := flow.NewTransaction().
SetPayer(serviceAcctAddr).
SetProposalKey(serviceAcctAddr, serviceAcctKey.Index, serviceAcctKey.SequenceNumber).
SetScript([]byte("transaction { prepare(auth: AuthAccount) {} }")).
SetScript([]byte("transaction { prepare(signer: auth(Storage) &Account) {} }")).
AddAuthorizer(serviceAcctAddr).
SetReferenceBlockID(GetReferenceBlockId(flowClient))

Expand Down
38 changes: 26 additions & 12 deletions examples/execute_script/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package main

import (
"context"
"errors"
"fmt"

"github.com/onflow/flow-go-sdk/access/http"
Expand All @@ -41,7 +42,7 @@ func demo() {
examples.Handle(err)

script := []byte(`
pub fun main(a: Int): Int {
access(all) fun main(a: Int): Int {
return a + 10
}
`)
Expand All @@ -52,10 +53,10 @@ func demo() {
fmt.Printf("\nValue: %s", value.String())

complexScript := []byte(`
pub struct User {
pub var balance: UFix64
pub var address: Address
pub var name: String
access(all) struct User {
access(all) var balance: UFix64
access(all) var address: Address
access(all) var name: String

init(name: String, address: Address, balance: UFix64) {
self.name = name
Expand All @@ -64,7 +65,7 @@ func demo() {
}
}

pub fun main(name: String): User {
access(all) fun main(name: String): User {
return User(
name: name,
address: 0x1,
Expand All @@ -78,7 +79,7 @@ func demo() {
}

type User struct {
balance uint64
balance string
address flow.Address
name string
}
Expand All @@ -88,15 +89,28 @@ func printComplexScript(value cadence.Value, err error) {
fmt.Printf("\nString value: %s", value.String())

s := value.(cadence.Struct)
balanceCdc, ok := s.FieldsMappedByName()["balance"].(cadence.UFix64)
if !ok {
examples.Handle(errors.New("incorrect balance"))
}
addressCdc, ok := s.FieldsMappedByName()["address"].(cadence.Address)
if !ok {
examples.Handle(errors.New("incorrect address"))
}
nameCdc, ok := s.FieldsMappedByName()["name"].(cadence.String)
if !ok {
examples.Handle(errors.New("incorrect name"))
}

u := User{
balance: s.Fields[0].ToGoValue().(uint64),
address: s.Fields[1].ToGoValue().([flow.AddressLength]byte),
name: s.Fields[2].ToGoValue().(string),
balance: balanceCdc.String(),
address: flow.BytesToAddress(addressCdc.Bytes()),
name: nameCdc.String(),
}

fmt.Printf("\nName: %s", u.name)
fmt.Printf("\nAddress: %s", u.address.String())
fmt.Printf("\nBalance: %d", u.balance)
fmt.Printf("\nAddress: 0x%s", u.address.String())
fmt.Printf("\nBalance: %s", u.balance)
}

func prepareDemo() {
Expand Down
30 changes: 25 additions & 5 deletions examples/flow.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,31 @@
}
},
"contracts": {
"FlowServiceAccount": "f8d6e0586b0a20c7",
"FlowFees": "e5a8b7f23e8b548f",
"FlowStorageFees": "f8d6e0586b0a20c7",
"FlowToken": "0ae53cb6e3f42a79",
"FungibleToken": "ee82856bf20e2aa6"
"FlowServiceAccount": {
"aliases": {
"emulator": "f8d6e0586b0a20c7"
}
},
"FlowFees": {
"aliases": {
"emulator": "e5a8b7f23e8b548f"
}
},
"FlowStorageFees": {
"aliases": {
"emulator": "f8d6e0586b0a20c7"
}
},
"FlowToken": {
"aliases": {
"emulator": "0ae53cb6e3f42a79"
}
},
"FungibleToken": {
"aliases": {
"emulator": "ee82856bf20e2aa6"
}
}
},
"networks": {
"emulator": "127.0.0.1:3569",
Expand Down
8 changes: 4 additions & 4 deletions examples/get_events/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ func preapreDemo() (*flow.Account, *flow.Transaction) {

// Deploy a contract with an event defined
contract := `
pub contract EventDemo {
pub event Add(x: Int, y: Int, sum: Int)
access(all) contract EventDemo {
access(all) event Add(x: Int, y: Int, sum: Int)

pub fun add(x: Int, y: Int) {
access(all) fun add(x: Int, y: Int) {
let sum = x + y
emit Add(x: x, y: y, sum: sum)
}
Expand All @@ -105,7 +105,7 @@ func preapreDemo() (*flow.Account, *flow.Transaction) {
import EventDemo from 0x%s

transaction {
prepare(auth: AuthAccount) {}
prepare(signer: auth(Storage) &Account) {}
execute {
EventDemo.add(x: 2, y: 3)
}
Expand Down
5 changes: 2 additions & 3 deletions examples/get_execution_data/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ func main() {

func demo() {
ctx := context.Background()
flowClient, err := grpc.NewClient("access-003.devnet46.nodes.onflow.org:9000")
flowClient, err := grpc.NewClient(grpc.TestnetHost)
examples.Handle(err)

// block, err := flowClient.GetLatestBlock(ctx, true)
block, err := flowClient.GetBlockByID(ctx, flow.HexToID("7582cc6e1bb5ca1784e309ca63013e9b7ecf34b74bf7fdb029aa0faa0deb7958err"))
block, err := flowClient.GetLatestBlock(ctx, true)
examples.Handle(err)
fmt.Printf("Block Height: %d\n", block.Height)
fmt.Printf("Block ID: %s\n", block.ID)
Expand Down
50 changes: 43 additions & 7 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ replace github.com/onflow/flow-go-sdk => ../

require (
github.com/onflow/cadence v1.2.2
github.com/onflow/flow-cli/flowkit v1.11.0
github.com/onflow/flow-go-sdk v0.41.17
github.com/onflow/flow-go-sdk v1.2.2
github.com/onflow/flowkit v1.19.0
github.com/spf13/afero v1.11.0
google.golang.org/grpc v1.63.2
)
Expand All @@ -21,10 +21,13 @@ require (
cloud.google.com/go/kms v1.15.7 // indirect
github.com/SaveTheRbtz/mph v0.1.1-0.20240117162131-4166ec7869bc // indirect
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/coreos/go-semver v0.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/ethereum/go-ethereum v1.13.10 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fxamacker/cbor/v2 v2.4.1-0.20230228173756-c0c9f774e40c // indirect
github.com/fxamacker/circlehash v0.3.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
Expand All @@ -34,29 +37,59 @@ require (
github.com/google/s2a-go v0.1.7 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/holiman/uint256 v1.3.0 // indirect
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/invopop/jsonschema v0.7.0 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/k0kubun/pp v3.0.1+incompatible // indirect
github.com/kevinburke/go-bindata v3.24.0+incompatible // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/logrusorgru/aurora/v4 v4.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiformats/go-base32 v0.1.0 // indirect
github.com/multiformats/go-base36 v0.2.0 // indirect
github.com/multiformats/go-multibase v0.2.0 // indirect
github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/onflow/atree v0.8.0 // indirect
github.com/onflow/crypto v0.25.1 // indirect
github.com/onflow/crypto v0.25.2 // indirect
github.com/onflow/flow-core-contracts/lib/go/templates v1.4.0 // indirect
github.com/onflow/flow-ft/lib/go/templates v1.0.1 // indirect
github.com/onflow/flow-go v0.38.0-preview.0.0.20241022154145-6a254edbec23 // indirect
github.com/onflow/flow-nft/lib/go/templates v1.2.1 // indirect
github.com/onflow/flow/protobuf/go/flow v0.4.7 // indirect
github.com/onflow/go-ethereum v1.13.4 // indirect
github.com/onflow/go-ethereum v1.14.7 // indirect
github.com/onflow/sdks v0.6.0-preview.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/psiemens/sconfig v0.1.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.15.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/texttheater/golang-levenshtein/levenshtein v0.0.0-20200805054039-cae8b0eaed6c // indirect
github.com/turbolent/prettier v0.0.0-20220320183459-661cc755135d // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v4 v4.3.11 // indirect
github.com/vmihailenco/tagparser v0.1.1 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/zeebo/blake3 v0.2.3 // indirect
go.opencensus.io v0.24.0 // indirect
Expand All @@ -65,9 +98,10 @@ require (
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.uber.org/goleak v1.3.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.17.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
Expand All @@ -81,5 +115,7 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.3.0 // indirect
)
Loading
Loading