s/contract/program/
This commit is contained in:
@ -54,12 +54,12 @@ showBalance()
|
|||||||
.then(showBalance)
|
.then(showBalance)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log(`\n== Creating account for the contract funds`);
|
console.log(`\n== Creating account for the contract funds`);
|
||||||
const transaction = solanaWeb3.SystemContract.createAccount(
|
const transaction = solanaWeb3.SystemProgram.createAccount(
|
||||||
account1.publicKey,
|
account1.publicKey,
|
||||||
contractFunds.publicKey,
|
contractFunds.publicKey,
|
||||||
50, // number of tokens to transfer
|
50, // number of tokens to transfer
|
||||||
0,
|
0,
|
||||||
solanaWeb3.BudgetContract.contractId,
|
solanaWeb3.BudgetProgram.programId,
|
||||||
);
|
);
|
||||||
return connection.sendTransaction(account1, transaction);
|
return connection.sendTransaction(account1, transaction);
|
||||||
})
|
})
|
||||||
@ -67,12 +67,12 @@ showBalance()
|
|||||||
.then(showBalance)
|
.then(showBalance)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log(`\n== Creating account for the contract state`);
|
console.log(`\n== Creating account for the contract state`);
|
||||||
const transaction = solanaWeb3.SystemContract.createAccount(
|
const transaction = solanaWeb3.SystemProgram.createAccount(
|
||||||
account1.publicKey,
|
account1.publicKey,
|
||||||
contractState.publicKey,
|
contractState.publicKey,
|
||||||
1, // sender pays 1 token to hold the contract state
|
1, // account1 pays 1 token to hold the contract state
|
||||||
solanaWeb3.BudgetContract.space,
|
solanaWeb3.BudgetProgram.space,
|
||||||
solanaWeb3.BudgetContract.contractId,
|
solanaWeb3.BudgetProgram.programId,
|
||||||
);
|
);
|
||||||
return connection.sendTransaction(account1, transaction);
|
return connection.sendTransaction(account1, transaction);
|
||||||
})
|
})
|
||||||
@ -80,12 +80,12 @@ showBalance()
|
|||||||
.then(showBalance)
|
.then(showBalance)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log(`\n== Initializing contract`);
|
console.log(`\n== Initializing contract`);
|
||||||
const transaction = solanaWeb3.BudgetContract.pay(
|
const transaction = solanaWeb3.BudgetProgram.pay(
|
||||||
contractFunds.publicKey,
|
contractFunds.publicKey,
|
||||||
contractState.publicKey,
|
contractState.publicKey,
|
||||||
account2.publicKey,
|
account2.publicKey,
|
||||||
50,
|
50,
|
||||||
solanaWeb3.BudgetContract.timestampCondition(account1.publicKey, new Date("2050")),
|
solanaWeb3.BudgetProgram.timestampCondition(account1.publicKey, new Date('2050')),
|
||||||
);
|
);
|
||||||
return connection.sendTransaction(contractFunds, transaction);
|
return connection.sendTransaction(contractFunds, transaction);
|
||||||
})
|
})
|
||||||
@ -93,11 +93,11 @@ showBalance()
|
|||||||
.then(showBalance)
|
.then(showBalance)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log(`\n== Witness contract`);
|
console.log(`\n== Witness contract`);
|
||||||
const transaction = solanaWeb3.BudgetContract.applyTimestamp(
|
const transaction = solanaWeb3.BudgetProgram.applyTimestamp(
|
||||||
account1.publicKey,
|
account1.publicKey,
|
||||||
contractState.publicKey,
|
contractState.publicKey,
|
||||||
account2.publicKey,
|
account2.publicKey,
|
||||||
new Date("2050"),
|
new Date('2050'),
|
||||||
);
|
);
|
||||||
return connection.sendTransaction(account1, transaction);
|
return connection.sendTransaction(account1, transaction);
|
||||||
})
|
})
|
||||||
|
@ -107,19 +107,19 @@ function serializeCondition(condition: BudgetCondition) {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory class for transactions to interact with the Budget contract
|
* Factory class for transactions to interact with the Budget program
|
||||||
*/
|
*/
|
||||||
export class BudgetContract {
|
export class BudgetProgram {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Public key that identifies the Budget Contract
|
* Public key that identifies the Budget program
|
||||||
*/
|
*/
|
||||||
static get contractId(): PublicKey {
|
static get programId(): PublicKey {
|
||||||
return '4uQeVj5tqViQh7yWWGStvkEG1Zmhx6uasJtWCJziofM';
|
return '4uQeVj5tqViQh7yWWGStvkEG1Zmhx6uasJtWCJziofM';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The amount of space this contract requires
|
* The amount of space this program requires
|
||||||
*/
|
*/
|
||||||
static get space(): number {
|
static get space(): number {
|
||||||
return 128;
|
return 128;
|
||||||
@ -184,7 +184,7 @@ export class BudgetContract {
|
|||||||
return new Transaction({
|
return new Transaction({
|
||||||
fee: 0,
|
fee: 0,
|
||||||
keys: [from, to],
|
keys: [from, to],
|
||||||
contractId: this.contractId,
|
programId: this.programId,
|
||||||
userdata: userdata.slice(0, pos),
|
userdata: userdata.slice(0, pos),
|
||||||
});
|
});
|
||||||
case 1:
|
case 1:
|
||||||
@ -205,7 +205,7 @@ export class BudgetContract {
|
|||||||
return new Transaction({
|
return new Transaction({
|
||||||
fee: 0,
|
fee: 0,
|
||||||
keys: [from, contract, to],
|
keys: [from, contract, to],
|
||||||
contractId: this.contractId,
|
programId: this.programId,
|
||||||
userdata: userdata.slice(0, pos),
|
userdata: userdata.slice(0, pos),
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -226,7 +226,7 @@ export class BudgetContract {
|
|||||||
return new Transaction({
|
return new Transaction({
|
||||||
fee: 0,
|
fee: 0,
|
||||||
keys: [from, contract, to],
|
keys: [from, contract, to],
|
||||||
contractId: this.contractId,
|
programId: this.programId,
|
||||||
userdata: userdata.slice(0, pos),
|
userdata: userdata.slice(0, pos),
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -245,7 +245,7 @@ export class BudgetContract {
|
|||||||
return new Transaction({
|
return new Transaction({
|
||||||
fee: 0,
|
fee: 0,
|
||||||
keys: [from, contract, to],
|
keys: [from, contract, to],
|
||||||
contractId: this.contractId,
|
programId: this.programId,
|
||||||
userdata,
|
userdata,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -257,7 +257,7 @@ export class BudgetContract {
|
|||||||
return new Transaction({
|
return new Transaction({
|
||||||
fee: 0,
|
fee: 0,
|
||||||
keys: [from, contract, to],
|
keys: [from, contract, to],
|
||||||
contractId: this.contractId,
|
programId: this.programId,
|
||||||
userdata,
|
userdata,
|
||||||
});
|
});
|
||||||
}
|
}
|
@ -2,5 +2,5 @@
|
|||||||
export {Account} from './account';
|
export {Account} from './account';
|
||||||
export {Connection} from './connection';
|
export {Connection} from './connection';
|
||||||
export {Transaction} from './transaction';
|
export {Transaction} from './transaction';
|
||||||
export {SystemContract} from './system-contract';
|
export {SystemProgram} from './system-program';
|
||||||
export {BudgetContract} from './budget-contract';
|
export {BudgetProgram} from './budget-program';
|
||||||
|
@ -6,13 +6,13 @@ import {Transaction} from './transaction';
|
|||||||
import type {PublicKey} from './account';
|
import type {PublicKey} from './account';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory class for transactions to interact with the System contract
|
* Factory class for transactions to interact with the System program
|
||||||
*/
|
*/
|
||||||
export class SystemContract {
|
export class SystemProgram {
|
||||||
/**
|
/**
|
||||||
* Public key that identifies the System Contract
|
* Public key that identifies the System program
|
||||||
*/
|
*/
|
||||||
static get contractId(): PublicKey {
|
static get programId(): PublicKey {
|
||||||
return '11111111111111111111111111111111';
|
return '11111111111111111111111111111111';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ export class SystemContract {
|
|||||||
newAccount: PublicKey,
|
newAccount: PublicKey,
|
||||||
tokens: number,
|
tokens: number,
|
||||||
space: number,
|
space: number,
|
||||||
contractId: ?PublicKey
|
programId: ?PublicKey
|
||||||
): Transaction {
|
): Transaction {
|
||||||
const userdata = Buffer.alloc(4 + 8 + 8 + 1 + 32);
|
const userdata = Buffer.alloc(4 + 8 + 8 + 1 + 32);
|
||||||
let pos = 0;
|
let pos = 0;
|
||||||
@ -38,12 +38,12 @@ export class SystemContract {
|
|||||||
userdata.writeUInt32LE(space, pos); // space as u64
|
userdata.writeUInt32LE(space, pos); // space as u64
|
||||||
pos += 8;
|
pos += 8;
|
||||||
|
|
||||||
if (contractId) {
|
if (programId) {
|
||||||
userdata.writeUInt8(1, pos); // 'Some'
|
userdata.writeUInt8(1, pos); // 'Some'
|
||||||
pos += 1;
|
pos += 1;
|
||||||
|
|
||||||
const contractIdBytes = Transaction.serializePublicKey(contractId);
|
const programIdBytes = Transaction.serializePublicKey(programId);
|
||||||
contractIdBytes.copy(userdata, pos);
|
programIdBytes.copy(userdata, pos);
|
||||||
pos += 32;
|
pos += 32;
|
||||||
} else {
|
} else {
|
||||||
userdata.writeUInt8(0, pos); // 'None'
|
userdata.writeUInt8(0, pos); // 'None'
|
||||||
@ -55,7 +55,7 @@ export class SystemContract {
|
|||||||
return new Transaction({
|
return new Transaction({
|
||||||
fee: 0,
|
fee: 0,
|
||||||
keys: [from, newAccount],
|
keys: [from, newAccount],
|
||||||
contractId: SystemContract.contractId,
|
programId: SystemProgram.programId,
|
||||||
userdata,
|
userdata,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -77,31 +77,31 @@ export class SystemContract {
|
|||||||
return new Transaction({
|
return new Transaction({
|
||||||
fee: 0,
|
fee: 0,
|
||||||
keys: [from, to],
|
keys: [from, to],
|
||||||
contractId: SystemContract.contractId,
|
programId: SystemProgram.programId,
|
||||||
userdata,
|
userdata,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a Transaction that assigns an account to a contract id
|
* Generate a Transaction that assigns an account to a program
|
||||||
*/
|
*/
|
||||||
static assign(from: PublicKey, contractId: PublicKey): Transaction {
|
static assign(from: PublicKey, programId: PublicKey): Transaction {
|
||||||
const userdata = Buffer.alloc(4 + 32);
|
const userdata = Buffer.alloc(4 + 32);
|
||||||
let pos = 0;
|
let pos = 0;
|
||||||
|
|
||||||
userdata.writeUInt32LE(1, pos); // Assign instruction
|
userdata.writeUInt32LE(1, pos); // Assign instruction
|
||||||
pos += 4;
|
pos += 4;
|
||||||
|
|
||||||
const contractIdBytes = Transaction.serializePublicKey(contractId);
|
const programIdBytes = Transaction.serializePublicKey(programId);
|
||||||
contractIdBytes.copy(userdata, pos);
|
programIdBytes.copy(userdata, pos);
|
||||||
pos += contractIdBytes.length;
|
pos += programIdBytes.length;
|
||||||
|
|
||||||
assert(pos === userdata.length);
|
assert(pos === userdata.length);
|
||||||
|
|
||||||
return new Transaction({
|
return new Transaction({
|
||||||
fee: 0,
|
fee: 0,
|
||||||
keys: [from],
|
keys: [from],
|
||||||
contractId: SystemContract.contractId,
|
programId: SystemProgram.programId,
|
||||||
userdata,
|
userdata,
|
||||||
});
|
});
|
||||||
}
|
}
|
@ -22,7 +22,7 @@ export type TransactionId = string;
|
|||||||
type TransactionCtorFields = {|
|
type TransactionCtorFields = {|
|
||||||
signature?: Buffer;
|
signature?: Buffer;
|
||||||
keys?: Array<PublicKey>;
|
keys?: Array<PublicKey>;
|
||||||
contractId?: PublicKey;
|
programId?: PublicKey;
|
||||||
fee?: number;
|
fee?: number;
|
||||||
userdata?: Buffer;
|
userdata?: Buffer;
|
||||||
|};
|
|};
|
||||||
@ -44,9 +44,9 @@ export class Transaction {
|
|||||||
keys: Array<PublicKey> = [];
|
keys: Array<PublicKey> = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contract Id to execute
|
* Program Id to execute
|
||||||
*/
|
*/
|
||||||
contractId: ?PublicKey;
|
programId: ?PublicKey;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A recent transaction id. Must be populated by the caller
|
* A recent transaction id. Must be populated by the caller
|
||||||
@ -59,7 +59,7 @@ export class Transaction {
|
|||||||
fee: number = 0;
|
fee: number = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contract input userdata to include
|
* Program input
|
||||||
*/
|
*/
|
||||||
userdata: Buffer = Buffer.alloc(0);
|
userdata: Buffer = Buffer.alloc(0);
|
||||||
|
|
||||||
@ -90,9 +90,9 @@ export class Transaction {
|
|||||||
pos += 32;
|
pos += 32;
|
||||||
}
|
}
|
||||||
|
|
||||||
// serialize `this.contractId`
|
// serialize `this.programId`
|
||||||
if (this.contractId) {
|
if (this.programId) {
|
||||||
const keyBytes = Transaction.serializePublicKey(this.contractId);
|
const keyBytes = Transaction.serializePublicKey(this.programId);
|
||||||
keyBytes.copy(transactionData, pos);
|
keyBytes.copy(transactionData, pos);
|
||||||
}
|
}
|
||||||
pos += 32;
|
pos += 32;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
// @flow
|
// @flow
|
||||||
|
|
||||||
import {Account} from '../src/account';
|
import {Account} from '../src/account';
|
||||||
import {BudgetContract} from '../src/budget-contract';
|
import {BudgetProgram} from '../src/budget-program';
|
||||||
|
|
||||||
test('pay', () => {
|
test('pay', () => {
|
||||||
const from = new Account();
|
const from = new Account();
|
||||||
@ -9,7 +9,7 @@ test('pay', () => {
|
|||||||
const to = new Account();
|
const to = new Account();
|
||||||
let transaction;
|
let transaction;
|
||||||
|
|
||||||
transaction = BudgetContract.pay(
|
transaction = BudgetProgram.pay(
|
||||||
from.publicKey,
|
from.publicKey,
|
||||||
contract.publicKey,
|
contract.publicKey,
|
||||||
to.publicKey,
|
to.publicKey,
|
||||||
@ -17,22 +17,22 @@ test('pay', () => {
|
|||||||
);
|
);
|
||||||
console.log('Pay:', transaction);
|
console.log('Pay:', transaction);
|
||||||
|
|
||||||
transaction = BudgetContract.pay(
|
transaction = BudgetProgram.pay(
|
||||||
from.publicKey,
|
from.publicKey,
|
||||||
contract.publicKey,
|
contract.publicKey,
|
||||||
to.publicKey,
|
to.publicKey,
|
||||||
123,
|
123,
|
||||||
BudgetContract.signatureCondition(from.publicKey),
|
BudgetProgram.signatureCondition(from.publicKey),
|
||||||
);
|
);
|
||||||
console.log('After:', transaction);
|
console.log('After:', transaction);
|
||||||
|
|
||||||
transaction = BudgetContract.pay(
|
transaction = BudgetProgram.pay(
|
||||||
from.publicKey,
|
from.publicKey,
|
||||||
contract.publicKey,
|
contract.publicKey,
|
||||||
to.publicKey,
|
to.publicKey,
|
||||||
123,
|
123,
|
||||||
BudgetContract.signatureCondition(from.publicKey),
|
BudgetProgram.signatureCondition(from.publicKey),
|
||||||
BudgetContract.timestampCondition(from.publicKey, new Date()),
|
BudgetProgram.timestampCondition(from.publicKey, new Date()),
|
||||||
);
|
);
|
||||||
console.log('Or:', transaction);
|
console.log('Or:', transaction);
|
||||||
});
|
});
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import {Account} from '../src/account';
|
import {Account} from '../src/account';
|
||||||
import {Connection} from '../src/connection';
|
import {Connection} from '../src/connection';
|
||||||
import {SystemContract} from '../src/system-contract';
|
import {SystemProgram} from '../src/system-program';
|
||||||
import {mockRpc} from './__mocks__/node-fetch';
|
import {mockRpc} from './__mocks__/node-fetch';
|
||||||
|
|
||||||
const url = 'http://testnet.solana.com:8899';
|
const url = 'http://testnet.solana.com:8899';
|
||||||
@ -274,7 +274,7 @@ test('transaction', async () => {
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
const transaction = SystemContract.move(
|
const transaction = SystemProgram.move(
|
||||||
accountFrom.publicKey,
|
accountFrom.publicKey,
|
||||||
accountTo.publicKey,
|
accountTo.publicKey,
|
||||||
10
|
10
|
||||||
|
Reference in New Issue
Block a user