Skip to content

Commit fddf1b3

Browse files
authored
Merge pull request #6 from mradkov/feature/tests
Add tests
2 parents 74a947e + 7b72141 commit fddf1b3

File tree

5 files changed

+220
-85
lines changed

5 files changed

+220
-85
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@aeternity/aepp-sdk": "^7.5.0",
2121
"aeproject-lib": "^2.2.0",
2222
"bignumber.js": "^9.0.0",
23-
"esm": "^3.2.25"
23+
"esm": "^3.2.25",
24+
"it-each": "^0.4.0"
2425
}
2526
}

test/bondCurveLinearTest.js

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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+
});

test/data.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
module.exports = [
2+
{
3+
totalSupply: 0,
4+
buy: { amount: 1123, aettos: 631688 },
5+
sell: { amount: 1, aettos: 0 },
6+
},
7+
{
8+
totalSupply: 2,
9+
buy: { amount: 44234, aettos: 978456080 },
10+
sell: { amount: 11231, aettos: 0 },
11+
},
12+
{
13+
totalSupply: 3,
14+
buy: { amount: 118723, aettos: 7048050257 },
15+
sell: { amount: 15523, aettos: 0 },
16+
},
17+
{
18+
totalSupply: 7,
19+
buy: { amount: 747239841, aettos: '279183695966771369' },
20+
sell: { amount: 3123123, aettos: 0 },
21+
},
22+
{
23+
totalSupply: 42,
24+
buy: { amount: 2348231, aettos: 2757195388614 },
25+
sell: { amount: 234923, aettos: 0 },
26+
},
27+
{
28+
totalSupply: 144,
29+
buy: { amount: 2349241, aettos: 2759807277986 },
30+
sell: { amount: 2349921, aettos: 0 },
31+
},
32+
{
33+
totalSupply: 256,
34+
buy: { amount: 5437811, aettos: 14786291753288 },
35+
sell: { amount: 42394, aettos: 0 },
36+
},
37+
{
38+
totalSupply: 1237,
39+
buy: { amount: 2349021, aettos: 2761857917219 },
40+
sell: { amount: 1283712, aettos: 0 },
41+
},
42+
{
43+
totalSupply: 88321,
44+
buy: { amount: 32095831, aettos: 517905951775863 },
45+
sell: { amount: 13882, aettos: 1129717160 },
46+
},
47+
{
48+
totalSupply: 9238123,
49+
buy: { amount: 1238171, aettos: 12204910943825 },
50+
sell: { amount: 142349, aettos: 1304905952027 },
51+
},
52+
{
53+
totalSupply: 2138123173912,
54+
buy: { amount: 2349241, aettos: '5022969382673188074' },
55+
sell: { amount: 49214234, aettos: '105224883181313760030' },
56+
},
57+
{
58+
totalSupply: 123987123123817,
59+
buy: { amount: 1223492, aettos: '151697253993472669488' },
60+
sell: { amount: 2349293492, aettos: '291279381856630211521932' },
61+
},
62+
];

test/exampleTest.js

Lines changed: 0 additions & 84 deletions
This file was deleted.

test/fracTest.js

Whitespace-only changes.

0 commit comments

Comments
 (0)