|
1 | 1 | import bs58 from 'bs58'; |
| 2 | +import BigNumber from 'bignumber.js'; |
2 | 3 |
|
3 | 4 | import { BaseUtils, isBase58 } from '@bitgo/sdk-core'; |
4 | 5 | import { coins, Nep141Token } from '@bitgo/statics'; |
@@ -114,6 +115,35 @@ export class Utils implements BaseUtils { |
114 | 115 | .map((coin) => coin as Nep141Token); |
115 | 116 | return token ? token[0] : undefined; |
116 | 117 | } |
| 118 | + |
| 119 | + /** |
| 120 | + * Convert from raw gas units to yocto Near value |
| 121 | + * |
| 122 | + * @param {String} gasUnits - raw gas units (e.g. "30000000000000" = 30TGas) |
| 123 | + * @returns {String} value in yoctoNear as a string |
| 124 | + */ |
| 125 | + convertGasUnitsToYoctoNear(gasUnits: string): string { |
| 126 | + const YOCTO_PER_NEAR = new BigNumber('1e24'); |
| 127 | + const NEAR_PER_TGAS = new BigNumber('0.0001'); // 1 TGas = 0.0001 Near |
| 128 | + const GAS_UNITS_PER_TGAS = new BigNumber('1e12'); // 1 TGas = 1e12 gas units |
| 129 | + |
| 130 | + const gas = new BigNumber(gasUnits); |
| 131 | + const tgas = gas.dividedBy(GAS_UNITS_PER_TGAS); |
| 132 | + const nearCost = tgas.multipliedBy(NEAR_PER_TGAS); |
| 133 | + const yoctoCost = nearCost.multipliedBy(YOCTO_PER_NEAR); |
| 134 | + |
| 135 | + return yoctoCost.integerValue(BigNumber.ROUND_FLOOR).toString(); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Convert the given record into base64 string |
| 140 | + * |
| 141 | + * @param {Record<String, String>} data the input in the form of a map |
| 142 | + * @returns {String} base64 encoded string |
| 143 | + */ |
| 144 | + convertToBase64(data: Record<string, string>): string { |
| 145 | + return Buffer.from(JSON.stringify(data)).toString('base64'); |
| 146 | + } |
117 | 147 | } |
118 | 148 |
|
119 | 149 | const utils = new Utils(); |
|
0 commit comments