Skip to content

Commit

Permalink
Merge pull request #333 from planetarium/feature/thor
Browse files Browse the repository at this point in the history
Apply changes for Thor planet
  • Loading branch information
U-lis authored Nov 14, 2024
2 parents 96e7b92 + e152818 commit d193f53
Show file tree
Hide file tree
Showing 17 changed files with 395 additions and 359 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ jobs:
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.4.2
version: 1.8.4
virtualenvs-in-project: true

- name: Cache Poetry
Expand Down Expand Up @@ -136,6 +136,7 @@ jobs:
HEADLESS: ${{ vars.HEADLESS }}
ODIN_GQL_URL: ${{ vars.ODIN_GQL_URL }}
HEIMDALL_GQL_URL: ${{ vars.HEIMDALL_GQL_URL }}
THOR_GQL_URL: ${{ vars.THOR_GQL_URL }}
KMS_KEY_ID: ${{ secrets.KMS_KEY_ID }}
ADHOC_KMS_KEY_ID: ${{ secrets.ADHOC_KMS_KEY_ID }}
GOOGLE_CREDENTIAL: ${{ secrets.GOOGLE_CREDENTIAL }}
Expand Down Expand Up @@ -174,6 +175,7 @@ jobs:
HEADLESS: ${{ vars.HEADLESS }}
ODIN_GQL_URL: ${{ vars.ODIN_GQL_URL }}
HEIMDALL_GQL_URL: ${{ vars.HEIMDALL_GQL_URL }}
THOR_GQL_URL: ${{ vars.THOR_GQL_URL }}
KMS_KEY_ID: ${{ secrets.KMS_KEY_ID }}
ADHOC_KMS_KEY_ID: ${{ secrets.ADHOC_KMS_KEY_ID }}
GOOGLE_CREDENTIAL: ${{ secrets.GOOGLE_CREDENTIAL }}
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/synth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ jobs:
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.4.2
version: 1.8.4
virtualenvs-in-project: true

