-
Notifications
You must be signed in to change notification settings - Fork 1
refactor: bet price functions #21
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
require('dotenv').config(); | ||
const { nanoid } = require('nanoid'); | ||
|
||
const { setupDatabase, teardownDatabase } = require('../utils/db_helper'); | ||
const ERC20 = require('../erc20_moc/Erc20.noblock'); | ||
|
@@ -473,3 +474,32 @@ test('Test Weird Jonas Case', async () => { | |
|
||
expect(await bet.calcSellFromAmount(989886n, 0)).toBe(490099n); | ||
}); | ||
|
||
test('Initial Quote Prices set correctly', () => { | ||
const bet = new Bet(`test-bet_${nanoid(10)}`, 4); | ||
const price = bet.calcInitialPrice(); | ||
expect(price).toEqual(0.25); | ||
}); | ||
|
||
test('Converts price to decimal', () => { | ||
const bet = new Bet('test-bet', 4); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do not use same bet id twice. you have dependency on the previous test here. the nanoid you use below is the way to go There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have updated thanks |
||
const price = bet.toUnitInterval(20000); | ||
expect(price).toEqual(0.5); | ||
}); | ||
|
||
test('outcome prices change on buy', async () => { | ||
const id = `test_id_${nanoid(10)}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 for nanoid |
||
const investorWalletId = `investorWalletId_${nanoid(10)}`; | ||
|
||
await WFAIR.mint(investorWalletId, investAmount); | ||
|
||
|
||
const bet = new Bet(id, 2); | ||
await bet.addLiquidity(liquidityProviderWallet, liquidityAmount); | ||
|
||
const price = await bet.calcBuyAllOutcomes(); | ||
expect(price[0]).toEqual(price[1]) | ||
await bet.buy(investorWalletId, 100000n, 0, 1n); | ||
const price2 = await bet.calcBuyAllOutcomes(); | ||
expect(price2).toEqual([18024n, 21729n]); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
const { Pool } = require('pg'); | ||
const format = require('pg-format'); | ||
const fs = require('fs'); | ||
|
||
const pool = new Pool({ | ||
|
@@ -116,6 +117,8 @@ const GET_LATEST_PRICE_ACTIONS = `select * from amm_price_action | |
where betid = $1 | ||
)`; | ||
|
||
const INSERT_PRICE_ACTION = 'INSERT INTO amm_price_action (betid, trx_timestamp, outcomeindex, quote) values %L'; | ||
|
||
/** | ||
* @returns {Promise<Client>} | ||
*/ | ||
|
@@ -173,6 +176,20 @@ async function rollbackDBTransaction(client) { | |
client.release(); | ||
} | ||
|
||
async function runInTransaction(commands) { | ||
const client = await createDBTransaction(); | ||
let results = []; | ||
try { | ||
for (const command of commands) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like it! should we move all function that require transaction to use this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes that would remove a lot of duplicate transaction creating and error handling code |
||
const resultLocal = await command(client); | ||
results.push(resultLocal); | ||
} | ||
await commitDBTransaction(client); | ||
} catch (error) { | ||
await rollbackDBTransaction(client); | ||
} | ||
} | ||
|
||
/** | ||
* Get the balance of a specific token from a user | ||
* Build for Transactions | ||
|
@@ -652,7 +669,7 @@ async function getAmmPriceActions(betId, timeOption) { | |
return res.rows.map(r => ({ | ||
outcomeIndex: r.outcomeindex, | ||
trxTimestamp: r.trunc, | ||
quote: Number(r.quote), | ||
quote: r.quote, | ||
})); | ||
} | ||
|
||
|
@@ -667,7 +684,15 @@ async function getLatestPriceActions(betId) { | |
return res.rows; | ||
} | ||
|
||
async function insertPriceActions(values) { | ||
const [result] = await runInTransaction([ | ||
client => client.query(format(INSERT_PRICE_ACTION, values)), | ||
]); | ||
return result.rows; | ||
} | ||
|
||
module.exports = { | ||
runInTransaction, | ||
pool, | ||
DIRECTION, | ||
CASINO_TRADE_STATE, | ||
|
@@ -706,5 +731,6 @@ module.exports = { | |
getCasinoTradesByUserAndStates, | ||
attemptCashout, | ||
getAmmPriceActions, | ||
getLatestPriceActions | ||
getLatestPriceActions, | ||
insertPriceActions, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do not use Number. floats are banned in this library :) use BigInt everywhere and decimals for prices. to store values in database use DECIMAL type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed, it is already decimal in db. thanks