Accounts

client

// an account meta list indicates which accounts will be used in a program.
// we can only read/write accounts in progarms via this list.
// (except some official vars)

// an account meta includs
//   - isSigner
//     when an account is a signer. it needs to sign the tx.
//   - isWritable
//     when an account is writable. its data can be modified in this 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("CDKLz6tftV4kSD8sPVBD6ACqZpDY4Zuxf8rgSEYzR4M2")

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)
	}

	firstAccount := types.NewAccount()
	fmt.Printf("first account: %v\n", firstAccount.PublicKey)

	secondAccount := types.NewAccount()
	fmt.Printf("second account: %v\n", secondAccount.PublicKey)

	tx, err := types.NewTransaction(types.NewTransactionParam{
		Signers: []types.Account{feePayer, firstAccount},
		Message: types.NewMessage(types.NewMessageParam{
			FeePayer:        feePayer.PublicKey,
			RecentBlockhash: res.Blockhash,
			Instructions: []types.Instruction{
				{
					ProgramID: programId,
					Accounts: []types.AccountMeta{
						{
							PubKey:     firstAccount.PublicKey,
							IsSigner:   true,
							IsWritable: false,
						},
						{
							PubKey:     secondAccount.PublicKey,
							IsSigner:   false,
							IsWritable: true,
						},
					},
					Data: []byte{},
				},
			},
		}),
	})
	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)
	}

	// 4jYHKXhZMoDL3HsRuYhFPhCiQJhtNjDzPv8FhSnH6cMi9mwBjgW649uoqvfBjpGbkdFB53NEUux6oq3GUV8e9YQA
	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

program

use solana_program::{
    account_info::{next_account_info, AccountInfo},
    entrypoint,
    entrypoint::ProgramResult,
    msg,
    pubkey::Pubkey,
};

entrypoint!(process_instruction);

fn process_instruction(
    _program_id: &Pubkey,
    accounts: &[AccountInfo],
    _instruction_data: &[u8],
) -> ProgramResult {
    let account_info_iter = &mut accounts.iter();

    let first_account_info = next_account_info(account_info_iter)?;
    msg!(&format!(
        "first: {} isSigner: {}, isWritable: {}",
        first_account_info.key.to_string(),
        first_account_info.is_signer,
        first_account_info.is_writable,
    ));

    let second_account_info = next_account_info(account_info_iter)?;
    msg!(&format!(
        "second: {} isSigner: {}, isWritable: {}",
        second_account_info.key.to_string(),
        second_account_info.is_signer,
        second_account_info.is_writable,
    ));

    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
34
35
Last Updated:
Contributors: Yihau Chen