anchor/examples/misc/tests/misc.js

177 lines
5.2 KiB
JavaScript
Raw Normal View History

const anchor = require("@project-serum/anchor");
const serumCmn = require("@project-serum/common");
2021-02-10 21:24:29 -08:00
const assert = require("assert");
describe("misc", () => {
// Configure the client to use the local cluster.
anchor.setProvider(anchor.Provider.env());
2021-03-24 10:35:51 -07:00
const program = anchor.workspace.Misc;
2021-04-11 17:23:43 -07:00
const misc2Program = anchor.workspace.Misc2;
2021-03-24 10:35:51 -07:00
it("Can allocate extra space for a state constructor", async () => {
const tx = await program.state.rpc.new();
const addr = await program.state.address();
const state = await program.state();
const accountInfo = await program.provider.connection.getAccountInfo(addr);
assert.ok(state.v.equals(Buffer.from([])));
assert.ok(accountInfo.data.length === 99);
});
2021-02-10 21:24:29 -08:00
2021-04-11 21:54:35 -07:00
const data = new anchor.web3.Account();
2021-02-10 21:24:29 -08:00
it("Can use u128 and i128", async () => {
const tx = await program.rpc.initialize(
new anchor.BN(1234),
new anchor.BN(22),
{
accounts: {
data: data.publicKey,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
},
signers: [data],
instructions: [await program.account.data.createInstruction(data)],
}
);
const dataAccount = await program.account.data(data.publicKey);
assert.ok(dataAccount.udata.eq(new anchor.BN(1234)));
assert.ok(dataAccount.idata.eq(new anchor.BN(22)));
});
2021-05-07 11:20:15 -07:00
it("Can use u16", async () => {
const data = new anchor.web3.Account();
const tx = await program.rpc.testU16(99, {
accounts: {
myAccount: data.publicKey,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
},
signers: [data],
instructions: [await program.account.dataU16.createInstruction(data)],
});
const dataAccount = await program.account.dataU16(data.publicKey);
assert.ok(dataAccount.data === 99);
});
it("Can embed programs into genesis from the Anchor.toml", async () => {
const pid = new anchor.web3.PublicKey(
"FtMNMKp9DZHKWUyVAsj3Q5QV8ow4P3fUPP7ZrWEQJzKr"
);
let accInfo = await anchor.getProvider().connection.getAccountInfo(pid);
assert.ok(accInfo.executable);
});
2021-04-11 21:54:35 -07:00
it("Can use the owner constraint", async () => {
await program.rpc.testOwner({
accounts: {
data: data.publicKey,
misc: program.programId,
},
});
await assert.rejects(
async () => {
await program.rpc.testOwner({
accounts: {
data: program.provider.wallet.publicKey,
misc: program.programId,
},
});
},
(err) => {
return true;
}
);
});
it("Can use the executable attribute", async () => {
await program.rpc.testExecutable({
accounts: {
program: program.programId,
},
});
await assert.rejects(
async () => {
await program.rpc.testExecutable({
accounts: {
program: program.provider.wallet.publicKey,
},
});
},
(err) => {
return true;
}
);
});
2021-04-11 17:23:43 -07:00
it("Can CPI to state instructions", async () => {
const oldData = new anchor.BN(0);
await misc2Program.state.rpc.new({
accounts: {
authority: program.provider.wallet.publicKey,
},
});
let stateAccount = await misc2Program.state();
assert.ok(stateAccount.data.eq(oldData));
assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
const newData = new anchor.BN(2134);
await program.rpc.testStateCpi(newData, {
accounts: {
authority: program.provider.wallet.publicKey,
cpiState: await misc2Program.state.address(),
misc2Program: misc2Program.programId,
},
});
stateAccount = await misc2Program.state();
assert.ok(stateAccount.data.eq(newData));
assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
});
it("Can create an associated program account", async () => {
const state = await program.state.address();
// Manual associated address calculation for test only. Clients should use
// the generated methods.
const [
associatedAccount,
nonce,
] = await anchor.web3.PublicKey.findProgramAddress(
[
Buffer.from([97, 110, 99, 104, 111, 114]), // b"anchor".
program.provider.wallet.publicKey.toBuffer(),
state.toBuffer(),
data.publicKey.toBuffer(),
],
program.programId
);
await assert.rejects(
async () => {
await program.account.testData(associatedAccount);
},
(err) => {
assert.ok(
err.toString() ===
`Error: Account does not exist ${associatedAccount.toString()}`
);
return true;
}
);
await program.rpc.testAssociatedAccountCreation(new anchor.BN(1234), {
accounts: {
myAccount: associatedAccount,
authority: program.provider.wallet.publicKey,
state,
data: data.publicKey,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
systemProgram: anchor.web3.SystemProgram.programId,
},
});
// Try out the generated associated method.
const account = await program.account.testData.associated(
program.provider.wallet.publicKey,
state,
2021-05-07 11:20:15 -07:00
data.publicKey
);
assert.ok(account.data.toNumber() === 1234);
});
2021-02-10 21:24:29 -08:00
});