diff --git a/package.json b/package.json index 47024eb..fb4d3d4 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "@aeternity/aepp-sdk": "^7.5.0", "aeproject-lib": "^2.2.0", "bignumber.js": "^9.0.0", - "esm": "^3.2.25" + "esm": "^3.2.25", + "it-each": "^0.4.0" } } diff --git a/test/bondCurveLinearTest.js b/test/bondCurveLinearTest.js new file mode 100644 index 0000000..427b099 --- /dev/null +++ b/test/bondCurveLinearTest.js @@ -0,0 +1,156 @@ +/* + * ISC License (ISC) + * Copyright (c) 2018 aeternity developers + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH + * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, + * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM + * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ + +require('it-each')({ testPerIteration: true }); +const { Universal, MemoryAccount, Node } = require('@aeternity/aepp-sdk'); +const BONDING_CURVE_LINEAR_CONTRACT = utils.readFileRelative( + './contracts/BondCurveLinear.aes', + 'utf-8', +); +const testData = require('./data'); + +const config = { + url: 'http://localhost:3001/', + internalUrl: 'http://localhost:3001/', + compilerUrl: 'http://localhost:3080', +}; + +describe('Bonding Curve Contract', () => { + let client, contract; + + before(async () => { + client = await Universal({ + nodes: [ + { + name: 'devnetNode', + instance: await Node(config), + }, + ], + accounts: [ + MemoryAccount({ + keypair: wallets[0], + }), + ], + networkId: 'ae_devnet', + compilerUrl: config.compilerUrl, + }); + }); + + it('Deploying Bond Contract', async () => { + contract = await client.getContractInstance(BONDING_CURVE_LINEAR_CONTRACT); + const init = await contract.methods.init(); + assert.equal(init.result.returnType, 'ok'); + }); + + describe('Buy current price tests', () => { + it.each( + [...testData], + 'Should get buy price for supply %s', + ['element'], + (p, next) => { + contract.methods.buy_price(p.totalSupply).then((result) => { + assert.equal( + result.decodedResult, + p.totalSupply + 1, + `Buy price incorrect for supply: ${p.totalSupply}`, + ); + next(); + }); + }, + ); + }); + + describe('Sell current price tests', () => { + it.each( + [...testData], + 'Should get sell price for supply %s', + ['element'], + (p, next) => { + contract.methods.sell_price(p.totalSupply).then((result) => { + assert.equal( + result.decodedResult, + p.totalSupply, + `Sell price incorrect for supply: ${p.totalSupply}`, + ); + next(); + }); + }, + ); + }); + + describe('Calculate Buy price tests', () => { + it.each( + [...testData], + 'Should calculate buy price for supply %s', + ['element'], + (p, next) => { + contract.methods + .calculate_buy_price(p.totalSupply, p.buy.amount) + .then((result) => { + assert.equal( + result.decodedResult, + p.buy.aettos, + `Calculation for buy price incorrect for: supply=${p.totalSupply} buy_amount=${p.buy.amount}`, + ); + next(); + }); + }, + ); + }); + + describe('Sell price tests', () => { + it.each( + [...testData], + 'Should calculate sell return for supply %s', + ['element'], + (p, next) => { + if (p.totalSupply >= p.sell.amount) { + contract.methods + .calculate_sell_return(p.totalSupply, p.sell.amount) + .then((result) => { + assert.equal( + result.decodedResult, + p.sell.aettos, + `Calculation for sell price incorrect for: supply=${p.totalSupply} sell_amount=${p.sell.amount}`, + ); + next(); + }); + } else { + contract.methods + .calculate_sell_return(p.totalSupply, p.sell.amount) + .then((result) => { + assert.equal( + result.decodedResult, + p.sell.aettos, + `Calculation for sell price incorrect for: supply=${p.totalSupply} sell_amount=${p.sell.amount}`, + ); + next(); + }) + .catch((e) => { + if ( + e.decodedError.indexOf( + 'ERROR_SELL_INSUFFICIENT_TOTAL_SUPPLY' > -1, + ) + ) { + next(); + } + }); + } + }, + ); + }); +}); diff --git a/test/data.js b/test/data.js new file mode 100644 index 0000000..bf0e5a5 --- /dev/null +++ b/test/data.js @@ -0,0 +1,62 @@ +module.exports = [ + { + totalSupply: 0, + buy: { amount: 1123, aettos: 631688 }, + sell: { amount: 1, aettos: 0 }, + }, + { + totalSupply: 2, + buy: { amount: 44234, aettos: 978456080 }, + sell: { amount: 11231, aettos: 0 }, + }, + { + totalSupply: 3, + buy: { amount: 118723, aettos: 7048050257 }, + sell: { amount: 15523, aettos: 0 }, + }, + { + totalSupply: 7, + buy: { amount: 747239841, aettos: '279183695966771369' }, + sell: { amount: 3123123, aettos: 0 }, + }, + { + totalSupply: 42, + buy: { amount: 2348231, aettos: 2757195388614 }, + sell: { amount: 234923, aettos: 0 }, + }, + { + totalSupply: 144, + buy: { amount: 2349241, aettos: 2759807277986 }, + sell: { amount: 2349921, aettos: 0 }, + }, + { + totalSupply: 256, + buy: { amount: 5437811, aettos: 14786291753288 }, + sell: { amount: 42394, aettos: 0 }, + }, + { + totalSupply: 1237, + buy: { amount: 2349021, aettos: 2761857917219 }, + sell: { amount: 1283712, aettos: 0 }, + }, + { + totalSupply: 88321, + buy: { amount: 32095831, aettos: 517905951775863 }, + sell: { amount: 13882, aettos: 1129717160 }, + }, + { + totalSupply: 9238123, + buy: { amount: 1238171, aettos: 12204910943825 }, + sell: { amount: 142349, aettos: 1304905952027 }, + }, + { + totalSupply: 2138123173912, + buy: { amount: 2349241, aettos: '5022969382673188074' }, + sell: { amount: 49214234, aettos: '105224883181313760030' }, + }, + { + totalSupply: 123987123123817, + buy: { amount: 1223492, aettos: '151697253993472669488' }, + sell: { amount: 2349293492, aettos: '291279381856630211521932' }, + }, +]; diff --git a/test/exampleTest.js b/test/exampleTest.js deleted file mode 100644 index a830914..0000000 --- a/test/exampleTest.js +++ /dev/null @@ -1,84 +0,0 @@ -/* - * ISC License (ISC) - * Copyright (c) 2018 aeternity developers - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH - * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, - * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM - * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR - * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR - * PERFORMANCE OF THIS SOFTWARE. - */ - -const { Universal, MemoryAccount, Node } = require('@aeternity/aepp-sdk'); -const BONDING_CURVE_CONTRACT = utils.readFileRelative( - './contracts/Bond.aes', - 'utf-8', -); - -const config = { - url: 'http://localhost:3001/', - internalUrl: 'http://localhost:3001/', - compilerUrl: 'http://localhost:3080', -}; - -describe('Bonding Curve Contract', () => { - let client, contract; - - before(async () => { - client = await Universal({ - nodes: [ - { - name: 'devnetNode', - instance: await Node(config), - }, - ], - accounts: [ - MemoryAccount({ - keypair: wallets[0], - }), - ], - networkId: 'ae_devnet', - compilerUrl: config.compilerUrl, - }); - }); - - it('Deploying Bond Contract', async () => { - contract = await client.getContractInstance(BONDING_CURVE_CONTRACT); - const init = await contract.methods.init(); - assert.equal(init.result.returnType, 'ok'); - }); - - -// it('Bonding Curve Contract Spend Successful', async () => { -// const receiverBalanceInitial = await owner.balance(receiverPublicKey); - -// let res; - -// res = await contract.call('buy', [], { amount: 1000 }); -// console.log(await res.decode()); - -// res = await contract.call('buy', [], { amount: 1000 }); -// console.log(await res.decode()); - -// res = await contract.call('my_balance_pretty', [], { amount: 0 }); -// console.log(await res.decode()); - -// res = await contract.call('sell', [7], { amount: 0 }); -// console.log(await res.decode()); - -// res = await contract.call('sell', [1], { amount: 0 }); -// console.log(await res.decode()); - -// res = await contract.call('my_balance_pretty', [], { amount: 0 }); -// console.log(await res.decode()); - -// //const receiverBalanceAfterwards = await owner.balance(receiverPublicKey); -// //assert.equal(parseInt(receiverBalanceInitial) + 1000, parseInt(receiverBalanceAfterwards)); // don't use parseInt, use a library like bignumber.js -// }); -}); diff --git a/test/fracTest.js b/test/fracTest.js deleted file mode 100644 index e69de29..0000000