UNPKG

@shogun-sdk/money-legos

Version:

Shogun Money Legos: clients and types for quotes, memes, prices, balances, fees, validations, etc.

185 lines 6.56 kB
import { FOUR_MEME_TOKEN_MANAGER_HELPER_3_ABI } from '../config/abis/fourMeme/tokenManagerHelper3.abi.js'; import { FOUR_MEME_TOKEN_MANAGER_LITE_ABI } from '../config/abis/fourMeme/tokenManagerLite.abi.js'; import { BSC_CHAIN_ID } from '../config/index.js'; // import { BSC_CHAIN_ID } from '../config/chains.js'; import { FOUR_MEME_BSC_TOKEN_MANAGER_HELPER_3_ADDRESS, FOUR_MEME_TOKEN_MANAGER_2_LITE_ADDRESS, } from '../constants/index.js'; import { ethers } from 'ethers'; import { getEVMEthersProviderWithFallback } from '../utils/index.js'; export class FourMeme { constructor() { Object.defineProperty(this, "tokenManagerV2Contract", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "tokenManagerHelperV3", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "_initialized", { enumerable: true, configurable: true, writable: true, value: false }); Object.defineProperty(this, "_initializing", { enumerable: true, configurable: true, writable: true, value: null }); } // Static factory method to create an instance static async create() { const instance = new FourMeme(); await instance.connect(); return instance; } async connect() { if (!this._initialized) { if (!this._initializing) { this._initializing = this.initialize(); } await this._initializing; } } async initialize() { if (this._initialized) return; try { const provider = await getEVMEthersProviderWithFallback(BSC_CHAIN_ID); this.tokenManagerHelperV3 = new TokenManagerHelperV3(provider); this.tokenManagerV2Contract = new ethers.Contract(FOUR_MEME_TOKEN_MANAGER_2_LITE_ADDRESS, FOUR_MEME_TOKEN_MANAGER_LITE_ABI, provider); this._initialized = true; this._initializing = null; } catch (error) { this._initializing = null; throw error; } } } export class TokenManagerHelperV3 { constructor(provider) { Object.defineProperty(this, "provider", { enumerable: true, configurable: true, writable: true, value: provider }); Object.defineProperty(this, "bscTokenManagerHelper3Contract", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.bscTokenManagerHelper3Contract = new ethers.Contract(FOUR_MEME_BSC_TOKEN_MANAGER_HELPER_3_ADDRESS, FOUR_MEME_TOKEN_MANAGER_HELPER_3_ABI, this.provider); } /** * Get information about the token. * * @param tokenAddress The address of your token */ async getTokenInfo(tokenAddress) { try { const tokenInfo = await this.bscTokenManagerHelper3Contract.getTokenInfo(tokenAddress); if (!tokenInfo.length) { return null; } const response = { version: tokenInfo[0], tokenManager: tokenInfo[1], quote: tokenInfo[2], lastPrice: tokenInfo[3], tradingFeeRate: tokenInfo[4], minTradingFee: tokenInfo[5], launchTime: tokenInfo[6], offers: tokenInfo[7], maxOffers: tokenInfo[8], funds: tokenInfo[9], maxFunds: tokenInfo[10], liquidityAdded: tokenInfo[11], }; return response; } catch (error) { return null; } } /** * Simulate a buy and get estimated results. * * @param tokenAddress The address of the token to purchase * @param tokenAmount The amount of the token the user wants to purchase * @param nativeAmount The amount of money the user wants to spend (in the quote currency) * * @example * - If the user wants to buy 10_000 tokens: * - Call with param 'tokenAmount' set to 10000*1e18 * * - If the user wants to spend 10 BNB to purchase tokens: * - Call with param 'nativeAmount' set to 10*1e18 */ async tryBuy(params) { try { const { tokenAddress, tokenAmount, nativeAmount } = params; let result; // only one of the two parameters should be provided if ((tokenAmount && nativeAmount) || (!tokenAmount && !nativeAmount)) { return null; } if (tokenAmount) { result = await this.bscTokenManagerHelper3Contract.tryBuy(tokenAddress, tokenAmount, BigInt(0)); } else { result = await this.bscTokenManagerHelper3Contract.tryBuy(tokenAddress, BigInt(0), nativeAmount); } if (!result.length) { return null; } const response = { tokenManager: result[0], quote: result[1], estimatedAmount: result[2], estimatedCost: result[3], estimatedFee: result[4], amountMsgValue: result[5], amountApproval: result[6], amountFunds: result[7], }; return response; } catch (error) { return null; } } /** * Just pre-calculate the result if the user sells a specified amount of tokens if needed. * * @param tokenAddress The address of the token to purchase * @param amount The amount of token that the user wants to sell */ async trySell(params) { try { const { tokenAddress, amount } = params; const result = await this.bscTokenManagerHelper3Contract.trySell(tokenAddress, amount); if (!result.length) { return null; } const response = { tokenManager: result[0], quote: result[1], funds: result[2], fee: result[3], }; return response; } catch (error) { return null; } } } //# sourceMappingURL=fourMeme.js.map