@rainbow-me/rainbow-common
Advanced tools
Comparing version 0.7.4 to 0.7.5
@@ -13,2 +13,4 @@ # Change Log | ||
### Changed | ||
* Upped limit for OpenSea assets | ||
* Fix for initially estimating gas | ||
@@ -15,0 +17,0 @@ ### Removed |
{ | ||
"name": "@rainbow-me/rainbow-common", | ||
"description": "Common library for web and mobile", | ||
"version": "0.7.4", | ||
"version": "0.7.5", | ||
"author": "Jin Chung <jin@rainbow.me>", | ||
@@ -6,0 +6,0 @@ "license": "GPL-3.0", |
@@ -223,2 +223,24 @@ import '@babel/polyfill'; | ||
test('estimateGasLimitForTokenTransfer8Decimals', async () => { | ||
const amount = undefined; | ||
const address = '0x1492004547FF0eFd778CC2c14E794B26B4701105'; | ||
const recipient = '0x1492004547FF0eFd778CC2c14E794B26B4701105'; | ||
const asset = { | ||
name: 'Exceed', | ||
symbol: 'EXC', | ||
address: '0x1eAe15d9f4FA16f5278D02d2f8bDA8b0dcd31f71', | ||
balance: { | ||
amount: "1.97899609544e+21", | ||
}, | ||
decimals: 8, | ||
}; | ||
const gasLimit = await estimateGasLimit({ | ||
asset, | ||
address, | ||
recipient, | ||
amount, | ||
}); | ||
expect(gasLimit).toBe(37695); | ||
}); | ||
test('estimateGasLimitForTokenTransfer', async () => { | ||
@@ -225,0 +247,0 @@ const amount = '0.01'; |
@@ -9,2 +9,3 @@ import { ethers } from 'ethers'; | ||
convertNumberToString, | ||
convertAmountFromBigNumber, | ||
convertAmountToBigNumber, | ||
@@ -15,2 +16,4 @@ convertAssetAmountFromBigNumber, | ||
convertAmountToAssetAmount, | ||
handleSignificantDecimals, | ||
multiply, | ||
} from '../helpers/bignumber'; | ||
@@ -109,8 +112,8 @@ import ethUnits from '../references/ethereum-units.json'; | ||
const to = transaction.to; | ||
const data = transaction.data ? transaction.data : '0x'; | ||
const value = transaction.amount ? toWei(transaction.amount) : '0x00'; | ||
const data = transaction.data ? transaction.data : '0x'; | ||
const _gasPrice = transaction.gasPrice || (await getGasPrice()); | ||
const estimateGasData = value === '0x00' ? { from, to, data } : { to, data }; | ||
const estimateGasData = { from, to, data, value }; | ||
const _gasLimit = | ||
transaction.gasLimit || (await estimateGas(estimateGasData)); | ||
const _gasPrice = transaction.gasPrice || (await getGasPrice()); | ||
const nonce = await getTransactionCount(from); | ||
@@ -174,2 +177,8 @@ const tx = { | ||
const estimateAssetBalancePortion = (assetBalance, decimals) => { | ||
const portion = multiply(convertAmountFromBigNumber(assetBalance), 0.1); | ||
const trimmed = handleSignificantDecimals(portion, decimals); | ||
return convertAmountToAssetAmount(trimmed, decimals); | ||
}; | ||
/** | ||
@@ -190,6 +199,7 @@ * @desc estimate gas limit | ||
amount && Number(amount) | ||
? convertAmountToBigNumber(amount) | ||
: asset.balance.amount * 0.1; | ||
? convertAmountToAssetAmount(amount, asset.decimals) | ||
: estimateAssetBalancePortion(asset.balance.amount, asset.decimals); | ||
let value = _amount.toString(); | ||
let _recipient = recipient; | ||
if (!isHexString(recipient)) { // TODO redundant logic | ||
if (!isHexString(recipient)) { | ||
_recipient = await web3Provider.resolveName(recipient); | ||
@@ -201,7 +211,6 @@ } | ||
: '0x737e583620f4ac1842d4e354789ca0c5e0651fbb'; | ||
let estimateGasData = { to: _recipient, data }; | ||
let estimateGasData = { from: address, to: _recipient, data, value }; | ||
if (asset.symbol !== 'ETH') { | ||
const transferMethodHash = smartContractMethods.token_transfer.hash; | ||
let value = convertAssetAmountFromBigNumber(_amount, asset.decimals); | ||
value = convertStringToHex(value); | ||
value = convertStringToHex(_amount); | ||
data = getDataString(transferMethodHash, [ | ||
@@ -212,9 +221,4 @@ removeHexPrefix(_recipient), | ||
estimateGasData = { from: address, to: asset.address, data, value: '0x0' }; | ||
gasLimit = await estimateGas(estimateGasData); | ||
} else { | ||
let value = convertAssetAmountFromBigNumber(_amount, asset.decimals); | ||
estimateGasData = { from: address, to: recipient, data, value }; | ||
gasLimit = await estimateGas(estimateGasData); | ||
} | ||
return gasLimit; | ||
return await estimateGas(estimateGasData); | ||
}; |
@@ -1,2 +0,9 @@ | ||
import { fromWei } from '../bignumber' | ||
import { | ||
convertAmountFromBigNumber, | ||
convertAmountToAssetAmount, | ||
convertAmountToBigNumber, | ||
handleSignificantDecimals, | ||
multiply, | ||
fromWei, | ||
} from '../bignumber' | ||
@@ -16,1 +23,46 @@ test('fromWei', () => { | ||
}); | ||
test('convertAmountFromBigNumber', () => { | ||
const testCases = [ | ||
{ input: "1.97899609544e+21", expectation: "1978.99609544" }, | ||
{ input: "2.0180611e+21", expectation: "2018.0611" }, | ||
{ input: "38587486895770700000", expectation: "38.5874868957707" }, | ||
{ input: "1000000000000000000", expectation: "1" }, | ||
] | ||
testCases.forEach(testCase => | ||
expect(convertAmountFromBigNumber(testCase.input)).toBe(testCase.expectation), | ||
); | ||
}); | ||
test('convertAmountToBigNumber', () => { | ||
const testCases = [ | ||
{ input: "1978.99609544", expectation: "1.97899609544e+21" }, | ||
{ input: "2018.0611", expectation: "2.0180611e+21" }, | ||
{ input: "38.5874868957707", expectation: "38587486895770700000" }, | ||
{ input: "1", expectation: "1000000000000000000" }, | ||
] | ||
testCases.forEach(testCase => | ||
expect(convertAmountToBigNumber(testCase.input)).toBe(testCase.expectation), | ||
); | ||
}); | ||
test('handleSignificantDecimals', () => { | ||
expect(handleSignificantDecimals("197.899609544", 8, 8)).toBe("197.89960954"); | ||
}); | ||
test('multiply', () => { | ||
expect(multiply("1978.99609544", "0.1")).toBe("197.899609544"); | ||
}); | ||
test('convertAmountToAssetAmount', () => { | ||
const testCases = [ | ||
{ input: ["1978.99609544", 8], expectation: "197899609544" }, | ||
{ input: ["0.01", 18], expectation: "10000000000000000" }, | ||
] | ||
testCases.forEach(testCase => | ||
expect(convertAmountToAssetAmount(...testCase.input).toString()).toBe(testCase.expectation), | ||
); | ||
}); |
@@ -205,9 +205,9 @@ import { get, isEmpty } from 'lodash'; | ||
const txDetails = { | ||
asset: asset, | ||
amount, | ||
asset, | ||
from: address, | ||
gasLimit: gasLimit, | ||
gasPrice: gasPrice.value.amount, | ||
nonce: null, | ||
to: recipient, | ||
nonce: null, | ||
amount: amount, | ||
gasPrice: gasPrice.value.amount, | ||
gasLimit: gasLimit, | ||
}; | ||
@@ -214,0 +214,0 @@ return createSignableTransaction(txDetails) |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1270383
10322