fix: rename userdata to data

This commit is contained in:
Michael Vines
2019-03-14 13:27:47 -07:00
parent 5d40f359c2
commit 6fe0e08a80
9 changed files with 144 additions and 144 deletions

View File

@ -40,7 +40,7 @@ declare module '@solana/web3.js' {
executable: boolean, executable: boolean,
owner: PublicKey, owner: PublicKey,
lamports: number, lamports: number,
userdata: Buffer, data: Buffer,
}; };
declare type AccountChangeCallback = (accountInfo: AccountInfo) => void; declare type AccountChangeCallback = (accountInfo: AccountInfo) => void;
@ -104,13 +104,13 @@ declare module '@solana/web3.js' {
declare type TransactionInstructionCtorFields = {| declare type TransactionInstructionCtorFields = {|
keys: ?Array<PublicKey>, keys: ?Array<PublicKey>,
programId?: PublicKey, programId?: PublicKey,
userdata?: Buffer, data?: Buffer,
|}; |};
declare export class TransactionInstruction { declare export class TransactionInstruction {
keys: Array<PublicKey>; keys: Array<PublicKey>;
programId: PublicKey; programId: PublicKey;
userdata: Buffer; data: Buffer;
} }
declare type SignaturePubkeyPair = {| declare type SignaturePubkeyPair = {|

View File

@ -58,18 +58,18 @@ export type BudgetCondition = SignatureCondition | TimestampCondition;
*/ */
function serializePayment(payment: Payment): Buffer { function serializePayment(payment: Payment): Buffer {
const toData = payment.to.toBuffer(); const toData = payment.to.toBuffer();
const userdata = Buffer.alloc(8 + toData.length); const data = Buffer.alloc(8 + toData.length);
userdata.writeUInt32LE(payment.amount, 0); data.writeUInt32LE(payment.amount, 0);
toData.copy(userdata, 8); toData.copy(data, 8);
return userdata; return data;
} }
/** /**
* @private * @private
*/ */
function serializeDate(when: Date): Buffer { function serializeDate(when: Date): Buffer {
const userdata = Buffer.alloc(8 + 20); const data = Buffer.alloc(8 + 20);
userdata.writeUInt32LE(20, 0); // size of timestamp as u64 data.writeUInt32LE(20, 0); // size of timestamp as u64
function iso(date) { function iso(date) {
function pad(number) { function pad(number) {
@ -94,8 +94,8 @@ function serializeDate(when: Date): Buffer {
'Z' 'Z'
); );
} }
userdata.write(iso(when), 8); data.write(iso(when), 8);
return userdata; return data;
} }
/** /**
@ -107,28 +107,28 @@ function serializeCondition(condition: BudgetCondition) {
const date = serializeDate(condition.when); const date = serializeDate(condition.when);
const from = condition.from.toBuffer(); const from = condition.from.toBuffer();
const userdata = Buffer.alloc(4 + date.length + from.length); const data = Buffer.alloc(4 + date.length + from.length);
userdata.writeUInt32LE(0, 0); // Condition enum = Timestamp data.writeUInt32LE(0, 0); // Condition enum = Timestamp
date.copy(userdata, 4); date.copy(data, 4);
from.copy(userdata, 4 + date.length); from.copy(data, 4 + date.length);
return userdata; return data;
} }
case 'signature': { case 'signature': {
const userdataLayout = BufferLayout.struct([ const dataLayout = BufferLayout.struct([
BufferLayout.u32('condition'), BufferLayout.u32('condition'),
Layout.publicKey('from'), Layout.publicKey('from'),
]); ]);
const from = condition.from.toBuffer(); const from = condition.from.toBuffer();
const userdata = Buffer.alloc(4 + from.length); const data = Buffer.alloc(4 + from.length);
userdataLayout.encode( dataLayout.encode(
{ {
instruction: 1, // Signature instruction: 1, // Signature
from, from,
}, },
userdata, data,
); );
return userdata; return data;
} }
default: default:
throw new Error(`Unknown condition type: ${condition.type}`); throw new Error(`Unknown condition type: ${condition.type}`);
@ -186,66 +186,66 @@ export class BudgetProgram {
amount: number, amount: number,
...conditions: Array<BudgetCondition> ...conditions: Array<BudgetCondition>
): Transaction { ): Transaction {
const userdata = Buffer.alloc(1024); const data = Buffer.alloc(1024);
let pos = 0; let pos = 0;
userdata.writeUInt32LE(0, pos); // NewBudget instruction data.writeUInt32LE(0, pos); // NewBudget instruction
pos += 4; pos += 4;
switch (conditions.length) { switch (conditions.length) {
case 0: case 0:
userdata.writeUInt32LE(0, pos); // Budget enum = Pay data.writeUInt32LE(0, pos); // Budget enum = Pay
pos += 4; pos += 4;
{ {
const payment = serializePayment({amount, to}); const payment = serializePayment({amount, to});
payment.copy(userdata, pos); payment.copy(data, pos);
pos += payment.length; pos += payment.length;
} }
return new Transaction().add({ return new Transaction().add({
keys: [from, to], keys: [from, to],
programId: this.programId, programId: this.programId,
userdata: userdata.slice(0, pos), data: data.slice(0, pos),
}); });
case 1: case 1:
userdata.writeUInt32LE(1, pos); // Budget enum = After data.writeUInt32LE(1, pos); // Budget enum = After
pos += 4; pos += 4;
{ {
const condition = conditions[0]; const condition = conditions[0];
const conditionData = serializeCondition(condition); const conditionData = serializeCondition(condition);
conditionData.copy(userdata, pos); conditionData.copy(data, pos);
pos += conditionData.length; pos += conditionData.length;
const paymentData = serializePayment({amount, to}); const paymentData = serializePayment({amount, to});
paymentData.copy(userdata, pos); paymentData.copy(data, pos);
pos += paymentData.length; pos += paymentData.length;
} }
return new Transaction().add({ return new Transaction().add({
keys: [from, program, to], keys: [from, program, to],
programId: this.programId, programId: this.programId,
userdata: userdata.slice(0, pos), data: data.slice(0, pos),
}); });
case 2: case 2:
userdata.writeUInt32LE(2, pos); // Budget enum = Or data.writeUInt32LE(2, pos); // Budget enum = Or
pos += 4; pos += 4;
for (let condition of conditions) { for (let condition of conditions) {
const conditionData = serializeCondition(condition); const conditionData = serializeCondition(condition);
conditionData.copy(userdata, pos); conditionData.copy(data, pos);
pos += conditionData.length; pos += conditionData.length;
const paymentData = serializePayment({amount, to}); const paymentData = serializePayment({amount, to});
paymentData.copy(userdata, pos); paymentData.copy(data, pos);
pos += paymentData.length; pos += paymentData.length;
} }
return new Transaction().add({ return new Transaction().add({
keys: [from, program, to], keys: [from, program, to],
programId: this.programId, programId: this.programId,
userdata: userdata.slice(0, pos), data: data.slice(0, pos),
}); });
default: default:
@ -268,28 +268,28 @@ export class BudgetProgram {
condition1: BudgetCondition, condition1: BudgetCondition,
condition2: BudgetCondition, condition2: BudgetCondition,
): Transaction { ): Transaction {
const userdata = Buffer.alloc(1024); const data = Buffer.alloc(1024);
let pos = 0; let pos = 0;
userdata.writeUInt32LE(0, pos); // NewBudget instruction data.writeUInt32LE(0, pos); // NewBudget instruction
pos += 4; pos += 4;
userdata.writeUInt32LE(3, pos); // Budget enum = And data.writeUInt32LE(3, pos); // Budget enum = And
pos += 4; pos += 4;
for (let condition of [condition1, condition2]) { for (let condition of [condition1, condition2]) {
const conditionData = serializeCondition(condition); const conditionData = serializeCondition(condition);
conditionData.copy(userdata, pos); conditionData.copy(data, pos);
pos += conditionData.length; pos += conditionData.length;
} }
const paymentData = serializePayment({amount, to}); const paymentData = serializePayment({amount, to});
paymentData.copy(userdata, pos); paymentData.copy(data, pos);
pos += paymentData.length; pos += paymentData.length;
return new Transaction().add({ return new Transaction().add({
keys: [from, program, to], keys: [from, program, to],
programId: this.programId, programId: this.programId,
userdata: userdata.slice(0, pos), data: data.slice(0, pos),
}); });
} }
@ -304,15 +304,15 @@ export class BudgetProgram {
when: Date, when: Date,
): Transaction { ): Transaction {
const whenData = serializeDate(when); const whenData = serializeDate(when);
const userdata = Buffer.alloc(4 + whenData.length); const data = Buffer.alloc(4 + whenData.length);
userdata.writeUInt32LE(1, 0); // ApplyTimestamp instruction data.writeUInt32LE(1, 0); // ApplyTimestamp instruction
whenData.copy(userdata, 4); whenData.copy(data, 4);
return new Transaction().add({ return new Transaction().add({
keys: [from, program, to], keys: [from, program, to],
programId: this.programId, programId: this.programId,
userdata, data,
}); });
} }
@ -325,22 +325,22 @@ export class BudgetProgram {
program: PublicKey, program: PublicKey,
to: PublicKey, to: PublicKey,
): Transaction { ): Transaction {
const userdataLayout = BufferLayout.struct([ const dataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
]); ]);
const userdata = Buffer.alloc(userdataLayout.span); const data = Buffer.alloc(dataLayout.span);
userdataLayout.encode( dataLayout.encode(
{ {
instruction: 2, // ApplySignature instruction instruction: 2, // ApplySignature instruction
}, },
userdata, data,
); );
return new Transaction().add({ return new Transaction().add({
keys: [from, program, to], keys: [from, program, to],
programId: this.programId, programId: this.programId,
userdata, data,
}); });
} }
} }

View File

@ -86,7 +86,7 @@ const AccountInfoResult = struct({
executable: 'boolean', executable: 'boolean',
owner: 'array', owner: 'array',
lamports: 'number', lamports: 'number',
userdata: 'array', data: 'array',
}); });
/** /**
@ -159,14 +159,14 @@ const SendTransactionRpcResult = jsonRpcResult('string');
* @typedef {Object} AccountInfo * @typedef {Object} AccountInfo
* @property {number} lamports Number of lamports assigned to the account * @property {number} lamports Number of lamports assigned to the account
* @property {PublicKey} owner Identifier of the program that owns the account * @property {PublicKey} owner Identifier of the program that owns the account
* @property {?Buffer} userdata Optional userdata assigned to the account * @property {?Buffer} data Optional data assigned to the account
* @property {boolean} executable `true` if this account's userdata contains a loaded program * @property {boolean} executable `true` if this account's data contains a loaded program
*/ */
type AccountInfo = { type AccountInfo = {
executable: boolean, executable: boolean,
owner: PublicKey, owner: PublicKey,
lamports: number, lamports: number,
userdata: Buffer, data: Buffer,
}; };
/** /**
@ -316,7 +316,7 @@ export class Connection {
executable: result.executable, executable: result.executable,
owner: new PublicKey(result.owner), owner: new PublicKey(result.owner),
lamports: result.lamports, lamports: result.lamports,
userdata: Buffer.from(result.userdata), data: Buffer.from(result.data),
}; };
} }
@ -593,7 +593,7 @@ export class Connection {
executable: result.executable, executable: result.executable,
owner: new PublicKey(result.owner), owner: new PublicKey(result.owner),
lamports: result.lamports, lamports: result.lamports,
userdata: Buffer.from(result.userdata), data: Buffer.from(result.data),
}); });
return true; return true;
} }
@ -667,7 +667,7 @@ export class Connection {
executable: result[1].executable, executable: result[1].executable,
owner: new PublicKey(result[1].owner), owner: new PublicKey(result[1].owner),
lamports: result[1].lamports, lamports: result[1].lamports,
userdata: Buffer.from(result[1].userdata), data: Buffer.from(result[1].data),
}, },
}); });
return true; return true;

View File

@ -46,7 +46,7 @@ export class Loader {
* @param data Program data * @param data Program data
*/ */
async load(program: Account, data: Array<number>) { async load(program: Account, data: Array<number>) {
const userdataLayout = BufferLayout.struct([ const dataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
BufferLayout.u32('offset'), BufferLayout.u32('offset'),
BufferLayout.u32('bytesLength'), BufferLayout.u32('bytesLength'),
@ -64,20 +64,20 @@ export class Loader {
let transactions = []; let transactions = [];
while (array.length > 0) { while (array.length > 0) {
const bytes = array.slice(0, chunkSize); const bytes = array.slice(0, chunkSize);
const userdata = Buffer.alloc(chunkSize + 16); const data = Buffer.alloc(chunkSize + 16);
userdataLayout.encode( dataLayout.encode(
{ {
instruction: 0, // Load instruction instruction: 0, // Load instruction
offset, offset,
bytes, bytes,
}, },
userdata, data,
); );
const transaction = new Transaction().add({ const transaction = new Transaction().add({
keys: [program.publicKey], keys: [program.publicKey],
programId: this.programId, programId: this.programId,
userdata, data,
}); });
transactions.push( transactions.push(
sendAndConfirmTransaction(this.connection, transaction, program), sendAndConfirmTransaction(this.connection, transaction, program),
@ -108,22 +108,22 @@ export class Loader {
* @param program `load()`ed Account * @param program `load()`ed Account
*/ */
async finalize(program: Account) { async finalize(program: Account) {
const userdataLayout = BufferLayout.struct([ const dataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
]); ]);
const userdata = Buffer.alloc(userdataLayout.span); const data = Buffer.alloc(dataLayout.span);
userdataLayout.encode( dataLayout.encode(
{ {
instruction: 1, // Finalize instruction instruction: 1, // Finalize instruction
}, },
userdata, data,
); );
const transaction = new Transaction().add({ const transaction = new Transaction().add({
keys: [program.publicKey], keys: [program.publicKey],
programId: this.programId, programId: this.programId,
userdata, data,
}); });
await sendAndConfirmTransaction(this.connection, transaction, program); await sendAndConfirmTransaction(this.connection, transaction, program);
} }

View File

@ -29,28 +29,28 @@ export class SystemProgram {
space: number, space: number,
programId: PublicKey, programId: PublicKey,
): Transaction { ): Transaction {
const userdataLayout = BufferLayout.struct([ const dataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
BufferLayout.ns64('lamports'), BufferLayout.ns64('lamports'),
BufferLayout.ns64('space'), BufferLayout.ns64('space'),
Layout.publicKey('programId'), Layout.publicKey('programId'),
]); ]);
const userdata = Buffer.alloc(userdataLayout.span); const data = Buffer.alloc(dataLayout.span);
userdataLayout.encode( dataLayout.encode(
{ {
instruction: 0, // Create Account instruction instruction: 0, // Create Account instruction
lamports, lamports,
space, space,
programId: programId.toBuffer(), programId: programId.toBuffer(),
}, },
userdata, data,
); );
return new Transaction().add({ return new Transaction().add({
keys: [from, newAccount], keys: [from, newAccount],
programId: SystemProgram.programId, programId: SystemProgram.programId,
userdata, data,
}); });
} }
@ -58,24 +58,24 @@ export class SystemProgram {
* Generate a Transaction that moves lamports from one account to another * Generate a Transaction that moves lamports from one account to another
*/ */
static move(from: PublicKey, to: PublicKey, amount: number): Transaction { static move(from: PublicKey, to: PublicKey, amount: number): Transaction {
const userdataLayout = BufferLayout.struct([ const dataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
BufferLayout.ns64('amount'), BufferLayout.ns64('amount'),
]); ]);
const userdata = Buffer.alloc(userdataLayout.span); const data = Buffer.alloc(dataLayout.span);
userdataLayout.encode( dataLayout.encode(
{ {
instruction: 2, // Move instruction instruction: 2, // Move instruction
amount, amount,
}, },
userdata, data,
); );
return new Transaction().add({ return new Transaction().add({
keys: [from, to], keys: [from, to],
programId: SystemProgram.programId, programId: SystemProgram.programId,
userdata, data,
}); });
} }
@ -83,24 +83,24 @@ export class SystemProgram {
* Generate a Transaction that assigns an account to a program * Generate a Transaction that assigns an account to a program
*/ */
static assign(from: PublicKey, programId: PublicKey): Transaction { static assign(from: PublicKey, programId: PublicKey): Transaction {
const userdataLayout = BufferLayout.struct([ const dataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
Layout.publicKey('programId'), Layout.publicKey('programId'),
]); ]);
const userdata = Buffer.alloc(userdataLayout.span); const data = Buffer.alloc(dataLayout.span);
userdataLayout.encode( dataLayout.encode(
{ {
instruction: 1, // Assign instruction instruction: 1, // Assign instruction
programId: programId.toBuffer(), programId: programId.toBuffer(),
}, },
userdata, data,
); );
return new Transaction().add({ return new Transaction().add({
keys: [from], keys: [from],
programId: SystemProgram.programId, programId: SystemProgram.programId,
userdata, data,
}); });
} }
} }

View File

@ -202,7 +202,7 @@ export class Token {
let transaction; let transaction;
const userdataLayout = BufferLayout.struct([ const dataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
Layout.uint64('supply'), Layout.uint64('supply'),
BufferLayout.u8('decimals'), BufferLayout.u8('decimals'),
@ -210,9 +210,9 @@ export class Token {
Layout.rustString('symbol'), Layout.rustString('symbol'),
]); ]);
let userdata = Buffer.alloc(1024); let data = Buffer.alloc(1024);
{ {
const encodeLength = userdataLayout.encode( const encodeLength = dataLayout.encode(
{ {
instruction: 0, // NewToken instruction instruction: 0, // NewToken instruction
supply: supply.toBuffer(), supply: supply.toBuffer(),
@ -220,9 +220,9 @@ export class Token {
name, name,
symbol, symbol,
}, },
userdata, data,
); );
userdata = userdata.slice(0, encodeLength); data = data.slice(0, encodeLength);
} }
// Allocate memory for the tokenAccount account // Allocate memory for the tokenAccount account
@ -230,7 +230,7 @@ export class Token {
owner.publicKey, owner.publicKey,
tokenAccount.publicKey, tokenAccount.publicKey,
1, 1,
1 + userdata.length, 1 + data.length,
programId, programId,
); );
await sendAndConfirmTransaction(connection, transaction, owner); await sendAndConfirmTransaction(connection, transaction, owner);
@ -238,7 +238,7 @@ export class Token {
transaction = new Transaction().add({ transaction = new Transaction().add({
keys: [tokenAccount.publicKey, initialAccountPublicKey], keys: [tokenAccount.publicKey, initialAccountPublicKey],
programId, programId,
userdata, data,
}); });
transaction.fee = 0; // TODO: Batch with the `SystemProgram.createAccount` and remove this line transaction.fee = 0; // TODO: Batch with the `SystemProgram.createAccount` and remove this line
await sendAndConfirmTransaction(connection, transaction, tokenAccount); await sendAndConfirmTransaction(connection, transaction, tokenAccount);
@ -263,16 +263,16 @@ export class Token {
const tokenAccount = new Account(); const tokenAccount = new Account();
let transaction; let transaction;
const userdataLayout = BufferLayout.struct([ const dataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
]); ]);
const userdata = Buffer.alloc(userdataLayout.span); const data = Buffer.alloc(dataLayout.span);
userdataLayout.encode( dataLayout.encode(
{ {
instruction: 1, // NewTokenAccount instruction instruction: 1, // NewTokenAccount instruction
}, },
userdata, data,
); );
// Allocate memory for the token // Allocate memory for the token
@ -293,7 +293,7 @@ export class Token {
transaction = new Transaction().add({ transaction = new Transaction().add({
keys, keys,
programId: this.programId, programId: this.programId,
userdata, data,
}); });
transaction.fee = 0; // TODO: Batch with the `SystemProgram.createAccount` and remove this line transaction.fee = 0; // TODO: Batch with the `SystemProgram.createAccount` and remove this line
await sendAndConfirmTransaction(this.connection, transaction, tokenAccount); await sendAndConfirmTransaction(this.connection, transaction, tokenAccount);
@ -312,12 +312,12 @@ export class Token {
); );
} }
const userdata = Buffer.from(accountInfo.userdata); const data = Buffer.from(accountInfo.data);
if (userdata.readUInt8(0) !== 1) { if (data.readUInt8(0) !== 1) {
throw new Error(`Invalid token userdata`); throw new Error(`Invalid token data`);
} }
const tokenInfo = TokenInfoLayout.decode(userdata, 1); const tokenInfo = TokenInfoLayout.decode(data, 1);
tokenInfo.supply = TokenAmount.fromBuffer(tokenInfo.supply); tokenInfo.supply = TokenAmount.fromBuffer(tokenInfo.supply);
return tokenInfo; return tokenInfo;
} }
@ -333,11 +333,11 @@ export class Token {
throw new Error(`Invalid token account owner`); throw new Error(`Invalid token account owner`);
} }
const userdata = Buffer.from(accountInfo.userdata); const data = Buffer.from(accountInfo.data);
if (userdata.readUInt8(0) !== 2) { if (data.readUInt8(0) !== 2) {
throw new Error(`Invalid token account userdata`); throw new Error(`Invalid token account data`);
} }
const tokenAccountInfo = TokenAccountInfoLayout.decode(userdata, 1); const tokenAccountInfo = TokenAccountInfoLayout.decode(data, 1);
tokenAccountInfo.token = new PublicKey(tokenAccountInfo.token); tokenAccountInfo.token = new PublicKey(tokenAccountInfo.token);
tokenAccountInfo.owner = new PublicKey(tokenAccountInfo.owner); tokenAccountInfo.owner = new PublicKey(tokenAccountInfo.owner);
@ -468,18 +468,18 @@ export class Token {
throw new Error('Account owner mismatch'); throw new Error('Account owner mismatch');
} }
const userdataLayout = BufferLayout.struct([ const dataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
Layout.uint64('amount'), Layout.uint64('amount'),
]); ]);
const userdata = Buffer.alloc(userdataLayout.span); const data = Buffer.alloc(dataLayout.span);
userdataLayout.encode( dataLayout.encode(
{ {
instruction: 2, // Transfer instruction instruction: 2, // Transfer instruction
amount: new TokenAmount(amount).toBuffer(), amount: new TokenAmount(amount).toBuffer(),
}, },
userdata, data,
); );
const keys = [owner, source, destination]; const keys = [owner, source, destination];
@ -489,7 +489,7 @@ export class Token {
return new TransactionInstruction({ return new TransactionInstruction({
keys, keys,
programId: this.programId, programId: this.programId,
userdata, data,
}); });
} }
@ -507,24 +507,24 @@ export class Token {
delegate: PublicKey, delegate: PublicKey,
amount: number | TokenAmount, amount: number | TokenAmount,
): TransactionInstruction { ): TransactionInstruction {
const userdataLayout = BufferLayout.struct([ const dataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
Layout.uint64('amount'), Layout.uint64('amount'),
]); ]);
const userdata = Buffer.alloc(userdataLayout.span); const data = Buffer.alloc(dataLayout.span);
userdataLayout.encode( dataLayout.encode(
{ {
instruction: 3, // Approve instruction instruction: 3, // Approve instruction
amount: new TokenAmount(amount).toBuffer(), amount: new TokenAmount(amount).toBuffer(),
}, },
userdata, data,
); );
return new TransactionInstruction({ return new TransactionInstruction({
keys: [owner, account, delegate], keys: [owner, account, delegate],
programId: this.programId, programId: this.programId,
userdata, data,
}); });
} }
@ -555,22 +555,22 @@ export class Token {
account: PublicKey, account: PublicKey,
newOwner: PublicKey, newOwner: PublicKey,
): TransactionInstruction { ): TransactionInstruction {
const userdataLayout = BufferLayout.struct([ const dataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'), BufferLayout.u32('instruction'),
]); ]);
const userdata = Buffer.alloc(userdataLayout.span); const data = Buffer.alloc(dataLayout.span);
userdataLayout.encode( dataLayout.encode(
{ {
instruction: 4, // SetOwner instruction instruction: 4, // SetOwner instruction
}, },
userdata, data,
); );
return new TransactionInstruction({ return new TransactionInstruction({
keys: [owner, account, newOwner], keys: [owner, account, newOwner],
programId: this.programId, programId: this.programId,
userdata, data,
}); });
} }
} }

