Skip to content

Commit

Permalink
JS - 0.9.67, makeExchangeTx (experimental, not final)
Browse files Browse the repository at this point in the history
  • Loading branch information
1aerostorm committed Feb 27, 2024
1 parent 773e314 commit 9532a16
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions golos-lib-js/src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const multiauth = require('./multiauth');
const middlewares = require('./middlewares');
const broadcast = require('./broadcast');
const config = require('./config');
const dex = require('./dex')
const formatter = require('./formatter')(api);
const memo = require('./auth/memo');
const messages = require('./auth/messages');
Expand All @@ -21,6 +22,7 @@ const golos = {
multiauth,
broadcast,
config,
dex,
formatter,
memo,
messages,
Expand Down
59 changes: 59 additions & 0 deletions golos-lib-js/src/dex/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import isFunction from 'lodash/isFunction'
import { Asset, Price } from '../utils'

const ORDER_MAX_EXPIRATION = 0xffffffff

function makeOrderID() {
return Math.floor(Date.now() / 1000)
}

async function makeExchangeTx(exchangeSteps, opts) {
const defOpts = {
op_type: 'limit_order_create',
orderid: (op, i, ops, step) => {
return makeOrderID()
}
}
opts = {...defOpts, ...opts}

const ops = []
let i = 0
for (const step of exchangeSteps) {
const op = {}

const copyField = (key, defVal) => {
if (key in opts) {
if (opts[key] !== undefined) {
op[key] = opts[key]
}
} else if (defVal !== undefined) {
op[key] = defVal
}
}

copyField('owner')
if ('orderid' in opts) op.orderid = opts.orderid

op.amount_to_sell = step.sell

const prc = await new Price(step.limit_price)
op.min_to_receive = Asset(step.sell).mul(prc).toString()

copyField('fill_or_kill', false)

op.expiration = opts.expiration || ORDER_MAX_EXPIRATION

if (isFunction(opts.orderid)) {
op.orderid = await opts.orderid(op, i++, ops, step)
}

ops.push([opts.op_type, op])
}
return ops
}

module.exports = {
ORDER_MAX_EXPIRATION,
makeOrderID,
makeExchangeTx,
};
2 changes: 2 additions & 0 deletions golos-lib-js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const multiauth = require('./multiauth');
const middlewares = require('./middlewares');
const broadcast = require('./broadcast');
const formatter = require('./formatter')(api);
const dex = require('./dex')
const memo = require('./auth/memo');
const messages = require('./auth/messages');
const config = require('./config');
Expand All @@ -20,6 +21,7 @@ module.exports = {
api,
auth,
broadcast,
dex,
formatter,
memo,
messages,
Expand Down

0 comments on commit 9532a16

Please sign in to comment.