fix: catch up to recent upstream changes
This commit is contained in:
@ -34,8 +34,10 @@ declare module '@solana/web3.js' {
|
|||||||
|
|
||||||
// === src/connection.js ===
|
// === src/connection.js ===
|
||||||
declare export type AccountInfo = {
|
declare export type AccountInfo = {
|
||||||
tokens: number,
|
executable: boolean;
|
||||||
|
loaderProgramId: PublicKey,
|
||||||
programId: PublicKey,
|
programId: PublicKey,
|
||||||
|
tokens: number,
|
||||||
userdata: Buffer,
|
userdata: Buffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +69,6 @@ declare module '@solana/web3.js' {
|
|||||||
): Transaction;
|
): Transaction;
|
||||||
static move(from: PublicKey, to: PublicKey, amount: number): Transaction;
|
static move(from: PublicKey, to: PublicKey, amount: number): Transaction;
|
||||||
static assign(from: PublicKey, programId: PublicKey): Transaction;
|
static assign(from: PublicKey, programId: PublicKey): Transaction;
|
||||||
static load(from: PublicKey, programId: PublicKey, name: string): Transaction;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// === src/transaction.js ===
|
// === src/transaction.js ===
|
||||||
@ -152,4 +153,20 @@ declare module '@solana/web3.js' {
|
|||||||
): Promise<void>;
|
): Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// === src/loader.js ===
|
||||||
|
declare export class Loader {
|
||||||
|
constructor(connection: Connection, programId: PublicKey) : Loader;
|
||||||
|
load(program: Account, offset: number, bytes: Array<number>): Promise<void>;
|
||||||
|
finalize(program: Account): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// === src/native-loader.js ===
|
||||||
|
declare export class NativeLoader {
|
||||||
|
static programId: PublicKey;
|
||||||
|
static load(
|
||||||
|
connection: Connection,
|
||||||
|
owner: Account,
|
||||||
|
programName: string,
|
||||||
|
): Promise<PublicKey>;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -83,6 +83,8 @@ function jsonRpcResult(resultDescription: any) {
|
|||||||
* Expected JSON RPC response for the "getAccountInfo" message
|
* Expected JSON RPC response for the "getAccountInfo" message
|
||||||
*/
|
*/
|
||||||
const GetAccountInfoRpcResult = jsonRpcResult({
|
const GetAccountInfoRpcResult = jsonRpcResult({
|
||||||
|
executable: 'boolean',
|
||||||
|
loader_program_id: 'array',
|
||||||
program_id: 'array',
|
program_id: 'array',
|
||||||
tokens: 'number',
|
tokens: 'number',
|
||||||
userdata: 'array',
|
userdata: 'array',
|
||||||
@ -138,8 +140,9 @@ const SendTokensRpcResult = jsonRpcResult('string');
|
|||||||
* @property {?Buffer} userdata Optional userdata assigned to the account
|
* @property {?Buffer} userdata Optional userdata assigned to the account
|
||||||
*/
|
*/
|
||||||
type AccountInfo = {
|
type AccountInfo = {
|
||||||
tokens: number,
|
executable: boolean;
|
||||||
programId: PublicKey,
|
programId: PublicKey,
|
||||||
|
tokens: number,
|
||||||
userdata: Buffer,
|
userdata: Buffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,8 +204,10 @@ export class Connection {
|
|||||||
assert(typeof result !== 'undefined');
|
assert(typeof result !== 'undefined');
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
executable: result.executable,
|
||||||
tokens: result.tokens,
|
tokens: result.tokens,
|
||||||
programId: new PublicKey(result.program_id),
|
programId: new PublicKey(result.program_id),
|
||||||
|
loaderProgramId: new PublicKey(result.loader_program_id),
|
||||||
userdata: Buffer.from(result.userdata),
|
userdata: Buffer.from(result.userdata),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,9 @@
|
|||||||
export {Account} from './account';
|
export {Account} from './account';
|
||||||
export {BudgetProgram} from './budget-program';
|
export {BudgetProgram} from './budget-program';
|
||||||
export {Connection} from './connection';
|
export {Connection} from './connection';
|
||||||
|
export {Loader} from './loader';
|
||||||
|
export {NativeLoader} from './native-loader';
|
||||||
export {PublicKey} from './publickey';
|
export {PublicKey} from './publickey';
|
||||||
export {SystemProgram} from './system-program';
|
export {SystemProgram} from './system-program';
|
||||||
export {Transaction} from './transaction';
|
|
||||||
export {Token, TokenAmount} from './token-program';
|
export {Token, TokenAmount} from './token-program';
|
||||||
|
export {Transaction} from './transaction';
|
||||||
|
97
web3.js/src/loader.js
Normal file
97
web3.js/src/loader.js
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
// @flow
|
||||||
|
|
||||||
|
import * as BufferLayout from 'buffer-layout';
|
||||||
|
|
||||||
|
import {PublicKey, Transaction} from '.';
|
||||||
|
import {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
|
||||||
|
import type {Account, Connection} from '.';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Program loader interface
|
||||||
|
*/
|
||||||
|
export class Loader {
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
connection: Connection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
programId: PublicKey;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param connection The connection to use
|
||||||
|
* @param programId Public key that identifies the loader
|
||||||
|
*/
|
||||||
|
constructor(connection: Connection, programId: PublicKey) {
|
||||||
|
Object.assign(this, {connection, programId});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load program data
|
||||||
|
*
|
||||||
|
* @param program Account to load the program info
|
||||||
|
* @param offset Account userdata offset to write `bytes` into
|
||||||
|
* @param bytes Program data
|
||||||
|
*/
|
||||||
|
async load(program: Account, offset: number, bytes: Array<number>) {
|
||||||
|
const userdataLayout = BufferLayout.struct([
|
||||||
|
BufferLayout.u32('instruction'),
|
||||||
|
BufferLayout.u32('offset'),
|
||||||
|
BufferLayout.u32('bytesLength'),
|
||||||
|
BufferLayout.u32('bytesLengthPadding'),
|
||||||
|
BufferLayout.seq(
|
||||||
|
BufferLayout.u8('byte'),
|
||||||
|
BufferLayout.offset(BufferLayout.u32(), -8),
|
||||||
|
'bytes'
|
||||||
|
),
|
||||||
|
]);
|
||||||
|
|
||||||
|
let userdata = Buffer.alloc(bytes.length + 16);
|
||||||
|
userdataLayout.encode(
|
||||||
|
{
|
||||||
|
instruction: 0, // Load instruction
|
||||||
|
offset,
|
||||||
|
bytes,
|
||||||
|
},
|
||||||
|
userdata,
|
||||||
|
);
|
||||||
|
|
||||||
|
const transaction = new Transaction({
|
||||||
|
fee: 0,
|
||||||
|
keys: [program.publicKey],
|
||||||
|
programId: this.programId,
|
||||||
|
userdata,
|
||||||
|
});
|
||||||
|
await sendAndConfirmTransaction(this.connection, program, transaction);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finalize an account loaded with program data for execution
|
||||||
|
*
|
||||||
|
* @param program `load()`ed Account
|
||||||
|
*/
|
||||||
|
async finalize(program: Account) {
|
||||||
|
const userdataLayout = BufferLayout.struct([
|
||||||
|
BufferLayout.u32('instruction'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const userdata = Buffer.alloc(userdataLayout.span);
|
||||||
|
console.log('sp',userdataLayout.span);
|
||||||
|
userdataLayout.encode(
|
||||||
|
{
|
||||||
|
instruction: 1, // Finalize instruction
|
||||||
|
},
|
||||||
|
userdata,
|
||||||
|
);
|
||||||
|
|
||||||
|
const transaction = new Transaction({
|
||||||
|
fee: 0,
|
||||||
|
keys: [program.publicKey],
|
||||||
|
programId: this.programId,
|
||||||
|
userdata,
|
||||||
|
});
|
||||||
|
await sendAndConfirmTransaction(this.connection, program, transaction);
|
||||||
|
}
|
||||||
|
}
|
50
web3.js/src/native-loader.js
Normal file
50
web3.js/src/native-loader.js
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
// @flow
|
||||||
|
|
||||||
|
import {Account, PublicKey, Loader, SystemProgram} from '.';
|
||||||
|
import {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
|
||||||
|
import type {Connection} from '.';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Factory class for transactions to interact with a program loader
|
||||||
|
*/
|
||||||
|
export class NativeLoader {
|
||||||
|
/**
|
||||||
|
* Public key that identifies the NativeLoader
|
||||||
|
*/
|
||||||
|
static get programId(): PublicKey {
|
||||||
|
return new PublicKey('0x0202020202020202020202020202020202020202020202020202020202020202');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a native program
|
||||||
|
*
|
||||||
|
* @param connection The connection to use
|
||||||
|
* @param owner User account to load the program with
|
||||||
|
* @param programName Name of the native program
|
||||||
|
*/
|
||||||
|
static async load(
|
||||||
|
connection: Connection,
|
||||||
|
owner: Account,
|
||||||
|
programName: string,
|
||||||
|
): Promise<PublicKey> {
|
||||||
|
const bytes = [...Buffer.from(programName)];
|
||||||
|
|
||||||
|
const programAccount = new Account();
|
||||||
|
|
||||||
|
// Allocate memory for the program account
|
||||||
|
const transaction = SystemProgram.createAccount(
|
||||||
|
owner.publicKey,
|
||||||
|
programAccount.publicKey,
|
||||||
|
1,
|
||||||
|
bytes.length + 1,
|
||||||
|
NativeLoader.programId,
|
||||||
|
);
|
||||||
|
await sendAndConfirmTransaction(connection, owner, transaction);
|
||||||
|
|
||||||
|
const loader = new Loader(connection, NativeLoader.programId);
|
||||||
|
await loader.load(programAccount, 0, bytes);
|
||||||
|
await loader.finalize(programAccount);
|
||||||
|
|
||||||
|
return programAccount.publicKey;
|
||||||
|
}
|
||||||
|
}
|
@ -105,35 +105,4 @@ export class SystemProgram {
|
|||||||
userdata,
|
userdata,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Load a dynamic program. Unstable API, will change
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
static load(from: PublicKey, programId: PublicKey, name: string): Transaction {
|
|
||||||
const userdataLayout = BufferLayout.struct([
|
|
||||||
BufferLayout.u32('instruction'),
|
|
||||||
Layout.publicKey('programId'),
|
|
||||||
Layout.rustString('name'),
|
|
||||||
]);
|
|
||||||
|
|
||||||
let userdata = Buffer.alloc(1024);
|
|
||||||
const encodeLength = userdataLayout.encode(
|
|
||||||
{
|
|
||||||
instruction: 3, // Load instruction
|
|
||||||
programId: programId.toBuffer(),
|
|
||||||
name,
|
|
||||||
},
|
|
||||||
userdata,
|
|
||||||
);
|
|
||||||
userdata = userdata.slice(0, encodeLength);
|
|
||||||
|
|
||||||
return new Transaction({
|
|
||||||
fee: 0,
|
|
||||||
keys: [from],
|
|
||||||
programId: SystemProgram.programId,
|
|
||||||
userdata,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -213,6 +213,11 @@ test('request airdrop', async () => {
|
|||||||
],
|
],
|
||||||
tokens: 42,
|
tokens: 42,
|
||||||
userdata: [],
|
userdata: [],
|
||||||
|
executable: false,
|
||||||
|
loader_program_id: [
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||||
|
],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
30
web3.js/test/native-loader.test.js
Normal file
30
web3.js/test/native-loader.test.js
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
// @flow
|
||||||
|
|
||||||
|
import {
|
||||||
|
Connection,
|
||||||
|
NativeLoader,
|
||||||
|
Transaction,
|
||||||
|
} from '../src';
|
||||||
|
import {mockRpcEnabled} from './__mocks__/node-fetch';
|
||||||
|
import {url} from './url';
|
||||||
|
import {newAccountWithTokens} from './new-account-with-tokens';
|
||||||
|
|
||||||
|
test('unstable - load', async () => {
|
||||||
|
if (mockRpcEnabled) {
|
||||||
|
console.log('non-live test skipped');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const connection = new Connection(url);
|
||||||
|
const from = await newAccountWithTokens(connection);
|
||||||
|
|
||||||
|
const noopProgramId = await NativeLoader.load(connection, from, 'noop');
|
||||||
|
const noopTransaction = new Transaction({
|
||||||
|
fee: 0,
|
||||||
|
keys: [from.publicKey],
|
||||||
|
programId: noopProgramId,
|
||||||
|
});
|
||||||
|
const signature = await connection.sendTransaction(from, noopTransaction);
|
||||||
|
expect(connection.confirmTransaction(signature)).resolves.toBe(true);
|
||||||
|
});
|
||||||
|
|
@ -3,13 +3,8 @@
|
|||||||
import {
|
import {
|
||||||
Account,
|
Account,
|
||||||
BudgetProgram,
|
BudgetProgram,
|
||||||
Connection,
|
|
||||||
SystemProgram,
|
SystemProgram,
|
||||||
Transaction,
|
|
||||||
} from '../src';
|
} from '../src';
|
||||||
import {mockRpcEnabled} from './__mocks__/node-fetch';
|
|
||||||
import {url} from './url';
|
|
||||||
import {newAccountWithTokens} from './new-account-with-tokens';
|
|
||||||
|
|
||||||
test('createAccount', () => {
|
test('createAccount', () => {
|
||||||
const from = new Account();
|
const from = new Account();
|
||||||
@ -61,33 +56,3 @@ test('assign', () => {
|
|||||||
// TODO: Validate transaction contents more
|
// TODO: Validate transaction contents more
|
||||||
});
|
});
|
||||||
|
|
||||||
test('unstable - load', async () => {
|
|
||||||
if (mockRpcEnabled) {
|
|
||||||
console.log('non-live test skipped');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const connection = new Connection(url);
|
|
||||||
const from = await newAccountWithTokens(connection);
|
|
||||||
const noopProgramId = (new Account()).publicKey;
|
|
||||||
|
|
||||||
const loadTransaction = SystemProgram.load(
|
|
||||||
from.publicKey,
|
|
||||||
noopProgramId,
|
|
||||||
'noop',
|
|
||||||
);
|
|
||||||
|
|
||||||
let signature = await connection.sendTransaction(from, loadTransaction);
|
|
||||||
expect(connection.confirmTransaction(signature)).resolves.toBe(true);
|
|
||||||
|
|
||||||
const noopTransaction = new Transaction({
|
|
||||||
fee: 0,
|
|
||||||
keys: [from.publicKey],
|
|
||||||
programId: noopProgramId,
|
|
||||||
});
|
|
||||||
signature = await connection.sendTransaction(from, noopTransaction);
|
|
||||||
expect(connection.confirmTransaction(signature)).resolves.toBe(true);
|
|
||||||
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
|
@ -120,6 +120,11 @@ test('create new token', async () => {
|
|||||||
10, 0, 0, 0, 0, 0, 0, 0, 84, 101, 115, 116, 32, 116, 111, 107, 101, 110,
|
10, 0, 0, 0, 0, 0, 0, 0, 84, 101, 115, 116, 32, 116, 111, 107, 101, 110,
|
||||||
4, 0, 0, 0, 0, 0, 0, 0, 84, 69, 83, 84
|
4, 0, 0, 0, 0, 0, 0, 0, 84, 69, 83, 84
|
||||||
],
|
],
|
||||||
|
executable: false,
|
||||||
|
loader_program_id: [
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||||
|
],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
@ -153,6 +158,11 @@ test('create new token', async () => {
|
|||||||
16, 39, 0, 0, 0, 0, 0, 0,
|
16, 39, 0, 0, 0, 0, 0, 0,
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
],
|
],
|
||||||
|
executable: false,
|
||||||
|
loader_program_id: [
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||||
|
],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
@ -204,6 +214,11 @@ test('create new token account', async () => {
|
|||||||
0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
0,
|
0,
|
||||||
],
|
],
|
||||||
|
executable: false,
|
||||||
|
loader_program_id: [
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||||
|
],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
@ -256,6 +271,11 @@ test('transfer', async () => {
|
|||||||
123, 0, 0, 0, 0, 0, 0, 0,
|
123, 0, 0, 0, 0, 0, 0, 0,
|
||||||
0,
|
0,
|
||||||
],
|
],
|
||||||
|
executable: false,
|
||||||
|
loader_program_id: [
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||||
|
],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
@ -293,6 +313,11 @@ test('transfer', async () => {
|
|||||||
123, 0, 0, 0, 0, 0, 0, 0,
|
123, 0, 0, 0, 0, 0, 0, 0,
|
||||||
0,
|
0,
|
||||||
],
|
],
|
||||||
|
executable: false,
|
||||||
|
loader_program_id: [
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||||
|
],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
@ -355,6 +380,11 @@ test('approve/revoke', async () => {
|
|||||||
1,
|
1,
|
||||||
...initialOwnerTokenAccount.toBuffer(),
|
...initialOwnerTokenAccount.toBuffer(),
|
||||||
],
|
],
|
||||||
|
executable: false,
|
||||||
|
loader_program_id: [
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||||
|
],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
@ -403,6 +433,11 @@ test('approve/revoke', async () => {
|
|||||||
1,
|
1,
|
||||||
...initialOwnerTokenAccount.toBuffer(),
|
...initialOwnerTokenAccount.toBuffer(),
|
||||||
],
|
],
|
||||||
|
executable: false,
|
||||||
|
loader_program_id: [
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||||
|
],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
|
Reference in New Issue
Block a user