Create Token Account
Associated Token Account (Recommended)
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/program/associated_token_account"
"github.com/blocto/solana-go-sdk/rpc"
"github.com/blocto/solana-go-sdk/types"
)
// FUarP2p5EnxD66vVDL4PWRoWMzA56ZVHG24hpEDFShEz
var feePayer, _ = types.AccountFromBase58("4TMFNY9ntAn3CHzguSAvDNLPRoQTaK3sWbQQXdDXaE6KWRBLufGL6PJdsD2koiEe3gGmMdRK3aAw7sikGNksHJrN")
// 9aE476sH92Vz7DMPyq5WLPkrKWivxeuTKEFKd2sZZcde
var alice, _ = types.AccountFromBase58("4voSPg3tYuWbKzimpQK9EbXHmuyy5fUrtXvpLDMLkmY6TRncaTHAKGD8jUg3maB5Jbrd9CkQg4qjJMyN6sQvnEF2")
var mintPubkey = common.PublicKeyFromString("F6tecPzBMF47yJ2EN6j2aGtE68yR5jehXcZYVZa6ZETo")
func main() {
c := client.NewClient(rpc.DevnetRPCEndpoint)
ata, _, err := common.FindAssociatedTokenAddress(alice.PublicKey, mintPubkey)
if err != nil {
log.Fatalf("find ata error, err: %v", err)
}
fmt.Println("ata:", ata.ToBase58())
res, err := c.GetLatestBlockhash(context.Background())
if err != nil {
log.Fatalf("get recent block hash error, err: %v\n", err)
}
tx, err := types.NewTransaction(types.NewTransactionParam{
Message: types.NewMessage(types.NewMessageParam{
FeePayer: feePayer.PublicKey,
RecentBlockhash: res.Blockhash,
Instructions: []types.Instruction{
associated_token_account.Create(associated_token_account.CreateParam{
Funder: feePayer.PublicKey,
Owner: alice.PublicKey,
Mint: mintPubkey,
AssociatedTokenAccount: ata,
}),
},
}),
Signers: []types.Account{feePayer},
})
if err != nil {
log.Fatalf("generate tx error, err: %v\n", err)
}
txhash, err := c.SendTransaction(context.Background(), tx)
if err != nil {
log.Fatalf("send raw tx error, err: %v\n", err)
}
log.Println("txhash:", txhash)
}
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
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
Random
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/program/system"
"github.com/blocto/solana-go-sdk/program/token"
"github.com/blocto/solana-go-sdk/rpc"
"github.com/blocto/solana-go-sdk/types"
)
// FUarP2p5EnxD66vVDL4PWRoWMzA56ZVHG24hpEDFShEz
var feePayer, _ = types.AccountFromBase58("4TMFNY9ntAn3CHzguSAvDNLPRoQTaK3sWbQQXdDXaE6KWRBLufGL6PJdsD2koiEe3gGmMdRK3aAw7sikGNksHJrN")
// 9aE476sH92Vz7DMPyq5WLPkrKWivxeuTKEFKd2sZZcde
var alice, _ = types.AccountFromBase58("4voSPg3tYuWbKzimpQK9EbXHmuyy5fUrtXvpLDMLkmY6TRncaTHAKGD8jUg3maB5Jbrd9CkQg4qjJMyN6sQvnEF2")
var mintPubkey = common.PublicKeyFromString("F6tecPzBMF47yJ2EN6j2aGtE68yR5jehXcZYVZa6ZETo")
func main() {
c := client.NewClient(rpc.DevnetRPCEndpoint)
aliceRandomTokenAccount := types.NewAccount()
fmt.Println("alice token account:", aliceRandomTokenAccount.PublicKey.ToBase58())
rentExemptionBalance, err := c.GetMinimumBalanceForRentExemption(context.Background(), token.TokenAccountSize)
if err != nil {
log.Fatalf("get min balacne for rent exemption, err: %v", err)
}
res, err := c.GetLatestBlockhash(context.Background())
if err != nil {
log.Fatalf("get recent block hash error, err: %v\n", err)
}
tx, err := types.NewTransaction(types.NewTransactionParam{
Message: types.NewMessage(types.NewMessageParam{
FeePayer: feePayer.PublicKey,
RecentBlockhash: res.Blockhash,
Instructions: []types.Instruction{
system.CreateAccount(system.CreateAccountParam{
From: feePayer.PublicKey,
New: aliceRandomTokenAccount.PublicKey,
Owner: common.TokenProgramID,
Lamports: rentExemptionBalance,
Space: token.TokenAccountSize,
}),
token.InitializeAccount(token.InitializeAccountParam{
Account: aliceRandomTokenAccount.PublicKey,
Mint: mintPubkey,
Owner: alice.PublicKey,
}),
},
}),
Signers: []types.Account{feePayer, aliceRandomTokenAccount},
})
if err != nil {
log.Fatalf("generate tx error, err: %v\n", err)
}
txhash, err := c.SendTransaction(context.Background(), tx)
if err != nil {
log.Fatalf("send tx error, err: %v\n", err)
}
log.Println("txhash:", txhash)
}
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
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