solana-go-sdk icon indicating copy to clipboard operation
solana-go-sdk copied to clipboard

Cant understand how to give instructions for raydium swap

Open shenyupeng opened this issue 1 year ago • 1 comments

shenyupeng avatar Jan 28 '24 06:01 shenyupeng

any updates?

@shenyupeng

0xdavid7 avatar Mar 06 '24 15:03 0xdavid7

@shenyupeng @tranhuyducseven in which version of Raydium are you interested?

I'd suggest taking a look at the documentation programs

example of swapping 100 USDC on Raydium V4

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/blocto/solana-go-sdk/client"
	"github.com/blocto/solana-go-sdk/common"
	"github.com/blocto/solana-go-sdk/rpc"
	"github.com/blocto/solana-go-sdk/types"
)

// FUarP2p5EnxD66vVDL4PWRoWMzA56ZVHG24hpEDFShEz
var feePayer, _ = types.AccountFromBase58("4TMFNY9ntAn3CHzguSAvDNLPRoQTaK3sWbQQXdDXaE6KWRBLufGL6PJdsD2koiEe3gGmMdRK3aAw7sikGNksHJrN")

var programId = common.PublicKeyFromString("675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8")

func main() {
	c := client.NewClient(rpc.MainnetRPCEndpoint)

	res, err := c.GetLatestBlockhash(context.Background())
	if err != nil {
		log.Fatalf("failed to get latest blockhash, err: %v\n", err)
	}

	tx, err := types.NewTransaction(types.NewTransactionParam{
		Signers: []types.Account{feePayer},
		Message: types.NewMessage(types.NewMessageParam{
			FeePayer:        feePayer.PublicKey,
			RecentBlockhash: res.Blockhash,
			Instructions: []types.Instruction{
				{
					ProgramID: programId,
					Accounts: []types.AccountMeta{
						// spl token
						{PubKey: common.TokenProgramID, IsWritable: false, IsSigner: false},
						// amm
						{PubKey: common.PublicKeyFromString("58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2"), IsWritable: true, IsSigner: false},
						{PubKey: common.PublicKeyFromString("5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1"), IsWritable: false, IsSigner: false},
						{PubKey: common.PublicKeyFromString("HmiHHzq4Fym9e1D4qzLS6LDDM3tNsCTBPDWHTLZ763jY"), IsWritable: true, IsSigner: false},
						{PubKey: common.PublicKeyFromString("CZza3Ej4Mc58MnxWA385itCC9jCo3L1D7zc3LKy1bZMR"), IsWritable: true, IsSigner: false},
						{PubKey: common.PublicKeyFromString("DQyrAcCrDXQ7NeoqGgDCZwBvWDcYmFCjSb9JtteuvPpz"), IsWritable: true, IsSigner: false},
						{PubKey: common.PublicKeyFromString("HLmqeL62xR1QoZ1HKKbXRrdN1p3phKpxRMb2VVopvBBz"), IsWritable: true, IsSigner: false},
						// serum
						{PubKey: common.PublicKeyFromString("srmqPvymJeFKQ4zGQed1GFppgkRHL9kaELCbyksJtPX"), IsWritable: false, IsSigner: false},
						{PubKey: common.PublicKeyFromString("8BnEgHoWFysVcuFFX7QztDmzuH8r5ZFvyP3sYwn1XTh6"), IsWritable: true, IsSigner: false},
						{PubKey: common.PublicKeyFromString("5jWUncPNBMZJ3sTHKmMLszypVkoRK6bfEQMQUHweeQnh"), IsWritable: true, IsSigner: false},
						{PubKey: common.PublicKeyFromString("EaXdHx7x3mdGA38j5RSmKYSXMzAFzzUXCLNBEDXDn1d5"), IsWritable: true, IsSigner: false},
						{PubKey: common.PublicKeyFromString("8CvwxZ9Db6XbLD46NZwwmVDZZRDy7eydFcAGkXKh9axa"), IsWritable: true, IsSigner: false},
						{PubKey: common.PublicKeyFromString("CKxTHwM9fPMRRvZmFnFoqKNd9pQR21c5Aq9bh5h9oghX"), IsWritable: true, IsSigner: false},
						{PubKey: common.PublicKeyFromString("6A5NHCj1yF6urc9wZNe6Bcjj4LVszQNj5DwAWG97yzMu"), IsWritable: true, IsSigner: false},
						{PubKey: common.PublicKeyFromString("CTz5UMLQm2SRWHzQnU62Pi4yJqbNGjgRBHqqp6oDHfF7"), IsWritable: false, IsSigner: false},
						// user
						{PubKey: common.PublicKeyFromString("====YOUR SRC ATA====="), IsWritable: true, IsSigner: false},
						{PubKey: common.PublicKeyFromString("====YOUR DST ATA====="), IsWritable: true, IsSigner: false},
						{PubKey: feePayer.PublicKey, IsWritable: false, IsSigner: true},
					},
					Data: []byte{
						// Encode the instruction
						// {
						// 	instruction: 9,        // take a look at the source code https://github.com/raydium-io/raydium-amm/blob/master/program/src/instruction.rs#L331
						// 	amountIn: 100_000_000, // your 100 USDC because USDC mint has 6 decimals
						// 	minAmountOut: 0.       // !! it's not recommended to leave it as 0
						// }
					},
				},
			},
		}),
	})
	if err != nil {
		log.Fatalf("failed to new a tx, err: %v", err)
	}

	sig, err := c.SendTransaction(context.Background(), tx)
	if err != nil {
		log.Fatalf("failed to send the tx, err: %v", err)
	}

	fmt.Println(sig)
}

0xvbetsun avatar Apr 06 '24 18:04 0xvbetsun