2018-09-26 09:29:24 -07:00
|
|
|
// @flow
|
|
|
|
|
2019-09-13 18:07:13 -06: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 18:07:13 -06: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 09:46:25 -06:00
|
|
|
expect(systemInstruction.fromPublicKey).toEqual(from.publicKey);
|
|
|
|
expect(systemInstruction.toPublicKey).toEqual(to.publicKey);
|
|
|
|
expect(systemInstruction.amount).toEqual(amount);
|
2019-09-13 18:07:13 -06: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 09:46:25 -06:00
|
|
|
expect(systemInstruction.fromPublicKey).toEqual(from.publicKey);
|
|
|
|
expect(systemInstruction.toPublicKey).toEqual(to.publicKey);
|
|
|
|
expect(systemInstruction.amount).toEqual(amount);
|
2019-09-13 18:07:13 -06: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 09:46:25 -06:00
|
|
|
expect(systemInstruction.fromPublicKey).toBeNull();
|
|
|
|
expect(systemInstruction.toPublicKey).toBeNull();
|
|
|
|
expect(systemInstruction.amount).toBeNull();
|
2019-09-13 18:07:13 -06:00
|
|
|
expect(systemInstruction.programId).toEqual(SystemProgram.programId);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('non-SystemInstruction error', () => {
|
|
|
|
const from = new Account();
|
|
|
|
const program = new Account();
|
|
|
|
const to = new Account();
|
|
|
|
|
|
|
|
const badProgramId = {
|
|
|
|
keys: [
|
|
|
|
{pubkey: from.publicKey, isSigner: true, isDebitable: true},
|
|
|
|
{pubkey: to.publicKey, isSigner: false, isDebitable: false},
|
|
|
|
],
|
|
|
|
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();
|
|
|
|
});
|