Skip to content

Commit 5d6ea36

Browse files
committed
Add most of sendTokens() implementation
1 parent bf5592b commit 5d6ea36

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/connection.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export type TransactionSignature = string;
1818
*/
1919
export type TransactionId = string;
2020

21-
type RpcRequest = (methodName: string, args: Array<string|number>) => any;
21+
type RpcRequest = (methodName: string, args: Array<any>) => any;
2222

2323
function createRpcRequest(url): RpcRequest {
2424
const server = jayson(
@@ -94,12 +94,12 @@ const RequestAirdropRpcResult = struct({
9494
error: 'any?',
9595
result: 'boolean?',
9696
});
97-
98-
function sleep(duration = 0) {
99-
return new Promise((accept) => {
100-
setTimeout(accept, duration);
101-
});
102-
}
97+
const SendTokensRpcResult = struct({
98+
jsonrpc: struct.literal('2.0'),
99+
id: 'string',
100+
error: 'any?',
101+
result: 'string?',
102+
});
103103

104104
export class Connection {
105105
_rpcRequest: RpcRequest;
@@ -182,11 +182,17 @@ export class Connection {
182182
// TODO: This is not the correct transaction payload
183183
`Transaction ${from.publicKey} ${to} ${amount}`
184184
);
185-
const signature = nacl.sign.detached(transaction, from.secretKey);
186-
187-
console.log('Created signature of length', signature.length);
188-
await sleep(500); // TODO: transmit transaction + signature
189-
throw new Error(`Unable to send ${amount} tokens to ${to}`);
185+
const signedTransaction = nacl.sign.detached(transaction, from.secretKey);
186+
console.log(typeof signedTransaction);
187+
console.log([...signedTransaction]);
188+
const unsafeRes = await this._rpcRequest('sendTransaction', [[...signedTransaction]]);
189+
const res = SendTokensRpcResult(unsafeRes);
190+
if (res.error) {
191+
throw new Error(res.error.message);
192+
}
193+
assert(typeof res.result !== 'undefined');
194+
assert(res.result);
195+
return res.result;
190196
}
191197
}
192198

test/connection.test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test.skip('get balance', async () => {
1414
expect(balance).toBeGreaterThanOrEqual(0);
1515
});
1616

17-
test.skip('throws on bad transaction', () => {
17+
test.skip('throws on bad transaction confirmation', () => {
1818
const connection = new Connection(url);
1919

2020
expect(connection.confirmTransaction('bad transaction signature'))
@@ -55,4 +55,12 @@ test.skip('request airdrop', async () => {
5555
expect(balance).toBe(42);
5656
});
5757

58+
test.skip('throws on bad transaction', () => {
59+
const account = new Account();
60+
const connection = new Connection(url);
61+
62+
expect(connection.sendTokens(account, account.publicKey, 123))
63+
.rejects.toThrow('Invalid request');
64+
});
65+
5866

0 commit comments

Comments
 (0)