Skip to content

Commit

Permalink
Improve tx submission
Browse files Browse the repository at this point in the history
- Set correct token program
- Add compute budget instruction, proper compute budget calculation is
  still needed

Signed-off-by: Lee Smet <[email protected]>
  • Loading branch information
LeeSmet committed Dec 23, 2024
1 parent 8cdd8e9 commit b698acc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
11 changes: 10 additions & 1 deletion bridges/stellar-solana/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,5 +171,14 @@ func main() {
panic(err)
}

sol.SubscribeTokenBurns(context.Background())
sub, err := sol.SubscribeTokenBurns(context.Background())
if err != nil {
fmt.Println(err)
panic(err)

}

for burn := range sub {
fmt.Println("Burning", burn.RawAmount(), "tokens with memo", burn.Memo())
}
}
16 changes: 14 additions & 2 deletions bridges/stellar-solana/solana/solana.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/davecgh/go-spew/spew"
"github.com/gagliardetto/solana-go"
budget "github.com/gagliardetto/solana-go/programs/compute-budget"
"github.com/gagliardetto/solana-go/programs/memo"
"github.com/gagliardetto/solana-go/programs/token"
"github.com/gagliardetto/solana-go/rpc"
Expand Down Expand Up @@ -37,6 +38,11 @@ var (
systemSig = solana.MustSignatureFromBase58("1111111111111111111111111111111111111111111111111111111111111111")
)

// Override the default "old" token program to the token program 2022
func init() {
token.SetProgramID(tokenProgram2022)
}

type Solana struct {
rpcClient *rpc.Client
wsClient *ws.Client
Expand Down Expand Up @@ -82,7 +88,9 @@ func (sol *Solana) MintTokens(ctx context.Context, info MintInfo) error {

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(),
token.NewMintToCheckedInstruction(info.Amount, mint.Decimals, tftAddress, to, *mint.MintAuthority, nil).Build(),
// TODO: Compute actual limit
budget.NewSetComputeUnitLimitInstruction(40000).Build(),
}, recent.Value.Blockhash, solana.TransactionPayer(sol.account.PublicKey()))
if err != nil {
return errors.Wrap(err, "failed to create mint transaction")
Expand Down Expand Up @@ -135,12 +143,13 @@ func (sol *Solana) SubscribeTokenBurns(ctx context.Context) (<-chan Burn, error)
if err != nil {
return nil, errors.Wrap(err, "failed to subscribe to token program errors")
}
defer sub.Unsubscribe()

ch := make(chan Burn, 10)
go func() {
// Close the channel in case the goroutine exits
defer close(ch)
// Also close the subscription in this case
defer sub.Unsubscribe()

for {
got, err := sub.Recv(ctx)
Expand Down Expand Up @@ -173,6 +182,9 @@ func (sol *Solana) SubscribeTokenBurns(ctx context.Context) (<-chan Burn, error)
continue
}

spew.Dump(tx)

// TODO: Compute limit is optional
ixLen := len(tx.Message.Instructions)
if len(tx.Message.Instructions) != 3 {
log.Debug().Int("ixLen", ixLen).Str("signature", got.Value.Signature.String()).Msg("Skipping Tx which did not have the expected 3 instructions")
Expand Down

0 comments on commit b698acc

Please sign in to comment.