Skip to content

Commit

Permalink
add get_pmob_balance, autocreate tables (#193)
Browse files Browse the repository at this point in the history
* add get_pmob_balance

* get_user_usd_balance/get_user_pmob_balance

* always autocreate table

* avoid recursing when autocreating table

* add FEE_PMOB to mc_util

* hide do_challenge
  • Loading branch information
technillogue authored Apr 7, 2022
1 parent 9e62fb8 commit 4ceea76
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
10 changes: 5 additions & 5 deletions contact/contactbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ async def do_mkgroup(self, message: Message, target_number: str) -> str:

async def payment_response(self, msg: Message, amount_pmob: int) -> str:
del amount_pmob
diff = await self.get_user_balance(msg.source) - self.usd_price
diff = await self.get_user_usd_balance(msg.source) - self.usd_price
if diff < 0:
return f"Please send another {abs(diff)} USD to buy a phone number"
if diff == 0:
Expand All @@ -196,7 +196,7 @@ async def do_status(self, message: Message) -> Union[list[str], str]:
if numbers:
return f"Hi {message.name}! We found several numbers {numbers} registered for your user. Try '/send {message.source} Hello from Forest Contact via {numbers[0]}!'."
# paid but not registered
if await self.get_user_balance(message.source) > 0 and not numbers:
if await self.get_user_usd_balance(message.source) > 0 and not numbers:
return [
"Welcome to the beta! Thank you for your payment. Please contact support to finish setting up your account by requesting to join this group. We will reach out within 12 hours.",
"https://signal.group/#CjQKINbHvfKoeUx_pPjipkXVspTj5HiTiUjoNQeNgmGvCmDnEhCTYgZZ0puiT-hUG0hUUwlS",
Expand Down Expand Up @@ -228,13 +228,13 @@ async def do_register(self, message: Message) -> Response:
"Upon payment, you will be able to select the area code for your new phone number!",
]

async def get_user_balance(self, account: str) -> float:
async def get_user_usd_balance(self, account: str) -> float:
res = await self.mobster.ledger_manager.get_usd_balance(account)
return float(round(res[0].get("balance"), 2))

async def do_balance(self, message: Message) -> str:
"""Check your balance"""
balance = await self.get_user_balance(message.source)
balance = await self.get_user_usd_balance(message.source)
return f"Your balance is {balance} USD"

async def do_pay(self, message: Message) -> str:
Expand All @@ -251,7 +251,7 @@ async def do_order(self, msg: Message) -> str:
"""Usage: /order <area code>"""
if not (msg.arg1 and len(msg.arg1) == 3 and msg.arg1.isnumeric()):
return """Usage: /order <area code>"""
diff = await self.get_user_balance(msg.source) - self.usd_price
diff = await self.get_user_usd_balance(msg.source) - self.usd_price
if diff < 0:
return "Make a payment with Signal Pay or /register first"
await self.routing_manager.sweep_expired_destinations()
Expand Down
7 changes: 6 additions & 1 deletion forest/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,10 +1014,14 @@ async def handle_message(self, message: Message) -> Response:
return None
return await super().handle_message(message)

async def get_user_balance(self, account: str) -> float:
async def get_user_usd_balance(self, account: str) -> float:
res = await self.mobster.ledger_manager.get_usd_balance(account)
return float(round(res[0].get("balance"), 2))

async def get_user_pmob_balance(self, account: str) -> float:
res = await self.mobster.ledger_manager.get_pmob_balance(account)
return res[0].get("balance")

async def handle_payment(self, message: Message) -> None:
"""Decode the receipt, then update balances.
Blocks on transaction completion, run concurrently"""
Expand Down Expand Up @@ -1651,6 +1655,7 @@ async def ask_email_question(

return email

@hide
async def do_challenge(self, msg: Message) -> Response:
"""Challenges a user to do a simple math problem,
optionally provided as an image to increase attacker complexity."""
Expand Down
2 changes: 2 additions & 0 deletions forest/payments_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
VALUES($1, $2, $3, $4, CURRENT_TIMESTAMP);",
get_usd_balance="SELECT COALESCE(SUM(amount_usd_cents)/100, 0.0) AS balance \
FROM {self.table} WHERE account=$1",
get_pmob_balance="SELECT COALESCE(SUM(amount_pmob), 0.0) AS balance \
FROM {self.table} WHERE account=$1",
)

InvoicePGEExpressions = PGExpressions(
Expand Down
24 changes: 20 additions & 4 deletions forest/pghelp.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ async def connect_pg(self) -> None:
self.pool = await asyncpg.create_pool(self.database)
pools.append(self.pool)

_autocreating_table = False

async def execute(
self,
qstring: str,
Expand All @@ -156,10 +158,24 @@ async def execute(
# )
# return self.execute(qstring, *args, timeout=timeout)
# _execute takes query, args, limit, timeout
result = await connection._execute(
qstring, args, 0, timeout, return_status=True
)
# list[asyncpg.Record], str, bool
try:
result = await connection._execute(
qstring, args, 0, timeout, return_status=True
)
# list[asyncpg.Record], str, bool
except asyncpg.UndefinedTableError:
if self._autocreating_table:
logging.error(
"would try creating the table, but we already tried to do that"
)
raise
self._autocreating_table = True
logging.info("creating table %s", self.table)
await self.create_table()
self._autocreating_table = False
result = await connection._execute(
qstring, args, 0, timeout, return_status=True
)
return result[0]
return None

Expand Down
1 change: 1 addition & 0 deletions mc_util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@


PMOB = Decimal("1e12")
FEE_PMOB = int(1e12 * 0.0004) # mobilecoin transaction fee in picomob.


def mob2pmob(x: Union[Decimal, float]) -> int:
Expand Down

0 comments on commit 4ceea76

Please sign in to comment.