Skip to content

Commit

Permalink
Attempt to submit mints
Browse files Browse the repository at this point in the history
Fails with error InvalidAccountData

Signed-off-by: Lee Smet <[email protected]>
  • Loading branch information
LeeSmet committed Dec 23, 2024
1 parent 756520b commit 8cdd8e9
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
17 changes: 16 additions & 1 deletion bridges/stellar-solana/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/mr-tron/base58"
"github.com/threefoldfoundation/tft/bridges/stellar-solana/solana"
)

Expand Down Expand Up @@ -150,7 +151,21 @@ import (
// }

func main() {
sol, err := solana.New(context.Background(), "local")
sol, err := solana.New(context.Background(), "local", "/home/lee/.config/solana/id.json")
if err != nil {
fmt.Println(err)
panic(err)
}

v, err := base58.Decode("9fZ6TL7TkdVsodPWJd2UYyA9oTMD3PZ1FKw1PX7jhQjc")
if err != nil {
fmt.Println(err)
panic(err)
}
var t [32]byte

copy(t[:], v)
err = sol.MintTokens(context.Background(), solana.MintInfo{Amount: 10000000000, TxID: "thisIsATxID", To: t})
if err != nil {
fmt.Println(err)
panic(err)
Expand Down
4 changes: 2 additions & 2 deletions bridges/stellar-solana/solana/burn.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package solana

// Burn of tokens on solana. A burn is for an amount of tokens and carries a memo
type Burn struct {
// Amount in the smallest unit
// Amount in lamports
amount uint64
// Decimals for 1 full token
decimals uint8
Expand All @@ -16,7 +16,7 @@ func (b Burn) Memo() string {
return b.memo
}

// RawAmount of tokens burned in the smallest possible unit
// RawAmount of tokens burned in lamports
func (b Burn) RawAmount() uint64 {
return b.amount
}
11 changes: 11 additions & 0 deletions bridges/stellar-solana/solana/mint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package solana

// MintInfo about a mint to attempt for new tokens on the solana network
type MintInfo struct {
// Amount of tokens in lamports
Amount uint64
// TxID transaction ID of the transaction which triggered this mint
TxID string
// To raw address bytes of the receiver of the tokens
To [32]byte
}
68 changes: 66 additions & 2 deletions bridges/stellar-solana/solana/solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"context"
"time"

"github.com/davecgh/go-spew/spew"
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/programs/memo"
"github.com/gagliardetto/solana-go/programs/token"
"github.com/gagliardetto/solana-go/rpc"
confirm "github.com/gagliardetto/solana-go/rpc/sendAndConfirmTransaction"
"github.com/gagliardetto/solana-go/rpc/ws"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -37,16 +40,77 @@ var (
type Solana struct {
rpcClient *rpc.Client
wsClient *ws.Client

account solana.PrivateKey
}

// New Solana client connected to the provided network
func New(ctx context.Context, network string) (*Solana, error) {
func New(ctx context.Context, network string, keyFile string) (*Solana, error) {
account, err := solana.PrivateKeyFromSolanaKeygenFile(keyFile)
if err != nil {
return nil, errors.Wrap(err, "could not load solana key file")
}

rpcClient, wsClient, err := getSolanaClient(ctx, network)
if err != nil {
return nil, errors.Wrap(err, "could not create Solana RPC client")
}

return &Solana{rpcClient: rpcClient, wsClient: wsClient}, nil
return &Solana{rpcClient: rpcClient, wsClient: wsClient, account: account}, nil
}

// MintTokens tries to mint new tokens with the given mint context.
func (sol *Solana) MintTokens(ctx context.Context, info MintInfo) error {
to := solana.PublicKeyFromBytes(info.To[:])

recent, err := sol.rpcClient.GetLatestBlockhash(ctx, rpc.CommitmentFinalized)
if err != nil {
return errors.Wrap(err, "failed to get latest finalized block hash")
}

var mint token.Mint
err = sol.rpcClient.GetAccountDataInto(ctx, tftAddress, &mint)
if err != nil {
return errors.Wrap(err, "failed to get token account info")
}

if mint.MintAuthority == nil {
return errors.New("can't mint token without mint authority")
}

spew.Dump(mint)

tx, err := solana.NewTransaction([]solana.Instruction{
memo.NewMemoInstruction([]byte(info.TxID), sol.account.PublicKey()).Build(),
token.NewMintToInstruction(info.Amount, tftAddress, to, *mint.MintAuthority, nil).Build(),
}, recent.Value.Blockhash, solana.TransactionPayer(sol.account.PublicKey()))
if err != nil {
return errors.Wrap(err, "failed to create mint transaction")
}

spew.Dump(tx)

_, err = tx.Sign(func(key solana.PublicKey) *solana.PrivateKey {
if sol.account.PublicKey().Equals(key) {
return &sol.account
}

return nil
})
if err != nil {
return errors.Wrap(err, "failed to sign mint transaction")
}

spew.Dump(tx)

_, err = confirm.SendAndConfirmTransaction(ctx, sol.rpcClient, sol.wsClient, tx)
if err != nil {
return errors.Wrap(err, "failed to submit mint transaction")
}

log.Info().Msg("Submitted mint tx")

return nil
}

// SubscribeTokenBurns creates a subscription for **NEW** token burn events on the current token. This does not return any previous burns.
Expand Down

0 comments on commit 8cdd8e9

Please sign in to comment.