2018-09-26 09:29:24 -07:00
|
|
|
// @flow
|
|
|
|
|
2019-09-13 17:07:13 -07:00
|
|
|
import {
|
|
|
|
Account,
|
|
|
|
BudgetProgram,
|
|
|
|
SystemInstruction,
|
|
|
|
SystemProgram,
|
|
|
|
Transaction,
|
|
|
|
} from '../src';
|
2018-09-26 09:29:24 -07:00
|
|
|
|
|
|
|
test('createAccount', () => {
|
|
|
|
const from = new Account();
|
|
|
|
const newAccount = new Account();
|
|
|
|
let transaction;
|
|
|
|
|
|
|
|
transaction = SystemProgram.createAccount(
|
|
|
|
from.publicKey,
|
|
|
|
newAccount.publicKey,
|
|
|
|
123,
|
|
|
|
BudgetProgram.space,
|
|
|
|
BudgetProgram.programId,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(transaction.keys).toHaveLength(2);
|
|
|
|
expect(transaction.programId).toEqual(SystemProgram.programId);
|
|
|
|
// TODO: Validate transaction contents more
|
|
|
|
});
|
|
|
|
|
2019-04-11 18:20:22 -07:00
|
|
|
test('transfer', () => {
|
2018-09-26 09:29:24 -07:00
|
|
|
const from = new Account();
|
|
|
|
const to = new Account();
|
|
|
|
let transaction;
|
|
|
|
|
2019-04-11 18:20:22 -07:00
|
|
|
transaction = SystemProgram.transfer(from.publicKey, to.publicKey, 123);
|
2018-09-26 09:29:24 -07:00
|
|
|
|
|
|
|
expect(transaction.keys).toHaveLength(2);
|
|
|
|
expect(transaction.programId).toEqual(SystemProgram.programId);
|
|
|
|
// TODO: Validate transaction contents more
|
|
|
|
});
|
|
|
|
|
|
|
|
test('assign', () => {
|
|
|
|
const from = new Account();
|
|
|
|
const to = new Account();
|
|
|
|
let transaction;
|
|
|
|
|
2018-11-04 11:41:21 -08:00
|
|
|
transaction = SystemProgram.assign(from.publicKey, to.publicKey);
|
2018-09-26 09:29:24 -07:00
|
|
|
|
|
|
|
expect(transaction.keys).toHaveLength(1);
|
|
|
|
expect(transaction.programId).toEqual(SystemProgram.programId);
|
|
|
|
// TODO: Validate transaction contents more
|
|
|
|
});
|
2019-09-13 17:07:13 -07:00
|
|
|
|
2019-12-09 19:50:01 -08:00
|
|
|
test('createAccountWithSeed', () => {
|
|
|
|
const from = new Account();
|
|
|
|
const newAccount = new Account();
|
|
|
|
let transaction;
|
|
|
|
|
|
|
|
transaction = SystemProgram.createAccountWithSeed(
|
|
|
|
from.publicKey,
|
|
|
|
newAccount.publicKey,
|
|
|
|
'hi there',
|
|
|
|
123,
|
|
|
|
BudgetProgram.space,
|
|
|
|
BudgetProgram.programId,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(transaction.keys).toHaveLength(2);
|
|
|
|
expect(transaction.programId).toEqual(SystemProgram.programId);
|
|
|
|
// TODO: Validate transaction contents more
|
|
|
|
});
|
|
|
|
|
2019-09-13 17:07:13 -07:00
|
|
|
test('SystemInstruction create', () => {
|
|
|
|
const from = new Account();
|
|
|
|
const to = new Account();
|
|
|
|
const program = new Account();
|
|
|
|
const amount = 42;
|
|
|
|
const space = 100;
|
|
|
|
const recentBlockhash = 'EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k'; // Arbitrary known recentBlockhash
|
|
|
|
const create = SystemProgram.createAccount(
|
|
|
|
from.publicKey,
|
|
|
|
to.publicKey,
|
|
|
|
amount,
|
|
|
|
space,
|
|
|
|
program.publicKey,
|
|
|
|
);
|
|
|
|
const transaction = new Transaction({recentBlockhash}).add(create);
|
|
|
|
|
|
|
|
const systemInstruction = SystemInstruction.from(transaction.instructions[0]);
|
2019-09-16 08:46:25 -07:00
|
|
|
expect(systemInstruction.fromPublicKey).toEqual(from.publicKey);
|
|
|
|
expect(systemInstruction.toPublicKey).toEqual(to.publicKey);
|
|
|
|
expect(systemInstruction.amount).toEqual(amount);
|
2019-09-13 17:07:13 -07:00
|
|
|
expect(systemInstruction.programId).toEqual(SystemProgram.programId);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('SystemInstruction transfer', () => {
|
|
|
|
const from = new Account();
|
|
|
|
const to = new Account();
|
|
|
|
const amount = 42;
|
|
|
|
const recentBlockhash = 'EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k'; // Arbitrary known recentBlockhash
|
|
|
|
const transfer = SystemProgram.transfer(from.publicKey, to.publicKey, amount);
|
|
|
|
const transaction = new Transaction({recentBlockhash}).add(transfer);
|
|
|
|
transaction.sign(from);
|
|
|
|
|
|
|
|
const systemInstruction = SystemInstruction.from(transaction.instructions[0]);
|
2019-09-16 08:46:25 -07:00
|
|
|
expect(systemInstruction.fromPublicKey).toEqual(from.publicKey);
|
|
|
|
expect(systemInstruction.toPublicKey).toEqual(to.publicKey);
|
|
|
|
expect(systemInstruction.amount).toEqual(amount);
|
2019-09-13 17:07:13 -07:00
|
|
|
expect(systemInstruction.programId).toEqual(SystemProgram.programId);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('SystemInstruction assign', () => {
|
|
|
|
const from = new Account();
|
|
|
|
const program = new Account();
|
|
|
|
const recentBlockhash = 'EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k'; // Arbitrary known recentBlockhash
|
|
|
|
const assign = SystemProgram.assign(from.publicKey, program.publicKey);
|
|
|
|
const transaction = new Transaction({recentBlockhash}).add(assign);
|
|
|
|
transaction.sign(from);
|
|
|
|
|
|
|
|
const systemInstruction = SystemInstruction.from(transaction.instructions[0]);
|
2019-09-16 08:46:25 -07:00
|
|
|
expect(systemInstruction.fromPublicKey).toBeNull();
|
|
|
|
expect(systemInstruction.toPublicKey).toBeNull();
|
|
|
|
expect(systemInstruction.amount).toBeNull();
|
2019-09-13 17:07:13 -07:00
|
|
|
expect(systemInstruction.programId).toEqual(SystemProgram.programId);
|
|
|
|
});
|
|
|
|
|
2019-12-09 19:50:01 -08:00
|
|
|
test('SystemInstruction createWithSeed', () => {
|
|
|
|
const from = new Account();
|
|
|
|
const to = new Account();
|
|
|
|
const program = new Account();
|
|
|
|
const amount = 42;
|
|
|
|
const space = 100;
|
|
|
|
const recentBlockhash = 'EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k'; // Arbitrary known recentBlockhash
|
|
|
|
const create = SystemProgram.createAccountWithSeed(
|
|
|
|
from.publicKey,
|
|
|
|
to.publicKey,
|
|
|
|
'hi there',
|
|
|
|
amount,
|
|
|
|
space,
|
|
|
|
program.publicKey,
|
|
|
|
);
|
|
|
|
const transaction = new Transaction({recentBlockhash}).add(create);
|
|
|
|
|
|
|
|
const systemInstruction = SystemInstruction.from(transaction.instructions[0]);
|
|
|
|
expect(systemInstruction.fromPublicKey).toEqual(from.publicKey);
|
|
|
|
expect(systemInstruction.toPublicKey).toEqual(to.publicKey);
|
|
|
|
expect(systemInstruction.amount).toEqual(amount);
|
|
|
|
expect(systemInstruction.programId).toEqual(SystemProgram.programId);
|
|
|
|
});
|
|
|
|
|
2019-09-13 17:07:13 -07:00
|
|
|
test('non-SystemInstruction error', () => {
|
|
|
|
const from = new Account();
|
|
|
|
const program = new Account();
|
|
|
|
const to = new Account();
|
|
|
|
|
|
|
|
const badProgramId = {
|
|
|
|
keys: [
|
2019-11-06 09:42:01 -08:00
|
|
|
{pubkey: from.publicKey, isSigner: true, isWritable: true},
|
|
|
|
{pubkey: to.publicKey, isSigner: false, isWritable: true},
|
2019-09-13 17:07:13 -07:00
|
|
|
],
|
|
|
|
programId: BudgetProgram.programId,
|
|
|
|
data: Buffer.from([2, 0, 0, 0]),
|
|
|
|
};
|
|
|
|
expect(() => {
|
|
|
|
new SystemInstruction(badProgramId);
|
|
|
|
}).toThrow();
|
|
|
|
|
|
|
|
const amount = 123;
|
|
|
|
const recentBlockhash = 'EETubP5AKHgjPAhzPAFcb8BAY1hMH639CWCFTqi3hq1k'; // Arbitrary known recentBlockhash
|
|
|
|
const budgetPay = BudgetProgram.pay(
|
|
|
|
from.publicKey,
|
|
|
|
program.publicKey,
|
|
|
|
to.publicKey,
|
|
|
|
amount,
|
|
|
|
);
|
|
|
|
const transaction = new Transaction({recentBlockhash}).add(budgetPay);
|
|
|
|
transaction.sign(from);
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
SystemInstruction.from(transaction.instructions[1]);
|
|
|
|
}).toThrow();
|
|
|
|
|
|
|
|
transaction.instructions[0].data[0] = 4;
|
|
|
|
expect(() => {
|
|
|
|
SystemInstruction.from(transaction.instructions[0]);
|
|
|
|
}).toThrow();
|
|
|
|
});
|