solana/web3.js/src/system-program.js

275 lines
7.1 KiB
JavaScript
Raw Normal View History

2018-09-18 12:46:59 -07:00
// @flow
import * as BufferLayout from 'buffer-layout';
2018-09-18 12:46:59 -07:00
import {Transaction, TransactionInstruction} from './transaction';
2018-09-30 18:42:45 -07:00
import {PublicKey} from './publickey';
import * as Layout from './layout';
import type {TransactionInstructionCtorFields} from './transaction';
/**
* System Instruction class
*/
export class SystemInstruction extends TransactionInstruction {
/**
* Type of SystemInstruction
*/
type: SystemInstructionType;
constructor(
opts?: TransactionInstructionCtorFields,
type?: SystemInstructionType,
) {
if (
opts &&
opts.programId &&
!opts.programId.equals(SystemProgram.programId)
) {
throw new Error('programId incorrect; not a SystemInstruction');
}
super(opts);
if (type) {
this.type = type;
}
}
static from(instruction: TransactionInstruction): SystemInstruction {
if (!instruction.programId.equals(SystemProgram.programId)) {
throw new Error('programId incorrect; not SystemProgram');
}
const instructionTypeLayout = BufferLayout.u32('instruction');
const typeIndex = instructionTypeLayout.decode(instruction.data);
let type;
2019-09-16 08:46:25 -07:00
for (const t in SystemInstructionLayout) {
if (SystemInstructionLayout[t].index == typeIndex) {
type = SystemInstructionLayout[t];
}
}
if (!type) {
throw new Error('Instruction type incorrect; not a SystemInstruction');
}
return new SystemInstruction(
{
keys: instruction.keys,
programId: instruction.programId,
data: instruction.data,
},
type,
);
}
/**
* The `from` public key of the instruction;
* returns null if SystemInstructionType does not support this field
*/
2019-09-16 08:46:25 -07:00
get fromPublicKey(): PublicKey | null {
if (
2019-09-16 08:46:25 -07:00
this.type == SystemInstructionLayout.Create ||
this.type == SystemInstructionLayout.CreateWithSeed ||
2019-09-16 08:46:25 -07:00
this.type == SystemInstructionLayout.Transfer
) {
return this.keys[0].pubkey;
}
return null;
}
/**
* The `to` public key of the instruction;
* returns null if SystemInstructionType does not support this field
*/
2019-09-16 08:46:25 -07:00
get toPublicKey(): PublicKey | null {
if (
2019-09-16 08:46:25 -07:00
this.type == SystemInstructionLayout.Create ||
this.type == SystemInstructionLayout.CreateWithSeed ||
2019-09-16 08:46:25 -07:00
this.type == SystemInstructionLayout.Transfer
) {
return this.keys[1].pubkey;
}
return null;
}
/**
* The `amount` or `lamports` of the instruction;
* returns null if SystemInstructionType does not support this field
*/
2019-09-16 08:46:25 -07:00
get amount(): number | null {
const data = this.type.layout.decode(this.data);
2019-09-16 08:46:25 -07:00
if (this.type == SystemInstructionLayout.Transfer) {
return data.amount;
} else if (
this.type == SystemInstructionLayout.Create ||
this.type == SystemInstructionLayout.CreateWithSeed
) {
return data.lamports;
}
return null;
}
}
/**
* @typedef {Object} SystemInstructionType
* @property (index} The System Instruction index (from solana-sdk)
* @property (BufferLayout} The BufferLayout to use to build data
*/
type SystemInstructionType = {|
index: number,
layout: typeof BufferLayout,
|};
/**
* An enumeration of valid SystemInstructionTypes
*/
2019-09-16 08:46:25 -07:00
const SystemInstructionLayout = Object.freeze({
Create: {
index: 0,
layout: BufferLayout.struct([
BufferLayout.u32('instruction'),
BufferLayout.ns64('lamports'),
BufferLayout.ns64('space'),
Layout.publicKey('programId'),
]),
},
2019-09-16 08:46:25 -07:00
Assign: {
index: 1,
layout: BufferLayout.struct([
BufferLayout.u32('instruction'),
Layout.publicKey('programId'),
]),
},
2019-09-16 08:46:25 -07:00
Transfer: {
index: 2,
layout: BufferLayout.struct([
BufferLayout.u32('instruction'),
BufferLayout.ns64('amount'),
]),
},
CreateWithSeed: {
index: 3,
layout: BufferLayout.struct([
BufferLayout.u32('instruction'),
Layout.publicKey('base'),
Layout.rustString('seed'),
BufferLayout.ns64('lamports'),
BufferLayout.ns64('space'),
Layout.publicKey('programId'),
]),
},
});
/**
* Populate a buffer of instruction data using the SystemInstructionType
*/
function encodeData(type: SystemInstructionType, fields: Object): Buffer {
const allocLength =
type.layout.span >= 0 ? type.layout.span : Layout.getAlloc(type, fields);
const data = Buffer.alloc(allocLength);
const layoutFields = Object.assign({instruction: type.index}, fields);
type.layout.encode(layoutFields, data);
return data;
}
2018-09-18 12:46:59 -07:00
/**
2018-09-20 10:10:46 -07:00
* Factory class for transactions to interact with the System program
2018-09-18 12:46:59 -07:00
*/
2018-09-20 10:10:46 -07:00
export class SystemProgram {
2018-09-18 12:46:59 -07:00
/**
2018-09-20 10:10:46 -07:00
* Public key that identifies the System program
2018-09-18 12:46:59 -07:00
*/
2018-09-20 10:10:46 -07:00
static get programId(): PublicKey {
2018-11-04 11:41:21 -08:00
return new PublicKey(
'0x000000000000000000000000000000000000000000000000000000000000000',
);
2018-09-18 12:46:59 -07:00
}
/**
* Generate a Transaction that creates a new account
*/
static createAccount(
from: PublicKey,
newAccount: PublicKey,
2019-03-05 17:52:13 -08:00
lamports: number,
2018-09-18 12:46:59 -07:00
space: number,
2018-11-04 11:41:21 -08:00
programId: PublicKey,
2018-09-18 12:46:59 -07:00
): Transaction {
2019-09-16 08:46:25 -07:00
const type = SystemInstructionLayout.Create;
const data = encodeData(type, {
lamports,
space,
programId: programId.toBuffer(),
});
2018-09-18 12:46:59 -07:00
return new Transaction().add({
keys: [
{pubkey: from, isSigner: true, isWritable: true},
{pubkey: newAccount, isSigner: true, isWritable: true},
],
2018-09-20 10:10:46 -07:00
programId: SystemProgram.programId,
2019-03-14 13:27:47 -07:00
data,
2018-09-18 12:46:59 -07:00
});
}
/**
* Generate a Transaction that transfers lamports from one account to another
2018-09-18 12:46:59 -07:00
*/
static transfer(from: PublicKey, to: PublicKey, amount: number): Transaction {
2019-09-16 08:46:25 -07:00
const type = SystemInstructionLayout.Transfer;
const data = encodeData(type, {amount});
2018-09-18 12:46:59 -07:00
return new Transaction().add({
keys: [
{pubkey: from, isSigner: true, isWritable: true},
{pubkey: to, isSigner: false, isWritable: true},
],
2018-09-20 10:10:46 -07:00
programId: SystemProgram.programId,
2019-03-14 13:27:47 -07:00
data,
2018-09-18 12:46:59 -07:00
});
}
/**
2018-09-20 10:10:46 -07:00
* Generate a Transaction that assigns an account to a program
2018-09-18 12:46:59 -07:00
*/
2018-09-20 10:10:46 -07:00
static assign(from: PublicKey, programId: PublicKey): Transaction {
2019-09-16 08:46:25 -07:00
const type = SystemInstructionLayout.Assign;
const data = encodeData(type, {programId: programId.toBuffer()});
2018-09-18 12:46:59 -07:00
return new Transaction().add({
keys: [{pubkey: from, isSigner: true, isWritable: true}],
2018-09-20 10:10:46 -07:00
programId: SystemProgram.programId,
2019-03-14 13:27:47 -07:00
data,
2018-09-18 12:46:59 -07:00
});
}
/**
* Generate a Transaction that creates a new account at
* an address generated with `from`, a seed, and programId
*/
static createAccountWithSeed(
from: PublicKey,
newAccount: PublicKey,
base: PublicKey,
seed: string,
lamports: number,
space: number,
programId: PublicKey,
): Transaction {
const type = SystemInstructionLayout.CreateWithSeed;
const data = encodeData(type, {
base: base.toBuffer(),
seed,
lamports,
space,
programId: programId.toBuffer(),
});
return new Transaction().add({
keys: [
{pubkey: from, isSigner: true, isWritable: true},
{pubkey: newAccount, isSigner: false, isWritable: true},
],
programId: SystemProgram.programId,
data,
});
}
2018-09-18 12:46:59 -07:00
}