UNPKG

@effectai/effect-js

Version:

Effect Network Javscript/Typescript SDK (for [https://effect.network](https://effect.network))

165 lines 5.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TokenService = exports.swapDirection = exports.DefiBoxPairEnum = void 0; const antelope_1 = require("@wharfkit/antelope"); var DefiBoxPairEnum; (function (DefiBoxPairEnum) { DefiBoxPairEnum[DefiBoxPairEnum["EosEfx"] = 191] = "EosEfx"; DefiBoxPairEnum[DefiBoxPairEnum["EosUsdt"] = 12] = "EosUsdt"; })(DefiBoxPairEnum || (exports.DefiBoxPairEnum = DefiBoxPairEnum = {})); var swapDirection; (function (swapDirection) { swapDirection["EfxToUsdt"] = "191-12"; swapDirection["UsdtToEfx"] = "12-191"; })(swapDirection || (exports.swapDirection = swapDirection = {})); class TokenService { constructor(client) { this.client = client; } /** * Returns a DefiBox pair, * @param pairEnum DefiBoxPairEnum * @returns DefiBoxPair */ async getDefiBoxPair(pairEnum) { try { const pairResponse = await this.client.eos.v1.chain.get_table_rows({ code: 'swap.defi', scope: 'swap.defi', table: 'pairs', limit: 1, lower_bound: antelope_1.UInt128.from(pairEnum.valueOf()), upper_bound: antelope_1.UInt128.from(pairEnum.valueOf()), }); const [pair] = pairResponse.rows; return pair; } catch (error) { console.error(error); throw new Error('Error retrieving EFX Ticker Price from DefiBox'); } } /** * Returns the current EFX price in USDT from DefiBox * @returns EFX price in USDT */ async getEfxPrice() { try { const eosEfxPair = await this.getDefiBoxPair(DefiBoxPairEnum.EosEfx); const eosUsdtPair = await this.getDefiBoxPair(DefiBoxPairEnum.EosUsdt); const efxUsdt = Number(eosEfxPair.price1_last) * Number(eosUsdtPair.price0_last); return efxUsdt; } catch (error) { console.error(error); throw new Error('Error retrieving EFX Ticker Price from DefiBox'); } } /** * Deposit EFX into vaccount */ async deposit(amount) { try { this.client.requireSession(); const vacc = await this.client.vaccount.get(); return await this.client.session.transact({ "action": { "account": this.client.config.tokenContract, "name": "transfer", "authorization": [{ "actor": this.client.session.actor, "permission": this.client.session.permission }], "data": { "from": this.client.session.actor, "to": this.client.config.vaccountContract, "quantity": antelope_1.Asset.from(amount, '4,EFX'), "memo": `${vacc.id}` } } }); } catch (error) { console.error(error); throw new Error('Error depositing EFX'); } } /** * TODO * Withdraw EFX from vaccount */ /** * TODO * Claim EFX from vaccount to user */ /** * TODO * Claim EFX from vaccount to staking account. */ /** * Swaps out of EFX into USDT * @param amount The amount of EFX to swap out of * @returns TransactResult */ async swapOut(amount) { try { this.client.requireSession(); const efxPrice = await this.getEfxPrice(); const valueAmount = efxPrice * amount; return await this.client.session.transact({ "action": { "account": "effecttokens", "name": "transfer", "authorization": [{ "actor": this.client.session.actor, "permission": this.client.session.permission }], "data": { "from": this.client.session.actor, "to": "swap.defi", "quantity": antelope_1.Asset.from(amount, '4,EFX'), "memo": `swap,${valueAmount},${swapDirection.EfxToUsdt}` } } }); } catch (error) { console.error(error); throw new Error('Error swapping out of EFX'); } } /** * Swaps into EFX from USDT * @param amount The amount of USDT to swap into EFX * @returns TransactResult */ async swapIn(amount) { try { this.client.requireSession(); const efxPrice = await this.getEfxPrice(); const valueAmount = 1 / efxPrice * amount; return await this.client.session.transact({ "action": { "account": "tethertether", "name": "transfer", "authorization": [{ "actor": this.client.session.actor, "permission": this.client.session.permission }], "data": { "from": this.client.session.actor, "to": "swap.defi", "quantity": antelope_1.Asset.from(amount, '4,USDT'), "memo": `swap,${valueAmount},${swapDirection.UsdtToEfx}` } } }); } catch (error) { console.error(error); throw new Error('Error swapping out of EFX'); } } } exports.TokenService = TokenService; //# sourceMappingURL=token.js.map