Data
client
// data is the most powerful part in an instruction
// we can pack everything into data, like number, pubkey ... whatever you want.
// we need to make them become a u8 array when we try to pack it in to a tx.
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("c6vyXkJqgA85rYnLiMqxXd39fusJWbRJkoF3jXTd96H")
func main() {
c := client.NewClient(rpc.DevnetRPCEndpoint)
res, err := c.GetLatestBlockhash(context.Background())
if err != nil {
log.Fatalf("failed to get latest blockhash, err: %v\n", err)
}
// (our example prgoram will parse the first byte as the selector then print remaining data.)
{
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{},
Data: []byte{0, 1, 2, 3, 4},
},
},
}),
})
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)
}
// 5X3qhwXJcjSZ3KqY8cTs5YbswBTS7yyqnfTn2diGwGERPrMNUjh9efc6Y9ABanfDUzaQN1n6BHHyMRjDJk2tfy1i
fmt.Println(sig)
}
{
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{},
Data: []byte{1, 5, 6, 7, 8},
},
},
}),
})
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)
}
// 5GETq1uLwMGdmsAH79pPByzGxQxXS1LqZ6Y9T6dBQ5qiMHFo5EgwDAHccFVEcc9hTYyj5zGfLX6j1uSz5NX7HZ8Q
fmt.Println(sig)
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
program
use solana_program::{
account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, msg,
program_error::ProgramError, pubkey::Pubkey,
};
entrypoint!(process_instruction);
fn process_instruction(
_program_id: &Pubkey,
_accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
let (selector, rest) = instruction_data
.split_first()
.ok_or(ProgramError::InvalidInstructionData)?;
match selector {
0 => msg!(&format!(
"first instruction is called. remaining data: {:?}",
rest,
)),
1 => msg!(&format!(
"second instruction is called. remaining data: {:?}",
rest,
)),
_ => {
msg!("invalid called");
return Err(ProgramError::InvalidInstructionData);
}
}
Ok(())
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33