- name: Cache Poetry
Expand Down Expand Up @@ -123,6 +123,7 @@ jobs:
HEADLESS: ${{ vars.HEADLESS }}
ODIN_GQL_URL: ${{ vars.ODIN_GQL_URL }}
HEIMDALL_GQL_URL: ${{ vars.HEIMDALL_GQL_URL }}
THOR_GQL_URL: ${{ vars.THOR_GQL_URL }}
KMS_KEY_ID: ${{ secrets.KMS_KEY_ID }}
ADHOC_KMS_KEY_ID: ${{ secrets.ADHOC_KMS_KEY_ID }}
GOOGLE_CREDENTIAL: ${{ secrets.GOOGLE_CREDENTIAL }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 1.4.2
version: 1.8.4
virtualenvs-in-project: true

- name: Cache Poetry
Expand Down
1 change: 1 addition & 0 deletions common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Config:
cdn_host_k: str
odin_gql_url: str
heimdall_gql_url: str
thor_gql_url: str
headless_gql_jwt_secret: str

# Multiplanetary
Expand Down
33 changes: 0 additions & 33 deletions common/utils/address.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,7 @@
import os

from common.utils.receipt import PlanetID


def format_addr(addr: str) -> str:
"""
Check and reformat input address if not starts with `0x`.
"""
if not addr.startswith("0x"):
addr = f"0x{addr}"
return addr.lower()


def get_vault_agent_address(planet_id: PlanetID) -> str:
if planet_id == PlanetID.ODIN:
address = os.environ["ODIN_AGENT_ADDRESS"]
elif planet_id == PlanetID.HEIMDALL:
address = os.environ["HEIMDALL_AGENT_ADDRESS"]
elif planet_id == PlanetID.ODIN_INTERNAL:
address = os.environ["ODIN_INTERNAL_AGENT_ADDRESS"]
elif planet_id == PlanetID.HEIMDALL_INTERNAL:
address = os.environ["HEIMDALL_INTERNAL_AGENT_ADDRESS"]
else:
raise ValueError(f"{planet_id!r} is not a value {PlanetID}")
return format_addr(address)


def get_vault_avatar_address(planet_id: PlanetID) -> str:
if planet_id == PlanetID.ODIN:
address = os.environ["ODIN_AVATAR_ADDRESS"]
elif planet_id == PlanetID.HEIMDALL:
address = os.environ["HEIMDALL_AVATAR_ADDRESS"]
elif planet_id == PlanetID.ODIN_INTERNAL:
address = os.environ["ODIN_INTERNAL_AVATAR_ADDRESS"]
elif planet_id == PlanetID.HEIMDALL_INTERNAL:
address = os.environ["HEIMDALL_INTERNAL_AVATAR_ADDRESS"]
else:
raise ValueError(f"{planet_id!r} is not a value {PlanetID}")
return format_addr(address)
12 changes: 9 additions & 3 deletions common/utils/transaction.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import datetime
from typing import Any, Dict, List, Optional, Tuple, Union
from typing import Any, Dict

import bencodex

from common.utils.receipt import PlanetID


def create_unsigned_tx(planet_id: PlanetID, public_key: str, address: str, nonce: int, plain_value: Dict[str, Any], timestamp: datetime.datetime) -> bytes:
def create_unsigned_tx(planet_id: PlanetID, public_key: str, address: str, nonce: int, plain_value: Dict[str, Any],
timestamp: datetime.datetime) -> bytes:
if address.startswith("0x"):
address = address[2:]
return bencodex.dumps({
Expand All @@ -33,20 +34,25 @@ def create_unsigned_tx(planet_id: PlanetID, public_key: str, address: str, nonce
b"u": [],
})


def append_signature_to_unsigned_tx(unsigned_tx: bytes, signature: bytes) -> bytes:
decoded = bencodex.loads(unsigned_tx)
decoded[b"S"] = signature
return bencodex.dumps(decoded)


def get_genesis_block_hash(planet_id: PlanetID) -> bytes:
switcher = {
PlanetID.ODIN: bytes.fromhex("4582250d0da33b06779a8475d283d5dd210c683b9b999d74d03fac4f58fa6bce"),
PlanetID.ODIN_INTERNAL: bytes.fromhex("4582250d0da33b06779a8475d283d5dd210c683b9b999d74d03fac4f58fa6bce"),
PlanetID.HEIMDALL: bytes.fromhex("729fa26958648a35b53e8e3905d11ec53b1b4929bf5f499884aed7df616f5913"),
PlanetID.HEIMDALL_INTERNAL: bytes.fromhex("729fa26958648a35b53e8e3905d11ec53b1b4929bf5f499884aed7df616f5913"),
# FIXME: Set to right genesis hash
PlanetID.THOR: bytes.fromhex("abc123"),
PlanetID.THOR_INTERNAL: bytes.fromhex("d55426c88650a1cb2cebc883e79721a4fad2ad5a62103fba5e899736b94bbacf"),
}

if planet_id not in switcher:
raise ValueError("Invalid planet id")

return switcher[planet_id]
15 changes: 15 additions & 0 deletions iap/api/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def product_list(agent_addr: str,
category_schema_list = []
purchase_history = get_purchase_history(sess, planet_id, agent_addr)
for category in all_category_list:
# Do not show Mileage category for thor chain
if planet_id in (PlanetID.THOR, PlanetID.THOR_INTERNAL) and category.name == "Mileage":
continue

cat_schema = CategorySchema.model_validate(category)
schema_dict = {}
for product in category.product_list:
Expand Down Expand Up @@ -74,6 +78,17 @@ def product_list(agent_addr: str,
else: # Product with no limitation
schema.buyable = True

# Thor chain
if planet_id in (PlanetID.THOR, PlanetID.THOR_INTERNAL):
schema.path = schema.path.replace(".png", "_THOR.png")
schema.popup_path_key += "_THOR"

schema.mileage *= 5
for item in schema.fungible_item_list:
item.amount *= 5
for fav in schema.fav_list:
fav.amount *= 5

schema_dict[product.id] = schema

cat_schema.product_list = list(schema_dict.values())
Expand Down
25 changes: 21 additions & 4 deletions iap/api/purchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ def check_required_level(sess, receipt: Receipt, product: Product) -> Receipt:
gql_url = os.environ.get("ODIN_GQL_URL")
elif receipt.planet_id in (PlanetID.HEIMDALL, PlanetID.HEIMDALL_INTERNAL):
gql_url = os.environ.get("HEIMDALL_GQL_URL")
elif receipt.planet_id in (PlanetID.THOR, PlanetID.THOR_INTERNAL):
gql_url = os.environ.get("THOR_GQL_URL")

gql = GQL(gql_url, jwt_secret=HEADLESS_GQL_JWT_SECRET)
query = f"""{{ stateQuery {{ avatar (avatarAddress: "{receipt.avatar_addr}") {{ level}} }} }}"""
Expand Down Expand Up @@ -359,10 +361,18 @@ def request_product(receipt_data: ReceiptSchema,
settings.REGION_NAME,
f"{os.environ.get('STAGE')}_9c_SEASON_PASS_HOST", False
)["Value"]
claim_list = [{"ticker": x.fungible_item_id, "amount": x.amount, "decimal_places": 0}
for x in product.fungible_item_list]
claim_list.extend([{"ticker": x.ticker, "amount": x.amount, "decimal_places": x.decimal_places}
for x in product.fav_list])
claim_list = [
{"ticker": x.fungible_item_id,
"amount": x.amount * (5 if receipt.planet_id in (PlanetID.THOR, PlanetID.THOR_INTERNAL) else 1),
"decimal_places": 0}
for x in product.fungible_item_list
]
claim_list.extend([
{"ticker": x.ticker,
"amount": x.amount * (5 if receipt.planet_id in (PlanetID.THOR, PlanetID.THOR_INTERNAL) else 1),
"decimal_places": x.decimal_places}
for x in product.fav_list
])
season_pass_type = "".join([x for x in body if x.isalpha()])
resp = requests.post(f"{season_pass_host}/api/user/upgrade",
json={
Expand Down Expand Up @@ -563,6 +573,13 @@ def mileage_product(receipt_data: FreeReceiptSchema,
receipt.msg = "This product it not for free"
raise_error(sess, receipt, ValueError(f"Requested product {product.id}::{product.name} is not mileage product"))

if receipt.planet_id in (PlanetID.THOR, PlanetID.THOR_INTERNAL):
receipt.status = ReceiptStatus.INVALID
receipt.msg = f"No mileage product for thor chain"
raise_error(sess, receipt,
ValueError(f"You cannot purchase mileage product {product.id}::{product.name} in Thor chain")
)

if ((product.open_timestamp and product.open_timestamp > datetime.now()) or
(product.close_timestamp and product.close_timestamp < datetime.now())):
receipt.status = ReceiptStatus.TIME_LIMIT
Expand Down
1 change: 1 addition & 0 deletions iap/iap_cdk_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
"HEADLESS": config.headless,
"ODIN_GQL_URL": config.odin_gql_url,
"HEIMDALL_GQL_URL": config.heimdall_gql_url,
"THOR_GQL_URL": config.thor_gql_url,
"CDN_HOST": config.cdn_host,
"CDN_HOST_K": config.cdn_host_k,
"PLANET_URL": config.planet_url,
Expand Down
5 changes: 4 additions & 1 deletion iap/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ def upsert_mileage(sess, product: Product, receipt: Receipt, mileage: Optional[M
"""
if mileage is None:
mileage = get_mileage(sess, PlanetID(receipt.planet_id), receipt.agent_addr)
mileage.mileage += (product.mileage or 0)
target_mileage = product.mileage or 0
if receipt.planet_id in (PlanetID.THOR, PlanetID.THOR_INTERNAL):
target_mileage *= 5
mileage.mileage += target_mileage
receipt.mileage_change = (product.mileage or 0) - (product.mileage_price or 0)
receipt.mileage_result = mileage.mileage
sess.add(mileage)
Expand Down
Loading

0 comments on commit d193f53

Please sign in to comment.