Skip to content

Commit

Permalink
Move comissions into transactions module
Browse files Browse the repository at this point in the history
  • Loading branch information
youaresofunny committed Mar 30, 2018
1 parent fb80f7e commit af00c88
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 52 deletions.
23 changes: 23 additions & 0 deletions src/actions/transactions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
import { ChainTypes } from 'bitsharesjs';
import * as types from '../mutations';
import API from '../services/api';
// eslint-disable-next-line
import { calcPortfolioDistributionChange } from 'lib/src/utils';

export const fetchComissions = async ({ commit }) => {
const { fees } = await API.Parameters.getComissions();
const operations = Object.keys(ChainTypes.operations);
const orderIdx = operations.indexOf('limit_order_create');
const transferIdx = operations.indexOf('transfer');

const { fee: orderFee } = fees[orderIdx][1];
const { fee: transeferFee, price_per_kbyte: kbytePrice } = fees[transferIdx][1];

const comissions = {
order: {
fee: orderFee
},
transfer: {
fee: transeferFee,
kbytePrice
}
};

commit(types.FETCH_FEES, { fees: comissions });
};

export const createOrdersFromDistribution = async (store) => {
const { commit, rootGetters, getters } = store;
const distribution = getters.getPendingDistribution;
Expand Down
27 changes: 19 additions & 8 deletions src/modules/transactions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Vue from 'vue';
import API from '../services/api';
import * as utils from '../utils';
import * as types from '../mutations';
import * as actions from '../actions/transactions';

Expand All @@ -9,7 +9,16 @@ const initialState = {
pendingTransfer: false,
pending: false,
error: null,
transactionsProcessing: false
transactionsProcessing: false,
fees: {
order: {
fee: 0
},
transfer: {
fee: 0,
kbytePrice: 0
}
}
};

const getters = {
Expand All @@ -19,14 +28,13 @@ const getters = {
hasPendingTransfer: state => state.pendingTransfer !== false,
areTransactionsProcessing: state => state.transactionsProcessing,
getPendingTransfer: state => state.pendingTransfer,
getMemoPrice: () => {
getOrderFee: state => state.fees.order.fee,
getTransferFee: state => state.fees.transfer.fee,
getMemoPrice: (state) => {
return (memo) => {
const price = API.Transactions.getMemoPrice(memo);
return price;
const kbytes = utils.getMemoSize(memo);
return state.fees.transfer.fee + (state.fees.transfer.kbytePrice * kbytes);
};
},
getTransferPrice: () => {
return API.Transactions.getTransferPrice().fee;
}
};

Expand Down Expand Up @@ -62,6 +70,9 @@ const mutations = {
},
[types.SET_PENDING_TRANSFER](state, { transaction }) {
state.pendingTransfer = transaction;
},
[types.FETCH_FEES](state, { fees }) {
state.fees = fees;
}
};

Expand Down
2 changes: 2 additions & 0 deletions src/mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export const PROCESS_PENDING_ORDERS_REQUEST = 'PROCESS_PENDING_ORDERS_REQUEST';
export const PROCESS_PENDING_ORDERS_COMPLETE = 'PROCESS_PENDING_ORDERS_COMPLETE';
export const PROCESS_PENDING_ORDERS_ERROR = 'PROCESS_PENDING_ORDERS_ERROR';

export const FETCH_FEES = 'FETCH_FEES';

export const SET_PENDING_TRANSFER = 'SET_PENDING_TRANSFER';

export const STORE_BACKUP_DATE = 'STORE_BACKUP_DATE';
Expand Down
14 changes: 1 addition & 13 deletions src/services/api/parameters.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Apis } from 'bitsharesjs-ws';
import { ChainTypes } from 'bitsharesjs';

const { operations } = ChainTypes;
let cacheParameters = false;

const getParameters = async () => {
Expand Down Expand Up @@ -29,14 +27,4 @@ export const getComissions = async () => {
return { fees, scale };
};

export const getComission = async (type) => {
const ops = Object.keys(operations);
const opIndex = ops.indexOf(type);
const { fees } = await getComissions();
if (opIndex > -1) {
return fees[opIndex][1].fee;
}
return false;
};

export default { getParameters, getComissions, getComission, getCachedComissions };
export default { getParameters, getComissions, getCachedComissions };
32 changes: 2 additions & 30 deletions src/services/api/transactions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { TransactionBuilder, ChainTypes, ops, PrivateKey } from 'bitsharesjs';
import { TransactionBuilder } from 'bitsharesjs';
import { ChainConfig } from 'bitsharesjs-ws';
import { getUser } from './account';
import { encryptMemo, getMemoPrivKey } from '../../utils';
import { getCachedComissions } from './parameters';


const signTransaction = async (transaction, { active, owner }) => {
Expand Down Expand Up @@ -89,31 +88,6 @@ 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 { fee, kbytePrice } = getTransferPrice();

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

const serialized = ops.memo_data.fromObject(encrypted);
const stringified = JSON.stringify(ops.memo_data.toHex(serialized));
const byteLength = Buffer.byteLength(stringified, 'hex');
const memoFee = Math.floor((kbytePrice * byteLength) / 1024);

return fee + memoFee;
};

const placeOrders = async ({ orders, keys }) => {
const transaction = new TransactionBuilder();
console.log('placing orders : ', orders);
Expand Down Expand Up @@ -143,7 +117,5 @@ const placeOrders = async ({ orders, keys }) => {
export default {
transferAsset,
signTransaction,
placeOrders,
getMemoPrice,
getTransferPrice
placeOrders
};
15 changes: 14 additions & 1 deletion src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Aes, TransactionHelper } from 'bitsharesjs';
import { Aes, TransactionHelper, PrivateKey, ops } from 'bitsharesjs';
/**
* Return object with keys = id of each element of array (element.id)
* @param {Array} array - array of data elements
Expand Down Expand Up @@ -134,6 +134,19 @@ export const getMemoPrivKey = (keys, publicKey) => {
return false;
};

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

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

const serialized = ops.memo_data.fromObject(encrypted);
const stringified = JSON.stringify(ops.memo_data.toHex(serialized));
const byteLength = Buffer.byteLength(stringified, 'hex');
return Math.floor(byteLength / 1024);
};


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

0 comments on commit af00c88

Please sign in to comment.