|
| 1 | +import os |
| 2 | +import json |
| 3 | +import re |
| 4 | +import requests |
| 5 | +import asyncio |
| 6 | +from exceptions import Web3ServiceApiError |
| 7 | +from web3 import Web3 |
| 8 | + |
| 9 | + |
| 10 | +class Web3Util: |
| 11 | + BADGE_CONTRACT_ABI = [ |
| 12 | + { |
| 13 | + "constant": True, |
| 14 | + "inputs": [ |
| 15 | + { |
| 16 | + "internalType": "address", |
| 17 | + "name": "owner", |
| 18 | + "type": "address" |
| 19 | + } |
| 20 | + ], |
| 21 | + "name": "balanceOf", |
| 22 | + "outputs": [ |
| 23 | + { |
| 24 | + "internalType": "uint256", |
| 25 | + "name": "", |
| 26 | + "type": "uint256" |
| 27 | + } |
| 28 | + ], |
| 29 | + "payable": False, |
| 30 | + "stateMutability": "view", |
| 31 | + "type": "function" |
| 32 | + }, |
| 33 | + { |
| 34 | + "constant": True, |
| 35 | + "inputs": [ |
| 36 | + { |
| 37 | + "internalType": "address", |
| 38 | + "name": "owner", |
| 39 | + "type": "address" |
| 40 | + }, |
| 41 | + { |
| 42 | + "internalType": "uint256", |
| 43 | + "name": "index", |
| 44 | + "type": "uint256" |
| 45 | + } |
| 46 | + ], |
| 47 | + "name": "tokenOfOwnerByIndex", |
| 48 | + "outputs": [ |
| 49 | + { |
| 50 | + "internalType": "uint256", |
| 51 | + "name": "", |
| 52 | + "type": "uint256" |
| 53 | + } |
| 54 | + ], |
| 55 | + "payable": False, |
| 56 | + "stateMutability": "view", |
| 57 | + "type": "function" |
| 58 | + }, |
| 59 | + { |
| 60 | + "constant": True, |
| 61 | + "inputs": [ |
| 62 | + { |
| 63 | + "internalType": "uint256", |
| 64 | + "name": "tokenId", |
| 65 | + "type": "uint256" |
| 66 | + } |
| 67 | + ], |
| 68 | + "name": "tokenURI", |
| 69 | + "outputs": [ |
| 70 | + { |
| 71 | + "internalType": "string", |
| 72 | + "name": "", |
| 73 | + "type": "string" |
| 74 | + } |
| 75 | + ], |
| 76 | + "payable": False, |
| 77 | + "stateMutability": "view", |
| 78 | + "type": "function" |
| 79 | + }, |
| 80 | + { |
| 81 | + "constant": True, |
| 82 | + "inputs": [ |
| 83 | + { |
| 84 | + "internalType": "uint256", |
| 85 | + "name": "", |
| 86 | + "type": "uint256" |
| 87 | + } |
| 88 | + ], |
| 89 | + "name": "badgeTypeSupply", |
| 90 | + "outputs": [ |
| 91 | + { |
| 92 | + "internalType": "uint256", |
| 93 | + "name": "", |
| 94 | + "type": "uint256" |
| 95 | + } |
| 96 | + ], |
| 97 | + "payable": False, |
| 98 | + "stateMutability": "view", |
| 99 | + "type": "function" |
| 100 | + } |
| 101 | + ] |
| 102 | + |
| 103 | + @staticmethod |
| 104 | + def create_contract_object(operation_url, abi, contract_address): |
| 105 | + # web3の初期化 |
| 106 | + provider = Web3.HTTPProvider(operation_url) |
| 107 | + web3 = Web3(provider) |
| 108 | + # コントラクトの作成 |
| 109 | + return web3.eth.contract(web3.toChecksumAddress(contract_address), abi=abi) |
| 110 | + |
| 111 | + @staticmethod |
| 112 | + def create_badge_contract_object(): |
| 113 | + response = requests.get(os.environ['WEB3_SERVICE_BASE_URL'] + '/api/badge/eth_address') |
| 114 | + if response.status_code is not 200: |
| 115 | + raise Web3ServiceApiError(response.text) |
| 116 | + badge_address = json.loads(response.text)['badge_contract_address'] |
| 117 | + return Web3Util.create_contract_object(os.environ['BADGE_OPERATION_URL'], Web3Util.BADGE_CONTRACT_ABI, |
| 118 | + badge_address) |
| 119 | + |
| 120 | + @staticmethod |
| 121 | + def get_badge_types(user_id): |
| 122 | + badge_contract = Web3Util.create_badge_contract_object() |
| 123 | + response = requests.get(f"{os.environ['WEB3_SERVICE_BASE_URL']}/api/users/{user_id}/eth_address") |
| 124 | + if response.status_code is not 200: |
| 125 | + raise Web3ServiceApiError(response.text) |
| 126 | + # バッジ連携していない場合は空配列を返却 |
| 127 | + badge_address = json.loads(response.text).get('public_chain_address') |
| 128 | + result = [] |
| 129 | + if badge_address is None: |
| 130 | + return result |
| 131 | + # 該当ユーザの全バッジのtypeを取得 |
| 132 | + # バッジの数を取得 |
| 133 | + badge_len = badge_contract.functions.balanceOf(badge_address).call() |
| 134 | + # バッジの情報を並列に取得 |
| 135 | + loop = asyncio.get_event_loop() |
| 136 | + coros = [Web3Util.__get_badge_type(badge_contract, badge_address, i, loop) for i in range(badge_len)] |
| 137 | + groups = asyncio.gather(*coros) |
| 138 | + results = loop.run_until_complete(groups) |
| 139 | + return list(set(results)) |
| 140 | + |
| 141 | + @staticmethod |
| 142 | + async def __get_badge_type(badge_contract, badge_address, badge_index, loop): |
| 143 | + # トークンIDを取得 |
| 144 | + token_id = await loop.run_in_executor(None, badge_contract.functions.tokenOfOwnerByIndex(badge_address, |
| 145 | + badge_index).call) |
| 146 | + # トークンのメタデータURIを取得 |
| 147 | + token_uri = await loop.run_in_executor(None, badge_contract.functions.tokenURI(token_id).call) |
| 148 | + type_match = re.match('.*/(\\d+)/metadata.json', token_uri) |
| 149 | + if type_match is not None: |
| 150 | + return int(type_match.group(1)) |
| 151 | + return 0 |
0 commit comments