Skip to content

Commit 7b5a31b

Browse files
authored
Merge pull request #6508 from BitGo/COIN-4908
feat(sdk-coin-near): handle near token recoveries
2 parents 84b3424 + 675f3b4 commit 7b5a31b

File tree

6 files changed

+708
-122
lines changed

6 files changed

+708
-122
lines changed

modules/sdk-coin-near/src/lib/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ export const STORAGE_DEPOSIT = 'storage_deposit';
1414
export const FUNGIBLE_TOKEN_RELATED_METHODS = [FT_TRANSFER, STORAGE_DEPOSIT];
1515

1616
export const HEX_REGEX = /^[0-9a-fA-F]+$/;
17+
18+
export const MAX_GAS_LIMIT_FOR_FT_TRANSFER = '30000000000000';

modules/sdk-coin-near/src/lib/utils.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import bs58 from 'bs58';
2+
import BigNumber from 'bignumber.js';
23

34
import { BaseUtils, isBase58 } from '@bitgo/sdk-core';
45
import { coins, Nep141Token } from '@bitgo/statics';
@@ -114,6 +115,35 @@ export class Utils implements BaseUtils {
114115
.map((coin) => coin as Nep141Token);
115116
return token ? token[0] : undefined;
116117
}
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+
}
117147
}
118148

119149
const utils = new Utils();

0 commit comments

Comments
 (0)