flash-sdk 1.0.0

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.
Files changed (98) hide show
  1. package/lib/CustodyAccount.d.ts +29 -0
  2. package/lib/CustodyAccount.d.ts.map +1 -0
  3. package/lib/CustodyAccount.js +64 -0
  4. package/lib/CustodyAccount.js.map +1 -0
  5. package/lib/OraclePrice.d.ts +18 -0
  6. package/lib/OraclePrice.d.ts.map +1 -0
  7. package/lib/OraclePrice.js +83 -0
  8. package/lib/OraclePrice.js.map +1 -0
  9. package/lib/PerpetualsClient.d.ts +1937 -0
  10. package/lib/PerpetualsClient.d.ts.map +1 -0
  11. package/lib/PerpetualsClient.js +1013 -0
  12. package/lib/PerpetualsClient.js.map +1 -0
  13. package/lib/PoolAccount.d.ts +34 -0
  14. package/lib/PoolAccount.d.ts.map +1 -0
  15. package/lib/PoolAccount.js +306 -0
  16. package/lib/PoolAccount.js.map +1 -0
  17. package/lib/PositionAccount.d.ts +26 -0
  18. package/lib/PositionAccount.d.ts.map +1 -0
  19. package/lib/PositionAccount.js +90 -0
  20. package/lib/PositionAccount.js.map +1 -0
  21. package/lib/client/src/CustodyAccount.d.ts +30 -0
  22. package/lib/client/src/CustodyAccount.d.ts.map +1 -0
  23. package/lib/client/src/CustodyAccount.js +64 -0
  24. package/lib/client/src/CustodyAccount.js.map +1 -0
  25. package/lib/client/src/OraclePrice.d.ts +19 -0
  26. package/lib/client/src/OraclePrice.d.ts.map +1 -0
  27. package/lib/client/src/OraclePrice.js +84 -0
  28. package/lib/client/src/OraclePrice.js.map +1 -0
  29. package/lib/client/src/PerpetualsClient.d.ts +1940 -0
  30. package/lib/client/src/PerpetualsClient.d.ts.map +1 -0
  31. package/lib/client/src/PerpetualsClient.js +1013 -0
  32. package/lib/client/src/PerpetualsClient.js.map +1 -0
  33. package/lib/client/src/PoolAccount.d.ts +35 -0
  34. package/lib/client/src/PoolAccount.d.ts.map +1 -0
  35. package/lib/client/src/PoolAccount.js +307 -0
  36. package/lib/client/src/PoolAccount.js.map +1 -0
  37. package/lib/client/src/PositionAccount.d.ts +27 -0
  38. package/lib/client/src/PositionAccount.d.ts.map +1 -0
  39. package/lib/client/src/PositionAccount.js +91 -0
  40. package/lib/client/src/PositionAccount.js.map +1 -0
  41. package/lib/client/src/constants/index.d.ts +12 -0
  42. package/lib/client/src/constants/index.d.ts.map +1 -0
  43. package/lib/client/src/constants/index.js +15 -0
  44. package/lib/client/src/constants/index.js.map +1 -0
  45. package/lib/client/src/index.d.ts +8 -0
  46. package/lib/client/src/index.d.ts.map +1 -0
  47. package/lib/client/src/index.js +24 -0
  48. package/lib/client/src/index.js.map +1 -0
  49. package/lib/client/src/target/types/perpetuals.d.ts +3539 -0
  50. package/lib/client/src/target/types/perpetuals.d.ts.map +1 -0
  51. package/lib/client/src/target/types/perpetuals.js +3541 -0
  52. package/lib/client/src/target/types/perpetuals.js.map +1 -0
  53. package/lib/client/src/types/index.d.ts +188 -0
  54. package/lib/client/src/types/index.d.ts.map +1 -0
  55. package/lib/client/src/types/index.js +57 -0
  56. package/lib/client/src/types/index.js.map +1 -0
  57. package/lib/client/src/utils/helpers.d.ts +8 -0
  58. package/lib/client/src/utils/helpers.d.ts.map +1 -0
  59. package/lib/client/src/utils/helpers.js +152 -0
  60. package/lib/client/src/utils/helpers.js.map +1 -0
  61. package/lib/constants/index.d.ts +11 -0
  62. package/lib/constants/index.d.ts.map +1 -0
  63. package/lib/constants/index.js +14 -0
  64. package/lib/constants/index.js.map +1 -0
  65. package/lib/index.d.ts +8 -0
  66. package/lib/index.d.ts.map +1 -0
  67. package/lib/index.js +24 -0
  68. package/lib/index.js.map +1 -0
  69. package/lib/target/types/perpetuals.d.ts +3538 -0
  70. package/lib/target/types/perpetuals.d.ts.map +1 -0
  71. package/lib/target/types/perpetuals.js +3540 -0
  72. package/lib/target/types/perpetuals.js.map +1 -0
  73. package/lib/tsconfig.tsbuildinfo +1 -0
  74. package/lib/types/index.d.ts +187 -0
  75. package/lib/types/index.d.ts.map +1 -0
  76. package/lib/types/index.js +56 -0
  77. package/lib/types/index.js.map +1 -0
  78. package/lib/utils/helpers.d.ts +7 -0
  79. package/lib/utils/helpers.d.ts.map +1 -0
  80. package/lib/utils/helpers.js +151 -0
  81. package/lib/utils/helpers.js.map +1 -0
  82. package/lib/utils/index.d.ts +7 -0
  83. package/lib/utils/index.js +151 -0
  84. package/package.json +35 -0
  85. package/readme.md +4 -0
  86. package/src/CustodyAccount.ts +90 -0
  87. package/src/OraclePrice.ts +108 -0
  88. package/src/PerpetualsClient.ts +983 -0
  89. package/src/PoolAccount.ts +434 -0
  90. package/src/PositionAccount.ts +57 -0
  91. package/src/constants/index.ts +17 -0
  92. package/src/index.ts +14 -0
  93. package/src/readme.md +4 -0
  94. package/src/target/types/perpetuals.js +3540 -0
  95. package/src/target/types/perpetuals.ts +7075 -0
  96. package/src/types/index.ts +260 -0
  97. package/src/utils/index.ts +169 -0
  98. package/tsconfig.json +23 -0
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.checkedDecimalDiv = exports.checkedDecimalMul = exports.checkedDecimalCeilMul = exports.checkedCeilDiv = exports.scaleToExponent = exports.getUnixTs = void 0;
7
+ var bn_js_1 = __importDefault(require("bn.js"));
8
+ var constants_1 = require("../constants");
9
+ var getUnixTs = function () {
10
+ return new Date().getTime() / 1000;
11
+ };
12
+ exports.getUnixTs = getUnixTs;
13
+ // recheck ?? logic
14
+ var scaleToExponent = function (arg, exponent, target_exponent) {
15
+ if (target_exponent.eq(exponent)) {
16
+ return arg;
17
+ }
18
+ var delta = target_exponent.sub(exponent);
19
+ if (delta.gt(constants_1.BN_ZERO)) {
20
+ // checked_div(arg, checked_pow(10, delta as usize)?)
21
+ return arg.div(new bn_js_1.default(10).pow(delta));
22
+ }
23
+ else {
24
+ // checked_mul(arg, checked_pow(10, (-delta) as usize)?)
25
+ return arg.mul(new bn_js_1.default(10).pow(delta.muln(-1)));
26
+ }
27
+ };
28
+ exports.scaleToExponent = scaleToExponent;
29
+ // ceil(a/b) = ((a + b - 1) / b) , if a>=0
30
+ var checkedCeilDiv = function (arg1, arg2) {
31
+ if (arg1.gt(constants_1.BN_ZERO)) {
32
+ if (arg1.eq(arg2) && !arg2.isZero()) {
33
+ return constants_1.BN_ONE;
34
+ }
35
+ var res = (arg1.sub(constants_1.BN_ONE)).div(arg2);
36
+ if (!res) {
37
+ throw Error("error :: MathOverflow");
38
+ }
39
+ return res.add(constants_1.BN_ONE);
40
+ }
41
+ else {
42
+ var res = (arg1).div(arg2);
43
+ if (!res) {
44
+ throw Error("error :: MathOverflow");
45
+ }
46
+ return res;
47
+ }
48
+ };
49
+ exports.checkedCeilDiv = checkedCeilDiv;
50
+ var checkedDecimalCeilMul = function (coefficient1, exponent1, coefficient2, exponent2, target_exponent) {
51
+ if (coefficient1.isZero() || coefficient2.isZero()) {
52
+ return constants_1.BN_ZERO;
53
+ }
54
+ var target_power = (exponent1.add(exponent2)).sub(target_exponent);
55
+ if (target_power.gt(constants_1.BN_ZERO)) {
56
+ // checked_as_u64(checked_mul(
57
+ // checked_mul(coefficient1 as u128, coefficient2 as u128)?,
58
+ // checked_pow(10u128, target_power as usize)?,
59
+ // )?)
60
+ return (coefficient1.mul(coefficient2)).mul(new bn_js_1.default(10).pow(target_power));
61
+ }
62
+ else {
63
+ // checked_as_u64(checked_ceil_div(
64
+ // checked_mul(coefficient1 as u128, coefficient2 as u128)?,
65
+ // checked_pow(10u128, (-target_power) as usize)?,
66
+ // )?)
67
+ // ceil(a/b) = ((a + b - 1) / b) , if a>=0
68
+ //
69
+ var a = (coefficient1.mul(coefficient2));
70
+ var b = (new bn_js_1.default(10).pow(target_power.muln(-1)));
71
+ // return (a.add(b).subn(1)).div(b);
72
+ return (0, exports.checkedCeilDiv)(a, b);
73
+ }
74
+ };
75
+ exports.checkedDecimalCeilMul = checkedDecimalCeilMul;
76
+ var checkedDecimalMul = function (coefficient1, exponent1, coefficient2, exponent2, target_exponent) {
77
+ if (coefficient1.isZero() || coefficient2.isZero()) {
78
+ return constants_1.BN_ZERO;
79
+ }
80
+ var target_power = (exponent1.add(exponent2)).sub(target_exponent);
81
+ if (target_power.gt(constants_1.BN_ZERO)) {
82
+ // checked_as_u64(checked_mul(
83
+ // checked_mul(coefficient1 as u128, coefficient2 as u128)?,
84
+ // checked_pow(10u128, target_power as usize)?,
85
+ // )?)
86
+ return (coefficient1.mul(coefficient2)).mul(new bn_js_1.default(10).pow(target_power));
87
+ }
88
+ else {
89
+ // checked_as_u64(checked_div(
90
+ // checked_mul(coefficient1 as u128, coefficient2 as u128)?,
91
+ // checked_pow(10u128, (-target_power) as usize)?,
92
+ // )?)
93
+ return (coefficient1.mul(coefficient2))
94
+ .div(new bn_js_1.default(10).pow(target_power.muln(-1)));
95
+ }
96
+ };
97
+ exports.checkedDecimalMul = checkedDecimalMul;
98
+ var checkedDecimalDiv = function (coefficient1, exponent1, coefficient2, exponent2, target_exponent) {
99
+ if (coefficient2.isZero()) {
100
+ // msg!("Error: Overflow in {} / {}", coefficient1, coefficient2);
101
+ // return err!(PerpetualsError::MathOverflow);
102
+ throw Error("\"Error: Overflow in ".concat(coefficient1, " / ").concat(coefficient2));
103
+ }
104
+ if (coefficient1.isZero()) {
105
+ return constants_1.BN_ZERO;
106
+ }
107
+ // compute scale factor for the dividend
108
+ var scale_factor = constants_1.BN_ZERO;
109
+ var target_power = (exponent1.sub(exponent2)).sub(target_exponent);
110
+ if (exponent1.gt(constants_1.BN_ZERO)) {
111
+ scale_factor = scale_factor.add(exponent1);
112
+ }
113
+ if (exponent2.lt(constants_1.BN_ZERO)) {
114
+ scale_factor = scale_factor.sub(exponent2);
115
+ target_power = target_power.add(exponent2);
116
+ }
117
+ if (target_exponent.lt(constants_1.BN_ZERO)) {
118
+ scale_factor = scale_factor.sub(target_exponent);
119
+ target_power = target_power.add(target_exponent);
120
+ }
121
+ var scaled_coeff1 = constants_1.BN_ZERO;
122
+ if (scale_factor.gt(constants_1.BN_ZERO)) {
123
+ // checked_mul(
124
+ // coefficient1 ,
125
+ // checked_pow(10u128, scale_factor as usize)?,
126
+ // )?
127
+ scaled_coeff1 = coefficient1.mul(new bn_js_1.default(10).pow(scale_factor));
128
+ }
129
+ else {
130
+ scaled_coeff1 = coefficient1;
131
+ }
132
+ ;
133
+ if (target_power.gte(constants_1.BN_ZERO)) {
134
+ // return (
135
+ // checked_mul(
136
+ // checked_div(scaled_coeff1, coefficient2),
137
+ // checked_pow(10u128, target_power),
138
+ // ))
139
+ return (scaled_coeff1.div(coefficient2))
140
+ .mul(new bn_js_1.default(10).pow(target_power));
141
+ }
142
+ else {
143
+ // return checked_as_u64(checked_div(
144
+ // checked_div(scaled_coeff1, coefficient2 as u128)?,
145
+ // checked_pow(10u128, (-target_power) as usize)?,
146
+ // ))
147
+ return (scaled_coeff1.div(coefficient2))
148
+ .div(new bn_js_1.default(10).pow(target_power.muln(-1)));
149
+ }
150
+ };
151
+ exports.checkedDecimalDiv = checkedDecimalDiv;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../src/utils/helpers.ts"],"names":[],"mappings":";;;;;;AAAA,gDAAuB;AACvB,0CAA+C;AAExC,IAAM,SAAS,GAAG;IACrB,OAAO,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,GAAG,IAAI,CAAC;AACrC,CAAC,CAAC;AAFS,QAAA,SAAS,aAElB;AAGF,oBAAoB;AACf,IAAM,eAAe,GAAG,UAAC,GAAO,EAAE,QAAY,EAAE,eAAmB;IACtE,IAAI,eAAe,CAAC,EAAE,CAAC,QAAQ,CAAC,EAAE;QAC9B,OAAO,GAAG,CAAC;KACd;IACD,IAAI,KAAK,GAAG,eAAe,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC1C,IAAI,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are const scaleToExponent: (arg: BN, exponent: BN, target_exponent: BN) => BN;
4
+ export declare const checkedCeilDiv: (arg1: BN, arg2: BN) => BN;
5
+ export declare const checkedDecimalCeilMul: (coefficient1: BN, exponent1: BN, coefficient2: BN, exponent2: BN, target_exponent: BN) => BN;
6
+ export declare const checkedDecimalMul: (coefficient1: BN, exponent1: BN, coefficient2: BN, exponent2: BN, target_exponent: BN) => BN;
7
+ export declare const checkedDecimalDiv: (coefficient1: BN, exponent1: BN, coefficient2: BN, exponent2: BN, target_exponent: BN) => BN;
@@ -0,0 +1,151 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.checkedDecimalDiv = exports.checkedDecimalMul = exports.checkedDecimalCeilMul = exports.checkedCeilDiv = exports.scaleToExponent = exports.getUnixTs = void 0;
7
+ var bn_js_1 = __importDefault(require("bn.js"));
8
+ var constants_1 = require("../constants");
9
+ var getUnixTs = function () {
10
+ return new Date().getTime() / 1000;
11
+ };
12
+ exports.getUnixTs = getUnixTs;
13
+ // recheck ?? logic
14
+ var scaleToExponent = function (arg, exponent, target_exponent) {
15
+ if (target_exponent.eq(exponent)) {
16
+ return arg;
17
+ }
18
+ var delta = target_exponent.sub(exponent);
19
+ if (delta.gt(constants_1.BN_ZERO)) {
20
+ // checked_div(arg, checked_pow(10, delta as usize)?)
21
+ return arg.div(new bn_js_1.default(10).pow(delta));
22
+ }
23
+ else {
24
+ // checked_mul(arg, checked_pow(10, (-delta) as usize)?)
25
+ return arg.mul(new bn_js_1.default(10).pow(delta.muln(-1)));
26
+ }
27
+ };
28
+ exports.scaleToExponent = scaleToExponent;
29
+ // ceil(a/b) = ((a + b - 1) / b) , if a>=0
30
+ var checkedCeilDiv = function (arg1, arg2) {
31
+ if (arg1.gt(constants_1.BN_ZERO)) {
32
+ if (arg1.eq(arg2) && !arg2.isZero()) {
33
+ return constants_1.BN_ONE;
34
+ }
35
+ var res = (arg1.sub(constants_1.BN_ONE)).div(arg2);
36
+ if (!res) {
37
+ throw Error("error :: MathOverflow");
38
+ }
39
+ return res.add(constants_1.BN_ONE);
40
+ }
41
+ else {
42
+ var res = (arg1).div(arg2);
43
+ if (!res) {
44
+ throw Error("error :: MathOverflow");
45
+ }
46
+ return res;
47
+ }
48
+ };
49
+ exports.checkedCeilDiv = checkedCeilDiv;
50
+ var checkedDecimalCeilMul = function (coefficient1, exponent1, coefficient2, exponent2, target_exponent) {
51
+ if (coefficient1.isZero() || coefficient2.isZero()) {
52
+ return constants_1.BN_ZERO;
53
+ }
54
+ var target_power = (exponent1.add(exponent2)).sub(target_exponent);
55
+ if (target_power.gt(constants_1.BN_ZERO)) {
56
+ // checked_as_u64(checked_mul(
57
+ // checked_mul(coefficient1 as u128, coefficient2 as u128)?,
58
+ // checked_pow(10u128, target_power as usize)?,
59
+ // )?)
60
+ return (coefficient1.mul(coefficient2)).mul(new bn_js_1.default(10).pow(target_power));
61
+ }
62
+ else {
63
+ // checked_as_u64(checked_ceil_div(
64
+ // checked_mul(coefficient1 as u128, coefficient2 as u128)?,
65
+ // checked_pow(10u128, (-target_power) as usize)?,
66
+ // )?)
67
+ // ceil(a/b) = ((a + b - 1) / b) , if a>=0
68
+ //
69
+ var a = (coefficient1.mul(coefficient2));
70
+ var b = (new bn_js_1.default(10).pow(target_power.muln(-1)));
71
+ // return (a.add(b).subn(1)).div(b);
72
+ return (0, exports.checkedCeilDiv)(a, b);
73
+ }
74
+ };
75
+ exports.checkedDecimalCeilMul = checkedDecimalCeilMul;
76
+ var checkedDecimalMul = function (coefficient1, exponent1, coefficient2, exponent2, target_exponent) {
77
+ if (coefficient1.isZero() || coefficient2.isZero()) {
78
+ return constants_1.BN_ZERO;
79
+ }
80
+ var target_power = (exponent1.add(exponent2)).sub(target_exponent);
81
+ if (target_power.gt(constants_1.BN_ZERO)) {
82
+ // checked_as_u64(checked_mul(
83
+ // checked_mul(coefficient1 as u128, coefficient2 as u128)?,
84
+ // checked_pow(10u128, target_power as usize)?,
85
+ // )?)
86
+ return (coefficient1.mul(coefficient2)).mul(new bn_js_1.default(10).pow(target_power));
87
+ }
88
+ else {
89
+ // checked_as_u64(checked_div(
90
+ // checked_mul(coefficient1 as u128, coefficient2 as u128)?,
91
+ // checked_pow(10u128, (-target_power) as usize)?,
92
+ // )?)
93
+ return (coefficient1.mul(coefficient2))
94
+ .div(new bn_js_1.default(10).pow(target_power.muln(-1)));
95
+ }
96
+ };
97
+ exports.checkedDecimalMul = checkedDecimalMul;
98
+ var checkedDecimalDiv = function (coefficient1, exponent1, coefficient2, exponent2, target_exponent) {
99
+ if (coefficient2.isZero()) {
100
+ // msg!("Error: Overflow in {} / {}", coefficient1, coefficient2);
101
+ // return err!(PerpetualsError::MathOverflow);
102
+ throw Error("\"Error: Overflow in ".concat(coefficient1, " / ").concat(coefficient2));
103
+ }
104
+ if (coefficient1.isZero()) {
105
+ return constants_1.BN_ZERO;
106
+ }
107
+ // compute scale factor for the dividend
108
+ var scale_factor = constants_1.BN_ZERO;
109
+ var target_power = (exponent1.sub(exponent2)).sub(target_exponent);
110
+ if (exponent1.gt(constants_1.BN_ZERO)) {
111
+ scale_factor = scale_factor.add(exponent1);
112
+ }
113
+ if (exponent2.lt(constants_1.BN_ZERO)) {
114
+ scale_factor = scale_factor.sub(exponent2);
115
+ target_power = target_power.add(exponent2);
116
+ }
117
+ if (target_exponent.lt(constants_1.BN_ZERO)) {
118
+ scale_factor = scale_factor.sub(target_exponent);
119
+ target_power = target_power.add(target_exponent);
120
+ }
121
+ var scaled_coeff1 = constants_1.BN_ZERO;
122
+ if (scale_factor.gt(constants_1.BN_ZERO)) {
123
+ // checked_mul(
124
+ // coefficient1 ,
125
+ // checked_pow(10u128, scale_factor as usize)?,
126
+ // )?
127
+ scaled_coeff1 = coefficient1.mul(new bn_js_1.default(10).pow(scale_factor));
128
+ }
129
+ else {
130
+ scaled_coeff1 = coefficient1;
131
+ }
132
+ ;
133
+ if (target_power.gte(constants_1.BN_ZERO)) {
134
+ // return (
135
+ // checked_mul(
136
+ // checked_div(scaled_coeff1, coefficient2),
137
+ // checked_pow(10u128, target_power),
138
+ // ))
139
+ return (scaled_coeff1.div(coefficient2))
140
+ .mul(new bn_js_1.default(10).pow(target_power));
141
+ }
142
+ else {
143
+ // return checked_as_u64(checked_div(
144
+ // checked_div(scaled_coeff1, coefficient2 as u128)?,
145
+ // checked_pow(10u128, (-target_power) as usize)?,
146
+ // ))
147
+ return (scaled_coeff1.div(coefficient2))
148
+ .div(new bn_js_1.default(10).pow(target_power.muln(-1)));
149
+ }
150
+ };
151
+ exports.checkedDecimalDiv = checkedDecimalDiv;
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "flash-sdk",
3
+ "version": "1.0.0",
4
+ "main": "lib/index.js",
5
+ "types": "lib/index.d.ts",
6
+ "description": "Client to interact with the Flash program on Solana",
7
+ "license": "MIT",
8
+ "repository": "https://github.com/flash-trade/perpetuals-closed.git",
9
+ "author": {
10
+ "name": "Flash Trade",
11
+ "email": "admin@flash.trade",
12
+ "url": "https://flash.trade"
13
+ },
14
+ "scripts": {
15
+ "build": "tsc"
16
+ },
17
+ "dependencies": {
18
+ "@coral-xyz/anchor": "^0.27.0",
19
+ "@solana/spl-token": "^0.3.6",
20
+ "@solana/web3.js": "^1.70.1",
21
+ "@types/node": "^20.0.0",
22
+ "bs58": "^5.0.0",
23
+ "fs": "^0.0.1-security",
24
+ "js-sha256": "^0.9.0",
25
+ "jsbi": "^4",
26
+ "node-fetch": "^3.3.0",
27
+ "ts-node": "^10.9.1"
28
+ },
29
+ "devDependencies": {
30
+ "@types/bn.js": "^5.1.1",
31
+ "prettier": "^2.8.1",
32
+ "ts-node": "^10.9.1",
33
+ "typescript": "^4.9.4"
34
+ }
35
+ }
package/readme.md ADDED
@@ -0,0 +1,4 @@
1
+ # strict JS types <=> Rust types
2
+
3
+ 1) u65 : unisigned intergar - so no decimals => BN
4
+ 2) u8 : number
@@ -0,0 +1,90 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ import { Assets, FeesStats, Custody, Fees, PricingParams, TradeStats, Permissions, BorrowRateParams, OracleParams, VolumeStats, PositionStats, BorrowRateState } from "./types";
3
+ import { PositionAccount } from "./PositionAccount";
4
+ import BN from "bn.js";
5
+ import { BN_ZERO, RATE_POWER } from "./constants";
6
+ import { checkedCeilDiv } from "./utils";
7
+
8
+
9
+ export class CustodyAccount {
10
+
11
+ publicKey: PublicKey;
12
+
13
+ // all Custody Type data IMP:: SHOULD MATCH NAMES
14
+ pool: PublicKey;
15
+ mint: PublicKey;
16
+ tokenAccount: PublicKey;
17
+ decimals: number;
18
+ isStable: boolean;
19
+ oracle: OracleParams;
20
+ pricing: PricingParams;
21
+ permissions: Permissions;
22
+ fees: Fees;
23
+ borrowRate: BorrowRateParams;
24
+
25
+ assets: Assets;
26
+ collectedFees: FeesStats;
27
+ volumeStats: VolumeStats;
28
+ tradeStats: TradeStats;
29
+
30
+ longPositions: PositionStats;
31
+ shortPositions: PositionStats;
32
+ borrowRateState: BorrowRateState;
33
+
34
+ constructor( publicKey : PublicKey, parseData : Custody) {
35
+ this.publicKey = publicKey;
36
+ Object.assign(this, parseData);
37
+ }
38
+
39
+ static from( publicKey: PublicKey, parseData : Custody): CustodyAccount {
40
+ return new CustodyAccount(
41
+ publicKey,
42
+ parseData
43
+ );
44
+ }
45
+
46
+ updateCustodyData(custody: Custody) {
47
+ Object.assign(this,{...custody})
48
+ }
49
+
50
+ getCumulativeInterest(curtime: BN) :BN {
51
+ if (curtime.gt(this.borrowRateState.lastUpdate)) {
52
+ let cumulative_interest = checkedCeilDiv(
53
+ (curtime.sub(this.borrowRateState.lastUpdate)).mul(this.borrowRateState.currentRate),
54
+ new BN(3600)
55
+ )
56
+ return this.borrowRateState.cumulativeInterest.add(cumulative_interest);
57
+ } else {
58
+ return (this.borrowRateState.cumulativeInterest)
59
+ }
60
+ }
61
+
62
+
63
+ getInterestAmountUsd( position: PositionAccount, curtime: BN) : BN {
64
+ if (position.sizeUsd.isZero()) {
65
+ return BN_ZERO;
66
+ }
67
+
68
+ let cumulative_interest = this.getCumulativeInterest(curtime);
69
+ console.log("cumulative_interest:",cumulative_interest.toString())
70
+
71
+ let position_interest : BN;
72
+ if (cumulative_interest.gt(position.cumulativeInterestSnapshot)) {
73
+ position_interest = cumulative_interest.sub(position.cumulativeInterestSnapshot);
74
+ } else {
75
+ position_interest = BN_ZERO;
76
+ };
77
+
78
+ // math::checked_as_u64(math::checked_div(
79
+ // math::checked_mul(position_interest, position.size_usd as u128)?,
80
+ // Perpetuals::RATE_POWER,
81
+ // )?)
82
+ return (position_interest.mul(position.sizeUsd))
83
+ .div(new BN(RATE_POWER))
84
+
85
+ }
86
+
87
+
88
+
89
+
90
+ }
@@ -0,0 +1,108 @@
1
+ import { BN } from "@coral-xyz/anchor";
2
+ import { Connection, PublicKey } from "@solana/web3.js";
3
+ import { isVariant, Position, Side } from "./types";
4
+ import { BN_ZERO, USD_DECIMALS } from "./constants";
5
+ import { checkedDecimalDiv, checkedDecimalMul } from "./utils";
6
+
7
+ // use something similar to mango v3 FixedNum I80F48 type : https://github.com/blockworks-foundation/mango-client-v3/blob/e81ac7cf127f4acbf5585dd2b1c0cabef1d6c437/src/utils/fixednum.ts#L8
8
+
9
+
10
+ export class OraclePrice {
11
+
12
+ price: BN;
13
+ exponent: BN;
14
+
15
+ constructor( parseData : { price : BN, exponent: BN}) {
16
+ Object.assign(this, parseData);
17
+ }
18
+
19
+ static from(parseData : { price : BN, exponent: BN}): OraclePrice {
20
+ return new OraclePrice(parseData);
21
+ }
22
+
23
+ cmp(other: OraclePrice): -1 | 0 | 1 {
24
+ // TODO make sure this works when they're diff signs or 0
25
+ // return this.data.cmp(x.getData());
26
+ let lhs:BN, rhs:BN;
27
+ if (this.exponent.eq(other.exponent)) {
28
+ lhs =this.price;
29
+ rhs = other.price;
30
+ } else if (this.exponent.lt(other.exponent)) {
31
+ // if let Ok(scaled_price) = other.scale_to_exponent(this.exponent) {
32
+ // (this.price, scaled_price.price)
33
+ // } else {
34
+ // return None;
35
+ // }
36
+ let scaled_price = other.scale_to_exponent(this.exponent);
37
+ lhs =this.price;
38
+ rhs = scaled_price.price;
39
+
40
+ } else {
41
+ let scaled_price = this.scale_to_exponent(other.exponent);
42
+ lhs =scaled_price.price;
43
+ rhs = other.price;
44
+ };
45
+ return lhs.cmp(rhs);
46
+ }
47
+
48
+
49
+ scale_to_exponent( target_exponent: BN) : OraclePrice {
50
+ if (target_exponent.eq(this.exponent)) {
51
+ return this;
52
+ }
53
+ let delta = target_exponent.sub(this.exponent);
54
+ if (delta.gt(BN_ZERO)) {
55
+ // Ok(OraclePrice {
56
+ // price: math::checked_div(self.price, math::checked_pow(10, delta as usize)?)?,
57
+ // exponent: target_exponent,
58
+ // })
59
+ return new OraclePrice({
60
+ price : this.price.div(new BN(10).pow(delta)),
61
+ exponent : target_exponent
62
+ })
63
+
64
+ } else {
65
+ // Ok(OraclePrice {
66
+ // price: math::checked_mul(self.price, math::checked_pow(10, (-delta) as usize)?)?,
67
+ // exponent: target_exponent,
68
+ // })
69
+ return new OraclePrice({
70
+ price : this.price.mul(new BN(10).pow(delta.mul(new BN(-1)))),
71
+ exponent : target_exponent
72
+ })
73
+ }
74
+ }
75
+
76
+ // Converts USD amount with implied USD_DECIMALS decimals to token amount
77
+ getTokenAmount( asset_amount_usd: BN, token_decimals: number) : BN {
78
+ if (asset_amount_usd.isZero() || this.price.isZero()) {
79
+ return BN_ZERO;
80
+ }
81
+ return checkedDecimalDiv(
82
+ asset_amount_usd,
83
+ new BN(-1* USD_DECIMALS),
84
+ this.price,
85
+ this.exponent,
86
+ new BN(-1 * token_decimals),
87
+ )
88
+ }
89
+
90
+ // Converts token amount to USD with implied USD_DECIMALS decimals using oracle price
91
+ getAssetAmountUsd( token_amount: BN, token_decimals: number) : BN {
92
+ if (token_amount.isZero() || this.price.isZero()) {
93
+ return BN_ZERO;
94
+ }
95
+ return checkedDecimalMul(
96
+ token_amount,
97
+ new BN(-1 * token_decimals),
98
+ this.price,
99
+ this.exponent,
100
+ new BN(-1 * USD_DECIMALS),
101
+ )
102
+ }
103
+
104
+ // get_pyth_price() {
105
+
106
+ // }
107
+
108
+ }