Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transfer fee #49

Merged
merged 2 commits into from
Mar 30, 2018
Merged
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
3 changes: 3 additions & 0 deletions src/modules/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ const getters = {
const price = API.Transactions.getMemoPrice(memo);
return price;
};
},
getTransferPrice: () => {
return API.Transactions.getTransferPrice().fee;
}
};

Expand Down
38 changes: 29 additions & 9 deletions src/services/api/transactions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TransactionBuilder, ChainTypes, ops, PrivateKey } from 'bitsharesjs';
import { ChainConfig } from 'bitsharesjs-ws';
import { getUser } from './account';
import { encryptMemo } from '../../utils';
import { encryptMemo, getMemoPrivKey } from '../../utils';
import { getCachedComissions } from './parameters';


Expand Down Expand Up @@ -36,7 +36,21 @@ const transferAsset = async (fromId, to, assetId, amount, keys, memo = false) =>
return { success: false, error: 'Destination user not found' };
}

const { active, owner } = keys;
const {
data: {
account: {
options: {
memo_key: memoKey
}
}
}
} = await getUser(fromId);

const memoPrivate = getMemoPrivKey(keys, memoKey);

if (!memoPrivate) {
return { success: false, error: 'Cant find key to encrypt memo' };
}

const transferObject = {
fee: {
Expand All @@ -53,7 +67,7 @@ const transferAsset = async (fromId, to, assetId, amount, keys, memo = false) =>

if (memo) {
try {
transferObject.memo = encryptMemo(memo, active, toAccount.data.account.options.memo_key);
transferObject.memo = encryptMemo(memo, memoPrivate, toAccount.data.account.options.memo_key);
} catch (error) {
return { success: false, error: 'Encrypt memo failed' };
}
Expand All @@ -65,7 +79,7 @@ const transferAsset = async (fromId, to, assetId, amount, keys, memo = false) =>
}, ChainConfig.expire_in_secs * 2000);

try {
await buildAndBroadcast('transfer', transferObject, { active, owner });
await buildAndBroadcast('transfer', transferObject, keys);
clearTimeout(broadcastTimeout);
resolve({ success: true });
} catch (error) {
Expand All @@ -75,15 +89,20 @@ const transferAsset = async (fromId, to, assetId, amount, keys, memo = false) =>
});
};

const getTransferPrice = () => {
const { fees } = getCachedComissions();
const operations = Object.keys(ChainTypes.operations);
const opIndex = operations.indexOf('transfer');
const { fee, price_per_kbyte: kbytePrice } = fees[opIndex][1];
return { fee, kbytePrice };
};

const getMemoPrice = (memo) => {
const privKey = '5KikQ23YhcM7jdfHbFBQg1G7Do5y6SgD9sdBZq7BqQWXmNH7gqo';
const memoToKey = 'BTS8eLeqSZZtB1YHdw7KjQxRSRmaKAseCxhUSqaLxUdqvdGpp6nck';
const pKey = PrivateKey.fromWif(privKey);

const { fees } = getCachedComissions();
const operations = Object.keys(ChainTypes.operations);
const opIndex = operations.indexOf('transfer');
const { fee, price_per_kbyte: kbytePrice } = fees[opIndex][1];
const { fee, kbytePrice } = getTransferPrice();

const encrypted = encryptMemo(memo, pKey, memoToKey);

Expand Down Expand Up @@ -125,5 +144,6 @@ export default {
transferAsset,
signTransaction,
placeOrders,
getMemoPrice
getMemoPrice,
getTransferPrice
};
13 changes: 13 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ export const decryptMemo = (memo, privateKey) => {
).toString('utf-8');
};

export const getMemoPrivKey = (keys, publicKey) => {
const { active, owner } = keys;
const ownerPubkey = owner.toPublicKey().toPublicKeyString();
const activePubkey = active.toPublicKey().toPublicKeyString();
if (publicKey === ownerPubkey) {
return owner;
}
if (publicKey === activePubkey) {
return active;
}
return false;
};


/** Calculates distribution 0..1 of total amount of assets expressed
* in base asset
Expand Down