SHARE
TWEET

Untitled

a guest Jul 21st, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { convertNumberToHex, convertUtf8ToNumber } from "@walletconnect/utils";
  2. import { convertAmountToRawNumber } from "../helpers/bignumber";
  3. import {
  4.   apiGetAccountNonce,
  5.   // apiGetGasLimit,
  6.   apiGetGasPrices
  7. } from "../helpers/api";
  8. import {
  9.   getDataString,
  10.   removeHexPrefix,
  11.   sanitizeHex
  12. } from "../helpers/utilities";
  13. import FUNCTIONS from "../constants/functions";
  14. import SUPPORTED_ASSETS from "../constants/supportedAssets";
  15. import ASSET_PRICES from "../constants/assetPrices";
  16.  
  17. export function getAsset(symbol: string, chainId: number) {
  18.   let result = null;
  19.   if (SUPPORTED_ASSETS[chainId]) {
  20.     result = SUPPORTED_ASSETS[chainId][symbol] || null;
  21.   }
  22.   return result;
  23. }
  24.  
  25. export function getAssetPrice(currency: string, symbol: string) {
  26.   let result = null;
  27.   if (ASSET_PRICES[currency]) {
  28.     result = ASSET_PRICES[currency][symbol] || null;
  29.   }
  30.   return result;
  31. }
  32.  
  33. export function isToken(asset: any) {
  34.   return !!asset.contractAddress;
  35. }
  36.  
  37. export async function formatTransaction(
  38.   from: string,
  39.   to: string,
  40.   amount: number,
  41.   currency: string,
  42.   symbol: string,
  43.   chainId: number
  44. ) {
  45.   const asset = getAsset(symbol, chainId);
  46.   const price = getAssetPrice(currency, symbol);
  47.  
  48.   amount = convertUtf8ToNumber(
  49.     convertAmountToRawNumber(
  50.       price ? amount * (1 / price) : amount,
  51.       asset ? asset.decimals : 18
  52.     )
  53.   );
  54.  
  55.   let value: string | number = "";
  56.   let data: string = "";
  57.   let gasLimit: string | number = "";
  58.  
  59.   if (isToken(asset)) {
  60.     const tokenAddress = asset ? asset.contractAddress : "";
  61.     value = "0x00";
  62.     data = getDataString(FUNCTIONS.TOKEN_TRANSFER, [
  63.       removeHexPrefix(to),
  64.       removeHexPrefix(convertNumberToHex(amount))
  65.     ]);
  66.     // TODO: Fix Gas Limit estimation
  67.     // gasLimit = await apiGetGasLimit(tokenAddress, data, chainId);
  68.     gasLimit = 40000;
  69.     to = tokenAddress;
  70.   } else {
  71.     value = amount;
  72.     data = "0x";
  73.     gasLimit = 21000;
  74.   }
  75.  
  76.   const nonce = await apiGetAccountNonce(from);
  77.  
  78.   let gasPrice;
  79.  
  80.   if (chainId === 100) {
  81.     const fixedGasPrice = 1.1;
  82.     gasPrice = convertUtf8ToNumber(convertAmountToRawNumber(fixedGasPrice, 9));
  83.   } else {
  84.     const gasPrices = await apiGetGasPrices();
  85.     gasPrice = convertUtf8ToNumber(
  86.       convertAmountToRawNumber(gasPrices.average.price, 9)
  87.     );
  88.   }
  89.  
  90.   const tx = {
  91.     from: sanitizeHex(from),
  92.     to: sanitizeHex(to),
  93.     nonce: nonce || "",
  94.     gasPrice: gasPrice || "",
  95.     gasLimit: gasLimit || "",
  96.     value: value || "",
  97.     data: data || "0x"
  98.   };
  99.  
  100.   return tx;
  101. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top