Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ts: Refactor #322

Merged
merged 16 commits into from
May 26, 2021
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ incremented for features.

## Breaking Changes

* ts: Retrieving deserialized accounts from the `<program>.account.<my-account>` and `<program>.state` namespaces now require explicitly invoking the `fetch` API. For example, `program.account.myAccount(<adddress>)` and `program.state()` is now `program.account.myAccount.fetch(<address>)` and `program.state.fetch()` ([#322](https://github.com/project-serum/anchor/pull/322)).
* lang: `#[account(associated)]` now requires `init` to be provided to create an associated account. If not provided, then the address will be assumed to exist, and a constraint will be added to ensure its correctness ([#318](https://github.com/project-serum/anchor/pull/318)).
* lang, ts: Change account discriminator pre-image of the `#[state]` account discriminator to be namespaced by "state:". This change should only be noticed by library maintainers ([#320](https://github.com/project-serum/anchor/pull/320)).
* lang, ts: Change domain delimiters for the pre-image of the instruciton sighash to be a single colon `:` to be consistent with accounts. This change should only be noticed by library maintainers.
* lang, ts: Change account discriminator pre-image of the `#[state]` account discriminator to be namespaced by "state:" ([#320](https://github.com/project-serum/anchor/pull/320)).
* lang, ts: Change domain delimiters for the pre-image of the instruciton sighash to be a single colon `:` to be consistent with accounts ([#321](https://github.com/project-serum/anchor/pull/321)).

## [0.6.0] - 2021-05-23

Expand Down
4 changes: 2 additions & 2 deletions examples/cashiers-check/tests/cashiers-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe("cashiers-check", () => {
],
});

const checkAccount = await program.account.check(check.publicKey);
const checkAccount = await program.account.check.fetch(check.publicKey);
assert.ok(checkAccount.from.equals(god));
assert.ok(checkAccount.to.equals(receiver));
assert.ok(checkAccount.amount.eq(new anchor.BN(100)));
Expand Down Expand Up @@ -91,7 +91,7 @@ describe("cashiers-check", () => {
},
});

const checkAccount = await program.account.check(check.publicKey);
const checkAccount = await program.account.check.fetch(check.publicKey);
assert.ok(checkAccount.burned === true);

let vaultAccount = await serumCmn.getTokenAccount(
Expand Down
4 changes: 2 additions & 2 deletions examples/chat/tests/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("chat", () => {
signers: [chatRoom],
});

const chat = await program.account.chatRoom(chatRoom.publicKey);
const chat = await program.account.chatRoom.fetch(chatRoom.publicKey);
const name = new TextDecoder("utf-8").decode(new Uint8Array(chat.name));
assert.ok(name.startsWith("Test Chat")); // [u8; 280] => trailing zeros.
assert.ok(chat.messages.length === 33607);
Expand Down Expand Up @@ -76,7 +76,7 @@ describe("chat", () => {
}

// Check the chat room state is as expected.
const chat = await program.account.chatRoom(chatRoom.publicKey);
const chat = await program.account.chatRoom.fetch(chatRoom.publicKey);
const name = new TextDecoder("utf-8").decode(new Uint8Array(chat.name));
assert.ok(name.startsWith("Test Chat")); // [u8; 280] => trailing zeros.
assert.ok(chat.messages.length === 33607);
Expand Down
4 changes: 2 additions & 2 deletions examples/composite/tests/composite.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ describe("composite", () => {
}
);

const dummyAAccount = await program.account.dummyA(dummyA.publicKey);
const dummyBAccount = await program.account.dummyB(dummyB.publicKey);
const dummyAAccount = await program.account.dummyA.fetch(dummyA.publicKey);
const dummyBAccount = await program.account.dummyB.fetch(dummyB.publicKey);

assert.ok(dummyAAccount.data.eq(new anchor.BN(1234)));
assert.ok(dummyBAccount.data.eq(new anchor.BN(4321)));
Expand Down
4 changes: 2 additions & 2 deletions examples/interface/tests/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe("interface", () => {
it("Is initialized!", async () => {
await counter.state.rpc.new(counterAuth.programId);

const stateAccount = await counter.state();
const stateAccount = await counter.state.fetch();
assert.ok(stateAccount.count.eq(new anchor.BN(0)));
assert.ok(stateAccount.authProgram.equals(counterAuth.programId));
});
Expand Down Expand Up @@ -39,7 +39,7 @@ describe("interface", () => {
authProgram: counterAuth.programId,
},
});
const stateAccount = await counter.state();
const stateAccount = await counter.state.fetch();
assert.ok(stateAccount.count.eq(new anchor.BN(3)));
});
});
34 changes: 17 additions & 17 deletions examples/lockup/tests/lockup.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe("Lockup and Registry", () => {
});

lockupAddress = await lockup.state.address();
const lockupAccount = await lockup.state();
const lockupAccount = await lockup.state.fetch();

assert.ok(lockupAccount.authority.equals(provider.wallet.publicKey));
assert.ok(lockupAccount.whitelist.length === WHITELIST_SIZE);
Expand All @@ -63,7 +63,7 @@ describe("Lockup and Registry", () => {
},
});

let lockupAccount = await lockup.state();
let lockupAccount = await lockup.state.fetch();
assert.ok(lockupAccount.authority.equals(newAuthority.publicKey));

await lockup.state.rpc.setAuthority(provider.wallet.publicKey, {
Expand All @@ -73,7 +73,7 @@ describe("Lockup and Registry", () => {
signers: [newAuthority],
});

lockupAccount = await lockup.state();
lockupAccount = await lockup.state.fetch();
assert.ok(lockupAccount.authority.equals(provider.wallet.publicKey));
});

Expand All @@ -97,7 +97,7 @@ describe("Lockup and Registry", () => {

await lockup.state.rpc.whitelistAdd(entries[0], { accounts });

let lockupAccount = await lockup.state();
let lockupAccount = await lockup.state.fetch();

assert.ok(lockupAccount.whitelist.length === 1);
assert.deepEqual(lockupAccount.whitelist, [entries[0]]);
Expand All @@ -106,7 +106,7 @@ describe("Lockup and Registry", () => {
await lockup.state.rpc.whitelistAdd(entries[k], { accounts });
}

lockupAccount = await lockup.state();
lockupAccount = await lockup.state.fetch();

assert.deepEqual(lockupAccount.whitelist, entries);

Expand All @@ -129,7 +129,7 @@ describe("Lockup and Registry", () => {
authority: provider.wallet.publicKey,
},
});
let lockupAccount = await lockup.state();
let lockupAccount = await lockup.state.fetch();
assert.deepEqual(lockupAccount.whitelist, entries.slice(1));
});

Expand Down Expand Up @@ -185,7 +185,7 @@ describe("Lockup and Registry", () => {
}
);

vestingAccount = await lockup.account.vesting(vesting.publicKey);
vestingAccount = await lockup.account.vesting.fetch(vesting.publicKey);

assert.ok(vestingAccount.beneficiary.equals(provider.wallet.publicKey));
assert.ok(vestingAccount.mint.equals(mint));
Expand Down Expand Up @@ -246,7 +246,7 @@ describe("Lockup and Registry", () => {
},
});

vestingAccount = await lockup.account.vesting(vesting.publicKey);
vestingAccount = await lockup.account.vesting.fetch(vesting.publicKey);
assert.ok(vestingAccount.outstanding.eq(new anchor.BN(0)));

const vaultAccount = await serumCmn.getTokenAccount(
Expand Down Expand Up @@ -287,7 +287,7 @@ describe("Lockup and Registry", () => {
accounts: { lockupProgram: lockup.programId },
});

const state = await registry.state();
const state = await registry.state.fetch();
assert.ok(state.lockupProgram.equals(lockup.programId));

// Should not allow a second initializatoin.
Expand Down Expand Up @@ -324,7 +324,7 @@ describe("Lockup and Registry", () => {
}
);

registrarAccount = await registry.account.registrar(registrar.publicKey);
registrarAccount = await registry.account.registrar.fetch(registrar.publicKey);

assert.ok(registrarAccount.authority.equals(provider.wallet.publicKey));
assert.equal(registrarAccount.nonce, nonce);
Expand Down Expand Up @@ -385,7 +385,7 @@ describe("Lockup and Registry", () => {

let txSigs = await provider.sendAll(allTxs);

memberAccount = await registry.account.member(member.publicKey);
memberAccount = await registry.account.member.fetch(member.publicKey);

assert.ok(memberAccount.registrar.equals(registrar.publicKey));
assert.ok(memberAccount.beneficiary.equals(provider.wallet.publicKey));
Expand Down Expand Up @@ -516,7 +516,7 @@ describe("Lockup and Registry", () => {
}
);

const vendorAccount = await registry.account.rewardVendor(
const vendorAccount = await registry.account.rewardVendor.fetch(
unlockedVendor.publicKey
);

Expand All @@ -531,7 +531,7 @@ describe("Lockup and Registry", () => {
assert.ok(vendorAccount.rewardEventQCursor === 0);
assert.deepEqual(vendorAccount.kind, rewardKind);

const rewardQAccount = await registry.account.rewardQueue(
const rewardQAccount = await registry.account.rewardQueue.fetch(
rewardQ.publicKey
);
assert.ok(rewardQAccount.head === 1);
Expand Down Expand Up @@ -571,7 +571,7 @@ describe("Lockup and Registry", () => {
let tokenAccount = await serumCmn.getTokenAccount(provider, token);
assert.ok(tokenAccount.amount.eq(new anchor.BN(200)));

const memberAccount = await registry.account.member(member.publicKey);
const memberAccount = await registry.account.member.fetch(member.publicKey);
assert.ok(memberAccount.rewardsCursor == 1);
});

Expand Down Expand Up @@ -635,7 +635,7 @@ describe("Lockup and Registry", () => {
}
);

const vendorAccount = await registry.account.rewardVendor(
const vendorAccount = await registry.account.rewardVendor.fetch(
lockedVendor.publicKey
);

Expand All @@ -653,7 +653,7 @@ describe("Lockup and Registry", () => {
JSON.stringify(lockedRewardKind)
);

const rewardQAccount = await registry.account.rewardQueue(
const rewardQAccount = await registry.account.rewardQueue.fetch(
rewardQ.publicKey
);
assert.ok(rewardQAccount.head === 2);
Expand Down Expand Up @@ -727,7 +727,7 @@ describe("Lockup and Registry", () => {
],
});

const lockupAccount = await lockup.account.vesting(
const lockupAccount = await lockup.account.vesting.fetch(
vendoredVesting.publicKey
);

Expand Down
18 changes: 9 additions & 9 deletions examples/misc/tests/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe("misc", () => {
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 state = await program.state.fetch();
const accountInfo = await program.provider.connection.getAccountInfo(addr);
assert.ok(state.v.equals(Buffer.from([])));
assert.ok(accountInfo.data.length === 99);
Expand All @@ -32,7 +32,7 @@ describe("misc", () => {
instructions: [await program.account.data.createInstruction(data)],
}
);
const dataAccount = await program.account.data(data.publicKey);
const dataAccount = await program.account.data.fetch(data.publicKey);
assert.ok(dataAccount.udata.eq(new anchor.BN(1234)));
assert.ok(dataAccount.idata.eq(new anchor.BN(22)));
});
Expand All @@ -47,7 +47,7 @@ describe("misc", () => {
signers: [data],
instructions: [await program.account.dataU16.createInstruction(data)],
});
const dataAccount = await program.account.dataU16(data.publicKey);
const dataAccount = await program.account.dataU16.fetch(data.publicKey);
assert.ok(dataAccount.data === 99);
});

Expand Down Expand Up @@ -110,7 +110,7 @@ describe("misc", () => {
authority: program.provider.wallet.publicKey,
},
});
let stateAccount = await misc2Program.state();
let stateAccount = await misc2Program.state.fetch();
assert.ok(stateAccount.data.eq(oldData));
assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
const newData = new anchor.BN(2134);
Expand All @@ -121,7 +121,7 @@ describe("misc", () => {
misc2Program: misc2Program.programId,
},
});
stateAccount = await misc2Program.state();
stateAccount = await misc2Program.state.fetch();
assert.ok(stateAccount.data.eq(newData));
assert.ok(stateAccount.auth.equals(program.provider.wallet.publicKey));
});
Expand All @@ -145,7 +145,7 @@ describe("misc", () => {
);
await assert.rejects(
async () => {
await program.account.testData(associatedAccount);
await program.account.testData.fetch(associatedAccount);
},
(err) => {
assert.ok(
Expand Down Expand Up @@ -234,7 +234,7 @@ describe("misc", () => {
instructions: [await program.account.dataI8.createInstruction(data)],
signers: [data],
});
const dataAccount = await program.account.dataI8(data.publicKey);
const dataAccount = await program.account.dataI8.fetch(data.publicKey);
assert.ok(dataAccount.data === -3);
});

Expand All @@ -250,14 +250,14 @@ describe("misc", () => {
instructions: [await program.account.dataI16.createInstruction(data)],
signers: [data],
});
const dataAccount = await program.account.dataI16(data.publicKey);
const dataAccount = await program.account.dataI16.fetch(data.publicKey);
assert.ok(dataAccount.data === -2048);

dataPubkey = data.publicKey;
});

it("Can use base58 strings to fetch an account", async () => {
const dataAccount = await program.account.dataI16(dataPubkey.toString());
const dataAccount = await program.account.dataI16.fetch(dataPubkey.toString());
assert.ok(dataAccount.data === -2048);
});
});
6 changes: 3 additions & 3 deletions examples/multisig/tests/multisig.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("multisig", () => {
signers: [multisig],
});

let multisigAccount = await program.account.multisig(multisig.publicKey);
let multisigAccount = await program.account.multisig.fetch(multisig.publicKey);

assert.equal(multisigAccount.nonce, nonce);
assert.ok(multisigAccount.threshold.eq(new anchor.BN(2)));
Expand Down Expand Up @@ -81,7 +81,7 @@ describe("multisig", () => {
signers: [transaction, ownerA],
});

const txAccount = await program.account.transaction(transaction.publicKey);
const txAccount = await program.account.transaction.fetch(transaction.publicKey);

assert.ok(txAccount.programId.equals(pid));
assert.deepEqual(txAccount.accounts, accounts);
Expand Down Expand Up @@ -124,7 +124,7 @@ describe("multisig", () => {
}),
});

multisigAccount = await program.account.multisig(multisig.publicKey);
multisigAccount = await program.account.multisig.fetch(multisig.publicKey);

assert.equal(multisigAccount.nonce, nonce);
assert.ok(multisigAccount.threshold.eq(new anchor.BN(2)));
Expand Down
8 changes: 4 additions & 4 deletions examples/tutorial/basic-1/tests/basic-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe("basic-1", () => {
// #endregion code-separated

// Fetch the newly created account from the cluster.
const account = await program.account.myAccount(myAccount.publicKey);
const account = await program.account.myAccount.fetch(myAccount.publicKey);

// Check it's state was initialized.
assert.ok(account.data.eq(new anchor.BN(1234)));
Expand Down Expand Up @@ -81,7 +81,7 @@ describe("basic-1", () => {
});

// Fetch the newly created account from the cluster.
const account = await program.account.myAccount(myAccount.publicKey);
const account = await program.account.myAccount.fetch(myAccount.publicKey);

// Check it's state was initialized.
assert.ok(account.data.eq(new anchor.BN(1234)));
Expand All @@ -108,7 +108,7 @@ describe("basic-1", () => {
// #endregion code-simplified

// Fetch the newly created account from the cluster.
const account = await program.account.myAccount(myAccount.publicKey);
const account = await program.account.myAccount.fetch(myAccount.publicKey);

// Check it's state was initialized.
assert.ok(account.data.eq(new anchor.BN(1234)));
Expand All @@ -133,7 +133,7 @@ describe("basic-1", () => {
});

// Fetch the newly updated account.
const account = await program.account.myAccount(myAccount.publicKey);
const account = await program.account.myAccount.fetch(myAccount.publicKey);

// Check it's state was mutated.
assert.ok(account.data.eq(new anchor.BN(4321)));
Expand Down
4 changes: 2 additions & 2 deletions examples/tutorial/basic-2/tests/basic-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('basic-2', () => {
instructions: [await program.account.counter.createInstruction(counter)],
})

let counterAccount = await program.account.counter(counter.publicKey)
let counterAccount = await program.account.counter.fetch(counter.publicKey)

assert.ok(counterAccount.authority.equals(provider.wallet.publicKey))
assert.ok(counterAccount.count.toNumber() === 0)
Expand All @@ -37,7 +37,7 @@ describe('basic-2', () => {
},
})

const counterAccount = await program.account.counter(counter.publicKey)
const counterAccount = await program.account.counter.fetch(counter.publicKey)

assert.ok(counterAccount.authority.equals(provider.wallet.publicKey))
assert.ok(counterAccount.count.toNumber() == 1)
Expand Down
Loading