UNPKG

5.83 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.TokenService = exports.swapDirection = exports.DefiBoxPairEnum = void 0;
4const antelope_1 = require("@wharfkit/antelope");
5var DefiBoxPairEnum;
6(function (DefiBoxPairEnum) {
7 DefiBoxPairEnum[DefiBoxPairEnum["EosEfx"] = 191] = "EosEfx";
8 DefiBoxPairEnum[DefiBoxPairEnum["EosUsdt"] = 12] = "EosUsdt";
9})(DefiBoxPairEnum || (exports.DefiBoxPairEnum = DefiBoxPairEnum = {}));
10var swapDirection;
11(function (swapDirection) {
12 swapDirection["EfxToUsdt"] = "191-12";
13 swapDirection["UsdtToEfx"] = "12-191";
14})(swapDirection || (exports.swapDirection = swapDirection = {}));
15class TokenService {
16 constructor(client) {
17 this.client = client;
18 }
19 /**
20 * Returns a DefiBox pair,
21 * @param pairEnum DefiBoxPairEnum
22 * @returns DefiBoxPair
23 */
24 async getDefiBoxPair(pairEnum) {
25 try {
26 const pairResponse = await this.client.eos.v1.chain.get_table_rows({
27 code: 'swap.defi',
28 scope: 'swap.defi',
29 table: 'pairs',
30 limit: 1,
31 lower_bound: antelope_1.UInt128.from(pairEnum.valueOf()),
32 upper_bound: antelope_1.UInt128.from(pairEnum.valueOf()),
33 });
34 const [pair] = pairResponse.rows;
35 return pair;
36 }
37 catch (error) {
38 console.error(error);
39 throw new Error('Error retrieving EFX Ticker Price from DefiBox');
40 }
41 }
42 /**
43 * Returns the current EFX price in USDT from DefiBox
44 * @returns EFX price in USDT
45 */
46 async getEfxPrice() {
47 try {
48 const eosEfxPair = await this.getDefiBoxPair(DefiBoxPairEnum.EosEfx);
49 const eosUsdtPair = await this.getDefiBoxPair(DefiBoxPairEnum.EosUsdt);
50 const efxUsdt = Number(eosEfxPair.price1_last) * Number(eosUsdtPair.price0_last);
51 return efxUsdt;
52 }
53 catch (error) {
54 console.error(error);
55 throw new Error('Error retrieving EFX Ticker Price from DefiBox');
56 }
57 }
58 /**
59 * Deposit EFX into vaccount
60 */
61 async deposit(amount) {
62 try {
63 this.client.requireSession();
64 const vacc = await this.client.vaccount.get();
65 return await this.client.session.transact({
66 "action": {
67 "account": this.client.config.tokenContract,
68 "name": "transfer",
69 "authorization": [{
70 "actor": this.client.session.actor,
71 "permission": this.client.session.permission
72 }],
73 "data": {
74 "from": this.client.session.actor,
75 "to": this.client.config.vaccountContract,
76 "quantity": antelope_1.Asset.from(amount, '4,EFX'),
77 "memo": `${vacc.id}`
78 }
79 }
80 });
81 }
82 catch (error) {
83 console.error(error);
84 throw new Error('Error depositing EFX');
85 }
86 }
87 /**
88 * TODO
89 * Withdraw EFX from vaccount
90 */
91 /**
92 * TODO
93 * Claim EFX from vaccount to user
94 */
95 /**
96 * TODO
97 * Claim EFX from vaccount to staking account.
98 */
99 /**
100 * Swaps out of EFX into USDT
101 * @param amount The amount of EFX to swap out of
102 * @returns TransactResult
103 */
104 async swapOut(amount) {
105 try {
106 this.client.requireSession();
107 const efxPrice = await this.getEfxPrice();
108 const valueAmount = efxPrice * amount;
109 return await this.client.session.transact({
110 "action": {
111 "account": "effecttokens",
112 "name": "transfer",
113 "authorization": [{
114 "actor": this.client.session.actor,
115 "permission": this.client.session.permission
116 }],
117 "data": {
118 "from": this.client.session.actor,
119 "to": "swap.defi",
120 "quantity": antelope_1.Asset.from(amount, '4,EFX'),
121 "memo": `swap,${valueAmount},${swapDirection.EfxToUsdt}`
122 }
123 }
124 });
125 }
126 catch (error) {
127 console.error(error);
128 throw new Error('Error swapping out of EFX');
129 }
130 }
131 /**
132 * Swaps into EFX from USDT
133 * @param amount The amount of USDT to swap into EFX
134 * @returns TransactResult
135 */
136 async swapIn(amount) {
137 try {
138 this.client.requireSession();
139 const efxPrice = await this.getEfxPrice();
140 const valueAmount = 1 / efxPrice * amount;
141 return await this.client.session.transact({
142 "action": {
143 "account": "tethertether",
144 "name": "transfer",
145 "authorization": [{
146 "actor": this.client.session.actor,
147 "permission": this.client.session.permission
148 }],
149 "data": {
150 "from": this.client.session.actor,
151 "to": "swap.defi",
152 "quantity": antelope_1.Asset.from(amount, '4,USDT'),
153 "memo": `swap,${valueAmount},${swapDirection.UsdtToEfx}`
154 }
155 }
156 });
157 }
158 catch (error) {
159 console.error(error);
160 throw new Error('Error swapping out of EFX');
161 }
162 }
163}
164exports.TokenService = TokenService;
165//# sourceMappingURL=token.js.map
\No newline at end of file