|
| 1 | +/* |
| 2 | + * ISC License (ISC) |
| 3 | + * Copyright (c) 2018 aeternity developers |
| 4 | + * |
| 5 | + * Permission to use, copy, modify, and/or distribute this software for any |
| 6 | + * purpose with or without fee is hereby granted, provided that the above |
| 7 | + * copyright notice and this permission notice appear in all copies. |
| 8 | + * |
| 9 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 10 | + * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 11 | + * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 12 | + * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 13 | + * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 14 | + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 15 | + * PERFORMANCE OF THIS SOFTWARE. |
| 16 | + */ |
| 17 | + |
| 18 | +require('it-each')({ testPerIteration: true }); |
| 19 | +const { Universal, MemoryAccount, Node } = require('@aeternity/aepp-sdk'); |
| 20 | +const BONDING_CURVE_LINEAR_CONTRACT = utils.readFileRelative( |
| 21 | + './contracts/BondCurveLinear.aes', |
| 22 | + 'utf-8', |
| 23 | +); |
| 24 | +const testData = require('./data'); |
| 25 | + |
| 26 | +const config = { |
| 27 | + url: 'http://localhost:3001/', |
| 28 | + internalUrl: 'http://localhost:3001/', |
| 29 | + compilerUrl: 'http://localhost:3080', |
| 30 | +}; |
| 31 | + |
| 32 | +describe('Bonding Curve Contract', () => { |
| 33 | + let client, contract; |
| 34 | + |
| 35 | + before(async () => { |
| 36 | + client = await Universal({ |
| 37 | + nodes: [ |
| 38 | + { |
| 39 | + name: 'devnetNode', |
| 40 | + instance: await Node(config), |
| 41 | + }, |
| 42 | + ], |
| 43 | + accounts: [ |
| 44 | + MemoryAccount({ |
| 45 | + keypair: wallets[0], |
| 46 | + }), |
| 47 | + ], |
| 48 | + networkId: 'ae_devnet', |
| 49 | + compilerUrl: config.compilerUrl, |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('Deploying Bond Contract', async () => { |
| 54 | + contract = await client.getContractInstance(BONDING_CURVE_LINEAR_CONTRACT); |
| 55 | + const init = await contract.methods.init(); |
| 56 | + assert.equal(init.result.returnType, 'ok'); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('Buy current price tests', () => { |
| 60 | + it.each( |
| 61 | + [...testData], |
| 62 | + 'Should get buy price for supply %s', |
| 63 | + ['element'], |
| 64 | + (p, next) => { |
| 65 | + contract.methods.buy_price(p.totalSupply).then((result) => { |
| 66 | + assert.equal( |
| 67 | + result.decodedResult, |
| 68 | + p.totalSupply + 1, |
| 69 | + `Buy price incorrect for supply: ${p.totalSupply}`, |
| 70 | + ); |
| 71 | + next(); |
| 72 | + }); |
| 73 | + }, |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('Sell current price tests', () => { |
| 78 | + it.each( |
| 79 | + [...testData], |
| 80 | + 'Should get sell price for supply %s', |
| 81 | + ['element'], |
| 82 | + (p, next) => { |
| 83 | + contract.methods.sell_price(p.totalSupply).then((result) => { |
| 84 | + assert.equal( |
| 85 | + result.decodedResult, |
| 86 | + p.totalSupply, |
| 87 | + `Sell price incorrect for supply: ${p.totalSupply}`, |
| 88 | + ); |
| 89 | + next(); |
| 90 | + }); |
| 91 | + }, |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('Calculate Buy price tests', () => { |
| 96 | + it.each( |
| 97 | + [...testData], |
| 98 | + 'Should calculate buy price for supply %s', |
| 99 | + ['element'], |
| 100 | + (p, next) => { |
| 101 | + contract.methods |
| 102 | + .calculate_buy_price(p.totalSupply, p.buy.amount) |
| 103 | + .then((result) => { |
| 104 | + assert.equal( |
| 105 | + result.decodedResult, |
| 106 | + p.buy.aettos, |
| 107 | + `Calculation for buy price incorrect for: supply=${p.totalSupply} buy_amount=${p.buy.amount}`, |
| 108 | + ); |
| 109 | + next(); |
| 110 | + }); |
| 111 | + }, |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + describe('Sell price tests', () => { |
| 116 | + it.each( |
| 117 | + [...testData], |
| 118 | + 'Should calculate sell return for supply %s', |
| 119 | + ['element'], |
| 120 | + (p, next) => { |
| 121 | + if (p.totalSupply >= p.sell.amount) { |
| 122 | + contract.methods |
| 123 | + .calculate_sell_return(p.totalSupply, p.sell.amount) |
| 124 | + .then((result) => { |
| 125 | + assert.equal( |
| 126 | + result.decodedResult, |
| 127 | + p.sell.aettos, |
| 128 | + `Calculation for sell price incorrect for: supply=${p.totalSupply} sell_amount=${p.sell.amount}`, |
| 129 | + ); |
| 130 | + next(); |
| 131 | + }); |
| 132 | + } else { |
| 133 | + contract.methods |
| 134 | + .calculate_sell_return(p.totalSupply, p.sell.amount) |
| 135 | + .then((result) => { |
| 136 | + assert.equal( |
| 137 | + result.decodedResult, |
| 138 | + p.sell.aettos, |
| 139 | + `Calculation for sell price incorrect for: supply=${p.totalSupply} sell_amount=${p.sell.amount}`, |
| 140 | + ); |
| 141 | + next(); |
| 142 | + }) |
| 143 | + .catch((e) => { |
| 144 | + if ( |
| 145 | + e.decodedError.indexOf( |
| 146 | + 'ERROR_SELL_INSUFFICIENT_TOTAL_SUPPLY' > -1, |
| 147 | + ) |
| 148 | + ) { |
| 149 | + next(); |
| 150 | + } |
| 151 | + }); |
| 152 | + } |
| 153 | + }, |
| 154 | + ); |
| 155 | + }); |
| 156 | +}); |
0 commit comments