Skip to content

Commit

Permalink
core/client/example: auto-request funds if needed, and other updates
Browse files Browse the repository at this point in the history
This updates the `core/client` example with several changes:

- tidy go.mod for current deps
- auto-generate a private key the one in the code is blank, making the example more easily runnable
- if balance is empty or dust, try to request funds from the faucet.  This part works, but is not more generally applicable if say they change the chain.  I might put this handling behind a boolean set in the `var` block next to the other settings. Note that #991 will add to the `chain_info` response a flag indicating if the network has gas enabled.  This can be helpful to direct logic depending on whether the network requires gas for transaction.
- fix the incorrect `records.ExportString` method call.  The `Call` method now returns a results struct containing records rather than the records themselves, so we should do `result.Records.ExportString`
  • Loading branch information
jchappelow authored Sep 20, 2024
1 parent d175b7f commit bd3be25
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 28 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ jobs:
task build:cli
task build:admin
- name: compile the core/client/example app
run: GOWORK=off go build -o /dev/null
working-directory: core/client/example

- name: compile the core/gatewayclient/example app
run: GOWORK=off go build -o /dev/null
working-directory: core/gatewayclient/example

- name: Pull math extension docker image
run: |
docker pull kwilbrennan/extensions-math:multi-arch --platform linux/amd64
Expand Down
1 change: 0 additions & 1 deletion core/client/client_test.go

This file was deleted.

5 changes: 3 additions & 2 deletions core/client/example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ replace github.com/kwilteam/kwil-db/core => ../..
replace github.com/kwilteam/kwil-db/parse => ../../../parse

require (
github.com/kwilteam/kwil-db/core v0.2.0-beta.1
github.com/kwilteam/kwil-db/parse v0.2.0-beta
github.com/kwilteam/kwil-db/core v0.3.0-beta.0.20240917145512-e6922733fc1a
github.com/kwilteam/kwil-db/parse v0.3.0-beta.0.20240917145512-e6922733fc1a
)

