-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
166 lines (148 loc) · 5.27 KB
/
test.c
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <stdlib.h>
#include "header/solana_lib.h"
const char *file_path = "wallet_keypair.json";
const char *file_path_payer = "wallet_keypair.json";
const char *file_path_mint = "wallet_keypair_mint.json";
const char *devnet_url = "https://api.devnet.solana.com";
void test_create_and_save_wallet(const char *file_path)
{
SolKeyPair *wallet = create_and_save_wallet(file_path);
SolKeyPair *payer = create_and_save_wallet(file_path_payer);
SolClient *mint = create_and_save_wallet(file_path_mint);
// Check if the all wallet creation succeeded
if (wallet != NULL && payer != NULL && mint != NULL)
{
// Print the wallet address
printf("Solana Wallet Address: %s\n", get_wallet_address(wallet));
printf("Solana payer Wallet Address: %s\n", get_wallet_address(payer));
printf("Solana mint Wallet Address: %s\n", get_wallet_address(mint));
}
else
{
printf("Failed to create wallet.\n");
}
}
void test_load_wallet_from_file(const char *file_path)
{
SolKeyPair *wallet = load_wallet_from_file(file_path);
// Check if the wallet loading succeeded
if (wallet != NULL)
{
SolPublicKey *pub = get_public_key(wallet);
// Print the loaded public key
printf("Loaded Solana Wallet Public Key: %s\n", pub->data);
// Print the wallet address
printf("Solana Wallet Address: %s\n", get_wallet_address(wallet));
}
else
{
printf("Failed to load wallet.\n");
}
}
void test_sol_client_new(const char *url)
{
SolClient *client = new_sol_client(url);
if (client != NULL)
{
printf("Solana Client created successfully.\n");
}
else
{
printf("Failed to create Solana Client.\n");
}
}
void test_sol_airdrop()
{
SolClient *client = new_sol_client(devnet_url);
if (client != NULL) {
SolKeyPair *wallet = load_wallet_from_file(file_path);
if (wallet != NULL) {
SolPublicKey *pub = get_public_key(wallet);
uint64_t lamports = 100000000;
bool success = request_airdrop(client, pub, lamports);
if (success) {
printf("Airdrop successful.\n");
} else {
printf("Airdrop failed.\n");
}
//get balance
uint64_t balance = get_balance(client, pub);
printf("Balance: %lu\n", balance);
} else {
printf("Failed to load wallet.\n");
}
}
else {
printf("Failed to create Solana Client.\n");
}
}
void test_create_spl_token(){
SolClient *client = new_sol_client(devnet_url);
if (client != NULL) {
SolKeyPair *payer = load_wallet_from_file(file_path_payer);
SolClient *mint = load_wallet_from_file(file_path_mint);
if (payer != NULL && mint != NULL) {
printf("Solana mint Wallet Address: %s\n", get_wallet_address(mint));
bool success = create_spl_token(client, payer, mint);
if (success) {
printf("SPL Token created successfully.\n");
} else {
printf("Failed to create SPL Token.\n");
}
SolMint *mint_info = get_mint_info(client, mint);
if (mint_info != NULL) {
printf("Mint Supply: %lu\n", mint_info->supply);
printf("Mint Decimals: %u\n", mint_info->decimals);
printf("Mint is initialized: %s\n", mint_info->is_initialized ? "true" : "false");
} else {
printf("Failed to get mint info.\n");
}
} else {
printf("Failed to create wallets.\n");
}
} else {
printf("Failed to create Solana Client.\n");
}
}
void test_mint_spl_token(){
SolClient *client = new_sol_client(devnet_url);
if (client != NULL) {
SolKeyPair *payer = load_wallet_from_file(file_path_payer);
SolClient *mint = load_wallet_from_file(file_path_mint);
if (payer != NULL && mint != NULL) {
SolKeyPair *recipient = create_and_save_wallet("wallet_keypair_recipient.json");
if (recipient != NULL) {
printf("Solana recipient Wallet Address: %s\n", get_wallet_address(recipient));
uint64_t amount = 1000000000000;
bool success = mint_spl(client, payer, mint, get_public_key(recipient), amount);
//get balance
uint64_t balance = get_associated_token_balance(client, get_public_key(recipient), mint);
if (success) {
printf("SPL Token minted successfully.\n");
printf("Recipient Balance: %lu\n", balance);
} else {
printf("Failed to mint SPL Token.\n");
}
} else {
printf("Failed to create recipient wallet.\n");
}
} else {
printf("Failed to create wallets.\n");
}
} else {
printf("Failed to create Solana Client.\n");
}
}
int main()
{
// Create and save the wallet
// test_create_and_save_wallet(file_path);
// Load and verify the wallet
test_load_wallet_from_file(file_path);
test_sol_client_new(devnet_url);
test_sol_airdrop();
test_create_spl_token();
test_mint_spl_token();
return 0;
}