2018-09-18 12:46:59 -07:00
|
|
|
// @flow
|
|
|
|
|
|
|
|
import assert from 'assert';
|
|
|
|
|
|
|
|
import {Transaction} from './transaction';
|
2018-09-30 18:42:45 -07:00
|
|
|
import {PublicKey} from './publickey';
|
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,
|
2018-09-20 14:22:11 -07:00
|
|
|
programId: PublicKey
|
2018-09-18 12:46:59 -07:00
|
|
|
): Transaction {
|
|
|
|
const userdata = Buffer.alloc(4 + 8 + 8 + 1 + 32);
|
|
|
|
let pos = 0;
|
|
|
|
|
|
|
|
userdata.writeUInt32LE(0, pos); // Create Account instruction
|
|
|
|
pos += 4;
|
|
|
|
|
|
|
|
userdata.writeUInt32LE(tokens, pos); // tokens as i64
|
|
|
|
pos += 8;
|
|
|
|
|
|
|
|
userdata.writeUInt32LE(space, pos); // space as u64
|
|
|
|
pos += 8;
|
|
|
|
|
2018-09-30 18:42:45 -07:00
|
|
|
const programIdBytes = programId.toBuffer();
|
2018-09-20 14:22:11 -07:00
|
|
|
programIdBytes.copy(userdata, pos);
|
2018-09-30 21:10:12 -07:00
|
|
|
pos += programIdBytes.length;
|
2018-09-18 12:46:59 -07:00
|
|
|
|
|
|
|
assert(pos <= userdata.length);
|
|
|
|
|
|
|
|
return new Transaction({
|
|
|
|
fee: 0,
|
|
|
|
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 userdata = Buffer.alloc(4 + 8);
|
|
|
|
let pos = 0;
|
|
|
|
userdata.writeUInt32LE(2, pos); // Move instruction
|
|
|
|
pos += 4;
|
|
|
|
|
|
|
|
userdata.writeUInt32LE(amount, pos); // amount as u64
|
|
|
|
pos += 8;
|
|
|
|
|
|
|
|
assert(pos === userdata.length);
|
|
|
|
|
|
|
|
return new Transaction({
|
|
|
|
fee: 0,
|
|
|
|
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 {
|
2018-09-18 12:46:59 -07:00
|
|
|
const userdata = Buffer.alloc(4 + 32);
|
|
|
|
let pos = 0;
|
|
|
|
|
|
|
|
userdata.writeUInt32LE(1, pos); // Assign instruction
|
|
|
|
pos += 4;
|
|
|
|
|
2018-09-30 18:42:45 -07:00
|
|
|
const programIdBytes = programId.toBuffer();
|
2018-09-20 10:10:46 -07:00
|
|
|
programIdBytes.copy(userdata, pos);
|
|
|
|
pos += programIdBytes.length;
|
2018-09-18 12:46:59 -07:00
|
|
|
|
|
|
|
assert(pos === userdata.length);
|
|
|
|
|
|
|
|
return new Transaction({
|
|
|
|
fee: 0,
|
|
|
|
keys: [from],
|
2018-09-20 10:10:46 -07:00
|
|
|
programId: SystemProgram.programId,
|
2018-09-18 12:46:59 -07:00
|
|
|
userdata,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|