-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #794 from The-K-R-O-K/illia-malachyn/784-add-more-…
…examples Add examples for new grpc endpoints
- Loading branch information
Showing
11 changed files
with
410 additions
and
53 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
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
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
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
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,76 @@ | ||
/* | ||
* Flow Go SDK | ||
* | ||
* Copyright Flow Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/onflow/flow-go-sdk/access/grpc" | ||
|
||
"github.com/onflow/flow-go-sdk" | ||
"github.com/onflow/flow-go-sdk/examples" | ||
) | ||
|
||
func main() { | ||
demo() | ||
} | ||
|
||
func demo() { | ||
ctx := context.Background() | ||
flowClient, err := grpc.NewClient(grpc.EmulatorHost) | ||
examples.Handle(err) | ||
|
||
serviceAcctAddr, serviceAcctKey, serviceSigner := examples.ServiceAccount(flowClient) | ||
|
||
tx := flow.NewTransaction(). | ||
SetPayer(serviceAcctAddr). | ||
SetProposalKey(serviceAcctAddr, serviceAcctKey.Index, serviceAcctKey.SequenceNumber). | ||
SetScript([]byte(` | ||
transaction { | ||
prepare(acc: &Account) {} | ||
execute { | ||
log("test") | ||
} | ||
} | ||
`)). | ||
AddAuthorizer(serviceAcctAddr). | ||
SetReferenceBlockID(examples.GetReferenceBlockId(flowClient)) | ||
|
||
err = tx.SignEnvelope(serviceAcctAddr, serviceAcctKey.Index, serviceSigner) | ||
examples.Handle(err) | ||
|
||
txResultChan, errChan, initErr := flowClient.SendAndSubscribeTransactionStatuses(ctx, *tx) | ||
examples.Handle(initErr) | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return | ||
case txResult, ok := <-txResultChan: | ||
if !ok { | ||
panic("transaction result channel is closed") | ||
} | ||
examples.Print(txResult) | ||
case err, ok := <-errChan: | ||
if !ok { | ||
panic("error channel is closed") | ||
} | ||
fmt.Printf("~~~ ERROR: %s ~~~\n", err.Error()) | ||
} | ||
} |
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,70 @@ | ||
/* | ||
* Flow Go SDK | ||
* | ||
* Copyright Flow Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/onflow/flow-go-sdk/access/grpc" | ||
|
||
"github.com/onflow/flow-go-sdk" | ||
"github.com/onflow/flow-go-sdk/examples" | ||
) | ||
|
||
func main() { | ||
demo() | ||
} | ||
|
||
func demo() { | ||
ctx := context.Background() | ||
flowClient, err := grpc.NewClient("access.testnet.nodes.onflow.org:9000") | ||
examples.Handle(err) | ||
|
||
header, err := flowClient.GetLatestBlockHeader(ctx, true) | ||
examples.Handle(err) | ||
fmt.Printf("Latest block height: %d\n", header.Height) | ||
fmt.Printf("Latest block ID: %s\n", header.ID) | ||
|
||
flowEVMTestnetAddress := "0x8c5303eaa26202d6" | ||
filter := flow.AccountStatusFilter{ | ||
EventFilter: flow.EventFilter{ | ||
Addresses: []string{flowEVMTestnetAddress}, | ||
}, | ||
} | ||
accountStatusesChan, errChan, initErr := flowClient.SubscribeAccountStatusesFromStartBlockID(ctx, header.ID, filter) | ||
examples.Handle(initErr) | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case accountStatus, ok := <-accountStatusesChan: | ||
if !ok { | ||
panic("account statuses channel is closed") | ||
} | ||
examples.Print(accountStatus) | ||
case err, ok := <-errChan: | ||
if !ok { | ||
panic("error channel is closed") | ||
} | ||
fmt.Printf("~~~ ERROR: %s ~~~\n", err.Error()) | ||
} | ||
} | ||
} |
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,64 @@ | ||
/* | ||
* Flow Go SDK | ||
* | ||
* Copyright Flow Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/onflow/flow-go-sdk/access/grpc" | ||
|
||
"github.com/onflow/flow-go-sdk" | ||
"github.com/onflow/flow-go-sdk/examples" | ||
) | ||
|
||
func main() { | ||
demo() | ||
} | ||
|
||
func demo() { | ||
ctx := context.Background() | ||
flowClient, err := grpc.NewClient("access.testnet.nodes.onflow.org:9000") | ||
examples.Handle(err) | ||
|
||
header, err := flowClient.GetLatestBlockHeader(ctx, true) | ||
examples.Handle(err) | ||
fmt.Printf("Block Height: %d\n", header.Height) | ||
fmt.Printf("Block ID: %s\n", header.ID) | ||
|
||
blockDigestsChan, errChan, initErr := flowClient.SubscribeBlockDigestsFromStartBlockID(ctx, header.ID, flow.BlockStatusFinalized) | ||
examples.Handle(initErr) | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case blockDigest, ok := <-blockDigestsChan: | ||
if !ok { | ||
panic("block digest channel is closed") | ||
} | ||
examples.Print(blockDigest) | ||
case err, ok := <-errChan: | ||
if !ok { | ||
panic("error channel is closed") | ||
} | ||
fmt.Printf("~~~ ERROR: %s ~~~\n", err.Error()) | ||
} | ||
} | ||
} |
Oops, something went wrong.