@orb-labs/orby-core 0.0.13 → 0.0.15

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 (141) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/dist/actions/operation.d.ts +1 -1
  3. package/dist/actions/operation.js +5 -3
  4. package/dist/cjs/actions/account_cluster.d.ts +32 -0
  5. package/dist/cjs/actions/account_cluster.js +211 -0
  6. package/dist/cjs/actions/admin.d.ts +17 -0
  7. package/dist/cjs/actions/admin.js +43 -0
  8. package/dist/cjs/actions/application.d.ts +6 -0
  9. package/dist/cjs/actions/application.js +20 -0
  10. package/dist/cjs/actions/blockchain.d.ts +16 -0
  11. package/dist/cjs/actions/blockchain.js +39 -0
  12. package/dist/cjs/actions/instance.d.ts +15 -0
  13. package/dist/cjs/actions/instance.js +98 -0
  14. package/dist/cjs/actions/operation.d.ts +99 -0
  15. package/dist/cjs/actions/operation.js +345 -0
  16. package/dist/cjs/actions/token.d.ts +15 -0
  17. package/dist/cjs/actions/token.js +50 -0
  18. package/dist/cjs/constants.d.ts +14 -0
  19. package/dist/cjs/constants.js +139 -0
  20. package/dist/cjs/entities/account.d.ts +14 -0
  21. package/dist/cjs/entities/account.js +49 -0
  22. package/dist/cjs/entities/financial/account_balance.d.ts +12 -0
  23. package/dist/cjs/entities/financial/account_balance.js +35 -0
  24. package/dist/cjs/entities/financial/asset.d.ts +31 -0
  25. package/dist/cjs/entities/financial/asset.js +42 -0
  26. package/dist/cjs/entities/financial/currency.d.ts +41 -0
  27. package/dist/cjs/entities/financial/currency.js +56 -0
  28. package/dist/cjs/entities/financial/currency_amount.d.ts +34 -0
  29. package/dist/cjs/entities/financial/currency_amount.js +99 -0
  30. package/dist/cjs/entities/financial/fungible_token.d.ts +41 -0
  31. package/dist/cjs/entities/financial/fungible_token.js +68 -0
  32. package/dist/cjs/entities/financial/fungible_token_amount.d.ts +36 -0
  33. package/dist/cjs/entities/financial/fungible_token_amount.js +102 -0
  34. package/dist/cjs/entities/financial/non_fungible_token.d.ts +39 -0
  35. package/dist/cjs/entities/financial/non_fungible_token.js +65 -0
  36. package/dist/cjs/entities/financial/semi_fungible_token.d.ts +41 -0
  37. package/dist/cjs/entities/financial/semi_fungible_token.js +67 -0
  38. package/dist/cjs/entities/library_request.d.ts +8 -0
  39. package/dist/cjs/entities/library_request.js +37 -0
  40. package/dist/cjs/entities/state.d.ts +22 -0
  41. package/dist/cjs/entities/state.js +106 -0
  42. package/dist/cjs/enums.d.ts +116 -0
  43. package/dist/cjs/enums.js +138 -0
  44. package/dist/cjs/index.d.ts +29 -0
  45. package/dist/cjs/index.js +49 -0
  46. package/dist/cjs/interfaces/account_cluster.d.ts +30 -0
  47. package/dist/cjs/interfaces/account_cluster.js +2 -0
  48. package/dist/cjs/interfaces/admin.d.ts +14 -0
  49. package/dist/cjs/interfaces/admin.js +2 -0
  50. package/dist/cjs/interfaces/application.d.ts +3 -0
  51. package/dist/cjs/interfaces/application.js +2 -0
  52. package/dist/cjs/interfaces/blockchain.d.ts +13 -0
  53. package/dist/cjs/interfaces/blockchain.js +2 -0
  54. package/dist/cjs/interfaces/instance.d.ts +12 -0
  55. package/dist/cjs/interfaces/instance.js +2 -0
  56. package/dist/cjs/interfaces/operation.d.ts +97 -0
  57. package/dist/cjs/interfaces/operation.js +2 -0
  58. package/dist/cjs/interfaces/orby.d.ts +9 -0
  59. package/dist/cjs/interfaces/orby.js +2 -0
  60. package/dist/cjs/interfaces/token.d.ts +12 -0
  61. package/dist/cjs/interfaces/token.js +2 -0
  62. package/dist/cjs/types.d.ts +152 -0
  63. package/dist/cjs/types.js +2 -0
  64. package/dist/cjs/utils/action_helpers.d.ts +22 -0
  65. package/dist/cjs/utils/action_helpers.js +274 -0
  66. package/dist/cjs/utils/utils.d.ts +10 -0
  67. package/dist/cjs/utils/utils.js +79 -0
  68. package/dist/cjs/utils/validateAndParseAddress.d.ts +10 -0
  69. package/dist/cjs/utils/validateAndParseAddress.js +29 -0
  70. package/dist/esm/actions/account_cluster.d.ts +32 -0
  71. package/dist/esm/actions/account_cluster.js +207 -0
  72. package/dist/esm/actions/admin.d.ts +17 -0
  73. package/dist/esm/actions/admin.js +39 -0
  74. package/dist/esm/actions/application.d.ts +6 -0
  75. package/dist/esm/actions/application.js +16 -0
  76. package/dist/esm/actions/blockchain.d.ts +16 -0
  77. package/dist/esm/actions/blockchain.js +35 -0
  78. package/dist/esm/actions/instance.d.ts +15 -0
  79. package/dist/esm/actions/instance.js +94 -0
  80. package/dist/esm/actions/operation.d.ts +99 -0
  81. package/dist/esm/actions/operation.js +341 -0
  82. package/dist/esm/actions/token.d.ts +15 -0
  83. package/dist/esm/actions/token.js +46 -0
  84. package/dist/esm/constants.d.ts +14 -0
  85. package/dist/esm/constants.js +133 -0
  86. package/dist/esm/entities/account.d.ts +14 -0
  87. package/dist/esm/entities/account.js +45 -0
  88. package/dist/esm/entities/financial/account_balance.d.ts +12 -0
  89. package/dist/esm/entities/financial/account_balance.js +31 -0
  90. package/dist/esm/entities/financial/asset.d.ts +31 -0
  91. package/dist/esm/entities/financial/asset.js +38 -0
  92. package/dist/esm/entities/financial/currency.d.ts +41 -0
  93. package/dist/esm/entities/financial/currency.js +49 -0
  94. package/dist/esm/entities/financial/currency_amount.d.ts +34 -0
  95. package/dist/esm/entities/financial/currency_amount.js +92 -0
  96. package/dist/esm/entities/financial/fungible_token.d.ts +41 -0
  97. package/dist/esm/entities/financial/fungible_token.js +64 -0
  98. package/dist/esm/entities/financial/fungible_token_amount.d.ts +36 -0
  99. package/dist/esm/entities/financial/fungible_token_amount.js +95 -0
  100. package/dist/esm/entities/financial/non_fungible_token.d.ts +39 -0
  101. package/dist/esm/entities/financial/non_fungible_token.js +61 -0
  102. package/dist/esm/entities/financial/semi_fungible_token.d.ts +41 -0
  103. package/dist/esm/entities/financial/semi_fungible_token.js +63 -0
  104. package/dist/esm/entities/library_request.d.ts +8 -0
  105. package/dist/esm/entities/library_request.js +30 -0
  106. package/dist/esm/entities/state.d.ts +22 -0
  107. package/dist/esm/entities/state.js +102 -0
  108. package/dist/esm/enums.d.ts +116 -0
  109. package/dist/esm/enums.js +135 -0
  110. package/dist/esm/index.d.ts +29 -0
  111. package/dist/esm/index.js +33 -0
  112. package/dist/esm/interfaces/account_cluster.d.ts +30 -0
  113. package/dist/esm/interfaces/account_cluster.js +1 -0
  114. package/dist/esm/interfaces/admin.d.ts +14 -0
  115. package/dist/esm/interfaces/admin.js +1 -0
  116. package/dist/esm/interfaces/application.d.ts +3 -0
  117. package/dist/esm/interfaces/application.js +1 -0
  118. package/dist/esm/interfaces/blockchain.d.ts +13 -0
  119. package/dist/esm/interfaces/blockchain.js +1 -0
  120. package/dist/esm/interfaces/instance.d.ts +12 -0
  121. package/dist/esm/interfaces/instance.js +1 -0
  122. package/dist/esm/interfaces/operation.d.ts +97 -0
  123. package/dist/esm/interfaces/operation.js +1 -0
  124. package/dist/esm/interfaces/orby.d.ts +9 -0
  125. package/dist/esm/interfaces/orby.js +1 -0
  126. package/dist/esm/interfaces/token.d.ts +12 -0
  127. package/dist/esm/interfaces/token.js +1 -0
  128. package/dist/esm/types.d.ts +152 -0
  129. package/dist/esm/types.js +1 -0
  130. package/dist/esm/utils/action_helpers.d.ts +22 -0
  131. package/dist/esm/utils/action_helpers.js +250 -0
  132. package/dist/esm/utils/utils.d.ts +10 -0
  133. package/dist/esm/utils/utils.js +70 -0
  134. package/dist/esm/utils/validateAndParseAddress.d.ts +10 -0
  135. package/dist/esm/utils/validateAndParseAddress.js +25 -0
  136. package/dist/interfaces/operation.d.ts +1 -1
  137. package/dist/tsconfig.cjs.tsbuildinfo +1 -0
  138. package/dist/tsconfig.esm.tsbuildinfo +1 -0
  139. package/dist/tsconfig.tsbuildinfo +1 -1
  140. package/dist/utils/action_helpers.js +1 -1
  141. package/package.json +16 -7
