Files
solana/web3.js/src/budget-program.js

339 lines
8.2 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
/**
* Represents a condition that is met by executing a `applySignature()`
* transaction
2018-09-20 20:20:37 -07:00
*
* @typedef {Object} SignatureCondition
* @property {string} type Must equal the string 'timestamp'
* @property {PublicKey} from Public key from which `applySignature()` will be accepted from
2018-09-18 12:46:59 -07:00
*/
export type SignatureCondition = {
type: 'signature';
from: PublicKey;
};
/**
* Represents a condition that is met by executing a `applyTimestamp()`
* transaction
2018-09-20 20:20:37 -07:00
*
2018-09-27 14:34:07 -06:00
* @typedef {Object} TimestampCondition
2018-09-20 20:20:37 -07:00
* @property {string} type Must equal the string 'timestamp'
* @property {PublicKey} from Public key from which `applyTimestamp()` will be accepted from
* @property {Date} when The timestamp that was observed
2018-09-18 12:46:59 -07:00
*/
2018-09-27 14:34:07 -06:00
export type TimestampCondition = {
2018-09-18 12:46:59 -07:00
type: 'timestamp';
from: PublicKey;
when: Date;
};
/**
* Represents a payment to a given public key
2018-09-20 20:20:37 -07:00
*
* @typedef {Object} Payment
* @property {number} amount Number of tokens
* @property {PublicKey} to Public key of the recipient
2018-09-18 12:46:59 -07:00
*/
export type Payment = {
amount: number;
to: PublicKey;
}
/**
2018-09-20 20:20:37 -07:00
* A condition that can unlock a payment
*
2018-09-27 14:34:07 -06:00
* @typedef {SignatureCondition|TimestampCondition} BudgetCondition
2018-09-18 12:46:59 -07:00
*/
2018-09-27 14:34:07 -06:00
export type BudgetCondition = SignatureCondition | TimestampCondition;
2018-09-18 12:46:59 -07:00
/**
* @private
*/
function serializePayment(payment: Payment): Buffer {
2018-09-30 18:42:45 -07:00
const toData = payment.to.toBuffer();
2018-09-18 12:46:59 -07:00
const userdata = Buffer.alloc(8 + toData.length);
userdata.writeUInt32LE(payment.amount, 0);
toData.copy(userdata, 8);
return userdata;
}
/**
* @private
*/
function serializeDate(
when: Date
): Buffer {
const userdata = Buffer.alloc(8 + 20);
userdata.writeUInt32LE(20, 0); // size of timestamp as u64
function iso(date) {
function pad(number) {
if (number < 10) {
return '0' + number;
}
return number;
}
return date.getUTCFullYear() +
'-' + pad(date.getUTCMonth() + 1) +
'-' + pad(date.getUTCDate()) +
'T' + pad(date.getUTCHours()) +
':' + pad(date.getUTCMinutes()) +
':' + pad(date.getUTCSeconds()) +
'Z';
}
userdata.write(iso(when), 8);
return userdata;
}
/**
* @private
*/
function serializeCondition(condition: BudgetCondition) {
switch(condition.type) {
case 'timestamp':
{
const date = serializeDate(condition.when);
2018-09-30 18:42:45 -07:00
const from = condition.from.toBuffer();
2018-09-18 12:46:59 -07:00
const userdata = Buffer.alloc(4 + date.length + from.length);
userdata.writeUInt32LE(0, 0); // Condition enum = Timestamp
date.copy(userdata, 4);
from.copy(userdata, 4 + date.length);
return userdata;
}
case 'signature':
{
const userdataLayout = BufferLayout.struct([
BufferLayout.u32('condition'),
Layout.publicKey('from'),
]);
2018-09-18 12:46:59 -07:00
const from = condition.from.toBuffer();
2018-09-18 12:46:59 -07:00
const userdata = Buffer.alloc(4 + from.length);
userdataLayout.encode(
{
instruction: 1, // Signature
from
},
userdata,
);
2018-09-18 12:46:59 -07:00
return userdata;
}
default:
throw new Error(`Unknown condition type: ${condition.type}`);
}
}
/**
2018-09-20 10:10:46 -07:00
* Factory class for transactions to interact with the Budget program
2018-09-18 12:46:59 -07:00
*/
2018-09-20 10:10:46 -07:00
export class BudgetProgram {
2018-09-18 12:46:59 -07:00
/**
2018-09-20 10:10:46 -07:00
* Public key that identifies the Budget program
2018-09-18 12:46:59 -07:00
*/
2018-09-20 10:10:46 -07:00
static get programId(): PublicKey {
2018-10-26 20:57:00 -07:00
return new PublicKey('0x8100000000000000000000000000000000000000000000000000000000000000');
2018-09-18 12:46:59 -07:00
}
2018-09-19 17:35:16 -07:00
/**
2018-09-20 10:10:46 -07:00
* The amount of space this program requires
2018-09-19 17:35:16 -07:00
*/
static get space(): number {
return 128;
}
2018-09-18 12:46:59 -07:00
/**
* Creates a timestamp condition
*/
2018-09-27 14:34:07 -06:00
static timestampCondition(from: PublicKey, when: Date) : TimestampCondition {
2018-09-18 12:46:59 -07:00
return {
type: 'timestamp',
from,
when,
};
}
/**
* Creates a signature condition
*/
static signatureCondition(from: PublicKey) : SignatureCondition {
return {
type: 'signature',
from,
};
}
/**
2018-09-26 09:49:59 -07:00
* Generates a transaction that transfers tokens once any of the conditions are met
2018-09-18 12:46:59 -07:00
*/
static pay(
from: PublicKey,
2018-09-26 08:45:33 -07:00
program: PublicKey,
2018-09-18 12:46:59 -07:00
to: PublicKey,
amount: number,
...conditions: Array<BudgetCondition>
): Transaction {
const userdata = Buffer.alloc(1024);
let pos = 0;
userdata.writeUInt32LE(0, pos); // NewContract instruction
pos += 4;
userdata.writeUInt32LE(amount, pos); // Contract.tokens
pos += 8;
switch (conditions.length) {
case 0:
userdata.writeUInt32LE(0, pos); // Budget enum = Pay
pos += 4;
{
const payment = serializePayment({amount, to});
payment.copy(userdata, pos);
pos += payment.length;
}
return new Transaction().add({
2018-09-18 12:46:59 -07:00
keys: [from, to],
2018-09-20 10:10:46 -07:00
programId: this.programId,
2018-09-18 12:46:59 -07:00
userdata: userdata.slice(0, pos),
});
case 1:
userdata.writeUInt32LE(1, pos); // Budget enum = After
pos += 4;
{
const condition = conditions[0];
const conditionData = serializeCondition(condition);
conditionData.copy(userdata, pos);
pos += conditionData.length;
const paymentData = serializePayment({amount, to});
paymentData.copy(userdata, pos);
pos += paymentData.length;
}
return new Transaction().add({
2018-09-26 08:45:33 -07:00
keys: [from, program, to],
2018-09-20 10:10:46 -07:00
programId: this.programId,
2018-09-18 12:46:59 -07:00
userdata: userdata.slice(0, pos),
});
case 2:
2018-09-26 09:49:59 -07:00
userdata.writeUInt32LE(2, pos); // Budget enum = Or
2018-09-18 12:46:59 -07:00
pos += 4;
for (let condition of conditions) {
const conditionData = serializeCondition(condition);
conditionData.copy(userdata, pos);
pos += conditionData.length;
const paymentData = serializePayment({amount, to});
paymentData.copy(userdata, pos);
pos += paymentData.length;
}
return new Transaction().add({
2018-09-26 08:45:33 -07:00
keys: [from, program, to],
2018-09-20 10:10:46 -07:00
programId: this.programId,
2018-09-18 12:46:59 -07:00
userdata: userdata.slice(0, pos),
});
default:
throw new Error(`A maximum of two conditions are support: ${conditions.length} provided`);
}
}
2018-09-26 09:49:59 -07:00
/**
* Generates a transaction that transfers tokens once both conditions are met
*/
static payOnBoth(
from: PublicKey,
program: PublicKey,
to: PublicKey,
amount: number,
condition1: BudgetCondition,
condition2: BudgetCondition,
): Transaction {
const userdata = Buffer.alloc(1024);
let pos = 0;
userdata.writeUInt32LE(0, pos); // NewContract instruction
pos += 4;
userdata.writeUInt32LE(amount, pos); // Contract.tokens
pos += 8;
userdata.writeUInt32LE(3, pos); // Budget enum = And
pos += 4;
for (let condition of [condition1, condition2]) {
const conditionData = serializeCondition(condition);
conditionData.copy(userdata, pos);
pos += conditionData.length;
}
const paymentData = serializePayment({amount, to});
paymentData.copy(userdata, pos);
pos += paymentData.length;
return new Transaction().add({
2018-09-26 09:49:59 -07:00
keys: [from, program, to],
programId: this.programId,
userdata: userdata.slice(0, pos),
});
}
/**
* Generates a transaction that applies a timestamp, which could enable a
* pending payment to proceed.
*/
2018-09-26 08:45:33 -07:00
static applyTimestamp(from: PublicKey, program: PublicKey, to: PublicKey, when: Date): Transaction {
2018-09-18 12:46:59 -07:00
const whenData = serializeDate(when);
const userdata = Buffer.alloc(4 + whenData.length);
userdata.writeUInt32LE(1, 0); // ApplyTimestamp instruction
whenData.copy(userdata, 4);
return new Transaction().add({
2018-09-26 08:45:33 -07:00
keys: [from, program, to],
2018-09-20 10:10:46 -07:00
programId: this.programId,
2018-09-18 12:46:59 -07:00
userdata,
});
}
2018-09-26 09:49:59 -07:00
/**
* Generates a transaction that applies a signature, which could enable a
* pending payment to proceed.
*/
2018-09-26 08:45:33 -07:00
static applySignature(from: PublicKey, program: PublicKey, to: PublicKey): Transaction {
const userdataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'),
]);
const userdata = Buffer.alloc(userdataLayout.span);
userdataLayout.encode(
{
instruction: 2, // ApplySignature instruction
},
userdata,
);
2018-09-18 12:46:59 -07:00
return new Transaction().add({
2018-09-26 08:45:33 -07:00
keys: [from, program, to],
2018-09-20 10:10:46 -07:00
programId: this.programId,
2018-09-18 12:46:59 -07:00
userdata,
});
}
}