refactor: employ prettier

This commit is contained in:
Michael Vines
2018-11-04 11:41:21 -08:00
parent 9a043344d5
commit 1d6abb17cf
35 changed files with 1498 additions and 856 deletions

View File

@ -1,6 +1,4 @@
module.exports = { // eslint-disable-line import/no-commonjs
'extends': [
"plugin:jest/recommended",
"../.eslintrc.js",
]
module.exports = {
// eslint-disable-line import/no-commonjs
extends: ['plugin:jest/recommended', '../.eslintrc.js'],
};

View File

@ -3,17 +3,17 @@
import fetch from 'node-fetch';
type RpcRequest = {
method: string;
params?: Array<any>;
method: string,
params?: Array<any>,
};
type RpcResponseError = {
message: string;
}
message: string,
};
type RpcResponseResult = any;
type RpcResponse = {
error: ?RpcResponseError;
result: ?RpcResponseResult;
error: ?RpcResponseError,
result: ?RpcResponseResult,
};
export const mockRpc: Array<[string, RpcRequest, RpcResponse]> = [];
@ -26,56 +26,58 @@ let mockNotice = true;
// Suppress lint: 'JestMockFn' is not defined
// eslint-disable-next-line no-undef
const mock: JestMockFn<any, any> = jest.fn(
(fetchUrl, fetchOptions) => {
if (!mockRpcEnabled) {
if (mockNotice) {
console.log(`Note: node-fetch mock is disabled, testing live against ${fetchUrl}`);
mockNotice = false;
}
return fetch(fetchUrl, fetchOptions);
const mock: JestMockFn<any, any> = jest.fn((fetchUrl, fetchOptions) => {
if (!mockRpcEnabled) {
if (mockNotice) {
console.log(
`Note: node-fetch mock is disabled, testing live against ${fetchUrl}`,
);
mockNotice = false;
}
return fetch(fetchUrl, fetchOptions);
}
expect(mockRpc.length).toBeGreaterThanOrEqual(1);
const [mockUrl, mockRequest, mockResponse] = mockRpc.shift();
expect(mockRpc.length).toBeGreaterThanOrEqual(1);
const [mockUrl, mockRequest, mockResponse] = mockRpc.shift();
expect(fetchUrl).toBe(mockUrl);
expect(fetchOptions).toMatchObject({
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
});
expect(fetchOptions.body).toBeDefined();
expect(fetchUrl).toBe(mockUrl);
expect(fetchOptions).toMatchObject({
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
});
expect(fetchOptions.body).toBeDefined();
const body = JSON.parse(fetchOptions.body);
expect(body).toMatchObject(Object.assign(
const body = JSON.parse(fetchOptions.body);
expect(body).toMatchObject(
Object.assign(
{},
{
jsonrpc: '2.0',
method: 'invalid',
},
mockRequest
));
mockRequest,
),
);
const response = Object.assign(
{},
{
jsonrpc: '2.0',
id: body.id,
error: {
message: 'invalid error message',
},
result: 'invalid response',
const response = Object.assign(
{},
{
jsonrpc: '2.0',
id: body.id,
error: {
message: 'invalid error message',
},
mockResponse,
);
return {
text: () => {
return Promise.resolve(JSON.stringify(response));
},
};
}
);
result: 'invalid response',
},
mockResponse,
);
return {
text: () => {
return Promise.resolve(JSON.stringify(response));
},
};
});
export default mock;

View File

@ -13,7 +13,10 @@ export class Client {
//console.log('MockClient', url, options);
if (!mockRpcEnabled) {
if (mockNotice) {
console.log('Note: rpc-websockets mock is disabled, testing live against', url);
console.log(
'Note: rpc-websockets mock is disabled, testing live against',
url,
);
mockNotice = false;
}
this.client = new RpcWebSocketClient(url, options);

View File

@ -10,12 +10,73 @@ test('generate new account', () => {
test('account from secret key', () => {
const secretKey = Buffer.from([
153, 218, 149, 89, 225, 94, 145, 62, 233, 171, 46, 83, 227,
223, 173, 87, 93, 163, 59, 73, 190, 17, 37, 187, 146, 46, 51,
73, 79, 73, 136, 40, 27, 47, 73, 9, 110, 62, 93, 189, 15, 207,
169, 192, 192, 205, 146, 217, 171, 59, 33, 84, 75, 52, 213, 221,
74, 101, 217, 139, 135, 139, 153, 34
153,
218,
149,
89,
225,
94,
145,
62,
233,
171,
46,
83,
227,
223,
173,
87,
93,
163,
59,
73,
190,
17,
37,
187,
146,
46,
51,
73,
79,
73,
136,
40,
27,
47,
73,
9,
110,
62,
93,
189,
15,
207,
169,
192,
192,
205,
146,
217,
171,
59,
33,
84,
75,
52,
213,
221,
74,
101,
217,
139,
135,
139,
153,
34,
]);
const account = new Account(secretKey);
expect(account.publicKey.toBase58()).toBe('2q7pyhPwAwZ3QMfZrnAbDhnh9mDUqycszcpf86VgQxhF');
expect(account.publicKey.toBase58()).toBe(
'2q7pyhPwAwZ3QMfZrnAbDhnh9mDUqycszcpf86VgQxhF',
);
});

View File

@ -33,4 +33,3 @@ test('load BPF program', async () => {
});
await sendAndConfirmTransaction(connection, from, transaction);
});

View File

@ -74,4 +74,3 @@ test('apply', () => {
expect(transaction.keys).toHaveLength(3);
// TODO: Validate transaction contents more
});

View File

@ -17,7 +17,6 @@ if (!mockRpcEnabled) {
jest.setTimeout(15000);
}
const errorMessage = 'Invalid request';
const errorResponse = {
error: {
@ -26,7 +25,6 @@ const errorResponse = {
result: undefined,
};
test('get account info - error', () => {
const account = new Account();
const connection = new Connection(url);
@ -40,11 +38,11 @@ test('get account info - error', () => {
errorResponse,
]);
expect(connection.getAccountInfo(account.publicKey))
.rejects.toThrow(errorMessage);
expect(connection.getAccountInfo(account.publicKey)).rejects.toThrow(
errorMessage,
);
});
test('get balance', async () => {
const account = new Account();
const connection = new Connection(url);
@ -58,7 +56,7 @@ test('get balance', async () => {
{
error: null,
result: 0,
}
},
]);
const balance = await connection.getBalance(account.publicKey);
@ -77,11 +75,11 @@ test('confirm transaction - error', () => {
params: [badTransactionSignature],
},
errorResponse,
]
);
]);
expect(connection.confirmTransaction(badTransactionSignature))
.rejects.toThrow(errorMessage);
expect(
connection.confirmTransaction(badTransactionSignature),
).rejects.toThrow(errorMessage);
mockRpc.push([
url,
@ -90,14 +88,13 @@ test('confirm transaction - error', () => {
params: [badTransactionSignature],
},
errorResponse,
]
);
]);
expect(connection.getSignatureStatus(badTransactionSignature))
.rejects.toThrow(errorMessage);
expect(
connection.getSignatureStatus(badTransactionSignature),
).rejects.toThrow(errorMessage);
});
test('get transaction count', async () => {
const connection = new Connection(url);
@ -110,9 +107,8 @@ test('get transaction count', async () => {
{
error: null,
result: 1000000,
}
]
);
},
]);
const count = await connection.getTransactionCount();
expect(count).toBeGreaterThanOrEqual(0);
@ -139,9 +135,8 @@ test('get finality', async () => {
{
error: null,
result: 123,
}
]
);
},
]);
const finality = await connection.getFinality();
expect(finality).toBeGreaterThanOrEqual(0);
@ -159,8 +154,9 @@ test('request airdrop', async () => {
},
{
error: null,
result: '1WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
}
result:
'1WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
]);
mockRpc.push([
url,
@ -170,8 +166,9 @@ test('request airdrop', async () => {
},
{
error: null,
result: '2WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
}
result:
'2WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
]);
mockRpc.push([
url,
@ -182,7 +179,7 @@ test('request airdrop', async () => {
{
error: null,
result: 42,
}
},
]);
await connection.requestAirdrop(account.publicKey, 40);
@ -201,18 +198,78 @@ test('request airdrop', async () => {
error: null,
result: {
program_id: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
],
tokens: 42,
userdata: [],
executable: false,
loader_program_id: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
],
}
}
},
},
]);
const accountInfo = await connection.getAccountInfo(account.publicKey);
@ -234,8 +291,9 @@ test('transaction', async () => {
},
{
error: null,
result: '0WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
}
result:
'0WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
]);
mockRpc.push([
url,
@ -246,7 +304,7 @@ test('transaction', async () => {
{
error: null,
result: 12,
}
},
]);
await connection.requestAirdrop(accountFrom.publicKey, 12);
expect(await connection.getBalance(accountFrom.publicKey)).toBe(12);
@ -259,8 +317,9 @@ test('transaction', async () => {
},
{
error: null,
result: '8WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
}
result:
'8WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
]);
mockRpc.push([
url,
@ -271,7 +330,7 @@ test('transaction', async () => {
{
error: null,
result: 21,
}
},
]);
await connection.requestAirdrop(accountTo.publicKey, 21);
expect(await connection.getBalance(accountTo.publicKey)).toBe(21);
@ -284,15 +343,15 @@ test('transaction', async () => {
},
{
error: null,
result: '3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
}
]
);
result:
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
]);
const transaction = SystemProgram.move(
accountFrom.publicKey,
accountTo.publicKey,
10
10,
);
const signature = await connection.sendTransaction(accountFrom, transaction);
@ -301,15 +360,14 @@ test('transaction', async () => {
{
method: 'confirmTransaction',
params: [
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk'
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
],
},
{
error: null,
result: true,
}
]
);
},
]);
let i = 0;
for (;;) {
@ -327,16 +385,17 @@ test('transaction', async () => {
{
method: 'getSignatureStatus',
params: [
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk'
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
],
},
{
error: null,
result: 'Confirmed',
}
]
},
]);
await expect(connection.getSignatureStatus(signature)).resolves.toBe(
'Confirmed',
);
await expect(connection.getSignatureStatus(signature)).resolves.toBe('Confirmed');
mockRpc.push([
url,
@ -347,7 +406,7 @@ test('transaction', async () => {
{
error: null,
result: 2,
}
},
]);
expect(await connection.getBalance(accountFrom.publicKey)).toBe(2);
@ -360,12 +419,11 @@ test('transaction', async () => {
{
error: null,
result: 31,
}
},
]);
expect(await connection.getBalance(accountTo.publicKey)).toBe(31);
});
test('multi-instruction transaction', async () => {
if (mockRpcEnabled) {
console.log('non-live test skipped');
@ -387,13 +445,8 @@ test('multi-instruction transaction', async () => {
const transaction = SystemProgram.move(
accountFrom.publicKey,
accountTo.publicKey,
10
)
.add(SystemProgram.move(
accountTo.publicKey,
accountFrom.publicKey,
10
));
10,
).add(SystemProgram.move(accountTo.publicKey, accountFrom.publicKey, 10));
const signature = await connection.sendTransaction(accountFrom, transaction);
let i = 0;
@ -406,13 +459,14 @@ test('multi-instruction transaction', async () => {
expect(++i).toBeLessThan(10);
await sleep(500);
}
await expect(connection.getSignatureStatus(signature)).resolves.toBe('Confirmed');
await expect(connection.getSignatureStatus(signature)).resolves.toBe(
'Confirmed',
);
expect(await connection.getBalance(accountFrom.publicKey)).toBe(12);
expect(await connection.getBalance(accountTo.publicKey)).toBe(21);
});
test('account change notification', async () => {
if (mockRpcEnabled) {
console.log('non-live test skipped');
@ -425,7 +479,10 @@ test('account change notification', async () => {
const mockCallback = jest.fn();
const subscriptionId = connection.onAccountChange(programAccount.publicKey, mockCallback);
const subscriptionId = connection.onAccountChange(
programAccount.publicKey,
mockCallback,
);
await connection.requestAirdrop(owner.publicKey, 42);
const transaction = SystemProgram.createAccount(
@ -442,7 +499,6 @@ test('account change notification', async () => {
await connection.removeAccountChangeListener(subscriptionId);
// mockCallback should be called twice
//
// retry a couple times if needed
@ -462,10 +518,13 @@ test('account change notification', async () => {
// First mockCallback call is due to SystemProgram.createAccount()
expect(mockCallback.mock.calls[0][0].tokens).toBe(42);
expect(mockCallback.mock.calls[0][0].executable).toBe(false);
expect(mockCallback.mock.calls[0][0].userdata).toEqual(Buffer.from([0, 0, 0]));
expect(mockCallback.mock.calls[0][0].userdata).toEqual(
Buffer.from([0, 0, 0]),
);
expect(mockCallback.mock.calls[0][0].programId).toEqual(BpfLoader.programId);
// Second mockCallback call is due to loader.load()
expect(mockCallback.mock.calls[1][0].userdata).toEqual(Buffer.from([1, 2, 3]));
expect(mockCallback.mock.calls[1][0].userdata).toEqual(
Buffer.from([1, 2, 3]),
);
});

View File

@ -1,8 +1,6 @@
// @flow
import {
Account,
} from '../../src';
import {Account} from '../../src';
import {url} from '../url';
import {mockRpc} from '../__mocks__/node-fetch';
@ -18,6 +16,6 @@ export function mockGetLastId() {
{
error: null,
result: lastId.publicKey.toBase58(),
}
},
]);
}

View File

@ -31,4 +31,3 @@ test('load native program', async () => {
await sendAndConfirmTransaction(connection, from, transaction);
});

View File

@ -1,13 +1,13 @@
// @flow
import {
Account,
Connection,
} from '../src';
import {Account, Connection} from '../src';
import {mockRpc} from './__mocks__/node-fetch';
import {url} from './url';
export async function newAccountWithTokens(connection: Connection, amount: number = 10): Promise<Account> {
export async function newAccountWithTokens(
connection: Connection,
amount: number = 10,
): Promise<Account> {
const account = new Account();
{
@ -20,13 +20,12 @@ export async function newAccountWithTokens(connection: Connection, amount: numbe
{
error: null,
// Signature doesn't matter
result: '3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
}
result:
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
]);
}
await connection.requestAirdrop(account.publicKey, amount);
return account;
}

View File

@ -4,39 +4,113 @@ import {PublicKey} from '../src/publickey';
test('invalid', () => {
expect(() => {
new PublicKey([
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
]);
}).toThrow();
expect(() => {
new PublicKey('0x300000000000000000000000000000000000000000000000000000000000000000000');
new PublicKey(
'0x300000000000000000000000000000000000000000000000000000000000000000000',
);
}).toThrow();
expect(() => {
new PublicKey('135693854574979916511997248057056142015550763280047535983739356259273198796800000');
new PublicKey(
'135693854574979916511997248057056142015550763280047535983739356259273198796800000',
);
}).toThrow();
});
test('equals', () => {
const arrayKey = new PublicKey([
3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
3,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
]);
const hexKey = new PublicKey('0x300000000000000000000000000000000000000000000000000000000000000');
const base56Key = new PublicKey('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
const hexKey = new PublicKey(
'0x300000000000000000000000000000000000000000000000000000000000000',
);
const base56Key = new PublicKey(
'CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3',
);
expect(arrayKey.equals(hexKey)).toBe(true);
expect(arrayKey.equals(base56Key)).toBe(true);
});
test('isPublicKey', () => {
const key = new PublicKey('0x100000000000000000000000000000000000000000000000000000000000000');
const key = new PublicKey(
'0x100000000000000000000000000000000000000000000000000000000000000',
);
expect(PublicKey.isPublicKey(key)).toBe(true);
expect(PublicKey.isPublicKey({})).toBe(false);
});
test('toBase58', () => {
const key = new PublicKey('0x300000000000000000000000000000000000000000000000000000000000000');
const key = new PublicKey(
'0x300000000000000000000000000000000000000000000000000000000000000',
);
expect(key.toBase58()).toBe('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
expect(key.toString()).toBe('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
@ -48,28 +122,92 @@ test('toBase58', () => {
expect(key3.toBase58()).toBe('11111111111111111111111111111111');
const key4 = new PublicKey([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
]);
expect(key4.toBase58()).toBe('11111111111111111111111111111111');
});
test('toBuffer', () => {
const key = new PublicKey('0x300000000000000000000000000000000000000000000000000000000000000');
const key = new PublicKey(
'0x300000000000000000000000000000000000000000000000000000000000000',
);
expect(key.toBuffer()).toHaveLength(32);
expect(key.toBase58()).toBe('CiDwVBFgWV9E5MvXWoLgnEgn2hK7rJikbvfWavzAQz3');
const key2 = new PublicKey('0x000000000000000000000000000000000000000000000000000000000000000');
const key2 = new PublicKey(
'0x000000000000000000000000000000000000000000000000000000000000000',
);
expect(key2.toBuffer()).toHaveLength(32);
expect(key2.toBase58()).toBe('11111111111111111111111111111111');
});
test('equals (II)', () => {
const key1 = new PublicKey([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
]);
const key2 = new PublicKey(key1.toBuffer());
expect(key1.equals(key2)).toBe(true);
});

View File

@ -1,10 +1,6 @@
// @flow
import {
Account,
BudgetProgram,
SystemProgram,
} from '../src';
import {Account, BudgetProgram, SystemProgram} from '../src';
test('createAccount', () => {
const from = new Account();
@ -29,30 +25,21 @@ test('move', () => {
const to = new Account();
let transaction;
transaction = SystemProgram.move(
from.publicKey,
to.publicKey,
123,
);
transaction = SystemProgram.move(from.publicKey, to.publicKey, 123);
expect(transaction.keys).toHaveLength(2);
expect(transaction.programId).toEqual(SystemProgram.programId);
// TODO: Validate transaction contents more
});
test('assign', () => {
const from = new Account();
const to = new Account();
let transaction;
transaction = SystemProgram.assign(
from.publicKey,
to.publicKey,
);
transaction = SystemProgram.assign(from.publicKey, to.publicKey);
expect(transaction.keys).toHaveLength(1);
expect(transaction.programId).toEqual(SystemProgram.programId);
// TODO: Validate transaction contents more
});

View File

@ -1,11 +1,6 @@
// @flow
import {
Connection,
PublicKey,
Token,
TokenAmount,
} from '../src';
import {Connection, PublicKey, Token, TokenAmount} from '../src';
import {SYSTEM_TOKEN_PROGRAM_ID} from '../src/token-program';
import {mockRpc, mockRpcEnabled} from './__mocks__/node-fetch';
import {url} from './url';
@ -37,12 +32,12 @@ function mockSendTransaction() {
},
{
error: null,
result: '3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
}
result:
'3WE5w4B7v59x6qjyC4FbG2FEKYKQfvsJwqSxNVmtMjT8TQ31hsZieDHcSgqzxiAoTL56n2w5TncjqEKjLhtF4Vk',
},
]);
}
// A token created by the first test and used by all subsequent tests
let testToken: Token;
@ -86,7 +81,7 @@ test('create new token', async () => {
new TokenAmount(10000),
'Test token',
'TEST',
2
2,
);
{
@ -104,18 +99,83 @@ test('create new token', async () => {
tokens: 1,
userdata: [
1,
16, 39, 0, 0, 0, 0, 0, 0,
16,
39,
0,
0,
0,
0,
0,
0,
2,
10, 0, 0, 0, 0, 0, 0, 0, 84, 101, 115, 116, 32, 116, 111, 107, 101, 110,
4, 0, 0, 0, 0, 0, 0, 0, 84, 69, 83, 84
10,
0,
0,
0,
0,
0,
0,
0,
84,
101,
115,
116,
32,
116,
111,
107,
101,
110,
4,
0,
0,
0,
0,
0,
0,
0,
84,
69,
83,
84,
],
executable: false,
loader_program_id: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
],
}
}
},
},
]);
}
@ -126,7 +186,6 @@ test('create new token', async () => {
expect(tokenInfo.name).toBe('Test token');
expect(tokenInfo.symbol).toBe('TEST');
{
// mock Token.accountInfo()'s getAccountInfo
mockRpc.push([
@ -144,16 +203,84 @@ test('create new token', async () => {
2,
...testToken.token.toBuffer(),
...initialOwner.publicKey.toBuffer(),
16, 39, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16,
39,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
],
executable: false,
loader_program_id: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
],
}
}
},
},
]);
}
@ -166,7 +293,6 @@ test('create new token', async () => {
expect(accountInfo.originalAmount.toNumber()).toBe(0);
});
test('create new token account', async () => {
const connection = new Connection(url);
connection._disableLastIdCaching = mockRpcEnabled;
@ -202,16 +328,53 @@ test('create new token account', async () => {
2,
...testToken.token.toBuffer(),
...destOwner.publicKey.toBuffer(),
0, 0, 0, 0, 0, 0, 0, 0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
],
executable: false,
loader_program_id: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
],
}
}
},
},
]);
}
@ -223,7 +386,6 @@ test('create new token account', async () => {
expect(accountInfo.source).toBe(null);
});
test('transfer', async () => {
const connection = new Connection(url);
connection._disableLastIdCaching = mockRpcEnabled;
@ -260,16 +422,53 @@ test('transfer', async () => {
2,
...testToken.token.toBuffer(),
...initialOwner.publicKey.toBuffer(),
123, 0, 0, 0, 0, 0, 0, 0,
123,
0,
0,
0,
0,
0,
0,
0,
0,
],
executable: false,
loader_program_id: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
],
}
}
},
},
]);
// mock Token.transfer() transaction
@ -278,12 +477,7 @@ test('transfer', async () => {
mockGetSignatureStatus();
}
await testToken.transfer(
initialOwner,
initialOwnerTokenAccount,
dest,
123
);
await testToken.transfer(initialOwner, initialOwnerTokenAccount, dest, 123);
{
// mock Token.accountInfo()'s getAccountInfo
@ -302,16 +496,53 @@ test('transfer', async () => {
2,
...testToken.token.toBuffer(),
...dest.toBuffer(),
123, 0, 0, 0, 0, 0, 0, 0,
123,
0,
0,
0,
0,
0,
0,
0,
0,
],
executable: false,
loader_program_id: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
],
}
}
},
},
]);
}
@ -319,7 +550,6 @@ test('transfer', async () => {
expect(destAccountInfo.amount.toNumber()).toBe(123);
});
test('approve/revoke', async () => {
const connection = new Connection(url);
connection._disableLastIdCaching = mockRpcEnabled;
@ -336,7 +566,10 @@ test('approve/revoke', async () => {
mockSendTransaction();
mockGetSignatureStatus();
}
const delegate = await testToken.newAccount(delegateOwner, initialOwnerTokenAccount);
const delegate = await testToken.newAccount(
delegateOwner,
initialOwnerTokenAccount,
);
{
// mock Token.approve() transaction
@ -349,7 +582,7 @@ test('approve/revoke', async () => {
initialOwner,
initialOwnerTokenAccount,
delegate,
456
456,
);
{
@ -369,18 +602,62 @@ test('approve/revoke', async () => {
2,
...testToken.token.toBuffer(),
...delegate.toBuffer(),
200, 1, 0, 0, 0, 0, 0, 0,
200,
1,
0,
0,
0,
0,
0,
0,
1,
...initialOwnerTokenAccount.toBuffer(),
200, 1, 0, 0, 0, 0, 0, 0,
200,
1,
0,
0,
0,
0,
0,
0,
],
executable: false,
loader_program_id: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
],
}
}
},
},
]);
}
@ -391,7 +668,9 @@ test('approve/revoke', async () => {
if (delegateAccountInfo.source === null) {
throw new Error('source should not be null');
} else {
expect(delegateAccountInfo.source.equals(initialOwnerTokenAccount)).toBe(true);
expect(delegateAccountInfo.source.equals(initialOwnerTokenAccount)).toBe(
true,
);
}
{
@ -401,11 +680,7 @@ test('approve/revoke', async () => {
mockGetSignatureStatus();
}
await testToken.revoke(
initialOwner,
initialOwnerTokenAccount,
delegate,
);
await testToken.revoke(initialOwner, initialOwnerTokenAccount, delegate);
{
// mock Token.accountInfo()'s getAccountInfo
@ -424,18 +699,62 @@ test('approve/revoke', async () => {
2,
...testToken.token.toBuffer(),
...delegate.toBuffer(),
0, 0, 0, 0, 0, 0, 0, 0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
...initialOwnerTokenAccount.toBuffer(),
0, 0, 0, 0, 0, 0, 0, 0,
0,
0,
0,
0,
0,
0,
0,
0,
],
executable: false,
loader_program_id: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
],
}
}
},
},
]);
}
@ -445,11 +764,12 @@ test('approve/revoke', async () => {
if (delegateAccountInfo.source === null) {
throw new Error('source should not be null');
} else {
expect(delegateAccountInfo.source.equals(initialOwnerTokenAccount)).toBe(true);
expect(delegateAccountInfo.source.equals(initialOwnerTokenAccount)).toBe(
true,
);
}
});
test('invalid approve', async () => {
if (mockRpcEnabled) {
console.log('non-live test skipped');
@ -466,26 +786,15 @@ test('invalid approve', async () => {
// account2 is not a delegate account of account1
await expect(
testToken.approve(
owner,
account1,
account2,
123
)
testToken.approve(owner, account1, account2, 123),
).rejects.toThrow();
// account1Delegate is not a delegate account of account2
await expect(
testToken.approve(
owner,
account2,
account1Delegate,
123
)
testToken.approve(owner, account2, account1Delegate, 123),
).rejects.toThrow();
});
test('fail on approve overspend', async () => {
if (mockRpcEnabled) {
console.log('non-live test skipped');
@ -507,50 +816,29 @@ test('fail on approve overspend', async () => {
10,
);
await testToken.approve(
owner,
account1,
account1Delegate,
2
);
await testToken.approve(owner, account1, account1Delegate, 2);
let delegateAccountInfo = await testToken.accountInfo(account1Delegate);
expect(delegateAccountInfo.amount.toNumber()).toBe(2);
expect(delegateAccountInfo.originalAmount.toNumber()).toBe(2);
await testToken.transfer(
owner,
account1Delegate,
account2,
1,
);
await testToken.transfer(owner, account1Delegate, account2, 1);
delegateAccountInfo = await testToken.accountInfo(account1Delegate);
expect(delegateAccountInfo.amount.toNumber()).toBe(1);
expect(delegateAccountInfo.originalAmount.toNumber()).toBe(2);
await testToken.transfer(
owner,
account1Delegate,
account2,
1,
);
await testToken.transfer(owner, account1Delegate, account2, 1);
delegateAccountInfo = await testToken.accountInfo(account1Delegate);
expect(delegateAccountInfo.amount.toNumber()).toBe(0);
expect(delegateAccountInfo.originalAmount.toNumber()).toBe(2);
await expect(
testToken.transfer(
owner,
account1Delegate,
account2,
1,
)
testToken.transfer(owner, account1Delegate, account2, 1),
).rejects.toThrow();
});
test('set owner', async () => {
if (mockRpcEnabled) {
console.log('non-live test skipped');
@ -566,12 +854,11 @@ test('set owner', async () => {
await testToken.setOwner(owner, account, newOwner.publicKey);
await expect(
testToken.setOwner(owner, account, newOwner.publicKey)
testToken.setOwner(owner, account, newOwner.publicKey),
).rejects.toThrow();
await testToken.setOwner(newOwner, account, owner.publicKey);
await expect(
testToken.setOwner(newOwner, account, owner.publicKey)
testToken.setOwner(newOwner, account, owner.publicKey),
).rejects.toThrow();
});

View File

@ -9,4 +9,3 @@ export const url = 'http://localhost:8899/';
//export const url = 'https://api.testnet.solana.com/';
//export const url = 'https://api.testnet.solana.com/';
//export const url = 'http://testnet.solana.com:8899/';