Skip to content

Commit

Permalink
Merge pull request #289 from planetarium/feature/claim-items
Browse files Browse the repository at this point in the history
Use "ClaimItems" action to send IAP product
  • Loading branch information
U-lis authored Aug 22, 2024
2 parents 5d0063e + 77b87f9 commit e27c70e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 51 deletions.
3 changes: 2 additions & 1 deletion common/_graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@


class GQL:
def __init__(self, url: str = f"{os.environ.get('HEADLESS')}/graphql", jwt_secret: str = None):
def __init__(self, url: str, jwt_secret: str = None):
assert url is not None
self._url = url
self.__jwt_secret = jwt_secret
self.client = None
Expand Down
9 changes: 9 additions & 0 deletions common/consts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

from common.utils.receipt import PlanetID

HOST_LIST = {
"development": [
os.environ.get("HEADLESS", "http://localhost")
Expand Down Expand Up @@ -29,3 +31,10 @@
"SOULSTONE_1003", # Valkyrie of Light
"SOULSTONE_1004", # Lil' Fenrir
)

GQL_DICT = {
PlanetID.ODIN: os.environ.get("ODIN_GQL_URL"),
PlanetID.ODIN_INTERNAL: os.environ.get("ODIN_GQL_URL"),
PlanetID.HEIMDALL: os.environ.get("HEIMDALL_GQL_URL"),
PlanetID.HEIMDALL_INTERNAL: os.environ.get("HEIMDALL_GQL_URL"),
}
1 change: 1 addition & 0 deletions iap/api/purchase.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ def free_product(receipt_data: FreeReceiptSchema,
"product_id": product.id,
"uuid": str(receipt.uuid),
"planet_id": receipt_data.planetId.decode('utf-8'),
"package_name": receipt.package_name,
}

resp = sqs.send_message(QueueUrl=SQS_URL, MessageBody=json.dumps(msg))
Expand Down
73 changes: 23 additions & 50 deletions worker/worker/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json
import logging
import os
import uuid
from dataclasses import dataclass
from typing import List, Optional, Tuple, Union

Expand All @@ -13,19 +12,21 @@
from common import logger
from common._crypto import Account
from common._graphql import GQL
from common.consts import GQL_DICT
from common.enums import TxStatus, PackageName
from common.lib9c.actions.claim_items import ClaimItems
from common.lib9c.models.address import Address
from common.lib9c.models.fungible_asset_value import FungibleAssetValue
from common.models.product import Product
from common.models.receipt import Receipt
from common.utils.actions import create_unload_my_garages_action_plain_value
from common.utils.aws import fetch_secrets, fetch_kms_key_id, fetch_parameter
from common.utils.aws import fetch_parameter, fetch_secrets, fetch_kms_key_id
from common.utils.receipt import PlanetID
from common.utils.transaction import create_unsigned_tx, append_signature_to_unsigned_tx

DB_URI = os.environ.get("DB_URI")
db_password = fetch_secrets(os.environ.get("REGION_NAME"), os.environ.get("SECRET_ARN"))["password"]
DB_URI = DB_URI.replace("[DB_PASSWORD]", db_password)
CURRENT_PLANET = PlanetID.ODIN if os.environ.get("STAGE") == "mainnet" else PlanetID.ODIN_INTERNAL
GQL_URL = f"{os.environ.get('HEADLESS')}/graphql"
HEADLESS_GQL_JWT_SECRET = fetch_parameter(
os.environ.get("REGION_NAME"),
f"{os.environ.get('STAGE')}_9c_IAP_HEADLESS_GQL_JWT_SECRET",
Expand All @@ -34,27 +35,6 @@

engine = create_engine(DB_URI, pool_size=5, max_overflow=5)

planet_dict = {
# Mainnet
PlanetID.ODIN: {
"agent": "0x1c2ae97380CFB4F732049e454F6D9A25D4967c6f",
"avatar": "0x41aEFE4cdDFb57C9dFfd490e17e571705c593dDc"
},
PlanetID.HEIMDALL: {
"agent": "0x1c2ae97380CFB4F732049e454F6D9A25D4967c6f",
"avatar": "0x41aEFE4cdDFb57C9dFfd490e17e571705c593dDc"
},
# Internal
PlanetID.ODIN_INTERNAL: {
"agent": "0x1c2ae97380CFB4F732049e454F6D9A25D4967c6f",
"avatar": "0x41aEFE4cdDFb57C9dFfd490e17e571705c593dDc"
},
PlanetID.HEIMDALL_INTERNAL: {
"agent": "0x1c2ae97380CFB4F732049e454F6D9A25D4967c6f",
"avatar": "0x41aEFE4cdDFb57C9dFfd490e17e571705c593dDc"
},
}


@dataclass
class SQSMessageRecord:
Expand Down Expand Up @@ -93,7 +73,9 @@ def process(sess: Session, message: SQSMessageRecord, nonce: int = None) -> Tupl
region_name = os.environ.get("REGION_NAME", "us-east-2")
logging.debug(f"STAGE: {stage} || REGION: {region_name}")
account = Account(fetch_kms_key_id(stage, region_name))
gql = GQL(GQL_URL, HEADLESS_GQL_JWT_SECRET)
planet_id: PlanetID = PlanetID(bytes(message.body["planet_id"], 'utf-8'))

gql = GQL(GQL_DICT[planet_id], HEADLESS_GQL_JWT_SECRET)
if not nonce:
nonce = gql.get_next_nonce(account.address)

Expand All @@ -103,39 +85,30 @@ def process(sess: Session, message: SQSMessageRecord, nonce: int = None) -> Tupl
.where(Product.id == message.body.get("product_id"))
)

planet_id: PlanetID = PlanetID(bytes(message.body["planet_id"], 'utf-8'))
agent_address = message.body.get("agent_addr")
avatar_address = message.body.get("avatar_addr")
package_name = PackageName(message.body.get("package_name"))
avatar_address = Address(message.body.get("avatar_addr"))
memo = json.dumps({"iap":
{"g_sku": product.google_sku,
"a_sku": product.apple_sku_k if package_name == PackageName.NINE_CHRONICLES_K
else product.apple_sku}
})
# Through bridge
if planet_id != CURRENT_PLANET:
agent_address = planet_dict[planet_id]["agent"]
avatar_address = planet_dict[planet_id]["avatar"]
memo = json.dumps([message.body.get("agent_addr"), message.body.get("avatar_addr"), memo])
fav_data = [x.to_fav_data(agent_address=agent_address, avatar_address=avatar_address) for x in product.fav_list]

item_data = [{
"fungibleId": x.fungible_item_id,
"count": x.amount
} for x in product.fungible_item_list]

unload_from_garage = create_unload_my_garages_action_plain_value(
_id=uuid.uuid1().hex,
fav_data=fav_data,
avatar_addr=avatar_address,
item_data=item_data,
memo=memo
)

claim_data = []
for item in product.fungible_item_list:
claim_data.append(FungibleAssetValue.from_raw_data(
ticker=item.fungible_item_id, decimal_places=0, amount=item.amount
))
for fav in product.fav_list:
claim_data.append(FungibleAssetValue.from_raw_data(
ticker=fav.ticker, decimal_places=fav.decimal_places, amount=fav.amount
))

action = ClaimItems(claim_data=[{"avatarAddress": avatar_address, "fungibleAssetValues": claim_data}], memo=memo)

unsigned_tx = create_unsigned_tx(
planet_id=PlanetID.ODIN if os.environ.get("STAGE") == "mainnet" else PlanetID.ODIN_INTERNAL,
planet_id=planet_id,
public_key=account.pubkey.hex(), address=account.address, nonce=nonce,
plain_value=unload_from_garage, timestamp=datetime.datetime.utcnow() + datetime.timedelta(days=1)
plain_value=action.plain_value, timestamp=datetime.datetime.utcnow() + datetime.timedelta(days=7)
)

signature = account.sign_tx(unsigned_tx)
Expand Down
2 changes: 2 additions & 0 deletions worker/worker_cdk_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
f"/iap",
"GOOGLE_PACKAGE_NAME": config.google_package_name,
"HEADLESS": config.headless,
"ODIN_GQL_URL": config.odin_gql_url,
"HEIMDALL_GQL_URL": config.heimdall_gql_url,
"PLANET_URL": config.planet_url,
"BRIDGE_DATA": config.bridge_data,
"HEADLESS_GQL_JWT_SECRET": config.headless_gql_jwt_secret,
Expand Down

0 comments on commit e27c70e

Please sign in to comment.