@@ -0,0 +1,45 @@
1
+ import { AccountType } from "../enums.js";
2
+ import { getChainIdFromOrbyChainId } from "../utils/utils.js";
3
+ export class Account {
4
+ static toAccount(account) {
5
+ const chainId = account?.chainId
6
+ ? getChainIdFromOrbyChainId(account.chainId)
7
+ : undefined;
8
+ const formattedAccountType = account?.accountType?.toLowerCase();
9
+ if (!chainId && formattedAccountType == AccountType.SCA) {
10
+ return undefined;
11
+ }
12
+ return new Account(account.address, formattedAccountType, account.vmType, chainId);
13
+ }
14
+ constructor(address, accountType, vmType, chainId) {
15
+ // TODO(felix): add this back
16
+ // invariant(!_.isUndefined(getBlockchainFromBlockchainId(chainId)), "CHAIN_ID");
17
+ this.address = address?.toLowerCase(); // validateAndParseAddress(address)?.toLowerCase();
18
+ this.chainId = chainId ? BigInt(chainId) : undefined;
19
+ this.accountType = accountType;
20
+ this.vmType = vmType;
21
+ this.key = Account.key(address, chainId);
22
+ if (this.accountType == AccountType.SCA) {
23
+ this.hasSufficientBalanceForGas = true;
24
+ }
25
+ else {
26
+ this.hasSufficientBalanceForGas = false;
27
+ }
28
+ }
29
+ static key(address, chainId) {
30
+ return chainId
31
+ ? `${chainId?.toString()?.toLowerCase()}-${address}`
32
+ : address;
33
+ }
34
+ toAccountModel() {
35
+ return {
36
+ address: this.address,
37
+ accountType: this.accountType?.toUpperCase(),
38
+ vmType: this.vmType?.toUpperCase(),
39
+ chainId: this.chainId ? "EIP155-" + this.chainId : undefined,
40
+ };
41
+ }
42
+ equals(other) {
43
+ return this.key == other?.key && this.accountType == other?.accountType;
44
+ }
45
+ }
@@ -0,0 +1,12 @@
1
+ import { Account } from "../account.js";
2
+ import { FungibleTokenAmount } from "./fungible_token_amount.js";
3
+ export declare class AccountBalance {
4
+ readonly amount: FungibleTokenAmount;
5
+ readonly account: Account;
6
+ static toAccountBalance(balance?: any): AccountBalance;
7
+ constructor(amount: FungibleTokenAmount, account: Account);
8
+ equals(other: AccountBalance): boolean;
9
+ lessThan(other: AccountBalance): boolean;
10
+ greaterThan(other: AccountBalance): boolean;
11
+ greaterThanOrEqual(other: AccountBalance): boolean;
12
+ }
@@ -0,0 +1,31 @@
1
+ import { Account } from "../account.js";
2
+ import { FungibleTokenAmount } from "./fungible_token_amount.js";
3
+ export class AccountBalance {
4
+ static toAccountBalance(balance) {
5
+ if (!balance) {
6
+ return undefined;
7
+ }
8
+ const amount = FungibleTokenAmount.toFungibleTokenAmount(balance.amount);
9
+ const account = Account.toAccount(balance.account);
10
+ return new AccountBalance(amount, account);
11
+ }
12
+ constructor(amount, account) {
13
+ this.amount = amount;
14
+ this.account = account;
15
+ }
16
+ equals(other) {
17
+ return (this.account.equals(other.account) && this.amount.equals(other.amount));
18
+ }
19
+ lessThan(other) {
20
+ return (this.account.equals(other.account) && this.amount.lessThan(other.amount));
21
+ }
22
+ greaterThan(other) {
23
+ return (this.account.equals(other.account) &&
24
+ this.amount.greaterThan(other.amount));
25
+ }
26
+ greaterThanOrEqual(other) {
27
+ return (this.account.equals(other.account) &&
28
+ (this.amount.greaterThan(other.amount) ||
29
+ this.amount.equals(other.amount)));
30
+ }
31
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * An Asset is any fungible financial instrument, including Ether, all ERC20 tokens, and NFTs
3
+ */
4
+ export declare class Asset {
5
+ /**
6
+ * The symbol of the asset, i.e. a short textual non-unique identifier
7
+ */
8
+ readonly symbol: string;
9
+ /**
10
+ * The name of the asset, i.e. a descriptive textual non-unique identifier
11
+ */
12
+ readonly name: string;
13
+ /**
14
+ * The name of the asset, i.e. a descriptive textual non-unique identifier
15
+ */
16
+ readonly coinGeckoId: string;
17
+ /**
18
+ * Constructs an instance of the `Asset`.
19
+ * @param symbol symbol of the asset
20
+ * @param name of the asset
21
+ */
22
+ constructor(symbol: string, name: string, coinGeckoId?: string);
23
+ /**
24
+ * Returns whether this asset is functionally equivalent to the other asset
25
+ * @param other the other asset
26
+ */
27
+ equals(other: Asset): boolean;
28
+ toAssetModel(): any;
29
+ static toAsset(asset?: any): Asset;
30
+ id(): string;
31
+ }
@@ -0,0 +1,38 @@
1
+ /**
2
+ * An Asset is any fungible financial instrument, including Ether, all ERC20 tokens, and NFTs
3
+ */
4
+ export class Asset {
5
+ /**
6
+ * Constructs an instance of the `Asset`.
7
+ * @param symbol symbol of the asset
8
+ * @param name of the asset
9
+ */
10
+ constructor(symbol, name, coinGeckoId) {
11
+ this.symbol = symbol;
12
+ this.name = name;
13
+ this.coinGeckoId = coinGeckoId;
14
+ }
15
+ /**
16
+ * Returns whether this asset is functionally equivalent to the other asset
17
+ * @param other the other asset
18
+ */
19
+ equals(other) {
20
+ if (this === other)
21
+ return true;
22
+ return this.symbol === other.symbol && this.name === other.name;
23
+ }
24
+ // 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
25
+ toAssetModel() {
26
+ return { symbol: this.symbol, name: this.name };
27
+ }
28
+ static toAsset(asset) {
29
+ if (!asset) {
30
+ return undefined;
31
+ }
32
+ const { symbol, name } = asset;
33
+ return new Asset(symbol, name);
34
+ }
35
+ id() {
36
+ return `${this.name}:${this.symbol}`;
37
+ }
38
+ }
@@ -0,0 +1,41 @@
1
+ import { Asset } from "./asset.js";
2
+ /**
3
+ * A currency is any fungible financial instrument, including Ether, all ERC20 tokens, and other chain-native currencies
4
+ */
5
+ export declare class Currency extends Asset {
6
+ /**
7
+ * Returns whether the currency is native to the chain and must be wrapped (e.g. Ether)
8
+ */
9
+ readonly isNative: boolean;
10
+ /**
11
+ * Returns whether the currency is a token that is usable in Uniswap without wrapping
12
+ */
13
+ readonly isToken: boolean;
14
+ /**
15
+ * The decimals used in representing currency amounts
16
+ */
17
+ readonly decimals: number;
18
+ /**
19
+ * The url for the currency logo
20
+ */
21
+ readonly logoUrl?: string;
22
+ /**
23
+ * Constructs an instance of the base class `Currency`.
24
+ * @param decimals decimals of the currency
25
+ * @param symbol symbol of the currency
26
+ * @param name of the currency
27
+ * @param isNative whether the currency is native to the chain and must be wrapped (e.g. Ether)
28
+ * @param isToken whether the currency is a token that is usable in Uniswap without wrapping
29
+ */
30
+ constructor(decimals: number, symbol: string, name: string, logoUrl?: string, isNative?: boolean, isToken?: boolean, coinGeckoId?: string);
31
+ /**
32
+ * Returns whether this currency is functionally equivalent to the other currency
33
+ * @param other the other currency
34
+ */
35
+ equals(other: Currency): boolean;
36
+ /**
37
+ * Returns the asset representation of this currency
38
+ */
39
+ asset(): Asset;
40
+ static toCurrency(currency?: any): Currency;
41
+ }
@@ -0,0 +1,49 @@
1
+ import invariant from "tiny-invariant";
2
+ import { Asset } from "./asset.js";
3
+ // This class is inspired by the Uniswap SDK's (@uniswap/sdk-core) CurrencyAmount class. Create this instance here
4
+ // because we represent currency amounts that are not fungible token amounts.
5
+ /**
6
+ * A currency is any fungible financial instrument, including Ether, all ERC20 tokens, and other chain-native currencies
7
+ */
8
+ export class Currency extends Asset {
9
+ /**
10
+ * Constructs an instance of the base class `Currency`.
11
+ * @param decimals decimals of the currency
12
+ * @param symbol symbol of the currency
13
+ * @param name of the currency
14
+ * @param isNative whether the currency is native to the chain and must be wrapped (e.g. Ether)
15
+ * @param isToken whether the currency is a token that is usable in Uniswap without wrapping
16
+ */
17
+ constructor(decimals, symbol, name, logoUrl, isNative, isToken, coinGeckoId) {
18
+ super(symbol, name, coinGeckoId);
19
+ invariant(decimals >= 0 && decimals < 255 && Number.isInteger(decimals), "DECIMALS");
20
+ this.decimals = decimals;
21
+ this.isNative = isNative ?? false;
22
+ this.isToken = isToken ?? true;
23
+ this.logoUrl = logoUrl;
24
+ }
25
+ /**
26
+ * Returns whether this currency is functionally equivalent to the other currency
27
+ * @param other the other currency
28
+ */
29
+ equals(other) {
30
+ if (this === other)
31
+ return true;
32
+ return (this.symbol === other.symbol &&
33
+ this.name === other.name &&
34
+ this.decimals === other.decimals);
35
+ }
36
+ /**
37
+ * Returns the asset representation of this currency
38
+ */
39
+ asset() {
40
+ return this;
41
+ }
42
+ static toCurrency(currency) {
43
+ if (!currency) {
44
+ return undefined;
45
+ }
46
+ const { asset, decimals, logoUrl, isNative } = currency;
47
+ return new Currency(decimals, asset.symbol, asset.name, logoUrl, isNative);
48
+ }
49
+ }
@@ -0,0 +1,34 @@
1
+ import JSBI from "jsbi";
2
+ import { Fraction, BigintIsh, Rounding } from "@uniswap/sdk-core";
3
+ import { Currency } from "./currency.js";
4
+ export declare class CurrencyAmount extends Fraction {
5
+ readonly currency: Currency;
6
+ readonly decimalScale: JSBI;
7
+ private amountInFiatCurrency?;
8
+ /**
9
+ * Returns a new currency amount instance from the unitless amount of token, i.e. the raw amount
10
+ * @param currency the currency in the amount
11
+ * @param rawAmount the raw token or ether amount
12
+ */
13
+ static fromRawAmount(currency: Currency, rawAmount: BigintIsh | bigint): CurrencyAmount;
14
+ static toCurrencyAmount(amount?: any): CurrencyAmount;
15
+ /**
16
+ * Construct a currency amount with a denominator that is not equal to 1
17
+ * @param currency the currency
18
+ * @param numerator the numerator of the fractional token amount
19
+ * @param denominator the denominator of the fractional token amount
20
+ */
21
+ static fromFractionalAmount(currency: Currency, numerator: BigintIsh, denominator: BigintIsh): CurrencyAmount;
22
+ protected constructor(currency: Currency, numerator: BigintIsh | bigint, denominator?: BigintIsh | bigint);
23
+ add(other: CurrencyAmount): CurrencyAmount;
24
+ subtract(other: CurrencyAmount): CurrencyAmount;
25
+ multiply(other: Fraction | BigintIsh): CurrencyAmount;
26
+ divide(other: Fraction | BigintIsh): CurrencyAmount;
27
+ cloneWithAmount(amount: bigint): CurrencyAmount;
28
+ toRawAmount(): bigint;
29
+ toCurrencyValue(price: CurrencyAmount): CurrencyAmount;
30
+ fiatValue(): CurrencyAmount;
31
+ toSignificant(significantDigits?: number, format?: object, rounding?: Rounding): string;
32
+ toFixed(decimalPlaces?: number, format?: object, rounding?: Rounding): string;
33
+ toExact(format?: object): string;
34
+ }
@@ -0,0 +1,92 @@
1
+ import invariant from "tiny-invariant";
2
+ import JSBI from "jsbi";
3
+ import { Fraction, MaxUint256, Rounding } from "@uniswap/sdk-core";
4
+ import { Currency } from "./currency.js";
5
+ import { Big } from "../../constants.js";
6
+ // This class is inspired by the Uniswap SDK's (@uniswap/sdk-core) CurrencyAmount class. Created this instance here
7
+ // because we have a need to represent a currency amount that is not a fungible token amount.
8
+ export class CurrencyAmount extends Fraction {
9
+ /**
10
+ * Returns a new currency amount instance from the unitless amount of token, i.e. the raw amount
11
+ * @param currency the currency in the amount
12
+ * @param rawAmount the raw token or ether amount
13
+ */
14
+ static fromRawAmount(currency, rawAmount) {
15
+ return new CurrencyAmount(currency, rawAmount);
16
+ }
17
+ static toCurrencyAmount(amount) {
18
+ if (!amount) {
19
+ return undefined;
20
+ }
21
+ const currency = Currency.toCurrency(amount.currency);
22
+ return CurrencyAmount.fromRawAmount(currency, amount.value);
23
+ }
24
+ /**
25
+ * Construct a currency amount with a denominator that is not equal to 1
26
+ * @param currency the currency
27
+ * @param numerator the numerator of the fractional token amount
28
+ * @param denominator the denominator of the fractional token amount
29
+ */
30
+ static fromFractionalAmount(currency, numerator, denominator) {
31
+ return new CurrencyAmount(currency, numerator, denominator);
32
+ }
33
+ constructor(currency, numerator, denominator) {
34
+ super(numerator.toString(), denominator?.toString());
35
+ invariant(JSBI.lessThanOrEqual(this.quotient, MaxUint256), "AMOUNT");
36
+ this.currency = currency;
37
+ this.decimalScale = JSBI.exponentiate(JSBI.BigInt(10), JSBI.BigInt(currency.decimals));
38
+ }
39
+ add(other) {
40
+ if (!other) {
41
+ return this;
42
+ }
43
+ invariant(this.currency.equals(other.currency), "CURRENCY");
44
+ const added = super.add(other);
45
+ return CurrencyAmount.fromFractionalAmount(this.currency, added.numerator, added.denominator);
46
+ }
47
+ subtract(other) {
48
+ invariant(this.currency.equals(other.currency), "CURRENCY");
49
+ const subtracted = super.subtract(other);
50
+ return CurrencyAmount.fromFractionalAmount(this.currency, subtracted.numerator, subtracted.denominator);
51
+ }
52
+ multiply(other) {
53
+ const multiplied = super.multiply(other);
54
+ return CurrencyAmount.fromFractionalAmount(this.currency, multiplied.numerator, multiplied.denominator);
55
+ }
56
+ divide(other) {
57
+ const divided = super.divide(other);
58
+ return CurrencyAmount.fromFractionalAmount(this.currency, divided.numerator, divided.denominator);
59
+ }
60
+ cloneWithAmount(amount) {
61
+ return CurrencyAmount.fromRawAmount(this.currency, amount);
62
+ }
63
+ toRawAmount() {
64
+ return BigInt(this.quotient.toString());
65
+ }
66
+ toCurrencyValue(price) {
67
+ const fraction = price.multiply(this);
68
+ const denominator = JSBI.multiply(fraction.denominator, this.decimalScale);
69
+ this.amountInFiatCurrency = CurrencyAmount.fromFractionalAmount(price.currency, fraction.numerator, denominator);
70
+ return this.amountInFiatCurrency;
71
+ }
72
+ fiatValue() {
73
+ return this.amountInFiatCurrency;
74
+ }
75
+ toSignificant(significantDigits = 6, format, rounding = Rounding.ROUND_DOWN) {
76
+ return super
77
+ .divide(this.decimalScale)
78
+ .toSignificant(significantDigits, format, rounding);
79
+ }
80
+ toFixed(decimalPlaces = this.currency.decimals, format, rounding = Rounding.ROUND_DOWN) {
81
+ invariant(decimalPlaces <= this.currency.decimals, "DECIMALS");
82
+ return super
83
+ .divide(this.decimalScale)
84
+ .toFixed(decimalPlaces, format, rounding);
85
+ }
86
+ toExact(format = { groupSeparator: "" }) {
87
+ Big.DP = this.currency.decimals;
88
+ return new Big(this.quotient.toString())
89
+ .div(this.decimalScale.toString())
90
+ .toFormat(format);
91
+ }
92
+ }
@@ -0,0 +1,41 @@
1
+ import { Currency } from "./currency.js";
2
+ import { TokenType } from "../../enums.js";
3
+ /**
4
+ * Represents an ERC20 token with a unique address and some metadata.
5
+ */
6
+ export declare class FungibleToken extends Currency {
7
+ readonly isToken: true;
8
+ /**
9
+ * The contract address on the chain on which this token lives
10
+ */
11
+ readonly address: string;
12
+ /**
13
+ * The chain ID on which this token resides
14
+ */
15
+ readonly chainId: bigint;
16
+ /**
17
+ * @param isNative If true, it means that this token is the native token of the chain
18
+ */
19
+ readonly isNative: boolean;
20
+ static toFungibleToken(token?: any): FungibleToken;
21
+ /**
22
+ * @param chainId The chain ID on which this token resides
23
+ * @param address The contract address on the chain on which this token lives
24
+ * @param decimals {@link Currency#decimals}
25
+ * @param symbol {@link Currency#symbol}
26
+ * @param name {@link Currency#name}
27
+ * @param bypassChecksum If true it only checks for length === 42, startsWith 0x and contains only hex characters
28
+ */
29
+ constructor(chainId: bigint, address: string, decimals: number, symbol: string, name: string, bypassChecksum?: boolean, isNative?: boolean, coinGeckoId?: string, logoUrl?: string);
30
+ /**
31
+ * Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
32
+ * @param other other token to compare
33
+ */
34
+ equals(other: FungibleToken): boolean;
35
+ /**
36
+ * Returns the currency object that this token represents
37
+ */
38
+ currency(): Currency;
39
+ type(): TokenType;
40
+ identifier(): string;
41
+ }
@@ -0,0 +1,64 @@
1
+ import { Currency } from "./currency.js";
2
+ import { checkValidAddress, validateAndParseAddress, } from "../../utils/validateAndParseAddress.js";
3
+ import { TokenType } from "../../enums.js";
4
+ /**
5
+ * Represents an ERC20 token with a unique address and some metadata.
6
+ */
7
+ export class FungibleToken extends Currency {
8
+ static toFungibleToken(token) {
9
+ if (!token) {
10
+ return undefined;
11
+ }
12
+ const { address, chainId, currency, coinGeckoId, isNative } = token;
13
+ return new FungibleToken(BigInt(chainId), address, currency.decimals, currency.asset.symbol, currency.asset.name, undefined, isNative, coinGeckoId);
14
+ }
15
+ /**
16
+ * @param chainId The chain ID on which this token resides
17
+ * @param address The contract address on the chain on which this token lives
18
+ * @param decimals {@link Currency#decimals}
19
+ * @param symbol {@link Currency#symbol}
20
+ * @param name {@link Currency#name}
21
+ * @param bypassChecksum If true it only checks for length === 42, startsWith 0x and contains only hex characters
22
+ */
23
+ constructor(chainId, address, decimals, symbol, name, bypassChecksum, isNative, coinGeckoId, logoUrl) {
24
+ // TODO(felix): add this back
25
+ // invariant(!_.isUndefined(getBlockchainFromBlockchainId(chainId)), "CHAIN_ID");
26
+ super(decimals, symbol, name, logoUrl, isNative, true, coinGeckoId);
27
+ this.isToken = true;
28
+ // TODO(felix): bypassChecksum is a little confusing since when bypassChecksum is true, we still validate the address
29
+ // bypassChecksum is derived from the isNative parameter, so we should remove it and just check if isNative here
30
+ if (bypassChecksum) {
31
+ this.address = checkValidAddress(address)?.toLowerCase();
32
+ }
33
+ else if (!isNative) {
34
+ this.address = validateAndParseAddress(address)?.toLowerCase();
35
+ }
36
+ else {
37
+ this.address = address;
38
+ }
39
+ this.isNative = isNative || false;
40
+ this.chainId = chainId;
41
+ }
42
+ /**
43
+ * Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
44
+ * @param other other token to compare
45
+ */
46
+ equals(other) {
47
+ if (this === other)
48
+ return true;
49
+ return (this.chainId == other?.chainId &&
50
+ this.address.toLowerCase() == other?.address?.toLowerCase());
51
+ }
52
+ /**
53
+ * Returns the currency object that this token represents
54
+ */
55
+ currency() {
56
+ return this;
57
+ }
58
+ type() {
59
+ return TokenType.FUNGIBLE_TOKEN;
60
+ }
61
+ identifier() {
62
+ return `${this.chainId}+${this.address.toLowerCase()}`;
63
+ }
64
+ }
@@ -0,0 +1,36 @@
1
+ import JSBI from "jsbi";
2
+ import { Fraction, BigintIsh, Rounding } from "@uniswap/sdk-core";
3
+ import { FungibleToken } from "./fungible_token.js";
4
+ import { CurrencyAmount } from "./currency_amount.js";
5
+ export declare class FungibleTokenAmount extends Fraction {
6
+ readonly token: FungibleToken;
7
+ readonly decimalScale: JSBI;
8
+ private amountInFiatCurrency?;
9
+ /**
10
+ * Returns a new fungibleToken amount instance from the unitless amount of token, i.e. the raw amount
11
+ * @param fungibleToken the fungibleToken in the amount
12
+ * @param rawAmount the raw token or ether amount
13
+ */
14
+ static fromRawAmount(fungibleToken: FungibleToken, rawAmount: BigintIsh | bigint, amountInFiatCurrency?: CurrencyAmount): FungibleTokenAmount;
15
+ /**
16
+ * Construct a fungibleToken amount with a denominator that is not equal to 1
17
+ * @param fungibleToken the fungibleToken
18
+ * @param numerator the numerator of the fractional token amount
19
+ * @param denominator the denominator of the fractional token amount
20
+ */
21
+ static fromFractionalAmount(fungibleToken: FungibleToken, numerator: BigintIsh, denominator: BigintIsh, amountInFiatCurrency?: CurrencyAmount): FungibleTokenAmount;
22
+ static toFungibleTokenAmount(amount?: any): FungibleTokenAmount;
23
+ protected constructor(fungibleToken: FungibleToken, numerator: BigintIsh | bigint, denominator?: BigintIsh | bigint, amountInFiatCurrency?: CurrencyAmount);
24
+ equals(other: FungibleTokenAmount): boolean;
25
+ add(other: FungibleTokenAmount): FungibleTokenAmount;
26
+ subtract(other: FungibleTokenAmount): FungibleTokenAmount;
27
+ multiply(other: Fraction | BigintIsh): FungibleTokenAmount;
28
+ divide(other: Fraction | BigintIsh): FungibleTokenAmount;
29
+ greaterThanOrEqual(other: FungibleTokenAmount): boolean;
30
+ toRawAmount(): bigint;
31
+ fiatValue(): CurrencyAmount;
32
+ toCurrencyValue(price: CurrencyAmount): CurrencyAmount;
33
+ toSignificant(significantDigits?: number, format?: object, rounding?: Rounding): string;
34
+ toFixed(decimalPlaces?: number, format?: object, rounding?: Rounding): string;
35
+ toExact(format?: object): string;
36
+ }
@@ -0,0 +1,95 @@
1
+ import invariant from "tiny-invariant";
2
+ import JSBI from "jsbi";
3
+ import { Fraction, Rounding, MaxUint256 } from "@uniswap/sdk-core";
4
+ import { FungibleToken } from "./fungible_token.js";
5
+ import { CurrencyAmount } from "./currency_amount.js";
6
+ import { Big } from "../../constants.js";
7
+ // This class is inspired by the Uniswap SDK's (@uniswap/sdk-core) CurrencyAmount class. Created this instance here
8
+ // because we have a need to represent a currency amount that is not a fungible token amount.
9
+ export class FungibleTokenAmount extends Fraction {
10
+ /**
11
+ * Returns a new fungibleToken amount instance from the unitless amount of token, i.e. the raw amount
12
+ * @param fungibleToken the fungibleToken in the amount
13
+ * @param rawAmount the raw token or ether amount
14
+ */
15
+ static fromRawAmount(fungibleToken, rawAmount, amountInFiatCurrency) {
16
+ return new FungibleTokenAmount(fungibleToken, rawAmount, undefined, amountInFiatCurrency);
17
+ }
18
+ /**
19
+ * Construct a fungibleToken amount with a denominator that is not equal to 1
20
+ * @param fungibleToken the fungibleToken
21
+ * @param numerator the numerator of the fractional token amount
22
+ * @param denominator the denominator of the fractional token amount
23
+ */
24
+ static fromFractionalAmount(fungibleToken, numerator, denominator, amountInFiatCurrency) {
25
+ return new FungibleTokenAmount(fungibleToken, numerator, denominator, amountInFiatCurrency);
26
+ }
27
+ static toFungibleTokenAmount(amount) {
28
+ if (!amount) {
29
+ return undefined;
30
+ }
31
+ const amountInFiatCurrency = CurrencyAmount.toCurrencyAmount(amount.amountInFiatCurrency);
32
+ return FungibleTokenAmount.fromRawAmount(FungibleToken.toFungibleToken(amount.token), amount.value, amountInFiatCurrency);
33
+ }
34
+ constructor(fungibleToken, numerator, denominator, amountInFiatCurrency) {
35
+ super(numerator.toString(), denominator?.toString());
36
+ invariant(JSBI.lessThanOrEqual(this.quotient, MaxUint256), "AMOUNT"); // TODO(imti): fix this
37
+ this.token = fungibleToken;
38
+ this.decimalScale = JSBI.exponentiate(JSBI.BigInt(10), JSBI.BigInt(fungibleToken.decimals));
39
+ this.amountInFiatCurrency = amountInFiatCurrency;
40
+ }
41
+ equals(other) {
42
+ return (this.token.equals(other.token) &&
43
+ this.toRawAmount() == other.toRawAmount());
44
+ }
45
+ add(other) {
46
+ invariant(this.token.equals(other.token), "FUNGIBLE_TOKEN");
47
+ const added = super.add(other);
48
+ return FungibleTokenAmount.fromFractionalAmount(this.token, added.numerator, added.denominator);
49
+ }
50
+ subtract(other) {
51
+ invariant(this.token.equals(other.token), "FUNGIBLE_TOKEN");
52
+ const subtracted = super.subtract(other);
53
+ return FungibleTokenAmount.fromFractionalAmount(this.token, subtracted.numerator, subtracted.denominator);
54
+ }
55
+ multiply(other) {
56
+ const multiplied = super.multiply(other);
57
+ return FungibleTokenAmount.fromFractionalAmount(this.token, multiplied.numerator, multiplied.denominator);
58
+ }
59
+ divide(other) {
60
+ const divided = super.divide(other);
61
+ return FungibleTokenAmount.fromFractionalAmount(this.token, divided.numerator, divided.denominator);
62
+ }
63
+ greaterThanOrEqual(other) {
64
+ return this.greaterThan(other) || this.equals(other);
65
+ }
66
+ toRawAmount() {
67
+ return BigInt(this.quotient.toString());
68
+ }
69
+ fiatValue() {
70
+ return this.amountInFiatCurrency;
71
+ }
72
+ toCurrencyValue(price) {
73
+ const fraction = price.multiply(this);
74
+ const denominator = JSBI.multiply(fraction.denominator, this.decimalScale);
75
+ this.amountInFiatCurrency = CurrencyAmount.fromFractionalAmount(price.currency, fraction.numerator, denominator);
76
+ return this.amountInFiatCurrency;
77
+ }
78
+ toSignificant(significantDigits = 6, format, rounding = Rounding.ROUND_DOWN) {
79
+ return super
80
+ .divide(this.decimalScale)
81
+ .toSignificant(significantDigits, format, rounding);
82
+ }
83
+ toFixed(decimalPlaces = this.token.decimals, format, rounding = Rounding.ROUND_DOWN) {
84
+ invariant(decimalPlaces <= this.token.decimals, "DECIMALS");
85
+ return super
86
+ .divide(this.decimalScale)
87
+ .toFixed(decimalPlaces, format, rounding);
88
+ }
89
+ toExact(format = { groupSeparator: "" }) {
90
+ Big.DP = this.token.decimals;
91
+ return new Big(this.quotient.toString())
92
+ .div(this.decimalScale.toString())
93
+ .toFormat(format);
94
+ }
95
+ }
@@ -0,0 +1,39 @@
1
+ import { Asset } from "./asset.js";
2
+ import { TokenType } from "../../enums.js";
3
+ /**
4
+ * Represents an ERC721 token with a unique address and some metadata.
5
+ */
6
+ export declare class NonFungibleToken extends Asset {
7
+ readonly isNative: false;
8
+ readonly isToken: true;
9
+ /**
10
+ * The contract address on the chain on which this token lives
11
+ */
12
+ readonly address: string;
13
+ /**
14
+ * The chain ID on which this token resides
15
+ */
16
+ readonly chainId: bigint;
17
+ readonly url?: string;
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 symbol {@link Currency#symbol}
22
+ * @param name {@link Currency#name}
23
+ * @param url The URL of the token's metadata
24
+ * @param bypassChecksum If true it only checks for length === 42, startsWith 0x and contains only hex characters
25
+ */
26
+ constructor(chainId: bigint, address: string, symbol: string, name: string, url?: string, bypassChecksum?: boolean, coinGeckoId?: string);
27
+ static toNonFungibleToken(nft?: any): NonFungibleToken;
28
+ /**
29
+ * Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
30
+ * @param other other token to compare
31
+ */
32
+ equals(other: NonFungibleToken): boolean;
33
+ /**
34
+ * Returns the asset representation of this currency
35
+ */
36
+ asset(): Asset;
37
+ type(): TokenType;
38
+ identifier(): string;
39
+ }