|
| 1 | +import chai from 'chai'; |
| 2 | +import chaiAsPromised from 'chai-as-promised'; |
| 3 | +import { address } from '@waves/ts-lib-crypto'; |
| 4 | +import { invokeScript, nodeInteraction as ni } from '@waves/waves-transactions'; |
| 5 | +import { create } from '@waves/node-api-js'; |
| 6 | + |
| 7 | +chai.use(chaiAsPromised); |
| 8 | +const { expect } = chai; |
| 9 | + |
| 10 | +const apiBase = process.env.API_NODE_URL; |
| 11 | +const chainId = 'R'; |
| 12 | + |
| 13 | +const api = create(apiBase); |
| 14 | + |
| 15 | +describe('lp_stable: doublePutOneTkn.mjs', /** @this {MochaSuiteModified} */() => { |
| 16 | + it('should successfully putOneTkn with autoStake false', async function () { |
| 17 | + const amAssetPart = 1e8; |
| 18 | + const prAssetPart = 1e8; |
| 19 | + const outLp = 1e10; |
| 20 | + const slippage = 1e3; |
| 21 | + const autoStake = false; |
| 22 | + const usdtAmount = 1e8; |
| 23 | + const usdnAmount = 1e8; |
| 24 | + const shouldAutoStake = false; |
| 25 | + |
| 26 | + const expectedPriceLast = 1e8; |
| 27 | + const expectedPriceHistory = 1e8; |
| 28 | + const expectedWriteAmAmt = 1e8; |
| 29 | + const expectedWritePrAmt = 0; |
| 30 | + const expectedEmitLpAmt = 1e10; |
| 31 | + const expectedSlippageCalc = 1e3; |
| 32 | + const expectedAmDiff = 0; |
| 33 | + const expectedPrDiff = 0; |
| 34 | + |
| 35 | + const lpStable = address(this.accounts.lpStable, chainId); |
| 36 | + |
| 37 | + const put = invokeScript({ |
| 38 | + dApp: lpStable, |
| 39 | + payment: [ |
| 40 | + { assetId: this.usdtAssetId, amount: usdtAmount }, |
| 41 | + { assetId: this.usdnAssetId, amount: usdnAmount }, |
| 42 | + ], |
| 43 | + call: { |
| 44 | + function: 'put', |
| 45 | + args: [ |
| 46 | + { type: 'integer', value: 0 }, |
| 47 | + { type: 'boolean', value: shouldAutoStake }, |
| 48 | + ], |
| 49 | + }, |
| 50 | + chainId, |
| 51 | + }, this.accounts.user1); |
| 52 | + await api.transactions.broadcast(put, {}); |
| 53 | + await ni.waitForTx(put.id, { apiBase }); |
| 54 | + |
| 55 | + const firstPutOneTkn = invokeScript({ |
| 56 | + dApp: lpStable, |
| 57 | + payment: [ |
| 58 | + { assetId: this.usdtAssetId, amount: usdtAmount }, |
| 59 | + ], |
| 60 | + call: { |
| 61 | + function: 'putOneTkn', |
| 62 | + args: [ |
| 63 | + { type: 'integer', value: amAssetPart }, |
| 64 | + { type: 'integer', value: prAssetPart }, |
| 65 | + { type: 'integer', value: outLp }, |
| 66 | + { type: 'integer', value: slippage }, |
| 67 | + { type: 'boolean', value: autoStake }, |
| 68 | + ], |
| 69 | + }, |
| 70 | + chainId, |
| 71 | + }, this.accounts.user1); |
| 72 | + await api.transactions.broadcast(firstPutOneTkn, {}); |
| 73 | + await ni.waitForTx(firstPutOneTkn.id, { apiBase }); |
| 74 | + |
| 75 | + const secondPutOneTkn = invokeScript({ |
| 76 | + dApp: lpStable, |
| 77 | + payment: [ |
| 78 | + { assetId: this.usdtAssetId, amount: usdtAmount }, |
| 79 | + ], |
| 80 | + call: { |
| 81 | + function: 'putOneTkn', |
| 82 | + args: [ |
| 83 | + { type: 'integer', value: amAssetPart }, |
| 84 | + { type: 'integer', value: prAssetPart }, |
| 85 | + { type: 'integer', value: outLp }, |
| 86 | + { type: 'integer', value: slippage }, |
| 87 | + { type: 'boolean', value: autoStake }, |
| 88 | + ], |
| 89 | + }, |
| 90 | + chainId, |
| 91 | + }, this.accounts.user1); |
| 92 | + await api.transactions.broadcast(secondPutOneTkn, {}); |
| 93 | + const { height, stateChanges, id } = await ni.waitForTx(secondPutOneTkn.id, { apiBase }); |
| 94 | + |
| 95 | + const { timestamp } = await api.blocks.fetchHeadersAt(height); |
| 96 | + const keyPriceHistory = `%s%s%d%d__price__history__${height}__${timestamp}`; |
| 97 | + |
| 98 | + expect(stateChanges.data).to.eql([{ |
| 99 | + key: '%s%s__price__last', |
| 100 | + type: 'integer', |
| 101 | + value: expectedPriceLast, |
| 102 | + }, { |
| 103 | + key: keyPriceHistory, |
| 104 | + type: 'integer', |
| 105 | + value: expectedPriceHistory, |
| 106 | + }, { |
| 107 | + key: `%s%s%s__P__${address(this.accounts.user1, chainId)}__${id}`, |
| 108 | + type: 'string', |
| 109 | + value: `%d%d%d%d%d%d%d%d%d%d__${expectedWriteAmAmt}__${expectedWritePrAmt}__${expectedEmitLpAmt}__${expectedPriceLast}__${slippage}__${expectedSlippageCalc}__${height}__${timestamp}__${expectedAmDiff}__${expectedPrDiff}`, |
| 110 | + }]); |
| 111 | + |
| 112 | + expect(stateChanges.transfers).to.eql([{ |
| 113 | + address: address(this.accounts.user1, chainId), |
| 114 | + asset: this.lpStableAssetId, |
| 115 | + amount: outLp, |
| 116 | + }]); |
| 117 | + |
| 118 | + expect(stateChanges.invokes.map((item) => [item.dApp, item.call.function])) |
| 119 | + .to.deep.include.members([ |
| 120 | + [address(this.accounts.lpStableAddon, chainId), 'ensureCanPutOneTkn'], |
| 121 | + [address(this.accounts.gwxReward, chainId), 'calcD'], |
| 122 | + [address(this.accounts.gwxReward, chainId), 'calcD'], |
| 123 | + [address(this.accounts.factoryV2, chainId), 'emit'], |
| 124 | + ]); |
| 125 | + }); |
| 126 | +}); |
0 commit comments