fix: add tx instruction->transfer data functionality

This commit is contained in:
Tyera Eulberg
2019-09-13 18:07:13 -06:00
committed by Michael Vines
parent ee3acbf1ba
commit daba1a7856
4 changed files with 257 additions and 47 deletions

View File

@ -1,6 +1,12 @@
// @flow
import {Account, BudgetProgram, SystemProgram} from '../src';
import {
Account,
BudgetProgram,
SystemInstruction,
SystemProgram,
Transaction,
} from '../src';
test('createAccount', () => {
const from = new Account();
@ -43,3 +49,95 @@ test('assign', () => {
expect(transaction.programId).toEqual(SystemProgram.programId);
// TODO: Validate transaction contents more
});
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]);
expect(systemInstruction.From).toEqual(from.publicKey);
expect(systemInstruction.To).toEqual(to.publicKey);
expect(systemInstruction.Amount).toEqual(amount);
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]);
expect(systemInstruction.From).toEqual(from.publicKey);
expect(systemInstruction.To).toEqual(to.publicKey);
expect(systemInstruction.Amount).toEqual(amount);
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]);
expect(systemInstruction.From).toBeNull();
expect(systemInstruction.To).toBeNull();
expect(systemInstruction.Amount).toBeNull();
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();
});