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

110 lines
2.6 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} from './transaction';
2018-09-30 18:42:45 -07:00
import {PublicKey} from './publickey';
import * as Layout from './layout';
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-03-14 13:27:47 -07:00
const dataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'),
2019-03-05 17:52:13 -08:00
BufferLayout.ns64('lamports'),
BufferLayout.ns64('space'),
Layout.publicKey('programId'),
]);
2019-03-14 13:27:47 -07:00
const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
{
instruction: 0, // Create Account instruction
2019-03-05 17:52:13 -08:00
lamports,
space,
programId: programId.toBuffer(),
},
2019-03-14 13:27:47 -07:00
data,
);
2018-09-18 12:46:59 -07:00
return new Transaction().add({
keys: [
{pubkey: from, isSigner: true},
{pubkey: newAccount, isSigner: false},
],
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
});
}
/**
2019-03-05 17:52:13 -08:00
* Generate a Transaction that moves lamports from one account to another
2018-09-18 12:46:59 -07:00
*/
static move(from: PublicKey, to: PublicKey, amount: number): Transaction {
2019-03-14 13:27:47 -07:00
const dataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'),
BufferLayout.ns64('amount'),
]);
2019-03-14 13:27:47 -07:00
const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
{
instruction: 2, // Move instruction
amount,
},
2019-03-14 13:27:47 -07:00
data,
);
2018-09-18 12:46:59 -07:00
return new Transaction().add({
keys: [{pubkey: from, isSigner: true}, {pubkey: to, isSigner: false}],
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-03-14 13:27:47 -07:00
const dataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'),
Layout.publicKey('programId'),
]);
2019-03-14 13:27:47 -07:00
const data = Buffer.alloc(dataLayout.span);
dataLayout.encode(
{
instruction: 1, // Assign instruction
programId: programId.toBuffer(),
},
2019-03-14 13:27:47 -07:00
data,
);
2018-09-18 12:46:59 -07:00
return new Transaction().add({
keys: [{pubkey: from, isSigner: 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
});
}
}