-
Notifications
You must be signed in to change notification settings - Fork 12
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
Add parameters api module #46
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dfe25e4
Add parameters api module
youaresofunny d0682e2
Remove console.log
youaresofunny 1140547
Add comissions
youaresofunny 6d2d138
Lint
youaresofunny 7f3fe17
Fix typo, add address check method
youaresofunny fdb16cd
Add validate address action
youaresofunny 81c0a37
Return working validation
youaresofunny 75f0cbe
Cache coins
youaresofunny 0836261
Add memo fee calculation
youaresofunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Apis } from 'bitsharesjs-ws'; | ||
import { ChainTypes } from 'bitsharesjs'; | ||
|
||
const { operations } = ChainTypes; | ||
let cacheParameters = false; | ||
|
||
const getParameters = async () => { | ||
if (cacheParameters) { | ||
return cacheParameters; | ||
} | ||
|
||
const [{ parameters }] = await Apis.instance().db_api().exec('get_objects', [['2.0.0']]); | ||
cacheParameters = parameters; | ||
return cacheParameters; | ||
}; | ||
|
||
export const getCachedComissions = () => { | ||
const { current_fees: { parameters: fees, scale } } = cacheParameters; | ||
return { fees, scale }; | ||
}; | ||
|
||
export const getComissions = async () => { | ||
if (cacheParameters) { | ||
return getCachedComissions(); | ||
} | ||
|
||
const { current_fees: { parameters: fees, scale } } = await getParameters(); | ||
console.log('Service:', fees); | ||
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { TransactionBuilder } from 'bitsharesjs'; | ||
import { TransactionBuilder, ChainTypes, ops, PrivateKey } from 'bitsharesjs'; | ||
import { ChainConfig } from 'bitsharesjs-ws'; | ||
import { getUser } from './account'; | ||
import { encryptMemo } from '../../utils'; | ||
import { getCachedComissions } from './parameters'; | ||
|
||
|
||
const signTransaction = async (transaction, { active, owner }) => { | ||
|
@@ -74,13 +75,25 @@ const transferAsset = async (fromId, to, assetId, amount, keys, memo = false) => | |
}); | ||
}; | ||
|
||
// const processOrders = async (orders) => { | ||
// const transaction = new TransactionBuilder(); | ||
// orders.forEach(o => transaction.add_type_operation('limit_order_create', o)); | ||
// API.Transactions.signTransaction(transaction, {active: k, owner: k}); | ||
// await transaction.set_required_fees() | ||
// console.log(await transaction.broadcast()); | ||
// } | ||
const getMemoPrice = (memo) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Что это такое? |
||
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 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(); | ||
|
@@ -111,5 +124,6 @@ const placeOrders = async ({ orders, keys }) => { | |
export default { | ||
transferAsset, | ||
signTransaction, | ||
placeOrders | ||
placeOrders, | ||
getMemoPrice | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.