@@ -18,7 +18,7 @@ export type TransactionSignature = string;
1818 */
1919export type TransactionId = string ;
2020
21- type RpcRequest = ( methodName : string , args : Array < string | number > ) => any ;
21+ type RpcRequest = ( methodName : string , args : Array < any > ) => any ;
2222
2323function 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
104104export 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
0 commit comments