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

129 lines
3.0 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-09-30 18:42:45 -07: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,
tokens: number,
space: number,
programId: PublicKey
2018-09-18 12:46:59 -07:00
): Transaction {
const userdataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'),
BufferLayout.ns64('tokens'),
BufferLayout.ns64('space'),
Layout.publicKey('programId'),
]);
const userdata = Buffer.alloc(userdataLayout.span);
userdataLayout.encode(
{
instruction: 0, // Create Account instruction
tokens,
space,
programId: programId.toBuffer(),
},
userdata,
);
2018-09-18 12:46:59 -07:00
return new Transaction().add({
2018-09-18 12:46:59 -07:00
keys: [from, newAccount],
2018-09-20 10:10:46 -07:00
programId: SystemProgram.programId,
2018-09-18 12:46:59 -07:00
userdata,
});
}
/**
* Generate a Transaction that moves tokens from one account to another
*/
static move(from: PublicKey, to: PublicKey, amount: number): Transaction {
const userdataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'),
BufferLayout.ns64('amount'),
]);
const userdata = Buffer.alloc(userdataLayout.span);
userdataLayout.encode(
{
instruction: 2, // Move instruction
amount,
},
userdata,
);
2018-09-18 12:46:59 -07:00
return new Transaction().add({
2018-09-18 12:46:59 -07:00
keys: [from, to],
2018-09-20 10:10:46 -07:00
programId: SystemProgram.programId,
2018-09-18 12:46:59 -07:00
userdata,
});
}
/**
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 {
const userdataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'),
Layout.publicKey('programId'),
]);
const userdata = Buffer.alloc(userdataLayout.span);
userdataLayout.encode(
{
instruction: 1, // Assign instruction
programId: programId.toBuffer(),
},
userdata,
);
2018-09-18 12:46:59 -07:00
return new Transaction().add({
2018-09-18 12:46:59 -07:00
keys: [from],
2018-09-20 10:10:46 -07:00
programId: SystemProgram.programId,
2018-09-18 12:46:59 -07:00
userdata,
});
}
/**
* Spawn a new program from an account
*/
static spawn(programId: PublicKey): Transaction {
const userdataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'),
]);
const userdata = Buffer.alloc(userdataLayout.span);
userdataLayout.encode(
{
instruction: 3, // Spawn instruction
},
userdata,
);
return new Transaction().add({
keys: [programId],
programId: SystemProgram.programId,
userdata,
});
}
2018-09-18 12:46:59 -07:00
}