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

Simple solana lamport transactional test #14

Closed
wants to merge 2 commits into from
Closed
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
90 changes: 90 additions & 0 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import (
"syscall"
"time"

confirm "github.com/gagliardetto/solana-go/rpc/sendAndConfirmTransaction"
"github.com/gagliardetto/solana-go/text"

"github.com/davecgh/go-spew/spew"
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
"github.com/gagliardetto/solana-go/rpc/ws"
"github.com/tiny-blob/tinyblob/pkg/routes"
"github.com/tiny-blob/tinyblob/pkg/services"
)
Expand Down Expand Up @@ -38,6 +45,89 @@ func main() {
}
}()

// Create a new WS client (used for confirming transactions)
wsClient, err := ws.Connect(context.Background(), rpc.DevNet_WS)
if err != nil {
panic(err)
}

// TODO Make around here……
accountFrom, err := solana.PrivateKeyFromSolanaKeygenFile("/Users/james/code/tinyblob/programs/target/deploy/tiny_blob-keypair.json")
if err != nil {
spew.Dump(err)
}

// triggerKeyPair := solana.NewWallet()
triggerKeyPair := solana.MustPublicKeyFromBase58("H2kY5LXxxBjiBR91dH9FisrLA597Njb4X6sbf8UhcdeN")
triggerKeyPairPrivateKey := solana.MustPrivateKeyFromBase58("2ZKnvMBKb2qiZTGnGYZBXkVdqho5zqrCf76SsNiwGcckJFrSbUKx7TEsmbJN9JPp4ujRjcA8CqSHN8MUT4sbr4Dt")
// triggerKeyPair := solana.MustPublicKeyFromBase58("4sVHU1NqyqNGRnE93tBRZovU2d4s1XKn9ogFjgobiFrR")
// spew.Dump(triggerKeyPair.PublicKey(), "<----------------------------")

// if true {
// // Airdrop 1 sol to the account so it will have something to transfer:
// out, err := c.SolRPC.RequestAirdrop(
// context.TODO(),
// accountFrom.PublicKey(),
// solana.LAMPORTS_PER_SOL*1,
// rpc.CommitmentFinalized,
// )
// if err != nil {
// panic(err)
// }
// fmt.Println("airdrop transaction signature:", out)
// time.Sleep(time.Second * 5)
// }

recentBlockHash, err := c.SolRPC.GetRecentBlockhash(context.TODO(), rpc.CommitmentFinalized)
if err != nil {
panic(err)
}

programID := accountFrom.PublicKey()

accounts := []*solana.AccountMeta{
{PublicKey: triggerKeyPair, IsSigner: true, IsWritable: true},
// {PublicKey: accountTo, IsSigner: false, IsWritable: true},
}

tx, err := solana.NewTransaction(
[]solana.Instruction{
solana.NewInstruction(programID, accounts, []byte{}),
},
recentBlockHash.Value.Blockhash,
)
if err != nil {
panic(err)
}

_, err = tx.Sign(
func(key solana.PublicKey) *solana.PrivateKey {
if triggerKeyPair.Equals(key) {
return &triggerKeyPairPrivateKey
}
return nil
},
)
if err != nil {
panic(fmt.Errorf("unable to sign transaction: %w", err))
}
// spew.Dump(tx)
// Pretty print the transaction:
tx.EncodeTree(text.NewTreeEncoder(os.Stdout, "Transfer SOL"))

// Send transaction, and wait for confirmation:
sig, err := confirm.SendAndConfirmTransaction(
context.TODO(),
c.SolRPC,
wsClient,
tx,
)
if err != nil {
spew.Dump("+++++++++++++++++")
panic(err)
}
spew.Dump(sig)

quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
<-quit
Expand Down
7 changes: 4 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ func GetConfig() (Config, error) {
type environment string

const (
EnvTest environment = "test"
EnvDev environment = "development"
EnvProd environment = "production"
EnvTest environment = "test"
EnvLocal environment = "local"
EnvDev environment = "development"
EnvProd environment = "production"
)

func SwitchEnv(env environment) {
Expand Down
34 changes: 32 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ go 1.20
require (
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/andybalholm/brotli v1.0.6
github.com/davecgh/go-spew v1.1.1
github.com/eko/gocache/v2 v2.3.1
github.com/gagliardetto/solana-go v1.8.4
github.com/go-playground/validator/v10 v10.16.0
github.com/go-redis/redis/v8 v8.11.5
github.com/gorilla/sessions v1.2.2
Expand All @@ -14,41 +16,58 @@ require (
github.com/labstack/gommon v0.4.0
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.3
golang.org/x/time v0.3.0
)

require (
contrib.go.opencensus.io/exporter/stackdriver v0.13.4 // indirect
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/XiaoMi/pegasus-go-client v0.0.0-20210427083443-f3b6b08bc4c2 // indirect
github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blendle/zapdriver v1.3.1 // indirect
github.com/bradfitz/gomemcache v0.0.0-20220106215444-fb4bf637b56d // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dfuse-io/logging v0.0.0-20201110202154-26697de88c79 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gagliardetto/binary v0.7.7 // indirect
github.com/gagliardetto/treeout v0.1.4 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/rpc v1.2.0 // indirect
github.com/gorilla/securecookie v1.1.2 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.3.3 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/logrusorgru/aurora v2.0.3+incompatible // 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.19 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mostynb/zstdpool-freelist v0.0.0-20201229113212-927304c0c3b1 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/pegasus-kv/thrift v0.13.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand All @@ -61,16 +80,27 @@ require (
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/streamingfast/logging v0.0.0-20220405224725-2755dab2ce75 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/teris-io/shortid v0.0.0-20201117134242-e59966efd125 // indirect
github.com/tidwall/gjson v1.9.3 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
go.mongodb.org/mongo-driver v1.11.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/ratelimit v0.2.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
Loading
Loading