diff --git a/package-lock.json b/package-lock.json index fa227e0..856eb80 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@kinecosystem/kin.js", - "version": "2.0.1", + "version": "2.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 732b67d..615b0fd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kinecosystem/kin.js", - "version": "2.0.1", + "version": "2.1.0", "description": "A typescript/javascript implementation of the Kin sdk", "main": "scripts/bin/index.js", "types": "scripts/bin/index.d.ts", diff --git a/scripts/src/blockchain.ts b/scripts/src/blockchain.ts index 334c6a6..26a302a 100644 --- a/scripts/src/blockchain.ts +++ b/scripts/src/blockchain.ts @@ -52,7 +52,7 @@ export class KinPayment { } public static async allFrom(collection: Server.CollectionPage): Promise { - return await Promise.all(collection.records.map(async record => await this.from(record))); + return await Promise.all(collection.records.filter(r => r.type === "payment").map(async record => await this.from(record))); } public readonly transaction: Server.TransactionRecord; @@ -105,9 +105,9 @@ export class Operations { this.server = server; } - public async send(operation: xdr.Operation, memoText?: string): Promise { + public async send(operation: xdr.Operation, options: { memo?: string, fee?: number } = {}): Promise { const account = await this.loadAccount(this.keys.publicKey()); // loads the sequence number - const transaction = this.createTransaction(account, operation, memoText); + const transaction = this.createTransaction(account, operation, options); return await this._send(transaction); } @@ -121,22 +121,24 @@ export class Operations { return (await this.server.operations().forTransaction(hash).call()).records[0] as Server.PaymentOperationRecord; } - public async createTransactionXDR(operation: xdr.Operation, memoText?: string): Promise { + public async createTransactionXDR(operation: xdr.Operation, options: { memo?: string, fee?: number } = {}): Promise { const account = await this.loadAccount(this.keys.publicKey()); // loads the sequence number - - const transaction = this.createTransaction(account, operation, memoText); + const transaction = this.createTransaction(account, operation, options); return transaction.toEnvelope().toXDR().toString("base64"); } - private createTransaction(account: Server.AccountResponse, operation: xdr.Operation, memoText?: string) { - const transactionBuilder = new TransactionBuilder(account); - transactionBuilder.addOperation(operation); - - if (memoText) { - transactionBuilder.addMemo(Memo.text(memoText)); + private createTransaction(account: Server.AccountResponse, operation: xdr.Operation, options: { memo?: string, fee?: number } = {}) { + const transactionOpts: TransactionBuilder.TransactionBuilderOptions = {}; + if (options.fee !== null && options.fee !== undefined) { + transactionOpts.fee = options.fee; + } + if (options.memo !== null && options.memo !== undefined) { + transactionOpts.memo = Memo.text(options.memo); } - transactionBuilder.setTimeout(TimeoutInfinite); + const transactionBuilder = new TransactionBuilder(account, transactionOpts); + transactionBuilder.addOperation(operation); + transactionBuilder.setTimeout(TimeoutInfinite); const transaction = transactionBuilder.build(); transaction.sign(this.keys); @@ -157,7 +159,7 @@ export class Operations { if (isTransactionError(e)) { throw new Error( `\nKin Blockchain Error:\ntransaction: ${ e.response.data.extras.result_codes.transaction }` + - `\n\toperations: ${ e.response.data.extras.result_codes.operations.join(",") }` + `\n\toperations: ${ (e.response.data.extras.result_codes.operations || []).join(",") }` ); } else { throw e; diff --git a/scripts/src/client.ts b/scripts/src/client.ts index efcc1dc..daa5235 100644 --- a/scripts/src/client.ts +++ b/scripts/src/client.ts @@ -61,9 +61,9 @@ export interface KinWallet { onPaymentReceived(listener: OnPaymentListener): void; - pay(recipient: Address, amount: number, memo?: string): Promise; + pay(recipient: Address, amount: number, options?: { memo?: string, fee?: number }): Promise; - getTransactionXdr(recipient: Address, amount: number, memo?: string): Promise; + getTransactionXdr(recipient: Address, amount: number, options?: { memo?: string, fee?: number }): Promise; toString(): string; } @@ -108,20 +108,32 @@ class PaymentStream { this.timer = undefined; const builder = this.network.server .payments() - .forAccount(this.accountId) - .order("desc"); + .forAccount(this.accountId); + let order: "asc" | "desc" = "desc"; if (this.cursor) { builder.cursor(this.cursor); + order = "asc"; } - - const payments = await builder.call(); + builder.order(order); + const blockchainPayments = await builder.call(); if (this.listener) { - (await getPaymentsFrom(payments)) - .forEach(payment => this.listener!(payment, this)); + let kinPayments = await getPaymentsFrom(blockchainPayments); + + if (order === "desc") { + kinPayments = kinPayments.reverse(); + } + kinPayments.forEach(payment => this.listener!(payment, this)); } + if (blockchainPayments.records.length) { + if (order === "asc") { + this.cursor = blockchainPayments.records[blockchainPayments.records.length - 1].paging_token; + } else { + this.cursor = blockchainPayments.records[0].paging_token; + } + } this.start(); } } @@ -152,32 +164,32 @@ class Wallet implements KinWallet { payments.start(); } - public async getTransactionXdr(recipient: Address, amount: number, memo?: string): Promise { + public async getTransactionXdr(recipient: Address, amount: number, options: { memo?: string, fee?: number } = {}): Promise { const op = Operation.payment({ destination: recipient, asset: Asset.native(), amount: amount.toString() }); - if (memo && typeof memo !== "string") { - memo = undefined; + if (options.memo && typeof options.memo !== "string") { + options.memo = undefined; } - return await this.operations.createTransactionXDR(op, memo); + return await this.operations.createTransactionXDR(op, options); } - public async pay(recipient: Address, amount: number, memo?: string): Promise { + public async pay(recipient: Address, amount: number, options: { memo?: string, fee?: number } = {}): Promise { const op = Operation.payment({ destination: recipient, asset: Asset.native(), amount: amount.toString() }); - if (memo && typeof memo !== "string") { - memo = undefined; + if (options.memo && typeof options.memo !== "string") { + options.memo = undefined; } - const payment = await this.operations.send(op, memo); + const payment = await this.operations.send(op, options); const operation = await this.operations.getPaymentOperationRecord(payment.hash); return fromBlockchainPayment(await KinPayment.from(operation)); } diff --git a/scripts/src/tester.ts b/scripts/src/tester.ts index 39570ea..d80ab6b 100644 --- a/scripts/src/tester.ts +++ b/scripts/src/tester.ts @@ -15,6 +15,9 @@ createWallet(network, keys).then(async wallet => { }); async function testWallet(wallet: KinWallet) { + const transactionXdr3 = await wallet.getTransactionXdr(publicKey, 1); + console.log("xdr", transactionXdr3); + console.log(wallet.balance.cached); console.log(wallet.toString()); console.log("================================="); @@ -28,14 +31,14 @@ async function testWallet(wallet: KinWallet) { stream.stop(); console.log("called stop"); } - console.log(`Got payment from ${ payment.sender } of ${ payment.amount } with memo ${ payment.memo }`); + console.log(`Got payment ${ payment.id } from ${ payment.sender } of ${ payment.amount } with memo ${ payment.memo }`); }); - const payment = await wallet.pay(publicKey, 1, memo); + const payment = await wallet.pay(publicKey, 1, { memo, fee: 100 }); console.log(`Sent payment to ${ payment.recipient } of ${ payment.amount } with memo ${ payment.memo }`); console.log("new balance: ", await wallet.balance.update()); console.log(wallet.toString()); - const transactionXdr = await wallet.getTransactionXdr(publicKey, 1, memo); + const transactionXdr = await wallet.getTransactionXdr(publicKey, 1, { memo, fee: 100 }); console.log("xdr", transactionXdr); }