Skip to content

Commit

Permalink
Flesh out @solana/spl-token with token2 instructions (solana-labs#410)
Browse files Browse the repository at this point in the history
* Fix token-js docs

* Add Freeze/Thaw instructions

* Add v2 instructions

* Add new instructions to js test

* Bump @solana/spl-token to v0.0.8

* Fix didThrow and remove invalidApprove
  • Loading branch information
CriesofCarrots authored Sep 10, 2020
1 parent 708bb43 commit 7141298
Show file tree
Hide file tree
Showing 4 changed files with 785 additions and 54 deletions.
15 changes: 12 additions & 3 deletions token/js/cli/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import {
createMint,
createAccount,
transfer,
transfer2,
approveRevoke,
invalidApprove,
failOnApproveOverspend,
setAuthority,
mintTo,
mintTo2,
multisig,
burn,
burn2,
freezeThawAccount,
closeAccount,
nativeToken,
} from './token-test';
Expand All @@ -29,18 +32,24 @@ async function main() {
await createAccount();
console.log('Run test: mintTo');
await mintTo();
console.log('Run test: mintTo2');
await mintTo2();
console.log('Run test: transfer');
await transfer();
console.log('Run test: transfer2');
await transfer2();
console.log('Run test: approveRevoke');
await approveRevoke();
console.log('Run test: invalidApprove');
await invalidApprove();
console.log('Run test: failOnApproveOverspend');
await failOnApproveOverspend();
console.log('Run test: setAuthority');
await setAuthority();
console.log('Run test: burn');
await burn();
console.log('Run test: burn2');
await burn2();
console.log('Run test: freezeThawAccount');
await freezeThawAccount();
console.log('Run test: closeAccount');
await closeAccount();
console.log('Run test: multisig');
Expand Down
145 changes: 122 additions & 23 deletions token/js/cli/token-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ function assert(condition, message) {
}
}

async function didThrow(func, args): Promise<boolean> {
async function didThrow(obj, func, args): Promise<boolean> {
try {
await func.apply(args);
await func.apply(testToken, args);
} catch (e) {
return true;
}
Expand Down Expand Up @@ -125,7 +125,7 @@ export async function createMint(): Promise<void> {
connection,
payer,
testMintAuthority.publicKey,
null,
testMintAuthority.publicKey,
2,
programId,
);
Expand All @@ -139,7 +139,11 @@ export async function createMint(): Promise<void> {
assert(mintInfo.supply.toNumber() === 0);
assert(mintInfo.decimals === 2);
assert(mintInfo.isInitialized === true);
assert(mintInfo.freezeAuthority === null);
if (mintInfo.freezeAuthority !== null) {
assert(mintInfo.freezeAuthority.equals(testMintAuthority.publicKey));
} else {
assert(mintInfo.freezeAuthority !== null);
}
}

export async function createAccount(): Promise<void> {
Expand Down Expand Up @@ -168,20 +172,67 @@ export async function mintTo(): Promise<void> {
assert(accountInfo.amount.toNumber() === 1000);
}

export async function mintTo2(): Promise<void> {
assert(
await didThrow(testToken, testToken.mintTo2, [
testAccount,
testMintAuthority,
[],
1000,
1,
]),
);

await testToken.mintTo2(testAccount, testMintAuthority, [], 1000, 2);

const mintInfo = await testToken.getMintInfo();
assert(mintInfo.supply.toNumber() === 2000);

const accountInfo = await testToken.getAccountInfo(testAccount);
assert(accountInfo.amount.toNumber() === 2000);
}

export async function transfer(): Promise<void> {
const destOwner = new Account();
const dest = await testToken.createAccount(destOwner.publicKey);

await testToken.transfer(testAccount, dest, testAccountOwner, [], 100);

const mintInfo = await testToken.getMintInfo();
assert(mintInfo.supply.toNumber() === 1000);
assert(mintInfo.supply.toNumber() === 2000);

let destAccountInfo = await testToken.getAccountInfo(dest);
assert(destAccountInfo.amount.toNumber() === 100);

let testAccountInfo = await testToken.getAccountInfo(testAccount);
assert(testAccountInfo.amount.toNumber() === 1900);
}

export async function transfer2(): Promise<void> {
const destOwner = new Account();
const dest = await testToken.createAccount(destOwner.publicKey);

assert(
await didThrow(testToken, testToken.transfer2, [
testAccount,
dest,
testAccountOwner,
[],
100,
1,
]),
);

await testToken.transfer2(testAccount, dest, testAccountOwner, [], 100, 2);

const mintInfo = await testToken.getMintInfo();
assert(mintInfo.supply.toNumber() === 2000);

let destAccountInfo = await testToken.getAccountInfo(dest);
assert(destAccountInfo.amount.toNumber() === 100);

let testAccountInfo = await testToken.getAccountInfo(testAccount);
assert(testAccountInfo.amount.toNumber() === 900);
assert(testAccountInfo.amount.toNumber() === 1800);
}

export async function approveRevoke(): Promise<void> {
Expand All @@ -206,18 +257,6 @@ export async function approveRevoke(): Promise<void> {
}
}

export async function invalidApprove(): Promise<void> {
const owner = new Account();
const account1 = await testToken.createAccount(owner.publicKey);
const account2 = await testToken.createAccount(owner.publicKey);
const delegate = new Account();

// account2 is not a delegate account of account1
assert(didThrow(testToken.approve, [account1, account2, owner, [], 123]));
// account1Delegate is not a delegate account of account2
assert(didThrow(testToken.approve, [account2, delegate, owner, [], 123]));
}

export async function failOnApproveOverspend(): Promise<void> {
const owner = new Account();
const account1 = await testToken.createAccount(owner.publicKey);
Expand All @@ -232,7 +271,7 @@ export async function failOnApproveOverspend(): Promise<void> {
assert(account1Info.amount.toNumber() == 10);
assert(account1Info.delegatedAmount.toNumber() == 2);
if (account1Info.delegate === null) {
throw new Error('deleage should not be null');
throw new Error('delegate should not be null');
} else {
assert(account1Info.delegate.equals(delegate.publicKey));
}
Expand All @@ -250,7 +289,15 @@ export async function failOnApproveOverspend(): Promise<void> {
assert(account1Info.delegate === null);
assert(account1Info.delegatedAmount.toNumber() == 0);

assert(didThrow(testToken.transfer, [account1, account2, delegate, [], 1]));
assert(
await didThrow(testToken, testToken.transfer, [
account1,
account2,
delegate,
[],
1,
]),
);
}

export async function setAuthority(): Promise<void> {
Expand All @@ -263,8 +310,8 @@ export async function setAuthority(): Promise<void> {
[],
);
assert(
didThrow(testToken.setAuthority, [
testAccountOwner,
await didThrow(testToken, testToken.setAuthority, [
testAccount,
newOwner.publicKey,
'AccountOwner',
testAccountOwner,
Expand All @@ -290,6 +337,53 @@ export async function burn(): Promise<void> {
assert(accountInfo.amount.toNumber() == amount - 1);
}

export async function burn2(): Promise<void> {
let accountInfo = await testToken.getAccountInfo(testAccount);
const amount = accountInfo.amount.toNumber();

assert(
await didThrow(testToken, testToken.burn2, [
testAccount,
testAccountOwner,
[],
1,
1,
]),
);

await testToken.burn2(testAccount, testAccountOwner, [], 1, 2);

accountInfo = await testToken.getAccountInfo(testAccount);
assert(accountInfo.amount.toNumber() == amount - 1);
}

export async function freezeThawAccount(): Promise<void> {
let accountInfo = await testToken.getAccountInfo(testAccount);
const amount = accountInfo.amount.toNumber();

await testToken.freezeAccount(testAccount, testMintAuthority, []);

const destOwner = new Account();
const dest = await testToken.createAccount(destOwner.publicKey);

assert(
await didThrow(testToken, testToken.transfer, [
testAccount,
dest,
testAccountOwner,
[],
100,
]),
);

await testToken.thawAccount(testAccount, testMintAuthority, []);

await testToken.transfer(testAccount, dest, testAccountOwner, [], 100);

let testAccountInfo = await testToken.getAccountInfo(testAccount);
assert(testAccountInfo.amount.toNumber() === amount - 100);
}

export async function closeAccount(): Promise<void> {
const closeAuthority = new Account();

Expand All @@ -312,7 +406,12 @@ export async function closeAccount(): Promise<void> {

// Check that accounts with non-zero token balance cannot be closed
assert(
didThrow(testToken.closeAccount, [testAccount, dest, closeAuthority, []]),
await didThrow(testToken, testToken.closeAccount, [
testAccount,
dest,
closeAuthority,
[],
]),
);

const connection = await getConnection();
Expand Down
Loading

0 comments on commit 7141298

Please sign in to comment.