chore: add send tx example
This commit is contained in:
@@ -1,7 +1,93 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/gagliardetto/solana-go"
|
||||
"github.com/gagliardetto/solana-go/programs/system"
|
||||
"github.com/gagliardetto/solana-go/rpc"
|
||||
"github.com/samlior/libsam/pkg/consts"
|
||||
"github.com/samlior/libsam/pkg/enum"
|
||||
"github.com/samlior/libsam/pkg/swqos"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Hello, World!")
|
||||
_privateKey := os.Getenv("PRIVATE_KEY")
|
||||
if _privateKey == "" {
|
||||
panic("PRIVATE_KEY is not set")
|
||||
}
|
||||
|
||||
privateKey, err := solana.PrivateKeyFromBase58(_privateKey)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
rpcUrl := os.Getenv("RPC_URL")
|
||||
if rpcUrl == "" {
|
||||
panic("RPC_URL is not set")
|
||||
}
|
||||
|
||||
exitSignal := make(chan os.Signal, 1)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
go func() {
|
||||
<-exitSignal
|
||||
cancel()
|
||||
}()
|
||||
|
||||
blockrazorClient, err := swqos.NewSWQoSClient(
|
||||
context.Background(),
|
||||
enum.SWQoSAgentBlockRazor,
|
||||
&swqos.SWQoSClientConfig{
|
||||
SendTxUrl: "frankfurt.solana-grpc.blockrazor.xyz:80",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
amount, _ := decimal.NewFromString("0.001")
|
||||
amountUint64 := amount.Mul(decimal.New(1, consts.SolDecimals)).BigInt().Uint64()
|
||||
|
||||
instructions := make([]solana.Instruction, 0)
|
||||
instructions = append(instructions, system.NewTransferInstruction(
|
||||
amountUint64,
|
||||
privateKey.PublicKey(),
|
||||
solana.MustPublicKeyFromBase58("EDi4rSy2LZgKJX74mbLTFk4mxoTgT6F7HxxzG2HBAFyK"), // blockrazor fee account
|
||||
).Build())
|
||||
|
||||
client := rpc.New(rpcUrl)
|
||||
result, err := client.GetLatestBlockhash(ctx, rpc.CommitmentFinalized)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
tx, err := solana.NewTransaction(
|
||||
instructions,
|
||||
result.Value.Blockhash,
|
||||
solana.TransactionPayer(privateKey.PublicKey()),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// sign tx
|
||||
_, err = tx.Sign(func(key solana.PublicKey) *solana.PrivateKey {
|
||||
if privateKey.PublicKey().Equals(key) {
|
||||
return &privateKey
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
err = blockrazorClient.SendTransaction(ctx, tx)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Println("Transaction sent successfully, tx hash:", tx.Signatures[0].String())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user