package org.p2p.solanaj.programs;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import org.p2p.solanaj.core.PublicKey;
import org.p2p.solanaj.core.TransactionInstruction;
import org.p2p.solanaj.core.AccountMeta;
import static org.bitcoinj.core.Utils.*;
public class SystemProgram {
public static void main(String[]args)
{
System.out.println("solana app transfer");
System.out.println("http://www.kikenn.com");
}
public static PublicKey PROGRAM_ID = new PublicKey("11111111111111111111111111111111");
public static final int PROGRAM_INDEX_CREATE_ACCOUNT = 0;
public static final int PROGRAM_INDEX_CREATE_ASSOCIATED_ACCOUNT = 2;
public static int PROGRAM_INDEX_TRANSFER =3;
public static TransactionInstruction transfer(PublicKey fromPublicKey, PublicKey toPublickKey, long lamports,boolean isSPLToken) {
ArrayList<AccountMeta> keys = new ArrayList<AccountMeta>();
keys.add(new AccountMeta(fromPublicKey, false, true));
keys.add(new AccountMeta(toPublickKey, false, true));
if(isSPLToken)
{
PROGRAM_ID = new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
byte[] data = new byte[1 + 8];
data[0] = (byte)((int)(255L & PROGRAM_INDEX_TRANSFER));
int64ToByteArrayLE(lamports, data, 1);
return new TransactionInstruction(PROGRAM_ID, keys, data);
} else {
PROGRAM_INDEX_TRANSFER=2;
byte[] data = new byte[4 + 8];
uint32ToByteArrayLE(PROGRAM_INDEX_TRANSFER, data, 0);
int64ToByteArrayLE(lamports, data, 4);
return new TransactionInstruction(PROGRAM_ID, keys, data);
}
}
public static TransactionInstruction createAssociatedTokenAccount(PublicKey address, PublicKey owner, PublicKey mint) {
ArrayList<AccountMeta> keys = new ArrayList<AccountMeta>();
keys.add(new AccountMeta(address, false, true));
keys.add(new AccountMeta(owner, false, false));
keys.add(new AccountMeta(mint, false, false));
keys.add(new AccountMeta(new PublicKey("11111111111111111111111111111111"), false, false));
keys.add(new AccountMeta(new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), false, false));
keys.add(new AccountMeta(new PublicKey("SysvarRent111111111111111111111111111111111"), false, false));
PROGRAM_ID = new PublicKey("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
byte[] data = new byte[1];
data[0]=0;
return new TransactionInstruction(PROGRAM_ID, keys, data);
}
public static TransactionInstruction createMemo(String memoString)
{
ArrayList<AccountMeta> keys = new ArrayList<AccountMeta>();
PROGRAM_ID = new PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr");
byte[] data = memoString.getBytes(StandardCharsets.UTF_8);
return new TransactionInstruction(PROGRAM_ID, keys, data);
}
public static TransactionInstruction createAccount(PublicKey fromPublicKey, PublicKey newAccountPublikkey,
long lamports, long space, PublicKey programId) {
ArrayList<AccountMeta> keys = new ArrayList<AccountMeta>();
keys.add(new AccountMeta(fromPublicKey, true, true));
keys.add(new AccountMeta(newAccountPublikkey, true, true));
byte[] data = new byte[4 + 8 + 8 + 32];
uint32ToByteArrayLE(PROGRAM_INDEX_CREATE_ACCOUNT, data, 0);
int64ToByteArrayLE(lamports, data, 4);
int64ToByteArrayLE(space, data, 12);
System.arraycopy(programId.toByteArray(), 0, data, 20, 32);
return new TransactionInstruction(PROGRAM_ID, keys, data);
}
}
solana app 交易指令设计
最新推荐文章于 2024-09-08 10:40:56 发布