fix: catch up to recent upstream changes
This commit is contained in:
@ -83,6 +83,8 @@ function jsonRpcResult(resultDescription: any) {
|
||||
* Expected JSON RPC response for the "getAccountInfo" message
|
||||
*/
|
||||
const GetAccountInfoRpcResult = jsonRpcResult({
|
||||
executable: 'boolean',
|
||||
loader_program_id: 'array',
|
||||
program_id: 'array',
|
||||
tokens: 'number',
|
||||
userdata: 'array',
|
||||
@ -138,8 +140,9 @@ const SendTokensRpcResult = jsonRpcResult('string');
|
||||
* @property {?Buffer} userdata Optional userdata assigned to the account
|
||||
*/
|
||||
type AccountInfo = {
|
||||
tokens: number,
|
||||
executable: boolean;
|
||||
programId: PublicKey,
|
||||
tokens: number,
|
||||
userdata: Buffer,
|
||||
}
|
||||
|
||||
@ -201,8 +204,10 @@ export class Connection {
|
||||
assert(typeof result !== 'undefined');
|
||||
|
||||
return {
|
||||
executable: result.executable,
|
||||
tokens: result.tokens,
|
||||
programId: new PublicKey(result.program_id),
|
||||
loaderProgramId: new PublicKey(result.loader_program_id),
|
||||
userdata: Buffer.from(result.userdata),
|
||||
};
|
||||
}
|
||||
|
@ -2,7 +2,9 @@
|
||||
export {Account} from './account';
|
||||
export {BudgetProgram} from './budget-program';
|
||||
export {Connection} from './connection';
|
||||
export {Loader} from './loader';
|
||||
export {NativeLoader} from './native-loader';
|
||||
export {PublicKey} from './publickey';
|
||||
export {SystemProgram} from './system-program';
|
||||
export {Transaction} from './transaction';
|
||||
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,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user