require (
Expand All @@ -24,6 +24,7 @@ require (
github.com/ethereum/go-ethereum v1.14.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/jrick/logrotate v1.1.2 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/supranational/blst v0.3.11 // indirect
Expand Down
8 changes: 6 additions & 2 deletions core/client/example/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,18 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/holiman/uint256 v1.2.4 h1:jUc4Nk8fm9jZabQuqr2JzednajVmBpC+oiTiXZJEApU=
github.com/holiman/uint256 v1.2.4/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
github.com/jrick/logrotate v1.1.2 h1:6ePk462NCX7TfKtNp5JJ7MbA2YIslkpfgP03TlTYMN0=
github.com/jrick/logrotate v1.1.2/go.mod h1:f9tdWggSVK3iqavGpyvegq5IhNois7KXmasU6/N96OQ=
github.com/klauspost/compress v1.15.15 h1:EF27CXIuDsYJ6mmvtBRlEuB2UVOqHG1tAXgZ7yIO+lw=
github.com/klauspost/compress v1.15.15/go.mod h1:ZcK2JAFqKOpnBlxcLsJzYfrS9X1akm9fHZNnD9+Vo/4=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kwilteam/kwil-db/core v0.3.0-beta.0.20240917145512-e6922733fc1a h1:O+zDWJJg7qGy5B6NJIVVGGif2npT/Np95f3ZaZVFWcU=
github.com/kwilteam/kwil-db/core v0.3.0-beta.0.20240917145512-e6922733fc1a/go.mod h1:rTXHWgWannGuOaR0vK2o7/kBXu5opLWZOqlAhLSRP1Y=
github.com/kwilteam/kwil-db/parse v0.3.0-beta.0.20240917145512-e6922733fc1a h1:50xB2vd2o8quYtRDqLR2RIHrLYI88W/1LajyKnVgRDM=
github.com/kwilteam/kwil-db/parse v0.3.0-beta.0.20240917145512-e6922733fc1a/go.mod h1:U0fAqxMa40ohMZVnc9ePgaU7GayxYDslH7mA+reCuRM=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
Expand Down Expand Up @@ -138,7 +144,5 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
rsc.io/tmplfunc v0.0.3 h1:53XFQh69AfOa8Tw0Jm7t+GV7KZhOi6jzsCzTtKbMvzU=
rsc.io/tmplfunc v0.0.3/go.mod h1:AG3sTPzElb1Io3Yg4voV9AGZJuleGAwaVRxL9M49PhA=
65 changes: 52 additions & 13 deletions core/client/example/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package main

// This is an example Kwil client application that demonstrates the use of the
// core/client.Client type to interact with a Kwil chain via an RPC provider.

import (
"context"
"fmt"
"io"
"log"
"math/big"
"net/http"
"slices"
"strings"
"time"

"github.com/kwilteam/kwil-db/core/client"
"github.com/kwilteam/kwil-db/core/crypto"
Expand All @@ -19,16 +26,34 @@ import (
)

const (
chainID = "longhorn"
chainID = "longhorn-2"
provider = "https://longhorn.kwil.com"

privKey = "..."
// If this chain requires gas, we need funds, and we can request funds from
// a faucet URL if needed.
gasEnabled = true
faucetURL = "https://kwil-faucet-server.onrender.com/funds" // if set and now balance, request test funds

// For the client, this is a secp256k1 private key. This is the same type of
// key used by Ethereum wallets. The `kwil-cli utils generate-key` command
// may be used to generate a new client key (the client's identity) if one
// is not already available.
// If left empty, this example app will generate an ephemeral private key.
privKey = "" // empty or 64 hexadecimal characters of a secp256k1 private key
)

func main() {
ctx := context.Background()
signer := makeEthSigner(privKey)
var signer auth.Signer
if privKey == "" {
key := genEthKey()
fmt.Println("generated private key: ", key.Hex())
fmt.Printf("public key: %x\n", key.PubKey().Bytes())
signer = &auth.EthPersonalSigner{Key: *key}
} else {
signer = makeEthSigner(privKey)
}
acctID := signer.Identity()
fmt.Printf("address: 0x%x\n", acctID)

ctypes.DefaultOptions()
opts := &ctypes.Options{
Expand Down Expand Up @@ -83,6 +108,25 @@ func main() {
}
}

const minBal int64 = 1e6 // dust
if gasEnabled && acctInfo.Balance.Cmp(big.NewInt(minBal)) < 0 /* && chainInfo.GasEnabled */ {
fmt.Println("Account lacks sufficient funds for transaction gas. Requesting funds.")
addr, _ := auth.EthSecp256k1Authenticator{}.Identifier(acctID)
req := `{"address": "` + addr + `"}`
resp, err := http.Post(faucetURL, "application/json", strings.NewReader(req))
if err != nil {
log.Fatalf("failed to request funds: %v", err)
}
bodyBts, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
log.Fatalf("failed to request funds (code %d): %v", resp.StatusCode, string(bodyBts))
}

fmt.Printf("funds requested: %v\n", string(bodyBts))
// cl.WaitTx(ctx, txHashFromPOSTResp, 500 * time.Millisecond)
time.Sleep(6 * time.Second)
}

// Deploy a Kuneiform schema called "was_here".
dbName := "was_here"
dbid := utils.GenerateDBID(dbName, acctID) // derive DBID
Expand All @@ -99,11 +143,6 @@ func main() {
log.Fatal(err)
}

const minBal int64 = 1e6 // dust
if acctInfo.Balance.Cmp(big.NewInt(minBal)) < 0 {
log.Fatalf("Account lacks sufficient funds to deploy a database.")
}

fmt.Printf("Deploying database %v...\n", schema.Name)
txHash, err := cl.DeployDatabase(ctx, schema, txOpts...)
if err != nil {
Expand Down Expand Up @@ -133,11 +172,11 @@ func main() {

// Use a read-only view call (no blockchain transaction) to list all entries
const getAllAction = "get_all"
records, err := cl.Call(ctx, dbid, getAllAction, nil)
result, err := cl.Call(ctx, dbid, getAllAction, nil)
if err != nil {
log.Fatal(err)
}
if tab := records.ExportString(); len(tab) == 0 {
if tab := result.Records.ExportString(); len(tab) == 0 {
fmt.Println("No data records in table.")
} else {
fmt.Println("All entries in tags table:")
Expand Down Expand Up @@ -168,7 +207,7 @@ func makeEthSigner(keyHex string) auth.Signer {
if err != nil {
panic(fmt.Sprintf("bad private key: %v", err))
}
return &auth.EthPersonalSigner{Key: *key}
return &auth.EthPersonalSigner{Key: *key} // , key.PubKey().Bytes()
}

func genEthKey() *crypto.Secp256k1PrivateKey {
Expand All @@ -181,7 +220,7 @@ func makeEdSigner(keyHex string) auth.Signer {
if err != nil {
panic(fmt.Sprintf("bad private key: %v", err))
}
return &auth.Ed25519Signer{*key}
return &auth.Ed25519Signer{Ed25519PrivateKey: *key}
}

func genEdKey() *crypto.Ed25519PrivateKey {
Expand Down
2 changes: 2 additions & 0 deletions core/gatewayclient/example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mykgwapp
mykgwapp.exe
7 changes: 4 additions & 3 deletions core/gatewayclient/example/go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
module mykgwapp

go 1.21.0
go 1.22

require github.com/kwilteam/kwil-db/core v0.1.1-0.20240514193332-c61ba10f8a1c
require github.com/kwilteam/kwil-db/core v0.3.0-beta.0.20240917145512-e6922733fc1a

require (
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.1 // indirect
github.com/cockroachdb/apd/v3 v3.2.1 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect
github.com/cstockton/go-conv v1.0.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/ethereum/go-ethereum v1.14.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/jrick/logrotate v1.1.2 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/supranational/blst v0.3.11 // indirect
Expand Down
12 changes: 8 additions & 4 deletions core/gatewayclient/example/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOF
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cockroachdb/apd/v3 v3.2.1 h1:U+8j7t0axsIgvQUqthuNm82HIrYXodOV2iWLWtEaIwg=
github.com/cockroachdb/apd/v3 v3.2.1/go.mod h1:klXJcjp+FffLTHlhIG69tezTDvdP065naDsHzKhYSqc=
github.com/cockroachdb/errors v1.11.1 h1:xSEW75zKaKCWzR3OfxXUxgrk/NtT4G1MiOv5lWZazG8=
github.com/cockroachdb/errors v1.11.1/go.mod h1:8MUxA3Gi6b25tYlFEBGLf+D8aISL+M4MIpiWMSNRfxw=
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b h1:r6VH0faHjZeQy818SGhaone5OnYfxFR/+AzdY3sf5aE=
Expand All @@ -32,8 +34,6 @@ github.com/crate-crypto/go-ipa v0.0.0-20231025140028-3c0104f4b233 h1:d28BXYi+wUp
github.com/crate-crypto/go-ipa v0.0.0-20231025140028-3c0104f4b233/go.mod h1:geZJZH3SzKCqnz5VT0q/DyIG/tvu/dZk+VIfXicupJs=
github.com/crate-crypto/go-kzg-4844 v1.0.0 h1:TsSgHwrkTKecKJ4kadtHi4b3xHW5dCFUDFnUp1TsawI=
github.com/crate-crypto/go-kzg-4844 v1.0.0/go.mod h1:1kMhvPgI0Ky3yIa+9lFySEBUBXkYxeOi8ZF1sYioxhc=
github.com/cstockton/go-conv v1.0.0 h1:zj/q/0MpQ/97XfiC9glWiohO8lhgR4TTnHYZifLTv6I=
github.com/cstockton/go-conv v1.0.0/go.mod h1:HuiHkkRgOA0IoBNPC7ysG7kNpjDYlgM7Kj62yQPxjy4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK09Y2A4Xv7EE0=
Expand Down Expand Up @@ -65,18 +65,22 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/holiman/uint256 v1.2.4 h1:jUc4Nk8fm9jZabQuqr2JzednajVmBpC+oiTiXZJEApU=
github.com/holiman/uint256 v1.2.4/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
github.com/jrick/logrotate v1.1.2 h1:6ePk462NCX7TfKtNp5JJ7MbA2YIslkpfgP03TlTYMN0=
github.com/jrick/logrotate v1.1.2/go.mod h1:f9tdWggSVK3iqavGpyvegq5IhNois7KXmasU6/N96OQ=
github.com/klauspost/compress v1.15.15 h1:EF27CXIuDsYJ6mmvtBRlEuB2UVOqHG1tAXgZ7yIO+lw=
github.com/klauspost/compress v1.15.15/go.mod h1:ZcK2JAFqKOpnBlxcLsJzYfrS9X1akm9fHZNnD9+Vo/4=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kwilteam/kwil-db/core v0.1.1-0.20240514193332-c61ba10f8a1c h1:mvF8/k3Ihyr0qK954FnfTJBBfjAXTucG6NmNdeUJKcM=
github.com/kwilteam/kwil-db/core v0.1.1-0.20240514193332-c61ba10f8a1c/go.mod h1:EsV7UHereNp3EA1CkFA+DROMbiTgQNY7vRiIm+Qsgic=
github.com/kwilteam/kwil-db/core v0.3.0-beta.0.20240917145512-e6922733fc1a h1:O+zDWJJg7qGy5B6NJIVVGGif2npT/Np95f3ZaZVFWcU=
github.com/kwilteam/kwil-db/core v0.3.0-beta.0.20240917145512-e6922733fc1a/go.mod h1:rTXHWgWannGuOaR0vK2o7/kBXu5opLWZOqlAhLSRP1Y=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7c=
github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8=
github.com/lib/pq v1.10.7 h1:p7ZhMD+KsSRozJr34udlUrhboJwWAgCg34+/ZZNvZZw=
github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 h1:I0XW9+e1XWDxdcEniV4rQAIOPUGDq67JSCiRCgGCZLI=
Expand Down
6 changes: 3 additions & 3 deletions core/gatewayclient/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
fmt.Println("Result hello: ", result1.ExportString())
fmt.Println("Result hello: ", result1.Records.ExportString())

fmt.Println("call auth_only")
// signing required, i.e. message will be printed
Expand All @@ -139,7 +139,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
fmt.Println("Result auth_only: ", result2.ExportString())
fmt.Println("Result auth_only: ", result2.Records.ExportString())

// NOTE: the message will be printed every time you call this action for the
// first time of current HTTP connection, but the following calls will not
Expand All @@ -149,5 +149,5 @@ func main() {
if err != nil {
log.Fatal(err)
}
fmt.Println("Result auth_only again: ", result3.ExportString())
fmt.Println("Result auth_only again: ", result3.Records.ExportString())
}

0 comments on commit bd3be25

Please sign in to comment.