fix: update accounts to readonly/writable
This commit is contained in:
parent
58550733fb
commit
d0c89f7fa3
|
@ -214,13 +214,13 @@ declare module '@solana/web3.js' {
|
|||
declare export type TransactionSignature = string;
|
||||
|
||||
declare type TransactionInstructionCtorFields = {|
|
||||
keys: ?Array<{pubkey: PublicKey, isSigner: boolean, isDebitable: boolean}>,
|
||||
keys: ?Array<{pubkey: PublicKey, isSigner: boolean, isWritable: boolean}>,
|
||||
programId?: PublicKey,
|
||||
data?: Buffer,
|
||||
|};
|
||||
|
||||
declare export class TransactionInstruction {
|
||||
keys: Array<{pubkey: PublicKey, isSigner: boolean, isDebitable: boolean}>;
|
||||
keys: Array<{pubkey: PublicKey, isSigner: boolean, isWritable: boolean}>;
|
||||
programId: PublicKey;
|
||||
data: Buffer;
|
||||
|
||||
|
|
|
@ -201,8 +201,8 @@ export class BudgetProgram {
|
|||
|
||||
return transaction.add({
|
||||
keys: [
|
||||
{pubkey: to, isSigner: false, isDebitable: false},
|
||||
{pubkey: program, isSigner: false, isDebitable: true},
|
||||
{pubkey: to, isSigner: false, isWritable: true},
|
||||
{pubkey: program, isSigner: false, isWritable: true},
|
||||
],
|
||||
programId: this.programId,
|
||||
data: trimmedData,
|
||||
|
@ -236,7 +236,7 @@ export class BudgetProgram {
|
|||
);
|
||||
|
||||
return transaction.add({
|
||||
keys: [{pubkey: program, isSigner: false, isDebitable: true}],
|
||||
keys: [{pubkey: program, isSigner: false, isWritable: true}],
|
||||
programId: this.programId,
|
||||
data: trimmedData,
|
||||
});
|
||||
|
@ -269,7 +269,7 @@ export class BudgetProgram {
|
|||
);
|
||||
|
||||
return transaction.add({
|
||||
keys: [{pubkey: program, isSigner: false, isDebitable: true}],
|
||||
keys: [{pubkey: program, isSigner: false, isWritable: true}],
|
||||
programId: this.programId,
|
||||
data: trimmedData,
|
||||
});
|
||||
|
@ -325,7 +325,7 @@ export class BudgetProgram {
|
|||
);
|
||||
|
||||
return transaction.add({
|
||||
keys: [{pubkey: program, isSigner: false, isDebitable: true}],
|
||||
keys: [{pubkey: program, isSigner: false, isWritable: true}],
|
||||
programId: this.programId,
|
||||
data: trimmedData,
|
||||
});
|
||||
|
@ -349,9 +349,9 @@ export class BudgetProgram {
|
|||
|
||||
return new Transaction().add({
|
||||
keys: [
|
||||
{pubkey: from, isSigner: true, isDebitable: true},
|
||||
{pubkey: program, isSigner: false, isDebitable: true},
|
||||
{pubkey: to, isSigner: false, isDebitable: false},
|
||||
{pubkey: from, isSigner: true, isWritable: true},
|
||||
{pubkey: program, isSigner: false, isWritable: true},
|
||||
{pubkey: to, isSigner: false, isWritable: false},
|
||||
],
|
||||
programId: this.programId,
|
||||
data,
|
||||
|
@ -379,9 +379,9 @@ export class BudgetProgram {
|
|||
|
||||
return new Transaction().add({
|
||||
keys: [
|
||||
{pubkey: from, isSigner: true, isDebitable: true},
|
||||
{pubkey: program, isSigner: false, isDebitable: true},
|
||||
{pubkey: to, isSigner: false, isDebitable: false},
|
||||
{pubkey: from, isSigner: true, isWritable: true},
|
||||
{pubkey: program, isSigner: false, isWritable: true},
|
||||
{pubkey: to, isSigner: false, isWritable: true},
|
||||
],
|
||||
programId: this.programId,
|
||||
data,
|
||||
|
|
|
@ -97,7 +97,7 @@ export class Loader {
|
|||
);
|
||||
|
||||
const transaction = new Transaction().add({
|
||||
keys: [{pubkey: program.publicKey, isSigner: true, isDebitable: true}],
|
||||
keys: [{pubkey: program.publicKey, isSigner: true, isWritable: true}],
|
||||
programId,
|
||||
data,
|
||||
});
|
||||
|
@ -137,8 +137,8 @@ export class Loader {
|
|||
|
||||
const transaction = new Transaction().add({
|
||||
keys: [
|
||||
{pubkey: program.publicKey, isSigner: true, isDebitable: true},
|
||||
{pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isDebitable: false},
|
||||
{pubkey: program.publicKey, isSigner: true, isWritable: true},
|
||||
{pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false},
|
||||
],
|
||||
programId,
|
||||
data,
|
||||
|
|
|
@ -183,8 +183,8 @@ export class SystemProgram {
|
|||
|
||||
return new Transaction().add({
|
||||
keys: [
|
||||
{pubkey: from, isSigner: true, isDebitable: true},
|
||||
{pubkey: newAccount, isSigner: false, isDebitable: true},
|
||||
{pubkey: from, isSigner: true, isWritable: true},
|
||||
{pubkey: newAccount, isSigner: false, isWritable: true},
|
||||
],
|
||||
programId: SystemProgram.programId,
|
||||
data,
|
||||
|
@ -200,10 +200,8 @@ export class SystemProgram {
|
|||
|
||||
return new Transaction().add({
|
||||
keys: [
|
||||
{pubkey: from, isSigner: true, isDebitable: true},
|
||||
// TEMP FIX: a better proposed solution is here:
|
||||
// https://github.com/solana-labs/solana-web3.js/issues/542
|
||||
{pubkey: to, isSigner: false, isDebitable: true},
|
||||
{pubkey: from, isSigner: true, isWritable: true},
|
||||
{pubkey: to, isSigner: false, isWritable: true},
|
||||
],
|
||||
programId: SystemProgram.programId,
|
||||
data,
|
||||
|
@ -218,7 +216,7 @@ export class SystemProgram {
|
|||
const data = encodeData(type, {programId: programId.toBuffer()});
|
||||
|
||||
return new Transaction().add({
|
||||
keys: [{pubkey: from, isSigner: true, isDebitable: true}],
|
||||
keys: [{pubkey: from, isSigner: true, isWritable: true}],
|
||||
programId: SystemProgram.programId,
|
||||
data,
|
||||
});
|
||||
|
|
|
@ -41,7 +41,7 @@ export const PACKET_DATA_SIZE = 1280 - 40 - 8;
|
|||
* @property {?Buffer} data
|
||||
*/
|
||||
export type TransactionInstructionCtorFields = {|
|
||||
keys?: Array<{pubkey: PublicKey, isSigner: boolean, isDebitable: boolean}>,
|
||||
keys?: Array<{pubkey: PublicKey, isSigner: boolean, isWritable: boolean}>,
|
||||
programId?: PublicKey,
|
||||
data?: Buffer,
|
||||
|};
|
||||
|
@ -57,7 +57,7 @@ export class TransactionInstruction {
|
|||
keys: Array<{
|
||||
pubkey: PublicKey,
|
||||
isSigner: boolean,
|
||||
isDebitable: boolean,
|
||||
isWritable: boolean,
|
||||
}> = [];
|
||||
|
||||
/**
|
||||
|
@ -171,8 +171,8 @@ export class Transaction {
|
|||
}
|
||||
|
||||
const keys = this.signatures.map(({publicKey}) => publicKey.toString());
|
||||
let numCreditOnlySignedAccounts = 0;
|
||||
let numCreditOnlyUnsignedAccounts = 0;
|
||||
let numReadonlySignedAccounts = 0;
|
||||
let numReadonlyUnsignedAccounts = 0;
|
||||
|
||||
const programIds = [];
|
||||
|
||||
|
@ -185,12 +185,12 @@ export class Transaction {
|
|||
signature: null,
|
||||
publicKey: keySignerPair.pubkey,
|
||||
});
|
||||
if (!keySignerPair.isDebitable) {
|
||||
numCreditOnlySignedAccounts += 1;
|
||||
if (!keySignerPair.isWritable) {
|
||||
numReadonlySignedAccounts += 1;
|
||||
}
|
||||
} else {
|
||||
if (!keySignerPair.isDebitable) {
|
||||
numCreditOnlyUnsignedAccounts += 1;
|
||||
if (!keySignerPair.isWritable) {
|
||||
numReadonlyUnsignedAccounts += 1;
|
||||
}
|
||||
}
|
||||
keys.push(keyStr);
|
||||
|
@ -206,7 +206,7 @@ export class Transaction {
|
|||
programIds.forEach(programId => {
|
||||
if (!keys.includes(programId)) {
|
||||
keys.push(programId);
|
||||
numCreditOnlyUnsignedAccounts += 1;
|
||||
numReadonlyUnsignedAccounts += 1;
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -274,8 +274,8 @@ export class Transaction {
|
|||
|
||||
const signDataLayout = BufferLayout.struct([
|
||||
BufferLayout.blob(1, 'numRequiredSignatures'),
|
||||
BufferLayout.blob(1, 'numCreditOnlySignedAccounts'),
|
||||
BufferLayout.blob(1, 'numCreditOnlyUnsignedAccounts'),
|
||||
BufferLayout.blob(1, 'numReadonlySignedAccounts'),
|
||||
BufferLayout.blob(1, 'numReadonlyUnsignedAccounts'),
|
||||
BufferLayout.blob(keyCount.length, 'keyCount'),
|
||||
BufferLayout.seq(Layout.publicKey('key'), keys.length, 'keys'),
|
||||
Layout.publicKey('recentBlockhash'),
|
||||
|
@ -283,10 +283,8 @@ export class Transaction {
|
|||
|
||||
const transaction = {
|
||||
numRequiredSignatures: Buffer.from([this.signatures.length]),
|
||||
numCreditOnlySignedAccounts: Buffer.from([numCreditOnlySignedAccounts]),
|
||||
numCreditOnlyUnsignedAccounts: Buffer.from([
|
||||
numCreditOnlyUnsignedAccounts,
|
||||
]),
|
||||
numReadonlySignedAccounts: Buffer.from([numReadonlySignedAccounts]),
|
||||
numReadonlyUnsignedAccounts: Buffer.from([numReadonlyUnsignedAccounts]),
|
||||
keyCount: Buffer.from(keyCount),
|
||||
keys: keys.map(key => new PublicKey(key).toBuffer()),
|
||||
recentBlockhash: Buffer.from(bs58.decode(recentBlockhash)),
|
||||
|
@ -450,14 +448,14 @@ export class Transaction {
|
|||
function isCreditDebit(
|
||||
i: number,
|
||||
numRequiredSignatures: number,
|
||||
numCreditOnlySignedAccounts: number,
|
||||
numCreditOnlyUnsignedAccounts: number,
|
||||
numReadonlySignedAccounts: number,
|
||||
numReadonlyUnsignedAccounts: number,
|
||||
numKeys: number,
|
||||
): boolean {
|
||||
return (
|
||||
i < numRequiredSignatures - numCreditOnlySignedAccounts ||
|
||||
i < numRequiredSignatures - numReadonlySignedAccounts ||
|
||||
(i >= numRequiredSignatures &&
|
||||
i < numKeys - numCreditOnlyUnsignedAccounts)
|
||||
i < numKeys - numReadonlyUnsignedAccounts)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -476,10 +474,10 @@ export class Transaction {
|
|||
|
||||
const numRequiredSignatures = byteArray.shift();
|
||||
// byteArray = byteArray.slice(1); // Skip numRequiredSignatures byte
|
||||
const numCreditOnlySignedAccounts = byteArray.shift();
|
||||
// byteArray = byteArray.slice(1); // Skip numCreditOnlySignedAccounts byte
|
||||
const numCreditOnlyUnsignedAccounts = byteArray.shift();
|
||||
// byteArray = byteArray.slice(1); // Skip numCreditOnlyUnsignedAccounts byte
|
||||
const numReadonlySignedAccounts = byteArray.shift();
|
||||
// byteArray = byteArray.slice(1); // Skip numReadonlySignedAccounts byte
|
||||
const numReadonlyUnsignedAccounts = byteArray.shift();
|
||||
// byteArray = byteArray.slice(1); // Skip numReadonlyUnsignedAccounts byte
|
||||
|
||||
const accountCount = shortvec.decodeLength(byteArray);
|
||||
let accounts = [];
|
||||
|
@ -532,11 +530,11 @@ export class Transaction {
|
|||
isSigner: transaction.signatures.some(
|
||||
keyObj => keyObj.publicKey.toString() === pubkey.toString(),
|
||||
),
|
||||
isDebitable: isCreditDebit(
|
||||
isWritable: isCreditDebit(
|
||||
j,
|
||||
numRequiredSignatures,
|
||||
numCreditOnlySignedAccounts,
|
||||
numCreditOnlyUnsignedAccounts,
|
||||
numReadonlySignedAccounts,
|
||||
numReadonlyUnsignedAccounts,
|
||||
accounts.length,
|
||||
),
|
||||
});
|
||||
|
|
|
@ -36,7 +36,7 @@ test('load BPF C program', async () => {
|
|||
|
||||
const programId = await BpfLoader.load(connection, from, data);
|
||||
const transaction = new Transaction().add({
|
||||
keys: [{pubkey: from.publicKey, isSigner: true, isDebitable: true}],
|
||||
keys: [{pubkey: from.publicKey, isSigner: true, isWritable: true}],
|
||||
programId,
|
||||
});
|
||||
await sendAndConfirmTransaction(connection, transaction, from);
|
||||
|
@ -61,7 +61,7 @@ test('load BPF Rust program', async () => {
|
|||
|
||||
const programId = await BpfLoader.load(connection, from, data);
|
||||
const transaction = new Transaction().add({
|
||||
keys: [{pubkey: from.publicKey, isSigner: true, isDebitable: true}],
|
||||
keys: [{pubkey: from.publicKey, isSigner: true, isWritable: true}],
|
||||
programId,
|
||||
});
|
||||
await sendAndConfirmTransaction(connection, transaction, from);
|
||||
|
|
|
@ -681,11 +681,6 @@ test('transaction', async () => {
|
|||
result: 31,
|
||||
},
|
||||
]);
|
||||
if (!mockRpcEnabled) {
|
||||
// Credit-only account credits are committed at the end of every slot;
|
||||
// this sleep is to ensure a full slot has elapsed
|
||||
await sleep((1000 * DEFAULT_TICKS_PER_SLOT) / NUM_TICKS_PER_SECOND);
|
||||
}
|
||||
expect(await connection.getBalance(accountTo.publicKey)).toBe(31);
|
||||
});
|
||||
|
||||
|
|
|
@ -111,8 +111,8 @@ test('non-SystemInstruction error', () => {
|
|||
|
||||
const badProgramId = {
|
||||
keys: [
|
||||
{pubkey: from.publicKey, isSigner: true, isDebitable: true},
|
||||
{pubkey: to.publicKey, isSigner: false, isDebitable: false},
|
||||
{pubkey: from.publicKey, isSigner: true, isWritable: true},
|
||||
{pubkey: to.publicKey, isSigner: false, isWritable: true},
|
||||
],
|
||||
programId: BudgetProgram.programId,
|
||||
data: Buffer.from([2, 0, 0, 0]),
|
||||
|
|
Loading…
Reference in New Issue