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

feat(test): improve the go tests #467

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .github/workflows/spell-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ on:
- "**/*.html"
- "**/*.rst"
- "!**/*.svg" # Exclude svg files from triggering the workflow
- "!**/*.sol"
- "**/*.go"
- "!**/*.sum"
- "!**/*.mod"
Expand Down
8 changes: 8 additions & 0 deletions docs/ftso/scaling/2-getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Remix from "@site/src/components/remix";
import FetchAnchorFeedsJs from "!!raw-loader!/examples/developer-hub-javascript/fetch_anchor_feeds.js";
import FetchAnchorFeedsPy from "!!raw-loader!/examples/developer-hub-python/fetch_anchor_feeds.py";
import FetchAnchorFeedsGo from "!!raw-loader!/examples/developer-hub-go/flare/fetch_anchor_feeds.go";
import FetchAnchorFeedsRs from "!!raw-loader!/examples/developer-hub-rust/src/bin/fetch_anchor_feeds.rs";
import FetchAndVerifyAnchorOnchainJs from "!!raw-loader!/examples/developer-hub-javascript/fetch_and_verify_anchor_onchain.js";
import FtsoV2AnchorFeedConsumer from "!!raw-loader!/examples/developer-hub-solidity/FtsoV2AnchorFeedConsumer.sol";

Expand Down Expand Up @@ -103,6 +104,13 @@ To fetch the feed values for FLR/USD, BTC/USD, and ETH/USD at the latest voting
</CodeBlock>

</TabItem>
<TabItem value="rust" label="Rust">

<CodeBlock language="Rust" title="fetch_anchor_feeds.rs">
{FetchAnchorFeedsRs}
</CodeBlock>

</TabItem>
</Tabs>

#### API response structure
Expand Down
299 changes: 299 additions & 0 deletions examples/developer-hub-go/flare/FtsoV2AnchorFeedConsumer.go

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions examples/developer-hub-go/flare/fetch_anchor_feeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ func FetchAnchorFeeds() ([]AnchorFeed, error) {
return feeds, json.Unmarshal(body, &feeds)
}

func main() {
feeds, err := FetchAnchorFeeds()
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("Anchor feeds: %+v\n", feeds)
}
func main() {
feeds, err := FetchAnchorFeeds()
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("Anchor feeds: %+v\n", feeds)
}
126 changes: 126 additions & 0 deletions examples/developer-hub-go/flare/verify_anchor_feeds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package flare

import (
"context"
"crypto/ecdsa"
"encoding/hex"
"fmt"
"log"
"math/big"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
)

func convertToByteArray(proof []string) ([][32]byte, error) {
var result [][32]byte

for _, str := range proof {

decoded, err := hex.DecodeString(str)
if err != nil {
return nil, fmt.Errorf("failed to decode string: %v", err)
}

if len(decoded) != 32 {
return nil, fmt.Errorf("decoded string is not 32 bytes: got %d bytes", len(decoded))
}

var byteArray [32]byte
copy(byteArray[:], decoded)

result = append(result, byteArray)
}

return result, nil
}

func VerifyAnchorFeeds() {
const (
PRIVATE_KEY = " "
RPC_URL = "https://coston2-api.flare.network/ext/C/rpc"
DEPLOYED_CONTRACT_ADDRESS = "0x069227C6A947d852c55655e41C6a382868627920"
)
var feed_Id [21]byte
var proof [][32]byte

feeds, err := FetchAnchorFeeds()
if err != nil {
log.Fatal(err)
}
copy(feed_Id[:], feeds[0].Body.ID)
votingRoundId := uint32(feeds[0].Body.VotingRoundID)
feedBody := FtsoV2InterfaceFeedData{
votingRoundId,
feed_Id,
int32(feeds[0].Body.Value),
uint16(feeds[0].Body.TurnoutBIPS),
int8(feeds[0].Body.Decimals),
}
proof, _ = convertToByteArray(feeds[0].Proof)

var feedWithProof = FtsoV2InterfaceFeedDataWithProof{
proof,
feedBody,
}

client, err := ethclient.Dial(RPC_URL)
if err != nil {
log.Fatal(err)
}

privateKey, err := crypto.HexToECDSA(PRIVATE_KEY)
if err != nil {
log.Fatalf("Failed to parse private key: %v", err)
}

publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("Invalid public key type")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)

nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatalf("Failed to fetch nonce: %v", err)
}

gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
log.Fatalf("Failed to fetch gas price: %v", err)
}

chainID, err := client.NetworkID(context.Background())
if err != nil {
log.Fatalf("Failed to fetch chain ID: %v", err)
}

auth, err := bind.NewKeyedTransactorWithChainID(privateKey, chainID)
if err != nil {
log.Fatalf("Failed to create transactor: %v", err)
}
auth.Nonce = big.NewInt(int64(nonce))
auth.Value = big.NewInt(0) //
auth.GasLimit = uint64(3000000)
auth.GasPrice = gasPrice
consumerAddr := common.HexToAddress(DEPLOYED_CONTRACT_ADDRESS)
anchorFeed, err := NewFtsoV2AnchorFeedConsumer(consumerAddr, client)
if err != nil {
log.Fatalf("Failed to initialize consumer contract: %v", err)
}
if err != nil {
log.Fatal(err)
}

tx, err := anchorFeed.SavePrice(auth, feedWithProof)
if err != nil {
log.Fatalf("Failed to save price: %v", err)
}
fmt.Printf("SavePrice transaction hash: %s\n", tx.Hash().Hex())

savedPrice, _ := anchorFeed.ProvenFeeds(&bind.CallOpts{}, votingRoundId, feed_Id)
fmt.Printf("%+v\n", savedPrice)
}
2 changes: 1 addition & 1 deletion examples/developer-hub-go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require github.com/ethereum/go-ethereum v1.14.12
require (
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/bits-and-blooms/bitset v1.20.0 // indirect
github.com/consensys/bavard v0.1.24 // indirect
github.com/consensys/bavard v0.1.25 // indirect
github.com/consensys/gnark-crypto v0.14.0 // indirect
github.com/crate-crypto/go-ipa v0.0.0-20240724233137-53bbb0ceb27a // indirect
github.com/crate-crypto/go-kzg-4844 v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions examples/developer-hub-go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 h1:zuQyyAK
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06/go.mod h1:7nc4anLGjupUW/PeY5qiNYsdNXj7zopG+eqsS7To5IQ=
github.com/consensys/bavard v0.1.24 h1:Lfe+bjYbpaoT7K5JTFoMi5wo9V4REGLvQQbHmatoN2I=
github.com/consensys/bavard v0.1.24/go.mod h1:k/zVjHHC4B+PQy1Pg7fgvG3ALicQw540Crag8qx+dZs=
github.com/consensys/bavard v0.1.25 h1:5YcSBnp03/HvfpKaIQLr/ecspTp2k8YNR5rQLOWvUyc=
github.com/consensys/bavard v0.1.25/go.mod h1:k/zVjHHC4B+PQy1Pg7fgvG3ALicQw540Crag8qx+dZs=
github.com/consensys/gnark-crypto v0.14.0 h1:DDBdl4HaBtdQsq/wfMwJvZNE80sHidrK3Nfrefatm0E=
github.com/consensys/gnark-crypto v0.14.0/go.mod h1:CU4UijNPsHawiVGNxe9co07FkzCeWHHrb1li/n1XoU0=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
Expand Down
1 change: 1 addition & 0 deletions examples/developer-hub-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ func main() {
coston2.MakeVolatilityIncentive()
coston2.SecureRandom()
flare.FetchAnchorFeeds()
flare.VerifyAnchorFeeds()
}
Loading
Loading