2021-06-07 07:10:18 -07:00
|
|
|
# # ⚠ Warning
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
|
|
|
# LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
# NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
#
|
|
|
|
# [🥭 Mango Markets](https://mango.markets/) support is available at:
|
|
|
|
# [Docs](https://docs.mango.markets/)
|
|
|
|
# [Discord](https://discord.gg/67jySBhxrg)
|
|
|
|
# [Twitter](https://twitter.com/mangomarkets)
|
|
|
|
# [Github](https://github.com/blockworks-foundation)
|
|
|
|
# [Email](mailto:hello@blockworks.foundation)
|
|
|
|
|
|
|
|
|
|
|
|
import logging
|
2021-10-04 10:27:07 -07:00
|
|
|
import numbers
|
2021-06-07 07:10:18 -07:00
|
|
|
import typing
|
|
|
|
|
|
|
|
from decimal import Decimal
|
|
|
|
from solana.publickey import PublicKey
|
|
|
|
from solana.rpc.types import TokenAccountOpts
|
|
|
|
|
|
|
|
from .context import Context
|
2022-01-11 06:36:08 -08:00
|
|
|
from .output import output
|
2022-02-24 11:40:05 -08:00
|
|
|
from .tokens import Instrument, Token
|
2021-06-07 07:10:18 -07:00
|
|
|
|
|
|
|
|
2021-11-09 05:23:36 -08:00
|
|
|
def _decimal_from_number(value: numbers.Number) -> Decimal:
|
|
|
|
# Decimal constructor can only handle these Number types:
|
|
|
|
# Union[Decimal, float, str, Tuple[int, Sequence[int], int]]
|
|
|
|
if isinstance(value, Decimal):
|
|
|
|
return value
|
|
|
|
if isinstance(value, int):
|
|
|
|
return Decimal(value)
|
|
|
|
if isinstance(value, float):
|
|
|
|
return Decimal(value)
|
|
|
|
if isinstance(value, str):
|
|
|
|
return Decimal(value)
|
|
|
|
raise Exception(f"Cannot handle conversion of {value} to Decimal.")
|
|
|
|
|
|
|
|
|
2021-11-08 03:39:09 -08:00
|
|
|
# # 🥭 InstrumentValue class
|
2021-06-07 07:10:18 -07:00
|
|
|
#
|
2021-11-08 03:39:09 -08:00
|
|
|
# The `InstrumentValue` class is a simple way of keeping a token and value together, and
|
2021-06-07 07:10:18 -07:00
|
|
|
# displaying them nicely consistently.
|
|
|
|
#
|
2021-11-08 03:39:09 -08:00
|
|
|
class InstrumentValue:
|
2021-11-09 05:23:36 -08:00
|
|
|
def __init__(self, token: Instrument, value: Decimal) -> None:
|
2021-12-13 03:15:24 -08:00
|
|
|
self._logger: logging.Logger = logging.getLogger(self.__class__.__name__)
|
2021-11-08 03:39:09 -08:00
|
|
|
self.token: Instrument = token
|
|
|
|
self.value: Decimal = value
|
2021-10-04 10:27:07 -07:00
|
|
|
if not isinstance(self.value, Decimal):
|
|
|
|
raise Exception(f"Value is {type(self.value)}, not Decimal: {self.value}")
|
2021-06-07 07:10:18 -07:00
|
|
|
|
2021-11-08 03:39:09 -08:00
|
|
|
def shift_to_native(self) -> "InstrumentValue":
|
2021-06-25 02:33:40 -07:00
|
|
|
new_value = self.token.shift_to_native(self.value)
|
2021-11-08 03:39:09 -08:00
|
|
|
return InstrumentValue(self.token, new_value)
|
2021-06-25 02:33:40 -07:00
|
|
|
|
2021-06-07 07:10:18 -07:00
|
|
|
@staticmethod
|
2022-02-09 11:31:50 -08:00
|
|
|
def fetch_total_value_or_none(
|
|
|
|
context: Context, account_public_key: PublicKey, token: Token
|
|
|
|
) -> typing.Optional["InstrumentValue"]:
|
2021-06-07 07:10:18 -07:00
|
|
|
opts = TokenAccountOpts(mint=token.mint)
|
|
|
|
|
2022-02-09 11:31:50 -08:00
|
|
|
token_accounts = context.client.get_token_accounts_by_owner(
|
|
|
|
account_public_key, opts
|
|
|
|
)
|
2021-06-07 07:10:18 -07:00
|
|
|
if len(token_accounts) == 0:
|
|
|
|
return None
|
|
|
|
|
|
|
|
total_value = Decimal(0)
|
|
|
|
for token_account in token_accounts:
|
2022-02-09 11:31:50 -08:00
|
|
|
token_balance: Decimal = context.client.get_token_account_balance(
|
|
|
|
token_account["pubkey"]
|
|
|
|
)
|
2021-09-13 08:15:20 -07:00
|
|
|
total_value += token_balance
|
2021-06-07 07:10:18 -07:00
|
|
|
|
2021-11-08 03:39:09 -08:00
|
|
|
return InstrumentValue(token, total_value)
|
2021-06-07 07:10:18 -07:00
|
|
|
|
|
|
|
@staticmethod
|
2022-02-09 11:31:50 -08:00
|
|
|
def fetch_total_value(
|
|
|
|
context: Context, account_public_key: PublicKey, token: Token
|
|
|
|
) -> "InstrumentValue":
|
|
|
|
value = InstrumentValue.fetch_total_value_or_none(
|
|
|
|
context, account_public_key, token
|
|
|
|
)
|
2021-06-07 07:10:18 -07:00
|
|
|
if value is None:
|
2021-11-08 03:39:09 -08:00
|
|
|
return InstrumentValue(token, Decimal(0))
|
2021-06-07 07:10:18 -07:00
|
|
|
return value
|
|
|
|
|
|
|
|
@staticmethod
|
2022-02-09 11:31:50 -08:00
|
|
|
def report(
|
|
|
|
values: typing.Sequence["InstrumentValue"],
|
|
|
|
reporter: typing.Callable[[str], None] = output,
|
|
|
|
) -> None:
|
2021-06-07 07:10:18 -07:00
|
|
|
for value in values:
|
|
|
|
reporter(f"{value.value:>18,.8f} {value.token.name}")
|
|
|
|
|
|
|
|
@staticmethod
|
2022-02-09 11:31:50 -08:00
|
|
|
def find_by_symbol(
|
|
|
|
values: typing.Sequence[typing.Optional["InstrumentValue"]], symbol: str
|
|
|
|
) -> "InstrumentValue":
|
2021-07-01 13:24:08 -07:00
|
|
|
found = [
|
2022-02-09 11:31:50 -08:00
|
|
|
value
|
|
|
|
for value in values
|
|
|
|
if value is not None
|
|
|
|
and value.token is not None
|
|
|
|
and value.token.symbol_matches(symbol)
|
|
|
|
]
|
2021-06-07 07:10:18 -07:00
|
|
|
if len(found) == 0:
|
|
|
|
raise Exception(f"Token '{symbol}' not found in token values: {values}")
|
|
|
|
|
|
|
|
if len(found) > 1:
|
2022-02-09 11:31:50 -08:00
|
|
|
raise Exception(
|
|
|
|
f"Token '{symbol}' matched multiple tokens in values: {values}"
|
|
|
|
)
|
2021-06-07 07:10:18 -07:00
|
|
|
|
|
|
|
return found[0]
|
|
|
|
|
|
|
|
@staticmethod
|
2022-02-09 11:31:50 -08:00
|
|
|
def find_by_token(
|
|
|
|
values: typing.Sequence[typing.Optional["InstrumentValue"]], token: Instrument
|
|
|
|
) -> "InstrumentValue":
|
2021-11-08 03:39:09 -08:00
|
|
|
return InstrumentValue.find_by_symbol(values, token.symbol)
|
2021-06-07 07:10:18 -07:00
|
|
|
|
|
|
|
@staticmethod
|
2022-02-09 11:31:50 -08:00
|
|
|
def changes(
|
|
|
|
before: typing.Sequence["InstrumentValue"],
|
|
|
|
after: typing.Sequence["InstrumentValue"],
|
|
|
|
) -> typing.Sequence["InstrumentValue"]:
|
2021-11-08 03:39:09 -08:00
|
|
|
changes: typing.List[InstrumentValue] = []
|
2021-06-07 07:10:18 -07:00
|
|
|
for before_balance in before:
|
2021-11-08 03:39:09 -08:00
|
|
|
after_balance = InstrumentValue.find_by_token(after, before_balance.token)
|
2022-02-09 11:31:50 -08:00
|
|
|
result = InstrumentValue(
|
|
|
|
before_balance.token, after_balance.value - before_balance.value
|
|
|
|
)
|
2021-06-07 07:10:18 -07:00
|
|
|
changes += [result]
|
|
|
|
|
|
|
|
return changes
|
|
|
|
|
2021-11-08 03:39:09 -08:00
|
|
|
def __add__(self, token_value_to_add: "InstrumentValue") -> "InstrumentValue":
|
2021-08-04 05:08:34 -07:00
|
|
|
if self.token != token_value_to_add.token:
|
|
|
|
raise Exception(
|
2022-02-09 11:31:50 -08:00
|
|
|
f"Cannot add InstrumentValues from different tokens ({self.token} and {token_value_to_add.token})."
|
|
|
|
)
|
2021-11-08 03:39:09 -08:00
|
|
|
return InstrumentValue(self.token, self.value + token_value_to_add.value)
|
2021-08-04 05:08:34 -07:00
|
|
|
|
2021-11-08 03:39:09 -08:00
|
|
|
def __sub__(self, token_value_to_subtract: "InstrumentValue") -> "InstrumentValue":
|
2021-08-04 05:08:34 -07:00
|
|
|
if self.token != token_value_to_subtract.token:
|
|
|
|
raise Exception(
|
2022-02-09 11:31:50 -08:00
|
|
|
f"Cannot subtract InstrumentValues from different tokens ({self.token} and {token_value_to_subtract.token})."
|
|
|
|
)
|
2021-11-08 03:39:09 -08:00
|
|
|
return InstrumentValue(self.token, self.value - token_value_to_subtract.value)
|
2021-08-04 05:08:34 -07:00
|
|
|
|
2021-11-08 03:39:09 -08:00
|
|
|
def __mul__(self, token_value_to_multiply: "InstrumentValue") -> "InstrumentValue":
|
|
|
|
# Multiplying by another InstrumentValue is assumed to be a token value multiplied by a token price.
|
2021-10-04 10:27:07 -07:00
|
|
|
# The result should be denominated in the currency of the price.
|
2022-02-09 11:31:50 -08:00
|
|
|
return InstrumentValue(
|
|
|
|
token_value_to_multiply.token, self.value * token_value_to_multiply.value
|
|
|
|
)
|
2021-10-04 10:27:07 -07:00
|
|
|
|
2021-11-09 05:23:36 -08:00
|
|
|
def __lt__(self, other: typing.Any) -> bool:
|
2021-10-04 10:27:07 -07:00
|
|
|
if isinstance(other, numbers.Number):
|
2021-11-09 05:23:36 -08:00
|
|
|
return self.value < _decimal_from_number(other)
|
2021-10-04 10:27:07 -07:00
|
|
|
|
2021-11-08 03:39:09 -08:00
|
|
|
if not isinstance(other, InstrumentValue):
|
2021-10-04 10:27:07 -07:00
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
if self.token != other.token:
|
|
|
|
raise Exception(
|
2022-02-09 11:31:50 -08:00
|
|
|
f"Cannot compare token values when one token is {self.token.symbol} and the other is {other.token.symbol}."
|
|
|
|
)
|
2021-10-04 10:27:07 -07:00
|
|
|
return self.value < other.value
|
|
|
|
|
2021-11-09 05:23:36 -08:00
|
|
|
def __gt__(self, other: typing.Any) -> bool:
|
2021-10-04 10:27:07 -07:00
|
|
|
if isinstance(other, numbers.Number):
|
2021-11-09 05:23:36 -08:00
|
|
|
return self.value > _decimal_from_number(other)
|
2021-10-04 10:27:07 -07:00
|
|
|
|
2021-11-08 03:39:09 -08:00
|
|
|
if not isinstance(other, InstrumentValue):
|
2021-10-04 10:27:07 -07:00
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
if self.token != other.token:
|
|
|
|
raise Exception(
|
2022-02-09 11:31:50 -08:00
|
|
|
f"Cannot compare token values when one token is {self.token.symbol} and the other is {other.token.symbol}."
|
|
|
|
)
|
2021-10-04 10:27:07 -07:00
|
|
|
return self.value > other.value
|
|
|
|
|
2021-08-04 05:08:34 -07:00
|
|
|
def __eq__(self, other: typing.Any) -> bool:
|
2022-02-09 11:31:50 -08:00
|
|
|
if (
|
|
|
|
isinstance(other, InstrumentValue)
|
|
|
|
and self.token == other.token
|
|
|
|
and self.value == other.value
|
|
|
|
):
|
2021-08-04 05:08:34 -07:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2021-11-09 05:23:36 -08:00
|
|
|
def __format__(self, format_spec: str) -> str:
|
2021-10-04 10:27:07 -07:00
|
|
|
return format(str(self), format_spec)
|
|
|
|
|
2021-06-07 07:10:18 -07:00
|
|
|
def __str__(self) -> str:
|
2021-12-13 04:06:42 -08:00
|
|
|
name = "« Un-Named Instrument »"
|
2021-07-01 13:24:08 -07:00
|
|
|
if self.token and self.token.name:
|
|
|
|
name = self.token.name
|
2021-12-13 04:06:42 -08:00
|
|
|
return f"« InstrumentValue: {self.value:>18,.8f} {name} »"
|
2021-06-07 07:10:18 -07:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"{self}"
|