@carrot-protocol/clend-rpc 0.0.1-mrgn-fork1-dev-7be6ef2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,73 @@
1
+ import { expect } from 'chai';
2
+ import { describe, it } from 'mocha';
3
+ import { BN } from '@coral-xyz/anchor';
4
+ import { uiToAmount, amountToUi } from '../src/math';
5
+
6
+ describe('Token Amount Conversions', () => {
7
+ describe('uiToTokenAmount', () => {
8
+ it('should convert UI amount to token amount BN', () => {
9
+ // Test with 6 decimals (USDC)
10
+ expect(uiToAmount(1.5, 6).toString()).to.equal("1500000");
11
+
12
+ // Test with 8 decimals (JLP)
13
+ expect(uiToAmount(2.75, 8).toString()).to.equal("275000000");
14
+
15
+ // Test with 0 decimals
16
+ expect(uiToAmount(5, 0).toString()).to.equal("5");
17
+
18
+ // Test with very small amount
19
+ expect(uiToAmount(0.000001, 6).toString()).to.equal("1");
20
+
21
+ // Test with very large amount
22
+ expect(uiToAmount(1000000, 6).toString()).to.equal("1000000000000");
23
+ });
24
+
25
+ it('should floor decimal values', () => {
26
+ // Test with partial decimal that should be floored
27
+ expect(uiToAmount(1.5555555, 6).toString()).to.equal("1555555");
28
+ expect(uiToAmount(1.9999, 6).toString()).to.equal("1999900");
29
+ });
30
+ });
31
+
32
+ describe('tokenAmountToUi', () => {
33
+ it('should convert token amount BN to UI amount', () => {
34
+ // Test with 6 decimals (USDC)
35
+ expect(amountToUi(new BN(1500000), 6)).to.equal(1.5);
36
+
37
+ // Test with 8 decimals (JLP)
38
+ expect(amountToUi(new BN(275000000), 8)).to.equal(2.75);
39
+
40
+ // Test with 0 decimals
41
+ expect(amountToUi(new BN(5), 0)).to.equal(5);
42
+
43
+ // Test with very small amount
44
+ expect(amountToUi(new BN(1), 6)).to.equal(0.000001);
45
+
46
+ // Test with very large amount
47
+ expect(amountToUi(new BN(1000000000000), 6)).to.equal(1000000);
48
+ });
49
+
50
+ it('should handle rounding in division', () => {
51
+ // JavaScript division might introduce small rounding errors
52
+ // This test verifies that our function handles them as expected
53
+ const bn = new BN(1000001);
54
+ const result = amountToUi(bn, 6);
55
+ expect(result).to.be.closeTo(1.000001, 0.0000001);
56
+ });
57
+ });
58
+
59
+ describe('Roundtrip Conversion', () => {
60
+ it('should correctly roundtrip UI to token amount and back', () => {
61
+ const originalUI = 3.14159;
62
+ const decimals = 6;
63
+
64
+ const tokenAmount = uiToAmount(originalUI, decimals);
65
+ const roundtrippedUI = amountToUi(tokenAmount, decimals);
66
+
67
+ // Due to flooring in uiToTokenAmount, we expect the roundtripped value
68
+ // to be slightly less than or equal to the original
69
+ expect(roundtrippedUI).to.be.at.most(originalUI);
70
+ expect(roundtrippedUI).to.be.closeTo(originalUI, 0.000001); // Within 1 of smallest unit
71
+ });
72
+ });
73
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "outDir": "./dist",
4
+ "rootDir": "./",
5
+ "declaration": true,
6
+ "module": "commonjs",
7
+ "target": "es2020",
8
+ "moduleResolution": "node",
9
+ "esModuleInterop": true,
10
+ "skipLibCheck": true,
11
+ "strict": true,
12
+ "forceConsistentCasingInFileNames": true,
13
+ "sourceMap": true,
14
+ "types": ["mocha", "chai"]
15
+ },
16
+ "include": ["src/**/*", "test/**/*"]
17
+ }