Skip to content
This repository has been archived by the owner on May 14, 2021. It is now read-only.

V2.1.0 specify fee #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
30 changes: 16 additions & 14 deletions scripts/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class KinPayment {
}

public static async allFrom(collection: Server.CollectionPage<Server.PaymentOperationRecord>): Promise<KinPayment[]> {
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;
Expand Down Expand Up @@ -105,9 +105,9 @@ export class Operations {
this.server = server;
}

public async send(operation: xdr.Operation<Operation>, memoText?: string): Promise<Server.TransactionRecord> {
public async send(operation: xdr.Operation<Operation>, options: { memo?: string, fee?: number } = {}): Promise<Server.TransactionRecord> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see that this options type: { memo?: string, fee?: number } is used at least 3 times.
just extract it into a type:

type TransactionsOptions = { memo?: string, fee?: number; };

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);
}

Expand All @@ -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<Operation>, memoText?: string): Promise<string> {
public async createTransactionXDR(operation: xdr.Operation<Operation>, options: { memo?: string, fee?: number } = {}): Promise<string> {
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<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<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);

Expand All @@ -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;
Expand Down
44 changes: 28 additions & 16 deletions scripts/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ export interface KinWallet {

onPaymentReceived(listener: OnPaymentListener): void;

pay(recipient: Address, amount: number, memo?: string): Promise<Payment>;
pay(recipient: Address, amount: number, options?: { memo?: string, fee?: number }): Promise<Payment>;

getTransactionXdr(recipient: Address, amount: number, memo?: string): Promise<string>;
getTransactionXdr(recipient: Address, amount: number, options?: { memo?: string, fee?: number }): Promise<string>;

toString(): string;
}
Expand Down Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -152,32 +164,32 @@ class Wallet implements KinWallet {
payments.start();
}

public async getTransactionXdr(recipient: Address, amount: number, memo?: string): Promise<string> {
public async getTransactionXdr(recipient: Address, amount: number, options: { memo?: string, fee?: number } = {}): Promise<string> {
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<Payment> {
public async pay(recipient: Address, amount: number, options: { memo?: string, fee?: number } = {}): Promise<Payment> {
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));
}
Expand Down
9 changes: 6 additions & 3 deletions scripts/src/tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("=================================");
Expand All @@ -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);
}