Skip to content

Commit b1423fa

Browse files
committed
update getFeeValue for create coin tx
1 parent a9c8009 commit b1423fa

File tree

7 files changed

+51
-20
lines changed

7 files changed

+51
-20
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module.exports = {
44
root: true,
5-
// parser: 'babel-eslint',
5+
parser: 'babel-eslint',
66
// parserOptions: {
77
// sourceType: 'module'
88
// },

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.20.0 - 2020.12.30
2+
- **BREAKING** change fee calculation for Create Coin tx (now it depends on unit value)
3+
- add `coinSymbolLength` param to `getFeeValue`
4+
15
## 0.19.0 - 2020.12.22
26
- **BREAKING** change unit value, which is used in fee calculation, from 0.001 to 0.1
37

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ Params:
3434
- txType: number or string, tx type
3535
- options: object
3636
- options.payload: string or Buffer, tx payload
37-
- options.coinSymbol: string, coin symbol if tx is coin creation
37+
- options.coinSymbol: string, coin symbol if tx is coin creation (can be replaced with `coinSymbolLength`)
38+
- options.coinSymbolLength: number, coin symbol length if tx is coin creation (can be replaced with `coinSymbol`)
3839
- options.multisendCount: number, count of recipients if tx is multisend
3940
```
4041
import { getFeeValue, TX_TYPE } from 'minterjs-util';
@@ -49,7 +50,10 @@ getFeeValue(TX_TYPE.SEND, {payload: 'asé'});
4950
// 0.018
5051
5152
getFeeValue(TX_TYPE.CREATE_COIN, {coinSymbol: 'ABCDEFG'});
52-
// 100
53+
// 10000
54+
55+
getFeeValue(TX_TYPE.CREATE_COIN, {coinSymbolLength: 6});
56+
// 100000
5357
5458
getFeeValue(TX_TYPE.MULTISEND, {multisendCount: 5});
5559
// 0.035

build/rollup.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import babel from 'rollup-plugin-babel';
88
export default {
99
input: 'src/index.js',
1010
plugins: [
11+
babel({
12+
babelrc: false,
13+
configFile: false,
14+
"plugins": [
15+
"@babel/plugin-proposal-optional-chaining",
16+
"@babel/plugin-proposal-numeric-separator",
17+
],
18+
}),
1119
commonjs({
1220
// namedExports: {
1321
// 'node_modules/ethereumjs-util/dist/index.js': [ 'stripHexPrefix', 'padToEven' ],

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "minterjs-util",
3-
"version": "0.19.0",
3+
"version": "0.20.0",
44
"description": "Utils for Minter",
55
"main": "dist/cjs/index.js",
66
"module": "src/index.js",

src/fee.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import {TX_TYPE} from './tx-types.js';
66
* @param txType
77
* @param {string|Buffer} [payload]
88
* @param {string} [coinSymbol]
9+
* @param {number} [coinSymbolLength]
910
* @param {number} [multisendCount]
1011
* @return {boolean|number}
1112
*/
12-
export function getFeeValue(txType, {payload, coinSymbol, multisendCount} = {}) {
13+
export function getFeeValue(txType, {payload, coinSymbol, coinSymbolLength, multisendCount} = {}) {
1314
// txType to string
1415
if (!isHexString(txType)) {
1516
txType = `0x${padToEven(txType.toString(16)).toUpperCase()}`;
@@ -34,27 +35,40 @@ export function getFeeValue(txType, {payload, coinSymbol, multisendCount} = {})
3435
const COIN_UNIT_PART = 1 / COIN_UNIT; // negate js math quirks, ex.: 18 * 0.001 = 0.018000000000000002
3536
// multisend fee = base fee + extra fee based on count
3637
const multisendExtraCountFee = txType === TX_TYPE.MULTISEND ? (multisendCount - 1) * MULTISEND_FEE_DELTA : 0;
37-
// coin symbol extra fee, value in base coin (not in units)
38-
const coinSymbolFee = txType === TX_TYPE.CREATE_COIN ? getCoinSymbolFee(coinSymbol) : 0;
39-
return (baseUnits + payloadLength * 2 + multisendExtraCountFee) / COIN_UNIT_PART + coinSymbolFee;
38+
// coin symbol extra fee, value in units (not in base coin)
39+
const coinSymbolFee = txType === TX_TYPE.CREATE_COIN ? getCoinSymbolFee(coinSymbol, coinSymbolLength) : 0;
40+
return (baseUnits + payloadLength * 2 + multisendExtraCountFee + coinSymbolFee) / COIN_UNIT_PART;
4041
}
4142

4243
//
4344
/**
4445
* @param {string} ticker
46+
* @param {number} [length]
4547
* @return {number} - value in base coin (not in units)
4648
*/
47-
export function getCoinSymbolFee(ticker) {
48-
return COIN_SYMBOL_FEES[ticker && ticker.length] || 100;
49+
export function getCoinSymbolFee(ticker, length) {
50+
if (!length) {
51+
length = ticker?.length;
52+
}
53+
if (!isValidLength(length)) {
54+
length = 7;
55+
}
56+
return COIN_SYMBOL_FEES[length];
57+
58+
// eslint-disable-next-line unicorn/consistent-function-scoping, no-shadow
59+
function isValidLength(length) {
60+
return length >= 3 && length <= 7;
61+
}
4962
}
5063

51-
// value in base coin (not in units)
64+
// value in units (not in base coin)
5265
// @See https://github.com/MinterTeam/minter-go-node/blob/master/core/transaction/create_coin.go#L93
5366
export const COIN_SYMBOL_FEES = {
54-
3: 1000000,
55-
4: 100000,
56-
5: 10000,
57-
6: 1000,
67+
3: 1_000_000_000,
68+
4: 100_000_000,
69+
5: 10_000_000,
70+
6: 1_000_000,
71+
7: 100_000,
5872
};
5973

6074
export const MULTISEND_FEE_DELTA = 5;

test/fee.test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ describe('getFeeValue', () => {
1818
});
1919

2020
test('create coin', () => {
21-
expect(getFeeValue(TX_TYPE.CREATE_COIN, {coinSymbol: 'ABCDEFG'})).toEqual(100);
22-
expect(getFeeValue(TX_TYPE.CREATE_COIN, {coinSymbol: 'ABCDEFGHIJ'})).toEqual(100);
23-
expect(getFeeValue(TX_TYPE.CREATE_COIN, {coinSymbol: 'ABC'})).toEqual(1000000);
21+
expect(getFeeValue(TX_TYPE.CREATE_COIN, {coinSymbol: 'ABCDEFG'})).toEqual(10_000);
22+
expect(getFeeValue(TX_TYPE.CREATE_COIN, {coinSymbol: 'ABCDEFGHIJ'})).toEqual(10_000);
23+
expect(getFeeValue(TX_TYPE.CREATE_COIN, {coinSymbol: 'ABC'})).toEqual(100_000_000);
24+
expect(getFeeValue(TX_TYPE.CREATE_COIN, {coinSymbolLength: 4})).toEqual(10_000_000);
2425
});
2526

26-
test('create coin without coinSymbolLength', () => {
27-
expect(getFeeValue(TX_TYPE.CREATE_COIN)).toEqual(100);
27+
test('create coin without coin symbol', () => {
28+
expect(getFeeValue(TX_TYPE.CREATE_COIN)).toEqual(10000);
2829
});
2930

3031
test('multisend', () => {

0 commit comments

Comments
 (0)