|
| 1 | +import chai from 'chai'; |
| 2 | +import chaiAsPromised from 'chai-as-promised'; |
| 3 | +import { address, publicKey } from '@waves/ts-lib-crypto'; |
| 4 | +import { |
| 5 | + invokeScript, nodeInteraction as ni, setScript, |
| 6 | +} from '@waves/waves-transactions'; |
| 7 | +import { create } from '@waves/node-api-js'; |
| 8 | +import { format } from 'path'; |
| 9 | +import ride from '@waves/ride-js'; |
| 10 | +import { readFile } from 'fs/promises'; |
| 11 | + |
| 12 | +chai.use(chaiAsPromised); |
| 13 | +const { expect } = chai; |
| 14 | + |
| 15 | +const apiBase = process.env.API_NODE_URL; |
| 16 | +const chainId = 'R'; |
| 17 | + |
| 18 | +const api = create(apiBase); |
| 19 | + |
| 20 | +describe('lp_stable_decimals_migration: put.mjs', /** @this {MochaSuiteModified} */() => { |
| 21 | + it('should successfully change the state in the same way after changing the script from lp_stable_old.ride to lp_stable.ride when executing the put and get method', async function () { |
| 22 | + const usdtAmount = 1e6; |
| 23 | + const usdnAmount = 1e6; |
| 24 | + const shouldAutoStake = false; |
| 25 | + |
| 26 | + const expectedOutLpAmt = 1e8; |
| 27 | + const expectedPriceLast = 1e8; |
| 28 | + const expectedPriceHistory = 1e8; |
| 29 | + const expectedInvokesCount = 1; |
| 30 | + |
| 31 | + const lpStable = address(this.accounts.lpStable, chainId); |
| 32 | + |
| 33 | + // putFirst |
| 34 | + // -------------------------------------------------------------------------------------------- |
| 35 | + const putFirst = invokeScript({ |
| 36 | + dApp: lpStable, |
| 37 | + payment: [ |
| 38 | + { assetId: this.usdtAssetId, amount: usdtAmount }, |
| 39 | + { assetId: this.usdnAssetId, amount: usdnAmount }, |
| 40 | + ], |
| 41 | + call: { |
| 42 | + function: 'put', |
| 43 | + args: [ |
| 44 | + { type: 'integer', value: 0 }, |
| 45 | + { type: 'boolean', value: shouldAutoStake }, |
| 46 | + ], |
| 47 | + }, |
| 48 | + chainId, |
| 49 | + }, this.accounts.user1); |
| 50 | + await api.transactions.broadcast(putFirst, {}); |
| 51 | + await ni.waitForTx(putFirst.id, { apiBase }); |
| 52 | + |
| 53 | + // setScript |
| 54 | + // -------------------------------------------------------------------------------------------- |
| 55 | + const ridePath = 'ride'; |
| 56 | + const lpStableV2Path = format({ dir: ridePath, base: 'lp_stable.ride' }); |
| 57 | + const lpStableAddonV2Path = format({ dir: ridePath, base: 'lp_stable_addon.ride' }); |
| 58 | + |
| 59 | + const { base64: base64LpStableV2 } = ride.compile( |
| 60 | + (await readFile(lpStableV2Path, { encoding: 'utf-8' })), |
| 61 | + ).result; |
| 62 | + const ssTxLpStableV2 = setScript({ |
| 63 | + script: base64LpStableV2, |
| 64 | + chainId, |
| 65 | + fee: 34e5, |
| 66 | + senderPublicKey: publicKey(this.accounts.lpStable), |
| 67 | + }, this.accounts.manager); |
| 68 | + await api.transactions.broadcast(ssTxLpStableV2, {}); |
| 69 | + await ni.waitForTx(ssTxLpStableV2.id, { apiBase }); |
| 70 | + |
| 71 | + const { base64: base64LpStableAddonV2 } = ride.compile( |
| 72 | + (await readFile(lpStableAddonV2Path, { encoding: 'utf-8' })), |
| 73 | + ).result; |
| 74 | + const ssTxLpStableAddonV2 = setScript({ |
| 75 | + script: base64LpStableAddonV2, |
| 76 | + chainId, |
| 77 | + fee: 14e5, |
| 78 | + }, this.accounts.lpStableAddon); |
| 79 | + await api.transactions.broadcast(ssTxLpStableAddonV2, {}); |
| 80 | + await ni.waitForTx(ssTxLpStableAddonV2.id, { apiBase }); |
| 81 | + |
| 82 | + // putAfterSetScript |
| 83 | + // -------------------------------------------------------------------------------------------- |
| 84 | + const putAfterSetScript = invokeScript({ |
| 85 | + dApp: lpStable, |
| 86 | + payment: [ |
| 87 | + { assetId: this.usdtAssetId, amount: usdtAmount }, |
| 88 | + { assetId: this.usdnAssetId, amount: usdnAmount }, |
| 89 | + ], |
| 90 | + call: { |
| 91 | + function: 'put', |
| 92 | + args: [ |
| 93 | + { type: 'integer', value: 0 }, |
| 94 | + { type: 'boolean', value: shouldAutoStake }, |
| 95 | + ], |
| 96 | + }, |
| 97 | + chainId, |
| 98 | + }, this.accounts.user1); |
| 99 | + await api.transactions.broadcast(putAfterSetScript, {}); |
| 100 | + await ni.waitForTx(putAfterSetScript.id, { apiBase }); |
| 101 | + |
| 102 | + // getAfterPut |
| 103 | + // -------------------------------------------------------------------------------------------- |
| 104 | + const getAfterPut = invokeScript({ |
| 105 | + dApp: lpStable, |
| 106 | + payment: [ |
| 107 | + { assetId: this.lpStableAssetId, amount: expectedOutLpAmt }, |
| 108 | + ], |
| 109 | + call: { |
| 110 | + function: 'get', |
| 111 | + args: [], |
| 112 | + }, |
| 113 | + chainId, |
| 114 | + }, this.accounts.user1); |
| 115 | + await api.transactions.broadcast(getAfterPut, {}); |
| 116 | + const { |
| 117 | + height: heightGetAfterPut, |
| 118 | + stateChanges: stateChangesGetAfterPut, |
| 119 | + id: idGetAfterPut, |
| 120 | + } = await ni.waitForTx(getAfterPut.id, { apiBase }); |
| 121 | + |
| 122 | + const { |
| 123 | + timestamp: timestampGetAfterPut, |
| 124 | + } = await api.blocks.fetchHeadersAt(heightGetAfterPut); |
| 125 | + const keyPriceHistoryGetAfterPut = `%s%s%d%d__price__history__${heightGetAfterPut}__${timestampGetAfterPut}`; |
| 126 | + |
| 127 | + // check getAfterPut |
| 128 | + // -------------------------------------------------------------------------------------------- |
| 129 | + expect(stateChangesGetAfterPut.data).to.eql([{ |
| 130 | + key: `%s%s%s__G__${address(this.accounts.user1, chainId)}__${idGetAfterPut}`, |
| 131 | + type: 'string', |
| 132 | + value: `%d%d%d%d%d%d__${usdtAmount}__${usdnAmount}__${expectedOutLpAmt}__${expectedPriceLast}__${heightGetAfterPut}__${timestampGetAfterPut}`, |
| 133 | + }, { |
| 134 | + key: '%s%s__price__last', |
| 135 | + type: 'integer', |
| 136 | + value: expectedPriceLast, |
| 137 | + }, { |
| 138 | + key: keyPriceHistoryGetAfterPut, |
| 139 | + type: 'integer', |
| 140 | + value: expectedPriceHistory, |
| 141 | + }]); |
| 142 | + |
| 143 | + expect(stateChangesGetAfterPut.transfers).to.eql([{ |
| 144 | + address: address(this.accounts.user1, chainId), |
| 145 | + asset: this.usdtAssetId, |
| 146 | + amount: usdtAmount, |
| 147 | + }, { |
| 148 | + address: address(this.accounts.user1, chainId), |
| 149 | + asset: this.usdnAssetId, |
| 150 | + amount: usdnAmount, |
| 151 | + }]); |
| 152 | + |
| 153 | + const { invokes: invokesGetAfterPut } = stateChangesGetAfterPut; |
| 154 | + expect(invokesGetAfterPut.length).to.eql(expectedInvokesCount); |
| 155 | + |
| 156 | + expect(invokesGetAfterPut[0].dApp).to.eql(address(this.accounts.factoryV2, chainId)); |
| 157 | + expect(invokesGetAfterPut[0].call.function).to.eql('burn'); |
| 158 | + expect(invokesGetAfterPut[0].call.args).to.eql([ |
| 159 | + { |
| 160 | + type: 'Int', |
| 161 | + value: expectedOutLpAmt, |
| 162 | + }]); |
| 163 | + expect(invokesGetAfterPut[0].stateChanges.burns).to.eql([{ |
| 164 | + assetId: this.lpStableAssetId, |
| 165 | + quantity: expectedOutLpAmt, |
| 166 | + }]); |
| 167 | + |
| 168 | + // getSecondAfterPut |
| 169 | + // -------------------------------------------------------------------------------------------- |
| 170 | + const getSecondAfterPut = invokeScript({ |
| 171 | + dApp: lpStable, |
| 172 | + payment: [ |
| 173 | + { assetId: this.lpStableAssetId, amount: expectedOutLpAmt }, |
| 174 | + ], |
| 175 | + call: { |
| 176 | + function: 'get', |
| 177 | + args: [], |
| 178 | + }, |
| 179 | + chainId, |
| 180 | + }, this.accounts.user1); |
| 181 | + await api.transactions.broadcast(getSecondAfterPut, {}); |
| 182 | + const { |
| 183 | + height: heightGetSecondAfterPut, |
| 184 | + stateChanges: stateChangesGetSecondAfterPut, |
| 185 | + id: idGetSecondAfterPut, |
| 186 | + } = await ni.waitForTx(getSecondAfterPut.id, { apiBase }); |
| 187 | + |
| 188 | + const { |
| 189 | + timestamp: timestampGetSecondAfterPut, |
| 190 | + } = await api.blocks.fetchHeadersAt(heightGetSecondAfterPut); |
| 191 | + const keyPriceHistoryGetSecondAfterPut = `%s%s%d%d__price__history__${heightGetSecondAfterPut}__${timestampGetSecondAfterPut}`; |
| 192 | + |
| 193 | + // check getSecondAfterPut |
| 194 | + // -------------------------------------------------------------------------------------------- |
| 195 | + expect(stateChangesGetSecondAfterPut.data).to.eql([{ |
| 196 | + key: `%s%s%s__G__${address(this.accounts.user1, chainId)}__${idGetSecondAfterPut}`, |
| 197 | + type: 'string', |
| 198 | + value: `%d%d%d%d%d%d__${usdtAmount}__${usdnAmount}__${expectedOutLpAmt}__${expectedPriceLast}__${heightGetSecondAfterPut}__${timestampGetSecondAfterPut}`, |
| 199 | + }, { |
| 200 | + key: '%s%s__price__last', |
| 201 | + type: 'integer', |
| 202 | + value: expectedPriceLast, |
| 203 | + }, { |
| 204 | + key: keyPriceHistoryGetSecondAfterPut, |
| 205 | + type: 'integer', |
| 206 | + value: expectedPriceHistory, |
| 207 | + }]); |
| 208 | + |
| 209 | + expect(stateChangesGetSecondAfterPut.transfers).to.eql([{ |
| 210 | + address: address(this.accounts.user1, chainId), |
| 211 | + asset: this.usdtAssetId, |
| 212 | + amount: usdtAmount, |
| 213 | + }, { |
| 214 | + address: address(this.accounts.user1, chainId), |
| 215 | + asset: this.usdnAssetId, |
| 216 | + amount: usdnAmount, |
| 217 | + }]); |
| 218 | + |
| 219 | + const { invokes: invokesGetAfterSecondPut } = stateChangesGetSecondAfterPut; |
| 220 | + expect(invokesGetAfterSecondPut.length).to.eql(expectedInvokesCount); |
| 221 | + |
| 222 | + expect(invokesGetAfterSecondPut[0].dApp) |
| 223 | + .to.eql(address(this.accounts.factoryV2, chainId)); |
| 224 | + expect(invokesGetAfterSecondPut[0].call.function).to.eql('burn'); |
| 225 | + expect(invokesGetAfterSecondPut[0].call.args).to.eql([ |
| 226 | + { |
| 227 | + type: 'Int', |
| 228 | + value: expectedOutLpAmt, |
| 229 | + }]); |
| 230 | + expect(invokesGetAfterSecondPut[0].stateChanges.burns).to.eql([{ |
| 231 | + assetId: this.lpStableAssetId, |
| 232 | + quantity: expectedOutLpAmt, |
| 233 | + }]); |
| 234 | + }); |
| 235 | +}); |
0 commit comments