@orb-labs/orby-core 0.0.14 → 0.0.16

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 (137) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/cjs/actions/account_cluster.js +211 -0
  3. package/dist/cjs/actions/admin.js +43 -0
  4. package/dist/cjs/actions/application.js +20 -0
  5. package/dist/cjs/actions/blockchain.js +39 -0
  6. package/dist/cjs/actions/instance.js +98 -0
  7. package/dist/{actions → cjs/actions}/operation.d.ts +2 -2
  8. package/dist/cjs/actions/operation.js +415 -0
  9. package/dist/cjs/actions/token.js +50 -0
  10. package/dist/cjs/constants.js +139 -0
  11. package/dist/cjs/entities/account.js +49 -0
  12. package/dist/cjs/entities/financial/account_balance.js +35 -0
  13. package/dist/cjs/entities/financial/asset.js +42 -0
  14. package/dist/cjs/entities/financial/currency.js +56 -0
  15. package/dist/cjs/entities/financial/currency_amount.js +99 -0
  16. package/dist/cjs/entities/financial/fungible_token.js +68 -0
  17. package/dist/cjs/entities/financial/fungible_token_amount.js +102 -0
  18. package/dist/cjs/entities/financial/non_fungible_token.js +65 -0
  19. package/dist/cjs/entities/financial/semi_fungible_token.js +67 -0
  20. package/dist/cjs/entities/library_request.js +37 -0
  21. package/dist/cjs/entities/state.js +106 -0
  22. package/dist/cjs/enums.js +138 -0
  23. package/dist/cjs/index.js +49 -0
  24. package/dist/cjs/interfaces/account_cluster.js +2 -0
  25. package/dist/cjs/interfaces/admin.js +2 -0
  26. package/dist/cjs/interfaces/application.js +2 -0
  27. package/dist/cjs/interfaces/blockchain.js +2 -0
  28. package/dist/cjs/interfaces/instance.js +2 -0
  29. package/dist/{interfaces → cjs/interfaces}/operation.d.ts +2 -2
  30. package/dist/cjs/interfaces/operation.js +2 -0
  31. package/dist/cjs/interfaces/orby.js +2 -0
  32. package/dist/cjs/interfaces/token.js +2 -0
  33. package/dist/{types.d.ts → cjs/types.d.ts} +20 -0
  34. package/dist/cjs/types.js +2 -0
  35. package/dist/cjs/utils/action_helpers.js +274 -0
  36. package/dist/cjs/utils/utils.js +79 -0
  37. package/dist/cjs/utils/validateAndParseAddress.js +29 -0
  38. package/dist/esm/actions/account_cluster.d.ts +32 -0
  39. package/dist/esm/actions/admin.d.ts +17 -0
  40. package/dist/esm/actions/application.d.ts +6 -0
  41. package/dist/esm/actions/blockchain.d.ts +16 -0
  42. package/dist/esm/actions/instance.d.ts +15 -0
  43. package/dist/esm/actions/operation.d.ts +99 -0
  44. package/dist/{actions → esm/actions}/operation.js +75 -5
  45. package/dist/esm/actions/token.d.ts +15 -0
  46. package/dist/{actions → esm/actions}/token.js +1 -1
  47. package/dist/esm/constants.d.ts +14 -0
  48. package/dist/esm/entities/account.d.ts +14 -0
  49. package/dist/{entities → esm/entities}/account.js +1 -1
  50. package/dist/esm/entities/financial/account_balance.d.ts +12 -0
  51. package/dist/esm/entities/financial/asset.d.ts +31 -0
  52. package/dist/esm/entities/financial/currency.d.ts +41 -0
  53. package/dist/esm/entities/financial/currency_amount.d.ts +34 -0
  54. package/dist/esm/entities/financial/fungible_token.d.ts +41 -0
  55. package/dist/esm/entities/financial/fungible_token_amount.d.ts +36 -0
  56. package/dist/esm/entities/financial/non_fungible_token.d.ts +39 -0
  57. package/dist/esm/entities/financial/semi_fungible_token.d.ts +41 -0
  58. package/dist/esm/entities/library_request.d.ts +8 -0
  59. package/dist/esm/entities/state.d.ts +22 -0
  60. package/dist/esm/enums.d.ts +116 -0
  61. package/dist/esm/index.d.ts +29 -0
  62. package/dist/esm/interfaces/account_cluster.d.ts +30 -0
  63. package/dist/esm/interfaces/admin.d.ts +14 -0
  64. package/dist/esm/interfaces/application.d.ts +3 -0
  65. package/dist/esm/interfaces/blockchain.d.ts +13 -0
  66. package/dist/esm/interfaces/instance.d.ts +12 -0
  67. package/dist/esm/interfaces/operation.d.ts +97 -0
  68. package/dist/esm/interfaces/orby.d.ts +9 -0
  69. package/dist/esm/interfaces/token.d.ts +12 -0
  70. package/dist/esm/types.d.ts +172 -0
  71. package/dist/esm/utils/action_helpers.d.ts +22 -0
  72. package/dist/esm/utils/utils.d.ts +10 -0
  73. package/dist/esm/utils/validateAndParseAddress.d.ts +10 -0
  74. package/dist/tsconfig.cjs.tsbuildinfo +1 -0
  75. package/dist/tsconfig.esm.tsbuildinfo +1 -0
  76. package/package.json +16 -7
  77. package/dist/tsconfig.tsbuildinfo +0 -1
  78. /package/dist/{actions → cjs/actions}/account_cluster.d.ts +0 -0
  79. /package/dist/{actions → cjs/actions}/admin.d.ts +0 -0
  80. /package/dist/{actions → cjs/actions}/application.d.ts +0 -0
  81. /package/dist/{actions → cjs/actions}/blockchain.d.ts +0 -0
  82. /package/dist/{actions → cjs/actions}/instance.d.ts +0 -0
  83. /package/dist/{actions → cjs/actions}/token.d.ts +0 -0
  84. /package/dist/{constants.d.ts → cjs/constants.d.ts} +0 -0
  85. /package/dist/{entities → cjs/entities}/account.d.ts +0 -0
  86. /package/dist/{entities → cjs/entities}/financial/account_balance.d.ts +0 -0
  87. /package/dist/{entities → cjs/entities}/financial/asset.d.ts +0 -0
  88. /package/dist/{entities → cjs/entities}/financial/currency.d.ts +0 -0
  89. /package/dist/{entities → cjs/entities}/financial/currency_amount.d.ts +0 -0
  90. /package/dist/{entities → cjs/entities}/financial/fungible_token.d.ts +0 -0
  91. /package/dist/{entities → cjs/entities}/financial/fungible_token_amount.d.ts +0 -0
  92. /package/dist/{entities → cjs/entities}/financial/non_fungible_token.d.ts +0 -0
  93. /package/dist/{entities → cjs/entities}/financial/semi_fungible_token.d.ts +0 -0
  94. /package/dist/{entities → cjs/entities}/library_request.d.ts +0 -0
  95. /package/dist/{entities → cjs/entities}/state.d.ts +0 -0
  96. /package/dist/{enums.d.ts → cjs/enums.d.ts} +0 -0
  97. /package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
  98. /package/dist/{interfaces → cjs/interfaces}/account_cluster.d.ts +0 -0
  99. /package/dist/{interfaces → cjs/interfaces}/admin.d.ts +0 -0
  100. /package/dist/{interfaces → cjs/interfaces}/application.d.ts +0 -0
  101. /package/dist/{interfaces → cjs/interfaces}/blockchain.d.ts +0 -0
  102. /package/dist/{interfaces → cjs/interfaces}/instance.d.ts +0 -0
  103. /package/dist/{interfaces → cjs/interfaces}/orby.d.ts +0 -0
  104. /package/dist/{interfaces → cjs/interfaces}/token.d.ts +0 -0
  105. /package/dist/{utils → cjs/utils}/action_helpers.d.ts +0 -0
  106. /package/dist/{utils → cjs/utils}/utils.d.ts +0 -0
  107. /package/dist/{utils → cjs/utils}/validateAndParseAddress.d.ts +0 -0
  108. /package/dist/{actions → esm/actions}/account_cluster.js +0 -0
  109. /package/dist/{actions → esm/actions}/admin.js +0 -0
  110. /package/dist/{actions → esm/actions}/application.js +0 -0
  111. /package/dist/{actions → esm/actions}/blockchain.js +0 -0
  112. /package/dist/{actions → esm/actions}/instance.js +0 -0
  113. /package/dist/{constants.js → esm/constants.js} +0 -0
  114. /package/dist/{entities → esm/entities}/financial/account_balance.js +0 -0
  115. /package/dist/{entities → esm/entities}/financial/asset.js +0 -0
  116. /package/dist/{entities → esm/entities}/financial/currency.js +0 -0
  117. /package/dist/{entities → esm/entities}/financial/currency_amount.js +0 -0
  118. /package/dist/{entities → esm/entities}/financial/fungible_token.js +0 -0
  119. /package/dist/{entities → esm/entities}/financial/fungible_token_amount.js +0 -0
  120. /package/dist/{entities → esm/entities}/financial/non_fungible_token.js +0 -0
  121. /package/dist/{entities → esm/entities}/financial/semi_fungible_token.js +0 -0
  122. /package/dist/{entities → esm/entities}/library_request.js +0 -0
  123. /package/dist/{entities → esm/entities}/state.js +0 -0
  124. /package/dist/{enums.js → esm/enums.js} +0 -0
  125. /package/dist/{index.js → esm/index.js} +0 -0
  126. /package/dist/{interfaces → esm/interfaces}/account_cluster.js +0 -0
  127. /package/dist/{interfaces → esm/interfaces}/admin.js +0 -0
  128. /package/dist/{interfaces → esm/interfaces}/application.js +0 -0
  129. /package/dist/{interfaces → esm/interfaces}/blockchain.js +0 -0
  130. /package/dist/{interfaces → esm/interfaces}/instance.js +0 -0
  131. /package/dist/{interfaces → esm/interfaces}/operation.js +0 -0
  132. /package/dist/{interfaces → esm/interfaces}/orby.js +0 -0
  133. /package/dist/{interfaces → esm/interfaces}/token.js +0 -0
  134. /package/dist/{types.js → esm/types.js} +0 -0
  135. /package/dist/{utils → esm/utils}/action_helpers.js +0 -0
  136. /package/dist/{utils → esm/utils}/utils.js +0 -0
  137. /package/dist/{utils → esm/utils}/validateAndParseAddress.js +0 -0
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Asset = void 0;
4
+ /**
5
+ * An Asset is any fungible financial instrument, including Ether, all ERC20 tokens, and NFTs
6
+ */
7
+ class Asset {
8
+ /**
9
+ * Constructs an instance of the `Asset`.
10
+ * @param symbol symbol of the asset
11
+ * @param name of the asset
12
+ */
13
+ constructor(symbol, name, coinGeckoId) {
14
+ this.symbol = symbol;
15
+ this.name = name;
16
+ this.coinGeckoId = coinGeckoId;
17
+ }
18
+ /**
19
+ * Returns whether this asset is functionally equivalent to the other asset
20
+ * @param other the other asset
21
+ */
22
+ equals(other) {
23
+ if (this === other)
24
+ return true;
25
+ return this.symbol === other.symbol && this.name === other.name;
26
+ }
27
+ // return Asset in the "@domains/orby/src/api/graphql/generated" but could not set type here because the generated code is not available for other domains
28
+ toAssetModel() {
29
+ return { symbol: this.symbol, name: this.name };
30
+ }
31
+ static toAsset(asset) {
32
+ if (!asset) {
33
+ return undefined;
34
+ }
35
+ const { symbol, name } = asset;
36
+ return new Asset(symbol, name);
37
+ }
38
+ id() {
39
+ return `${this.name}:${this.symbol}`;
40
+ }
41
+ }
42
+ exports.Asset = Asset;
@@ -0,0 +1,56 @@
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.Currency = void 0;
7
+ const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
8
+ const asset_js_1 = require("./asset.js");
9
+ // This class is inspired by the Uniswap SDK's (@uniswap/sdk-core) CurrencyAmount class. Create this instance here
10
+ // because we represent currency amounts that are not fungible token amounts.
11
+ /**
12
+ * A currency is any fungible financial instrument, including Ether, all ERC20 tokens, and other chain-native currencies
13
+ */
14
+ class Currency extends asset_js_1.Asset {
15
+ /**
16
+ * Constructs an instance of the base class `Currency`.
17
+ * @param decimals decimals of the currency
18
+ * @param symbol symbol of the currency
19
+ * @param name of the currency
20
+ * @param isNative whether the currency is native to the chain and must be wrapped (e.g. Ether)
21
+ * @param isToken whether the currency is a token that is usable in Uniswap without wrapping
22
+ */
23
+ constructor(decimals, symbol, name, logoUrl, isNative, isToken, coinGeckoId) {
24
+ super(symbol, name, coinGeckoId);
25
+ (0, tiny_invariant_1.default)(decimals >= 0 && decimals < 255 && Number.isInteger(decimals), "DECIMALS");
26
+ this.decimals = decimals;
27
+ this.isNative = isNative ?? false;
28
+ this.isToken = isToken ?? true;
29
+ this.logoUrl = logoUrl;
30
+ }
31
+ /**
32
+ * Returns whether this currency is functionally equivalent to the other currency
33
+ * @param other the other currency
34
+ */
35
+ equals(other) {
36
+ if (this === other)
37
+ return true;
38
+ return (this.symbol === other.symbol &&
39
+ this.name === other.name &&
40
+ this.decimals === other.decimals);
41
+ }
42
+ /**
43
+ * Returns the asset representation of this currency
44
+ */
45
+ asset() {
46
+ return this;
47
+ }
48
+ static toCurrency(currency) {
49
+ if (!currency) {
50
+ return undefined;
51
+ }
52
+ const { asset, decimals, logoUrl, isNative } = currency;
53
+ return new Currency(decimals, asset.symbol, asset.name, logoUrl, isNative);
54
+ }
55
+ }
56
+ exports.Currency = Currency;
@@ -0,0 +1,99 @@
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.CurrencyAmount = void 0;
7
+ const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
8
+ const jsbi_1 = __importDefault(require("jsbi"));
9
+ const sdk_core_1 = require("@uniswap/sdk-core");
10
+ const currency_js_1 = require("./currency.js");
11
+ const constants_js_1 = require("../../constants.js");
12
+ // This class is inspired by the Uniswap SDK's (@uniswap/sdk-core) CurrencyAmount class. Created this instance here
13
+ // because we have a need to represent a currency amount that is not a fungible token amount.
14
+ class CurrencyAmount extends sdk_core_1.Fraction {
15
+ /**
16
+ * Returns a new currency amount instance from the unitless amount of token, i.e. the raw amount
17
+ * @param currency the currency in the amount
18
+ * @param rawAmount the raw token or ether amount
19
+ */
20
+ static fromRawAmount(currency, rawAmount) {
21
+ return new CurrencyAmount(currency, rawAmount);
22
+ }
23
+ static toCurrencyAmount(amount) {
24
+ if (!amount) {
25
+ return undefined;
26
+ }
27
+ const currency = currency_js_1.Currency.toCurrency(amount.currency);
28
+ return CurrencyAmount.fromRawAmount(currency, amount.value);
29
+ }
30
+ /**
31
+ * Construct a currency amount with a denominator that is not equal to 1
32
+ * @param currency the currency
33
+ * @param numerator the numerator of the fractional token amount
34
+ * @param denominator the denominator of the fractional token amount
35
+ */
36
+ static fromFractionalAmount(currency, numerator, denominator) {
37
+ return new CurrencyAmount(currency, numerator, denominator);
38
+ }
39
+ constructor(currency, numerator, denominator) {
40
+ super(numerator.toString(), denominator?.toString());
41
+ (0, tiny_invariant_1.default)(jsbi_1.default.lessThanOrEqual(this.quotient, sdk_core_1.MaxUint256), "AMOUNT");
42
+ this.currency = currency;
43
+ this.decimalScale = jsbi_1.default.exponentiate(jsbi_1.default.BigInt(10), jsbi_1.default.BigInt(currency.decimals));
44
+ }
45
+ add(other) {
46
+ if (!other) {
47
+ return this;
48
+ }
49
+ (0, tiny_invariant_1.default)(this.currency.equals(other.currency), "CURRENCY");
50
+ const added = super.add(other);
51
+ return CurrencyAmount.fromFractionalAmount(this.currency, added.numerator, added.denominator);
52
+ }
53
+ subtract(other) {
54
+ (0, tiny_invariant_1.default)(this.currency.equals(other.currency), "CURRENCY");
55
+ const subtracted = super.subtract(other);
56
+ return CurrencyAmount.fromFractionalAmount(this.currency, subtracted.numerator, subtracted.denominator);
57
+ }
58
+ multiply(other) {
59
+ const multiplied = super.multiply(other);
60
+ return CurrencyAmount.fromFractionalAmount(this.currency, multiplied.numerator, multiplied.denominator);
61
+ }
62
+ divide(other) {
63
+ const divided = super.divide(other);
64
+ return CurrencyAmount.fromFractionalAmount(this.currency, divided.numerator, divided.denominator);
65
+ }
66
+ cloneWithAmount(amount) {
67
+ return CurrencyAmount.fromRawAmount(this.currency, amount);
68
+ }
69
+ toRawAmount() {
70
+ return BigInt(this.quotient.toString());
71
+ }
72
+ toCurrencyValue(price) {
73
+ const fraction = price.multiply(this);
74
+ const denominator = jsbi_1.default.multiply(fraction.denominator, this.decimalScale);
75
+ this.amountInFiatCurrency = CurrencyAmount.fromFractionalAmount(price.currency, fraction.numerator, denominator);
76
+ return this.amountInFiatCurrency;
77
+ }
78
+ fiatValue() {
79
+ return this.amountInFiatCurrency;
80
+ }
81
+ toSignificant(significantDigits = 6, format, rounding = sdk_core_1.Rounding.ROUND_DOWN) {
82
+ return super
83
+ .divide(this.decimalScale)
84
+ .toSignificant(significantDigits, format, rounding);
85
+ }
86
+ toFixed(decimalPlaces = this.currency.decimals, format, rounding = sdk_core_1.Rounding.ROUND_DOWN) {
87
+ (0, tiny_invariant_1.default)(decimalPlaces <= this.currency.decimals, "DECIMALS");
88
+ return super
89
+ .divide(this.decimalScale)
90
+ .toFixed(decimalPlaces, format, rounding);
91
+ }
92
+ toExact(format = { groupSeparator: "" }) {
93
+ constants_js_1.Big.DP = this.currency.decimals;
94
+ return new constants_js_1.Big(this.quotient.toString())
95
+ .div(this.decimalScale.toString())
96
+ .toFormat(format);
97
+ }
98
+ }
99
+ exports.CurrencyAmount = CurrencyAmount;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FungibleToken = void 0;
4
+ const currency_js_1 = require("./currency.js");
5
+ const validateAndParseAddress_js_1 = require("../../utils/validateAndParseAddress.js");
6
+ const enums_js_1 = require("../../enums.js");
7
+ /**
8
+ * Represents an ERC20 token with a unique address and some metadata.
9
+ */
10
+ class FungibleToken extends currency_js_1.Currency {
11
+ static toFungibleToken(token) {
12
+ if (!token) {
13
+ return undefined;
14
+ }
15
+ const { address, chainId, currency, coinGeckoId, isNative } = token;
16
+ return new FungibleToken(BigInt(chainId), address, currency.decimals, currency.asset.symbol, currency.asset.name, undefined, isNative, coinGeckoId);
17
+ }
18
+ /**
19
+ * @param chainId The chain ID on which this token resides
20
+ * @param address The contract address on the chain on which this token lives
21
+ * @param decimals {@link Currency#decimals}
22
+ * @param symbol {@link Currency#symbol}
23
+ * @param name {@link Currency#name}
24
+ * @param bypassChecksum If true it only checks for length === 42, startsWith 0x and contains only hex characters
25
+ */
26
+ constructor(chainId, address, decimals, symbol, name, bypassChecksum, isNative, coinGeckoId, logoUrl) {
27
+ // TODO(felix): add this back
28
+ // invariant(!_.isUndefined(getBlockchainFromBlockchainId(chainId)), "CHAIN_ID");
29
+ super(decimals, symbol, name, logoUrl, isNative, true, coinGeckoId);
30
+ this.isToken = true;
31
+ // TODO(felix): bypassChecksum is a little confusing since when bypassChecksum is true, we still validate the address
32
+ // bypassChecksum is derived from the isNative parameter, so we should remove it and just check if isNative here
33
+ if (bypassChecksum) {
34
+ this.address = (0, validateAndParseAddress_js_1.checkValidAddress)(address)?.toLowerCase();
35
+ }
36
+ else if (!isNative) {
37
+ this.address = (0, validateAndParseAddress_js_1.validateAndParseAddress)(address)?.toLowerCase();
38
+ }
39
+ else {
40
+ this.address = address;
41
+ }
42
+ this.isNative = isNative || false;
43
+ this.chainId = chainId;
44
+ }
45
+ /**
46
+ * Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
47
+ * @param other other token to compare
48
+ */
49
+ equals(other) {
50
+ if (this === other)
51
+ return true;
52
+ return (this.chainId == other?.chainId &&
53
+ this.address.toLowerCase() == other?.address?.toLowerCase());
54
+ }
55
+ /**
56
+ * Returns the currency object that this token represents
57
+ */
58
+ currency() {
59
+ return this;
60
+ }
61
+ type() {
62
+ return enums_js_1.TokenType.FUNGIBLE_TOKEN;
63
+ }
64
+ identifier() {
65
+ return `${this.chainId}+${this.address.toLowerCase()}`;
66
+ }
67
+ }
68
+ exports.FungibleToken = FungibleToken;
@@ -0,0 +1,102 @@
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.FungibleTokenAmount = void 0;
7
+ const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
8
+ const jsbi_1 = __importDefault(require("jsbi"));
9
+ const sdk_core_1 = require("@uniswap/sdk-core");
10
+ const fungible_token_js_1 = require("./fungible_token.js");
11
+ const currency_amount_js_1 = require("./currency_amount.js");
12
+ const constants_js_1 = require("../../constants.js");
13
+ // This class is inspired by the Uniswap SDK's (@uniswap/sdk-core) CurrencyAmount class. Created this instance here
14
+ // because we have a need to represent a currency amount that is not a fungible token amount.
15
+ class FungibleTokenAmount extends sdk_core_1.Fraction {
16
+ /**
17
+ * Returns a new fungibleToken amount instance from the unitless amount of token, i.e. the raw amount
18
+ * @param fungibleToken the fungibleToken in the amount
19
+ * @param rawAmount the raw token or ether amount
20
+ */
21
+ static fromRawAmount(fungibleToken, rawAmount, amountInFiatCurrency) {
22
+ return new FungibleTokenAmount(fungibleToken, rawAmount, undefined, amountInFiatCurrency);
23
+ }
24
+ /**
25
+ * Construct a fungibleToken amount with a denominator that is not equal to 1
26
+ * @param fungibleToken the fungibleToken
27
+ * @param numerator the numerator of the fractional token amount
28
+ * @param denominator the denominator of the fractional token amount
29
+ */
30
+ static fromFractionalAmount(fungibleToken, numerator, denominator, amountInFiatCurrency) {
31
+ return new FungibleTokenAmount(fungibleToken, numerator, denominator, amountInFiatCurrency);
32
+ }
33
+ static toFungibleTokenAmount(amount) {
34
+ if (!amount) {
35
+ return undefined;
36
+ }
37
+ const amountInFiatCurrency = currency_amount_js_1.CurrencyAmount.toCurrencyAmount(amount.amountInFiatCurrency);
38
+ return FungibleTokenAmount.fromRawAmount(fungible_token_js_1.FungibleToken.toFungibleToken(amount.token), amount.value, amountInFiatCurrency);
39
+ }
40
+ constructor(fungibleToken, numerator, denominator, amountInFiatCurrency) {
41
+ super(numerator.toString(), denominator?.toString());
42
+ (0, tiny_invariant_1.default)(jsbi_1.default.lessThanOrEqual(this.quotient, sdk_core_1.MaxUint256), "AMOUNT"); // TODO(imti): fix this
43
+ this.token = fungibleToken;
44
+ this.decimalScale = jsbi_1.default.exponentiate(jsbi_1.default.BigInt(10), jsbi_1.default.BigInt(fungibleToken.decimals));
45
+ this.amountInFiatCurrency = amountInFiatCurrency;
46
+ }
47
+ equals(other) {
48
+ return (this.token.equals(other.token) &&
49
+ this.toRawAmount() == other.toRawAmount());
50
+ }
51
+ add(other) {
52
+ (0, tiny_invariant_1.default)(this.token.equals(other.token), "FUNGIBLE_TOKEN");
53
+ const added = super.add(other);
54
+ return FungibleTokenAmount.fromFractionalAmount(this.token, added.numerator, added.denominator);
55
+ }
56
+ subtract(other) {
57
+ (0, tiny_invariant_1.default)(this.token.equals(other.token), "FUNGIBLE_TOKEN");
58
+ const subtracted = super.subtract(other);
59
+ return FungibleTokenAmount.fromFractionalAmount(this.token, subtracted.numerator, subtracted.denominator);
60
+ }
61
+ multiply(other) {
62
+ const multiplied = super.multiply(other);
63
+ return FungibleTokenAmount.fromFractionalAmount(this.token, multiplied.numerator, multiplied.denominator);
64
+ }
65
+ divide(other) {
66
+ const divided = super.divide(other);
67
+ return FungibleTokenAmount.fromFractionalAmount(this.token, divided.numerator, divided.denominator);
68
+ }
69
+ greaterThanOrEqual(other) {
70
+ return this.greaterThan(other) || this.equals(other);
71
+ }
72
+ toRawAmount() {
73
+ return BigInt(this.quotient.toString());
74
+ }
75
+ fiatValue() {
76
+ return this.amountInFiatCurrency;
77
+ }
78
+ toCurrencyValue(price) {
79
+ const fraction = price.multiply(this);
80
+ const denominator = jsbi_1.default.multiply(fraction.denominator, this.decimalScale);
81
+ this.amountInFiatCurrency = currency_amount_js_1.CurrencyAmount.fromFractionalAmount(price.currency, fraction.numerator, denominator);
82
+ return this.amountInFiatCurrency;
83
+ }
84
+ toSignificant(significantDigits = 6, format, rounding = sdk_core_1.Rounding.ROUND_DOWN) {
85
+ return super
86
+ .divide(this.decimalScale)
87
+ .toSignificant(significantDigits, format, rounding);
88
+ }
89
+ toFixed(decimalPlaces = this.token.decimals, format, rounding = sdk_core_1.Rounding.ROUND_DOWN) {
90
+ (0, tiny_invariant_1.default)(decimalPlaces <= this.token.decimals, "DECIMALS");
91
+ return super
92
+ .divide(this.decimalScale)
93
+ .toFixed(decimalPlaces, format, rounding);
94
+ }
95
+ toExact(format = { groupSeparator: "" }) {
96
+ constants_js_1.Big.DP = this.token.decimals;
97
+ return new constants_js_1.Big(this.quotient.toString())
98
+ .div(this.decimalScale.toString())
99
+ .toFormat(format);
100
+ }
101
+ }
102
+ exports.FungibleTokenAmount = FungibleTokenAmount;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NonFungibleToken = void 0;
4
+ const asset_js_1 = require("./asset.js");
5
+ const validateAndParseAddress_js_1 = require("../../utils/validateAndParseAddress.js");
6
+ const enums_js_1 = require("../../enums.js");
7
+ /**
8
+ * Represents an ERC721 token with a unique address and some metadata.
9
+ */
10
+ class NonFungibleToken extends asset_js_1.Asset {
11
+ /**
12
+ * @param chainId The chain ID on which this token resides
13
+ * @param address The contract address on the chain on which this token lives
14
+ * @param symbol {@link Currency#symbol}
15
+ * @param name {@link Currency#name}
16
+ * @param url The URL of the token's metadata
17
+ * @param bypassChecksum If true it only checks for length === 42, startsWith 0x and contains only hex characters
18
+ */
19
+ constructor(chainId, address, symbol, name, url, bypassChecksum, coinGeckoId) {
20
+ // TODO(felix): add this back
21
+ // invariant(!_.isUndefined(getBlockchainFromBlockchainId(chainId)), "CHAIN_ID");
22
+ super(symbol, name, coinGeckoId);
23
+ this.isNative = false;
24
+ this.isToken = true;
25
+ if (bypassChecksum) {
26
+ this.address = (0, validateAndParseAddress_js_1.checkValidAddress)(address)?.toLowerCase();
27
+ }
28
+ else {
29
+ this.address = (0, validateAndParseAddress_js_1.validateAndParseAddress)(address)?.toLowerCase();
30
+ }
31
+ this.chainId = chainId;
32
+ this.url = url;
33
+ }
34
+ static toNonFungibleToken(nft) {
35
+ if (!nft) {
36
+ return undefined;
37
+ }
38
+ const asset = asset_js_1.Asset.toAsset(nft.asset);
39
+ return new NonFungibleToken(nft.chainId, nft.address, asset.symbol, asset.name, nft.url);
40
+ }
41
+ /**
42
+ * Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
43
+ * @param other other token to compare
44
+ */
45
+ equals(other) {
46
+ // short circuit on reference equality
47
+ if (this === other)
48
+ return true;
49
+ return (this.chainId === other.chainId &&
50
+ this.address === other.address.toLowerCase());
51
+ }
52
+ /**
53
+ * Returns the asset representation of this currency
54
+ */
55
+ asset() {
56
+ return this;
57
+ }
58
+ type() {
59
+ return enums_js_1.TokenType.NON_FUNGIBLE_TOKEN;
60
+ }
61
+ identifier() {
62
+ return `${this.chainId}+${this.address.toLowerCase()}`;
63
+ }
64
+ }
65
+ exports.NonFungibleToken = NonFungibleToken;
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SemiFungibleToken = void 0;
4
+ const currency_js_1 = require("./currency.js");
5
+ const validateAndParseAddress_js_1 = require("../../utils/validateAndParseAddress.js");
6
+ const enums_js_1 = require("../../enums.js");
7
+ /**
8
+ * Represents an ERC1155 token with a unique address and some metadata.
9
+ */
10
+ class SemiFungibleToken extends currency_js_1.Currency {
11
+ /**
12
+ *
13
+ * @param chainId The chain ID on which this token resides
14
+ * @param address The contract address on the chain on which this token lives
15
+ * @param decimals {@link Currency#decimals}
16
+ * @param symbol {@link Currency#symbol}
17
+ * @param name {@link Currency#name}
18
+ * @param url The URL of the token's metadata
19
+ * @param bypassChecksum If true it only checks for length === 42, startsWith 0x and contains only hex characters
20
+ */
21
+ constructor(chainId, address, decimals, symbol, name, url, bypassChecksum, coinGeckoId) {
22
+ // TODO(felix): add this back
23
+ // invariant(!_.isUndefined(getBlockchainFromBlockchainId(chainId)), "CHAIN_ID");
24
+ super(decimals, symbol, name, url, false, true, coinGeckoId);
25
+ this.isNative = false;
26
+ this.isToken = true;
27
+ if (bypassChecksum) {
28
+ this.address = (0, validateAndParseAddress_js_1.checkValidAddress)(address)?.toLowerCase();
29
+ }
30
+ else {
31
+ this.address = (0, validateAndParseAddress_js_1.validateAndParseAddress)(address)?.toLowerCase();
32
+ }
33
+ this.chainId = chainId;
34
+ this.url = url;
35
+ }
36
+ static toSemiFungibleToken(sft) {
37
+ if (!sft) {
38
+ return undefined;
39
+ }
40
+ const currency = currency_js_1.Currency.toCurrency(sft.currency);
41
+ return new SemiFungibleToken(sft.chainId, sft.address, currency.decimals, currency.symbol, currency.name, sft.url);
42
+ }
43
+ /**
44
+ * Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
45
+ * @param other other token to compare
46
+ */
47
+ equals(other) {
48
+ // short circuit on reference equality
49
+ if (this === other)
50
+ return true;
51
+ return (this.chainId === other.chainId &&
52
+ this.address === other.address.toLowerCase());
53
+ }
54
+ /**
55
+ * Returns the currency object that this token represents
56
+ */
57
+ currency() {
58
+ return this;
59
+ }
60
+ type() {
61
+ return enums_js_1.TokenType.SEMI_FUNGIBLE_TOKEN;
62
+ }
63
+ identifier() {
64
+ return `${this.chainId}+${this.address.toLowerCase()}`;
65
+ }
66
+ }
67
+ exports.SemiFungibleToken = SemiFungibleToken;
@@ -0,0 +1,37 @@
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.LibraryRequest = void 0;
7
+ const enums_js_1 = require("../enums.js");
8
+ const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
9
+ class LibraryRequest {
10
+ constructor(library, client, provider) {
11
+ if (library == enums_js_1.LIBRARY_TYPE.VIEM) {
12
+ (0, tiny_invariant_1.default)(client, "CLIENT");
13
+ (0, tiny_invariant_1.default)(!provider, "PROVIDER");
14
+ this.client = client;
15
+ }
16
+ else if (library == enums_js_1.LIBRARY_TYPE.ETHERS) {
17
+ (0, tiny_invariant_1.default)(provider, "PROVIDER");
18
+ (0, tiny_invariant_1.default)(!client, "CLIENT");
19
+ this.provider = provider;
20
+ }
21
+ else {
22
+ (0, tiny_invariant_1.default)(false, "LIBRARY");
23
+ }
24
+ this.library = library;
25
+ this.client = client;
26
+ this.provider = provider;
27
+ }
28
+ sendRequest(method, params) {
29
+ if (this.library == enums_js_1.LIBRARY_TYPE.VIEM) {
30
+ return this.client.request({ method, params });
31
+ }
32
+ else if (this.library == enums_js_1.LIBRARY_TYPE.ETHERS) {
33
+ return this.provider.send(method, params);
34
+ }
35
+ }
36
+ }
37
+ exports.LibraryRequest = LibraryRequest;
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.State = void 0;
4
+ const fungible_token_amount_js_1 = require("./financial/fungible_token_amount.js");
5
+ const non_fungible_token_js_1 = require("./financial/non_fungible_token.js");
6
+ const semi_fungible_token_js_1 = require("./financial/semi_fungible_token.js");
7
+ class State {
8
+ constructor(fungibleTokens, nonFungibleTokens, semiFungibleTokens) {
9
+ this.fungibleTokens = fungibleTokens.reduce((acc, amount) => {
10
+ acc.set(amount.token.identifier(), amount);
11
+ return acc;
12
+ }, new Map());
13
+ this.nonFungibleTokens = nonFungibleTokens.reduce((acc, token) => {
14
+ acc.set(token.identifier(), token);
15
+ return acc;
16
+ }, new Map());
17
+ this.semiFungibleTokens = semiFungibleTokens.reduce((acc, token) => {
18
+ acc.set(token.identifier(), token);
19
+ return acc;
20
+ }, new Map());
21
+ }
22
+ static fromFungibleTokenAmounts(fungibleTokenAmounts) {
23
+ return new State(fungibleTokenAmounts, [], []);
24
+ }
25
+ static fromTokenAndAmount(token, amount) {
26
+ if (!token || !amount) {
27
+ return undefined;
28
+ }
29
+ const fungibleTokenAmount = fungible_token_amount_js_1.FungibleTokenAmount.fromRawAmount(token, amount);
30
+ return State.fromFungibleTokenAmounts([fungibleTokenAmount]);
31
+ }
32
+ getFungibleTokens() {
33
+ return Array.from(this.fungibleTokens.values());
34
+ }
35
+ getNonFungibleTokens() {
36
+ return Array.from(this.nonFungibleTokens.values());
37
+ }
38
+ getSemiFungibleTokens() {
39
+ return Array.from(this.semiFungibleTokens.values());
40
+ }
41
+ addTokens(fungibleTokens) {
42
+ fungibleTokens.forEach((amount) => {
43
+ const identifier = amount.token.identifier();
44
+ const currentAmount = this.fungibleTokens.get(identifier);
45
+ if (currentAmount) {
46
+ this.fungibleTokens.set(identifier, currentAmount.add(amount));
47
+ }
48
+ else {
49
+ this.fungibleTokens.set(identifier, amount);
50
+ }
51
+ });
52
+ return this;
53
+ }
54
+ addState(other) {
55
+ other?.fungibleTokens?.forEach((amount) => {
56
+ const identifier = amount.token.identifier();
57
+ const currentAmount = this.fungibleTokens.get(identifier);
58
+ if (currentAmount) {
59
+ this.fungibleTokens.set(identifier, currentAmount.add(amount));
60
+ }
61
+ else {
62
+ this.fungibleTokens.set(identifier, amount);
63
+ }
64
+ });
65
+ return this;
66
+ }
67
+ deductState(other) {
68
+ other.fungibleTokens.forEach((amount) => {
69
+ const identifier = amount.token.identifier();
70
+ const currentAmount = this.fungibleTokens.get(identifier);
71
+ if (currentAmount?.toRawAmount() > amount?.toRawAmount()) {
72
+ this.fungibleTokens.set(identifier, currentAmount.subtract(amount));
73
+ }
74
+ else if (currentAmount) {
75
+ this.fungibleTokens.delete(identifier);
76
+ }
77
+ });
78
+ return this;
79
+ }
80
+ equals(other) {
81
+ if (!other) {
82
+ return false;
83
+ }
84
+ return (this.fungibleTokens.size == other.fungibleTokens.size &&
85
+ Array.from(this.fungibleTokens.entries()).every(([key, value]) => {
86
+ const otherValue = other.fungibleTokens.get(key);
87
+ return otherValue && value.equals(otherValue);
88
+ }));
89
+ }
90
+ addStates(states) {
91
+ states.forEach((state) => this.addState(state));
92
+ return this;
93
+ }
94
+ static mergeStates(states) {
95
+ return states.reduce((acc, state) => acc.addState(state), new State([], [], []));
96
+ }
97
+ static toState(state) {
98
+ if (!state)
99
+ return undefined;
100
+ const fungibleTokenAmounts = state.fungibleTokenAmounts.map((ft) => fungible_token_amount_js_1.FungibleTokenAmount.toFungibleTokenAmount(ft));
101
+ const nonFungibleTokenAmounts = state.nonFungibleTokenAmounts.map((nft) => non_fungible_token_js_1.NonFungibleToken.toNonFungibleToken(nft));
102
+ const semiFungibleTokenAmounts = state.semiFungibleTokenAmounts.map((sft) => semi_fungible_token_js_1.SemiFungibleToken.toSemiFungibleToken(sft));
103
+ return new State(fungibleTokenAmounts, nonFungibleTokenAmounts, semiFungibleTokenAmounts);
104
+ }
105
+ }
106
+ exports.State = State;