Add most of sendTokens() implementation
This commit is contained in:
parent
ac976a66f0
commit
41a658c336
|
@ -18,7 +18,7 @@ export type TransactionSignature = string;
|
||||||
*/
|
*/
|
||||||
export type TransactionId = string;
|
export type TransactionId = string;
|
||||||
|
|
||||||
type RpcRequest = (methodName: string, args: Array<string|number>) => any;
|
type RpcRequest = (methodName: string, args: Array<any>) => any;
|
||||||
|
|
||||||
function createRpcRequest(url): RpcRequest {
|
function createRpcRequest(url): RpcRequest {
|
||||||
const server = jayson(
|
const server = jayson(
|
||||||
|
@ -94,12 +94,12 @@ const RequestAirdropRpcResult = struct({
|
||||||
error: 'any?',
|
error: 'any?',
|
||||||
result: 'boolean?',
|
result: 'boolean?',
|
||||||
});
|
});
|
||||||
|
const SendTokensRpcResult = struct({
|
||||||
function sleep(duration = 0) {
|
jsonrpc: struct.literal('2.0'),
|
||||||
return new Promise((accept) => {
|
id: 'string',
|
||||||
setTimeout(accept, duration);
|
error: 'any?',
|
||||||
});
|
result: 'string?',
|
||||||
}
|
});
|
||||||
|
|
||||||
export class Connection {
|
export class Connection {
|
||||||
_rpcRequest: RpcRequest;
|
_rpcRequest: RpcRequest;
|
||||||
|
@ -182,11 +182,17 @@ export class Connection {
|
||||||
// TODO: This is not the correct transaction payload
|
// TODO: This is not the correct transaction payload
|
||||||
`Transaction ${from.publicKey} ${to} ${amount}`
|
`Transaction ${from.publicKey} ${to} ${amount}`
|
||||||
);
|
);
|
||||||
const signature = nacl.sign.detached(transaction, from.secretKey);
|
const signedTransaction = nacl.sign.detached(transaction, from.secretKey);
|
||||||
|
console.log(typeof signedTransaction);
|
||||||
console.log('Created signature of length', signature.length);
|
console.log([...signedTransaction]);
|
||||||
await sleep(500); // TODO: transmit transaction + signature
|
const unsafeRes = await this._rpcRequest('sendTransaction', [[...signedTransaction]]);
|
||||||
throw new Error(`Unable to send ${amount} tokens to ${to}`);
|
const res = SendTokensRpcResult(unsafeRes);
|
||||||
|
if (res.error) {
|
||||||
|
throw new Error(res.error.message);
|
||||||
|
}
|
||||||
|
assert(typeof res.result !== 'undefined');
|
||||||
|
assert(res.result);
|
||||||
|
return res.result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ test.skip('get balance', async () => {
|
||||||
expect(balance).toBeGreaterThanOrEqual(0);
|
expect(balance).toBeGreaterThanOrEqual(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
test.skip('throws on bad transaction', () => {
|
test.skip('throws on bad transaction confirmation', () => {
|
||||||
const connection = new Connection(url);
|
const connection = new Connection(url);
|
||||||
|
|
||||||
expect(connection.confirmTransaction('bad transaction signature'))
|
expect(connection.confirmTransaction('bad transaction signature'))
|
||||||
|
@ -55,4 +55,12 @@ test.skip('request airdrop', async () => {
|
||||||
expect(balance).toBe(42);
|
expect(balance).toBe(42);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test.skip('throws on bad transaction', () => {
|
||||||
|
const account = new Account();
|
||||||
|
const connection = new Connection(url);
|
||||||
|
|
||||||
|
expect(connection.sendTokens(account, account.publicKey, 123))
|
||||||
|
.rejects.toThrow('Invalid request');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue