WalletMgr.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import MetaMask from './MetaMask.js';
  2. import WalletConnect from './WalletConnect.js';
  3. import BinanceChain from './BinanceChain.js';
  4. import Erc20 from './contract/Erc20'
  5. import UniswapV2Router02 from './contract/UniswapV2Router02'
  6. import UniswapV2Library from './contract/UniswapV2Library'
  7. var tp = require('tp-js-sdk')
  8. let chainTab = {
  9. BSC: [{
  10. chainId: "0x38", // A 0x-prefixed hexadecimal string
  11. chainName: "Binance Smart Chain Mainnet",
  12. nativeCurrency: {
  13. name: "BNB",
  14. symbol: "BNB", // 2-6 characters long
  15. decimals: 18,
  16. },
  17. rpcUrls: ['https://bsc-dataseed.binance.org/:443'],
  18. blockExplorerUrls: ['https://bscscan.com/'],
  19. }],
  20. BSCTest: [{
  21. chainId: "0x61", // A 0x-prefixed hexadecimal string
  22. chainName: "Binance Smart Chain Testnet",
  23. nativeCurrency: {
  24. name: "tBNB",
  25. symbol: "tBNB", // 2-6 characters long
  26. decimals: 18,
  27. },
  28. rpcUrls: ['https://data-seed-prebsc-1-s1.binance.org:8545'],
  29. blockExplorerUrls: ['https://bscscan.com/'],
  30. }],
  31. Rinkeby: [{
  32. chainId: "0x4", // A 0x-prefixed hexadecimal string
  33. chainName: "Rinkeby Test",
  34. nativeCurrency: {
  35. name: "ETH",
  36. symbol: "ETH", // 2-6 characters long
  37. decimals: 18,
  38. },
  39. rpcUrls: ['https://rinkeby.infura.io/v3/9aa3d95b3bc440fa88ea12eaa4456161'],
  40. blockExplorerUrls: ['https://rinkeby.etherscan.io'],
  41. }],
  42. }
  43. const WalletEnum = {
  44. MetaMask: 1,
  45. WalletConnect: 2,
  46. BinanceChain: 3,
  47. }
  48. const ReturnCodeEnum ={
  49. SUCCESS : 0,
  50. METAMASK_NOT_INSTALL:101,
  51. NOT_CONNECTED:102,
  52. NOT_CURRENT_CHAIN:103,
  53. SIGNATURE_FAILED:105,
  54. CONTRACT_ERROR:200,
  55. }
  56. var currentChain = chainTab.BSCTest;
  57. var walletCurType=null;
  58. let currentWallet = null;
  59. function init (_type) {
  60. switch (_type) {
  61. case WalletEnum.MetaMask:
  62. currentWallet = MetaMask;
  63. break
  64. case WalletEnum.WalletConnect:
  65. currentWallet = WalletConnect;
  66. break
  67. case WalletEnum.BinanceChain:
  68. currentWallet = BinanceChain;
  69. break
  70. default:
  71. break;
  72. }
  73. localStorage.setItem('walletConnectType', walletCurType)
  74. }
  75. function changeWalletType (_type,fun) {
  76. walletCurType = _type;
  77. init(walletCurType);
  78. currentWallet.init(this,fun);
  79. }
  80. function crateContract(provider){
  81. Erc20.init(provider,this)
  82. UniswapV2Router02.init(provider,this)
  83. UniswapV2Library.init(provider,this)
  84. }
  85. function isTokenPocker () {
  86. return tp.isConnected();
  87. }
  88. function getWalletCurType () {
  89. return walletCurType;
  90. }
  91. function connectWallectAccount (func) {
  92. currentWallet.connectWallectAccount(func);
  93. }
  94. async function addTokensToMetaMask(address){
  95. if(currentWallet == MetaMask){
  96. let info = await Erc20.getTokenInfo(address);
  97. console.log(info.symbol, info.decimals)
  98. MetaMask.addTokens(info);
  99. }
  100. }
  101. export default {
  102. WalletEnum,
  103. currentChain,
  104. // currentAccount,
  105. // currentChaidId,
  106. ReturnCodeEnum,
  107. getWalletCurType,
  108. init,
  109. changeWalletType,
  110. connectWallectAccount,
  111. isTokenPocker,
  112. crateContract,
  113. addTokensToMetaMask,
  114. }