|
| 1 | +const crypto = require('crypto'); |
| 2 | +const assert = require('assert'); |
| 3 | +const { ethers } = require('ethers'); |
| 4 | +require('../../inspect'); |
| 5 | +const log = require('debug')(`e2e:${require('path').basename(__filename, '.e2e.js')}`); |
| 6 | + |
| 7 | +const { getEthBalance } = require('../../tasks/getEthBalance'); |
| 8 | +const { setEthBalance } = require('../../tasks/setEthBalance'); |
| 9 | +const { getCollateralBalance } = require('../../tasks/getCollateralBalance'); |
| 10 | +const { getTokenBalance } = require('../../tasks/getTokenBalance'); |
| 11 | +const { isCollateralApproved } = require('../../tasks/isCollateralApproved'); |
| 12 | +const { approveCollateral } = require('../../tasks/approveCollateral'); |
| 13 | +const { setTokenBalance } = require('../../tasks/setTokenBalance'); |
| 14 | +const { syncTime } = require('../../tasks/syncTime'); |
| 15 | +const { setConfigUint } = require('../../tasks/setConfigUint'); |
| 16 | +const { getConfigUint } = require('../../tasks/getConfigUint'); |
| 17 | +const { wrapCollateral } = require('../../tasks/wrapCollateral'); |
| 18 | +const { unwrapCollateral } = require('../../tasks/unwrapCollateral'); |
| 19 | +const { spotSell } = require('../../tasks/spotSell'); |
| 20 | +const { spotBuy } = require('../../tasks/spotBuy'); |
| 21 | + |
| 22 | +describe(require('path').basename(__filename, '.e2e.js'), function () { |
| 23 | + const provider = new ethers.providers.JsonRpcProvider( |
| 24 | + process.env.RPC_URL || 'http://127.0.0.1:8545' |
| 25 | + ); |
| 26 | + const wallet = ethers.Wallet.createRandom().connect(provider); |
| 27 | + const address = wallet.address; |
| 28 | + const privateKey = wallet.privateKey; |
| 29 | + |
| 30 | + let snapshot; |
| 31 | + before('Create snapshot', async () => { |
| 32 | + snapshot = await provider.send('evm_snapshot', []); |
| 33 | + log('Create snapshot', { snapshot }); |
| 34 | + }); |
| 35 | + after('Restore snapshot', async () => { |
| 36 | + log('Restore snapshot', { snapshot }); |
| 37 | + await provider.send('evm_revert', [snapshot]); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should sync time of the fork', async () => { |
| 41 | + await syncTime(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should disable withdrawal timeout', async () => { |
| 45 | + await setConfigUint({ key: 'accountTimeoutWithdraw', value: 0 }); |
| 46 | + assert.equal(await getConfigUint('accountTimeoutWithdraw'), 0); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should create new random wallet', async () => { |
| 50 | + log({ wallet: wallet.address, pk: wallet.privateKey }); |
| 51 | + assert.ok(wallet.address); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should set ETH balance to 100', async () => { |
| 55 | + assert.equal(await getEthBalance({ address }), 0, 'New wallet has 0 ETH balance'); |
| 56 | + await setEthBalance({ address, balance: 100 }); |
| 57 | + assert.equal(await getEthBalance({ address }), 100); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should set stataBasUSDC balance to 1000', async () => { |
| 61 | + assert.equal( |
| 62 | + await getCollateralBalance({ address, symbol: 'stataBasUSDC' }), |
| 63 | + 0, |
| 64 | + 'New wallet has 0 stataBasUSDC balance' |
| 65 | + ); |
| 66 | + await setTokenBalance({ |
| 67 | + wallet, |
| 68 | + balance: 1000, |
| 69 | + tokenAddress: require('../../deployments/extras.json').stataBasUSDC_address, |
| 70 | + friendlyWhale: '0xBA12222222228d8Ba445958a75a0704d566BF2C8', |
| 71 | + }); |
| 72 | + assert.equal(await getCollateralBalance({ address, symbol: 'stataBasUSDC' }), 1000); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should approve stataBasUSDC spending for SpotMarket', async () => { |
| 76 | + assert.equal( |
| 77 | + await isCollateralApproved({ |
| 78 | + address, |
| 79 | + symbol: 'stataBasUSDC', |
| 80 | + spenderAddress: require('../../deployments/SpotMarketProxy.json').address, |
| 81 | + }), |
| 82 | + false, |
| 83 | + 'New wallet has not allowed SpotMarket stataBasUSDC spending' |
| 84 | + ); |
| 85 | + await approveCollateral({ |
| 86 | + privateKey, |
| 87 | + symbol: 'stataBasUSDC', |
| 88 | + spenderAddress: require('../../deployments/SpotMarketProxy.json').address, |
| 89 | + }); |
| 90 | + assert.equal( |
| 91 | + await isCollateralApproved({ |
| 92 | + address, |
| 93 | + symbol: 'stataBasUSDC', |
| 94 | + spenderAddress: require('../../deployments/SpotMarketProxy.json').address, |
| 95 | + }), |
| 96 | + true |
| 97 | + ); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should wrap 1000 stataBasUSDC -> sStataUSDC', async () => { |
| 101 | + const synthBalance = await wrapCollateral({ |
| 102 | + wallet, |
| 103 | + symbol: 'stataBasUSDC', |
| 104 | + synthAddress: require('../../deployments/extras.json').synth_stata_usdc_token_address, |
| 105 | + synthMarketId: require('../../deployments/extras.json').synth_stata_usdc_market_id, |
| 106 | + amount: 1000, |
| 107 | + }); |
| 108 | + assert.equal(synthBalance, 1000); |
| 109 | + assert.equal( |
| 110 | + await getTokenBalance({ |
| 111 | + walletAddress: address, |
| 112 | + tokenAddress: require('../../deployments/extras.json').synth_stata_usdc_token_address, |
| 113 | + }), |
| 114 | + 1000 |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should swap 500 sStataUSDC -> snxUSD', async () => { |
| 119 | + assert.equal(await getCollateralBalance({ address, symbol: 'snxUSD' }), 0); |
| 120 | + await spotSell({ |
| 121 | + wallet, |
| 122 | + marketId: require('../../deployments/extras.json').synth_stata_usdc_market_id, |
| 123 | + synthAmount: 500, |
| 124 | + minUsdAmount: 400, |
| 125 | + }); |
| 126 | + assert.ok( |
| 127 | + (await getCollateralBalance({ address, symbol: 'snxUSD' })) >= 400, |
| 128 | + 'snxUSD balance >= 400' |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should approve snxUSD spending for SpotMarket', async () => { |
| 133 | + assert.equal( |
| 134 | + await isCollateralApproved({ |
| 135 | + address, |
| 136 | + symbol: 'snxUSD', |
| 137 | + spenderAddress: require('../../deployments/SpotMarketProxy.json').address, |
| 138 | + }), |
| 139 | + false, |
| 140 | + 'New wallet has not allowed SpotMarket snxUSD spending' |
| 141 | + ); |
| 142 | + await approveCollateral({ |
| 143 | + privateKey, |
| 144 | + symbol: 'snxUSD', |
| 145 | + spenderAddress: require('../../deployments/SpotMarketProxy.json').address, |
| 146 | + }); |
| 147 | + assert.equal( |
| 148 | + await isCollateralApproved({ |
| 149 | + address, |
| 150 | + symbol: 'snxUSD', |
| 151 | + spenderAddress: require('../../deployments/SpotMarketProxy.json').address, |
| 152 | + }), |
| 153 | + true |
| 154 | + ); |
| 155 | + }); |
| 156 | + |
| 157 | + it('should swap 400 snxUSD -> sStataUSDC', async () => { |
| 158 | + await spotBuy({ |
| 159 | + wallet, |
| 160 | + marketId: require('../../deployments/extras.json').synth_stata_usdc_market_id, |
| 161 | + usdAmount: 400, |
| 162 | + minAmountReceived: 300, |
| 163 | + }); |
| 164 | + assert.ok( |
| 165 | + (await getTokenBalance({ |
| 166 | + walletAddress: address, |
| 167 | + tokenAddress: require('../../deployments/extras.json').synth_stata_usdc_token_address, |
| 168 | + })) >= |
| 169 | + 500 + 300, |
| 170 | + `sStataUSDC balance >= ${500 + 300}` |
| 171 | + ); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should unwrap 500 sStataUSDC -> stataBasUSDC', async () => { |
| 175 | + const synthBalance = await unwrapCollateral({ |
| 176 | + wallet, |
| 177 | + symbol: 'stataBasUSDC', |
| 178 | + synthAddress: require('../../deployments/extras.json').synth_stata_usdc_token_address, |
| 179 | + synthMarketId: require('../../deployments/extras.json').synth_stata_usdc_market_id, |
| 180 | + amount: 500, |
| 181 | + }); |
| 182 | + assert.ok(synthBalance < 500); |
| 183 | + assert.equal(await getCollateralBalance({ address, symbol: 'stataBasUSDC' }), 500); |
| 184 | + }); |
| 185 | +}); |
0 commit comments