Skip to content

Commit

Permalink
Fix using standard API of UAL
Browse files Browse the repository at this point in the history
  • Loading branch information
chendatony31 committed Oct 9, 2020
1 parent 93f47f3 commit 639462f
Showing 1 changed file with 52 additions and 31 deletions.
83 changes: 52 additions & 31 deletions src/utils/evolutiondex.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,24 @@ import { evodexConfig } from '../config'
import { getScatterError } from './getScatterError'
import axiosUtil from './axios.util'

import { JsonRpc } from 'eosjs'

const { asset, number_to_asset: numberToAsset } = eosCommon
const defaultState = { pairs: [], tokens: [] }
const axios = axiosUtil(evodexConfig.api, evodexConfig.apiFailover)

const getRpc = (ual) => {
const endpoint = ual.chains[0].rpcEndpoints[0]

const rpcEndpoint = ual.activeUser.buildRpcEndpoint(endpoint)

return new JsonRpc(rpcEndpoint)
}

const getAccountName = async (ual) => {
return await ual.activeUser.getAccountName(ual)
}

const amountToAsset = (amount = '0', currentAsset) => {
if (isNaN(amount)) {
return asset(`0 ${currentAsset.symbol.code().toString()}`)
Expand Down Expand Up @@ -208,9 +222,11 @@ const getUserTokenBalance = async (ual, pool) => {
return
}

const response = await ual.activeUser.rpc.get_currency_balance(
const rpc = getRpc(ual)

const response = await rpc.get_currency_balance(
pool.contract,
ual.activeUser.accountName,
await getAccountName(ual),
pool.asset.symbol.code().toString()
)

Expand All @@ -221,7 +237,7 @@ const exchange = async (amount, pair, ual) => {
const { assetToGive, assetToReceive } = getExchangeAssets(amount, pair)
const authorization = [
{
actor: ual.activeUser.accountName,
actor: await getAccountName(ual),
permission: 'active'
}
]
Expand All @@ -236,9 +252,9 @@ const exchange = async (amount, pair, ual) => {
account: pair.pool2.contract,
name: 'open',
data: {
owner: ual.activeUser.accountName,
owner: await getAccountName(ual),
symbol: pair.pool2.asset.symbol.toString(),
ram_payer: ual.activeUser.accountName
ram_payer: await getAccountName(ual)
}
}
]
Expand All @@ -253,12 +269,12 @@ const exchange = async (amount, pair, ual) => {
account: pair.from.contract,
name: 'transfer',
data: {
from: ual.activeUser.accountName,
from: await getAccountName(ual),
to: evodexConfig.contract,
quantity: assetToGive.toString(),
memo: `exchange: ${
pair.token
},${assetToReceive.toString()},sent using evodex.io`
},${assetToReceive.toString()},sent using evodex.io`
}
}
]
Expand All @@ -273,11 +289,16 @@ const exchange = async (amount, pair, ual) => {
throw new Error(getScatterError(error))
}
}


const getUserPools = async (ual) => {
const { rows } = await ual.activeUser.rpc.get_table_rows({

const rpc = getRpc(ual)

const { rows } = await rpc.get_table_rows({
json: true,
code: evodexConfig.contract,
scope: ual.activeUser.accountName,
scope: await getAccountName(ual),
table: 'accounts'
})

Expand Down Expand Up @@ -316,7 +337,7 @@ const addLiquidity = async (amount, pair, ual) => {
const { baseAsset, asset1, asset2 } = getAddLiquidityAssets(amount, pair)
const authorization = [
{
actor: ual.activeUser.accountName,
actor: await getAccountName(ual),
permission: 'active'
}
]
Expand All @@ -325,8 +346,8 @@ const addLiquidity = async (amount, pair, ual) => {
name: 'closeext',
authorization,
data: {
user: ual.activeUser.accountName,
to: ual.activeUser.accountName,
user: await getAccountName(ual),
to: await getAccountName(ual),
memo: ''
}
}
Expand All @@ -338,8 +359,8 @@ const addLiquidity = async (amount, pair, ual) => {
name: 'openext',
authorization,
data: {
user: ual.activeUser.accountName,
payer: ual.activeUser.accountName,
user: await getAccountName(ual),
payer: await getAccountName(ual),
ext_symbol: {
contract: pair.pool1.contract,
sym: pair.pool1.asset.symbol.toString()
Expand All @@ -351,8 +372,8 @@ const addLiquidity = async (amount, pair, ual) => {
name: 'openext',
authorization,
data: {
user: ual.activeUser.accountName,
payer: ual.activeUser.accountName,
user: await getAccountName(ual),
payer: await getAccountName(ual),
ext_symbol: {
contract: pair.pool2.contract,
sym: pair.pool2.asset.symbol.toString()
Expand All @@ -364,7 +385,7 @@ const addLiquidity = async (amount, pair, ual) => {
name: 'transfer',
authorization,
data: {
from: ual.activeUser.accountName,
from: await getAccountName(ual),
to: evodexConfig.contract,
quantity: asset1.toString(),
memo: ''
Expand All @@ -375,7 +396,7 @@ const addLiquidity = async (amount, pair, ual) => {
name: 'transfer',
authorization,
data: {
from: ual.activeUser.accountName,
from: await getAccountName(ual),
to: evodexConfig.contract,
quantity: asset2.toString(),
memo: ''
Expand All @@ -386,7 +407,7 @@ const addLiquidity = async (amount, pair, ual) => {
name: 'addliquidity',
authorization,
data: {
user: ual.activeUser.accountName,
user: await getAccountName(ual),
to_buy: baseAsset.toString(),
max_asset1: asset1.toString(),
max_asset2: asset2.toString()
Expand Down Expand Up @@ -457,7 +478,7 @@ const removeLiquidity = async (amount, pair, ual) => {
const { baseAsset, asset1, asset2 } = getRemoveLiquidityAssets(amount, pair)
const authorization = [
{
actor: ual.activeUser.accountName,
actor: await getAccountName(ual),
permission: 'active'
}
]
Expand All @@ -469,8 +490,8 @@ const removeLiquidity = async (amount, pair, ual) => {
name: 'openext',
authorization,
data: {
user: ual.activeUser.accountName,
payer: ual.activeUser.accountName,
user: await getAccountName(ual),
payer: await getAccountName(ual),
ext_symbol: {
contract: pair.pool1.contract,
sym: pair.pool1.asset.symbol.toString()
Expand All @@ -482,8 +503,8 @@ const removeLiquidity = async (amount, pair, ual) => {
name: 'openext',
authorization,
data: {
user: ual.activeUser.accountName,
payer: ual.activeUser.accountName,
user: await getAccountName(ual),
payer: await getAccountName(ual),
ext_symbol: {
contract: pair.pool2.contract,
sym: pair.pool2.asset.symbol.toString()
Expand All @@ -495,7 +516,7 @@ const removeLiquidity = async (amount, pair, ual) => {
name: 'remliquidity',
authorization,
data: {
user: ual.activeUser.accountName,
user: await getAccountName(ual),
to_sell: baseAsset.toString(),
min_asset1: asset1.toString(),
min_asset2: asset2.toString()
Expand All @@ -506,8 +527,8 @@ const removeLiquidity = async (amount, pair, ual) => {
name: 'closeext',
authorization,
data: {
user: ual.activeUser.accountName,
to: ual.activeUser.accountName,
user: await getAccountName(ual),
to: await getAccountName(ual),
ext_symbol: {
contract: pair.pool1.contract,
sym: pair.pool1.asset.symbol.toString()
Expand All @@ -520,8 +541,8 @@ const removeLiquidity = async (amount, pair, ual) => {
name: 'closeext',
authorization,
data: {
user: ual.activeUser.accountName,
to: ual.activeUser.accountName,
user: await getAccountName(ual),
to: await getAccountName(ual),
ext_symbol: {
contract: pair.pool2.contract,
sym: pair.pool2.asset.symbol.toString()
Expand All @@ -545,7 +566,7 @@ const voteFee = async (amount, pair, ual) => {
try {
const authorization = [
{
actor: ual.activeUser.accountName,
actor: await getAccountName(ual),
permission: 'active'
}
]
Expand All @@ -557,7 +578,7 @@ const voteFee = async (amount, pair, ual) => {
name: 'votefee',
authorization,
data: {
user: ual.activeUser.accountName,
user: await getAccountName(ual),
pair_token: pair.supply.symbol.code().toString(),
fee_voted: parseInt(parseFloat(amount) * 100)
}
Expand Down

0 comments on commit 639462f

Please sign in to comment.