rewardSwap.tuning.test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* global artifacts, web3, contract */
  2. require('chai').use(require('bn-chai')(web3.utils.BN)).use(require('chai-as-promised')).should()
  3. const { toBN, fromWei } = require('web3-utils')
  4. const { takeSnapshot, revertSnapshot, mineBlock } = require('../scripts/ganacheHelper')
  5. const Torn = artifacts.require('TORNMock')
  6. const RewardSwap = artifacts.require('RewardSwapMock')
  7. const tornConfig = require('torn-token')
  8. const RLP = require('rlp')
  9. // Set time to beginning of a second
  10. async function timeReset() {
  11. const delay = 1000 - new Date().getMilliseconds()
  12. await new Promise((resolve) => setTimeout(resolve, delay))
  13. await mineBlock()
  14. }
  15. async function getNextAddr(sender, offset = 0) {
  16. const nonce = await web3.eth.getTransactionCount(sender)
  17. return (
  18. '0x' +
  19. web3.utils
  20. .sha3(RLP.encode([sender, Number(nonce) + Number(offset)]))
  21. .slice(12)
  22. .substring(14)
  23. )
  24. }
  25. // todo mock's fixed timestamp interferes with simulation
  26. contract.skip('RewardSwap simulation', (accounts) => {
  27. let torn
  28. let rewardSwap
  29. const sender = accounts[0]
  30. const recipient = accounts[1]
  31. const tornCap = toBN(tornConfig.torn.cap)
  32. const miningCap = toBN(tornConfig.torn.distribution.miningV2.amount)
  33. const initialTornBalance = toBN(tornConfig.miningV2.initialBalance)
  34. const poolWeight = 1e11
  35. let snapshotId
  36. async function increaseTimeDays(days, verbose = true) {
  37. if (verbose) {
  38. console.log(`Skipping ${days} days`)
  39. }
  40. const timestamp = await rewardSwap.getTimestamp()
  41. await rewardSwap.setTimestamp(Number(timestamp) + 60 * 60 * 24 * Number(days))
  42. }
  43. async function exchange(points, verbose = true) {
  44. const balanceBefore = await torn.balanceOf(recipient)
  45. await rewardSwap.swap(recipient, points, { from: sender })
  46. const balanceAfter = await torn.balanceOf(recipient)
  47. const poolSize = await rewardSwap.tornVirtualBalance()
  48. if (verbose) {
  49. console.log(
  50. `Exchanged ${points} points for ${fromWei(
  51. balanceAfter.sub(balanceBefore),
  52. )} TORN. Remaining in pool ${fromWei(poolSize)} TORN`,
  53. )
  54. }
  55. }
  56. before(async () => {
  57. const swapExpectedAddr = await getNextAddr(accounts[0], 1)
  58. torn = await Torn.new(sender, 0, [
  59. { to: swapExpectedAddr, amount: miningCap.toString() },
  60. { to: sender, amount: tornCap.sub(miningCap).toString() },
  61. ])
  62. rewardSwap = await RewardSwap.new(
  63. torn.address,
  64. sender,
  65. miningCap.toString(),
  66. initialTornBalance.toString(),
  67. poolWeight,
  68. )
  69. snapshotId = await takeSnapshot()
  70. })
  71. beforeEach(async () => {
  72. await timeReset()
  73. })
  74. describe('Swap Simulations', () => {
  75. it('init', async () => {
  76. console.log('Cap', fromWei(miningCap))
  77. console.log('Virtual balance', fromWei(await rewardSwap.tornVirtualBalance()))
  78. })
  79. it('rates', async () => {
  80. let k = toBN(1)
  81. for (let i = 0; i < 18; i++) {
  82. console.log(
  83. `Expected return for 10^${i} points: ${fromWei(await rewardSwap.getExpectedReturn(k))} TORN`,
  84. )
  85. k = k.mul(toBN(10))
  86. }
  87. })
  88. it.skip('sim1', async () => {
  89. await exchange(1e8)
  90. await exchange(1e8)
  91. await increaseTimeDays(1)
  92. await exchange(1e8)
  93. })
  94. it('equilibrium sim', async () => {
  95. const amountPerDay = 7.2e9
  96. for (let i = 0; i <= 450; i++) {
  97. const verbose = i < 15 || i % 30 === 0 || (i > 360 && i < 370)
  98. if (verbose) {
  99. console.log(`Day ${i}`)
  100. }
  101. await exchange(amountPerDay, verbose)
  102. await increaseTimeDays(1, false)
  103. }
  104. })
  105. })
  106. afterEach(async () => {
  107. await revertSnapshot(snapshotId.result)
  108. // eslint-disable-next-line require-atomic-updates
  109. snapshotId = await takeSnapshot()
  110. })
  111. })