View File

@ -27,12 +27,12 @@ export const PACKET_DATA_SIZE = 512;
* @typedef {Object} TransactionInstructionCtorFields * @typedef {Object} TransactionInstructionCtorFields
* @property {?Array<PublicKey>} keys * @property {?Array<PublicKey>} keys
* @property {?PublicKey} programId * @property {?PublicKey} programId
* @property {?Buffer} userdata * @property {?Buffer} data
*/ */
type TransactionInstructionCtorFields = {| type TransactionInstructionCtorFields = {|
keys?: Array<PublicKey>, keys?: Array<PublicKey>,
programId?: PublicKey, programId?: PublicKey,
userdata?: Buffer, data?: Buffer,
|}; |};
/** /**
@ -52,7 +52,7 @@ export class TransactionInstruction {
/** /**
* Program input * Program input
*/ */
userdata: Buffer = Buffer.alloc(0); data: Buffer = Buffer.alloc(0);
constructor(opts?: TransactionInstructionCtorFields) { constructor(opts?: TransactionInstructionCtorFields) {
opts && Object.assign(this, opts); opts && Object.assign(this, opts);
@ -182,19 +182,19 @@ export class Transaction {
shortvec.encodeLength(programIdCount, programIds.length); shortvec.encodeLength(programIdCount, programIds.length);
const instructions = this.instructions.map(instruction => { const instructions = this.instructions.map(instruction => {
const {userdata, programId} = instruction; const {data, programId} = instruction;
let keyIndicesCount = []; let keyIndicesCount = [];
shortvec.encodeLength(keyIndicesCount, instruction.keys.length); shortvec.encodeLength(keyIndicesCount, instruction.keys.length);
let userdataCount = []; let dataCount = [];
shortvec.encodeLength(userdataCount, instruction.userdata.length); shortvec.encodeLength(dataCount, instruction.data.length);
return { return {
programIdIndex: programIds.indexOf(programId.toString()), programIdIndex: programIds.indexOf(programId.toString()),
keyIndicesCount: Buffer.from(keyIndicesCount), keyIndicesCount: Buffer.from(keyIndicesCount),
keyIndices: Buffer.from( keyIndices: Buffer.from(
instruction.keys.map(key => keys.indexOf(key.toString())), instruction.keys.map(key => keys.indexOf(key.toString())),
), ),
userdataLength: Buffer.from(userdataCount), dataLength: Buffer.from(dataCount),
userdata, data,
}; };
}); });
@ -222,11 +222,11 @@ export class Transaction {
instruction.keyIndices.length, instruction.keyIndices.length,
'keyIndices', 'keyIndices',
), ),
BufferLayout.blob(instruction.userdataLength.length, 'userdataLength'), BufferLayout.blob(instruction.dataLength.length, 'dataLength'),
BufferLayout.seq( BufferLayout.seq(
BufferLayout.u8('userdatum'), BufferLayout.u8('userdatum'),
instruction.userdata.length, instruction.data.length,
'userdata', 'data',
), ),
]); ]);
const length = instructionLayout.encode( const length = instructionLayout.encode(
@ -405,9 +405,9 @@ export class Transaction {
* Deprecated method * Deprecated method
* @private * @private
*/ */
get userdata(): Buffer { get data(): Buffer {
invariant(this.instructions.length === 1); invariant(this.instructions.length === 1);
return this.instructions[0].userdata; return this.instructions[0].data;
} }
/** /**
@ -462,9 +462,9 @@ export class Transaction {
const accountIndexCount = shortvec.decodeLength(byteArray); const accountIndexCount = shortvec.decodeLength(byteArray);
instruction.accountIndex = byteArray.slice(0, accountIndexCount); instruction.accountIndex = byteArray.slice(0, accountIndexCount);
byteArray = byteArray.slice(accountIndexCount); byteArray = byteArray.slice(accountIndexCount);
const userdataLength = shortvec.decodeLength(byteArray); const dataLength = shortvec.decodeLength(byteArray);
instruction.userdata = byteArray.slice(0, userdataLength); instruction.data = byteArray.slice(0, dataLength);
byteArray = byteArray.slice(userdataLength); byteArray = byteArray.slice(dataLength);
instructions.push(instruction); instructions.push(instruction);
} }
@ -482,7 +482,7 @@ export class Transaction {
let instructionData = { let instructionData = {
keys: [], keys: [],
programId: new PublicKey(programs[instructions[i].programIndex]), programId: new PublicKey(programs[instructions[i].programIndex]),
userdata: Buffer.from(instructions[i].userdata), data: Buffer.from(instructions[i].data),
}; };
for (let j = 0; j < instructions[i].accountIndex.length; j++) { for (let j = 0; j < instructions[i].accountIndex.length; j++) {
instructionData.keys.push( instructionData.keys.push(

View File

@ -214,7 +214,7 @@ test('request airdrop', async () => {
0, 0,
], ],
lamports: 42, lamports: 42,
userdata: [], data: [],
executable: false, executable: false,
}, },
}, },
@ -222,7 +222,7 @@ test('request airdrop', async () => {
const accountInfo = await connection.getAccountInfo(account.publicKey); const accountInfo = await connection.getAccountInfo(account.publicKey);
expect(accountInfo.lamports).toBe(42); expect(accountInfo.lamports).toBe(42);
expect(accountInfo.userdata).toHaveLength(0); expect(accountInfo.data).toHaveLength(0);
expect(accountInfo.owner).toEqual(SystemProgram.programId); expect(accountInfo.owner).toEqual(SystemProgram.programId);
}); });
@ -472,7 +472,7 @@ test('account change notification', async () => {
expect(mockCallback.mock.calls[0][0].lamports).toBe(41); expect(mockCallback.mock.calls[0][0].lamports).toBe(41);
expect(mockCallback.mock.calls[0][0].owner).toEqual(BpfLoader.programId); expect(mockCallback.mock.calls[0][0].owner).toEqual(BpfLoader.programId);
expect(mockCallback.mock.calls[0][0].executable).toBe(false); expect(mockCallback.mock.calls[0][0].executable).toBe(false);
expect(mockCallback.mock.calls[0][0].userdata).toEqual( expect(mockCallback.mock.calls[0][0].data).toEqual(
Buffer.from([1, 2, 3]), Buffer.from([1, 2, 3]),
); );
}); });
@ -534,7 +534,7 @@ test('program account change notification', async () => {
BpfLoader.programId, BpfLoader.programId,
); );
expect(mockCallback.mock.calls[0][0].accountInfo.executable).toBe(false); expect(mockCallback.mock.calls[0][0].accountInfo.executable).toBe(false);
expect(mockCallback.mock.calls[0][0].accountInfo.userdata).toEqual( expect(mockCallback.mock.calls[0][0].accountInfo.data).toEqual(
Buffer.from([1, 2, 3]), Buffer.from([1, 2, 3]),
); );
}); });

View File

@ -95,7 +95,7 @@ test('create new token', async () => {
result: { result: {
owner: [...SYSTEM_TOKEN_PROGRAM_ID.toBuffer()], owner: [...SYSTEM_TOKEN_PROGRAM_ID.toBuffer()],
lamports: 1, lamports: 1,
userdata: [ data: [
1, 1,
16, 16,
39, 39,
@ -163,7 +163,7 @@ test('create new token', async () => {
result: { result: {
owner: [...SYSTEM_TOKEN_PROGRAM_ID.toBuffer()], owner: [...SYSTEM_TOKEN_PROGRAM_ID.toBuffer()],
lamports: 1, lamports: 1,
userdata: [ data: [
2, 2,
...testToken.token.toBuffer(), ...testToken.token.toBuffer(),
...initialOwner.publicKey.toBuffer(), ...initialOwner.publicKey.toBuffer(),
@ -252,7 +252,7 @@ test('create new token account', async () => {
result: { result: {
owner: [...SYSTEM_TOKEN_PROGRAM_ID.toBuffer()], owner: [...SYSTEM_TOKEN_PROGRAM_ID.toBuffer()],
lamports: 1, lamports: 1,
userdata: [ data: [
2, 2,
...testToken.token.toBuffer(), ...testToken.token.toBuffer(),
...destOwner.publicKey.toBuffer(), ...destOwner.publicKey.toBuffer(),
@ -310,7 +310,7 @@ test('transfer', async () => {
result: { result: {
owner: [...SYSTEM_TOKEN_PROGRAM_ID.toBuffer()], owner: [...SYSTEM_TOKEN_PROGRAM_ID.toBuffer()],
lamports: 1, lamports: 1,
userdata: [ data: [
2, 2,
...testToken.token.toBuffer(), ...testToken.token.toBuffer(),
...initialOwner.publicKey.toBuffer(), ...initialOwner.publicKey.toBuffer(),
@ -349,7 +349,7 @@ test('transfer', async () => {
result: { result: {
owner: [...SYSTEM_TOKEN_PROGRAM_ID.toBuffer()], owner: [...SYSTEM_TOKEN_PROGRAM_ID.toBuffer()],
lamports: 1, lamports: 1,
userdata: [ data: [
2, 2,
...testToken.token.toBuffer(), ...testToken.token.toBuffer(),
...dest.toBuffer(), ...dest.toBuffer(),
@ -420,7 +420,7 @@ test('approve/revoke', async () => {
result: { result: {
owner: [...SYSTEM_TOKEN_PROGRAM_ID.toBuffer()], owner: [...SYSTEM_TOKEN_PROGRAM_ID.toBuffer()],
lamports: 1, lamports: 1,
userdata: [ data: [
2, 2,
...testToken.token.toBuffer(), ...testToken.token.toBuffer(),
...delegate.toBuffer(), ...delegate.toBuffer(),
@ -482,7 +482,7 @@ test('approve/revoke', async () => {
result: { result: {
owner: [...SYSTEM_TOKEN_PROGRAM_ID.toBuffer()], owner: [...SYSTEM_TOKEN_PROGRAM_ID.toBuffer()],
lamports: 1, lamports: 1,
userdata: [ data: [
2, 2,
...testToken.token.toBuffer(), ...testToken.token.toBuffer(),
...delegate.toBuffer(), ...delegate.toBuffer(),