Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
fix: catchup to upstream loader changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mvines committed Oct 18, 2018
1 parent 817cf74 commit 6766b67
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions module.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ declare module '@solana/web3.js' {
): Transaction;
static move(from: PublicKey, to: PublicKey, amount: number): Transaction;
static assign(from: PublicKey, programId: PublicKey): Transaction;
static spawn(programId: PublicKey): Transaction;
}

// === src/transaction.js ===
Expand Down
7 changes: 5 additions & 2 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import * as BufferLayout from 'buffer-layout';

import {PublicKey, Transaction} from '.';
import {PublicKey, SystemProgram, Transaction} from '.';
import {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
import type {Account, Connection} from '.';

Expand Down Expand Up @@ -85,12 +85,15 @@ export class Loader {
userdata,
);

const transaction = new Transaction({
let transaction = new Transaction({
fee: 0,
keys: [program.publicKey],
programId: this.programId,
userdata,
});
await sendAndConfirmTransaction(this.connection, program, transaction);

transaction = SystemProgram.spawn(program.publicKey);
await sendAndConfirmTransaction(this.connection, program, transaction);
}
}
24 changes: 24 additions & 0 deletions src/system-program.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,28 @@ export class SystemProgram {
userdata,
});
}

/**
* Spawn a new program from an account
*/
static spawn(programId: PublicKey): Transaction {
const userdataLayout = BufferLayout.struct([
BufferLayout.u32('instruction'),
]);

const userdata = Buffer.alloc(userdataLayout.span);
userdataLayout.encode(
{
instruction: 3, // Spawn instruction
},
userdata,
);

return new Transaction({
fee: 0,
keys: [programId],
programId: SystemProgram.programId,
userdata,
});
}
}
4 changes: 2 additions & 2 deletions test/native-loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {mockRpcEnabled} from './__mocks__/node-fetch';
import {url} from './url';
import {newAccountWithTokens} from './new-account-with-tokens';

test('unstable - load', async () => {
test('load noop program', async () => {
if (mockRpcEnabled) {
console.log('non-live test skipped');
return;
Expand All @@ -25,6 +25,6 @@ test('unstable - load', async () => {
programId: noopProgramId,
});
const signature = await connection.sendTransaction(from, noopTransaction);
expect(connection.confirmTransaction(signature)).resolves.toBe(true);
await expect(connection.confirmTransaction(signature)).resolves.toBe(true);
});

0 comments on commit 6766b67

Please sign in to comment.