def exchange_contracts(exchange_address): if config.IBET_SB_EXCHANGE_CONTRACT_ADDRESS is not None and \ exchange_address == to_checksum_address(config.IBET_SB_EXCHANGE_CONTRACT_ADDRESS): ExchangeContract = Contract.get_contract( "IbetStraightBondExchange", exchange_address) exchange = "IbetStraightBondExchange" elif config.IBET_MEMBERSHIP_EXCHANGE_CONTRACT_ADDRESS is not None and \ exchange_address == to_checksum_address(config.IBET_MEMBERSHIP_EXCHANGE_CONTRACT_ADDRESS): ExchangeContract = Contract.get_contract("IbetMembershipExchange", exchange_address) exchange = "IbetMembershipExchange" elif config.IBET_CP_EXCHANGE_CONTRACT_ADDRESS is not None and \ exchange_address == to_checksum_address(config.IBET_CP_EXCHANGE_CONTRACT_ADDRESS): ExchangeContract = Contract.get_contract("IbetCouponExchange", exchange_address) exchange = "IbetCouponExchange" elif config.IBET_SHARE_EXCHANGE_CONTRACT_ADDRESS is not None and \ exchange_address == to_checksum_address(config.IBET_SHARE_EXCHANGE_CONTRACT_ADDRESS): ExchangeContract = Contract.get_contract("IbetOTCExchange", exchange_address) exchange = "IbetOTCExchange" else: raise InvalidParameterError(description="Invalid Address") return ExchangeContract, exchange
def __init__(self, sink, db): self.exchange_list = [] # 債券取引コントラクト登録 if IBET_SB_EXCHANGE_CONTRACT_ADDRESS is not None: bond_exchange_contract = Contract.get_contract( "IbetStraightBondExchange", IBET_SB_EXCHANGE_CONTRACT_ADDRESS) self.exchange_list.append(bond_exchange_contract) # 会員権取引コントラクト登録 if IBET_MEMBERSHIP_EXCHANGE_CONTRACT_ADDRESS is not None: membership_exchange_contract = Contract.get_contract( "IbetMembershipExchange", IBET_MEMBERSHIP_EXCHANGE_CONTRACT_ADDRESS) self.exchange_list.append(membership_exchange_contract) # クーポン取引コントラクト登録 if IBET_CP_EXCHANGE_CONTRACT_ADDRESS is not None: coupon_exchange_contract = Contract.get_contract( "IbetCouponExchange", IBET_CP_EXCHANGE_CONTRACT_ADDRESS) self.exchange_list.append(coupon_exchange_contract) # OTC取引コントラクト登録 if IBET_SHARE_EXCHANGE_CONTRACT_ADDRESS is not None: self.share_exchange_contract = Contract.get_contract( "IbetOTCExchange", IBET_SHARE_EXCHANGE_CONTRACT_ADDRESS) self.exchange_list.append(self.share_exchange_contract) else: self.share_exchange_contract = "" self.sink = sink self.latest_block = web3.eth.blockNumber self.db = db
def bond_exchange_contract(payment_gateway_address, personalinfo_address, exchange_regulator_service_address): deployer = eth_account['deployer'] issuer = eth_account['issuer'] trader = eth_account['trader'] web3.eth.defaultAccount = deployer['account_address'] storage_address, _ = Contract.deploy_contract( 'ExchangeStorage', [], deployer['account_address']) args = [ payment_gateway_address, personalinfo_address, storage_address, exchange_regulator_service_address ] contract_address, abi = Contract.deploy_contract( 'IbetStraightBondExchange', args, deployer['account_address']) storage = Contract.get_contract('ExchangeStorage', storage_address) storage.functions.upgradeVersion(contract_address).transact( {'from': deployer['account_address'], 'gas': 4000000} ) # 取引参加者登録 ExchangeRegulatorService = \ Contract.get_contract('ExchangeRegulatorService', exchange_regulator_service_address) ExchangeRegulatorService.functions.register(issuer['account_address'], False). \ transact({'from': deployer['account_address'], 'gas': 4000000}) ExchangeRegulatorService.functions.register(trader['account_address'], False). \ transact({'from': deployer['account_address'], 'gas': 4000000}) return {'address': contract_address, 'abi': abi}
def get_token_list(self): self.token_list = [] ListContract = Contract.get_contract("TokenList", TOKEN_LIST_CONTRACT_ADDRESS) listed_tokens = self.db.query(Listing).all() for listed_token in listed_tokens: token_info = ListContract.functions.getTokenByAddress( listed_token.token_address).call() if token_info[1] == "IbetCoupon": token_contract = Contract.get_contract( "IbetCoupon", listed_token.token_address) self.token_list.append(token_contract)
def issue_bond_token(invoker, attribute): web3.eth.defaultAccount = invoker['account_address'] interestPaymentDate = json.dumps( { 'interestPaymentDate1': attribute['interestPaymentDate1'], 'interestPaymentDate2': attribute['interestPaymentDate2'], 'interestPaymentDate3': attribute['interestPaymentDate3'], 'interestPaymentDate4': attribute['interestPaymentDate4'], 'interestPaymentDate5': attribute['interestPaymentDate5'], 'interestPaymentDate6': attribute['interestPaymentDate6'], 'interestPaymentDate7': attribute['interestPaymentDate7'], 'interestPaymentDate8': attribute['interestPaymentDate8'], 'interestPaymentDate9': attribute['interestPaymentDate9'], 'interestPaymentDate10': attribute['interestPaymentDate10'], 'interestPaymentDate11': attribute['interestPaymentDate11'], 'interestPaymentDate12': attribute['interestPaymentDate12'], } ) arguments = [ attribute['name'], attribute['symbol'], attribute['totalSupply'], attribute['faceValue'], attribute['redemptionDate'], attribute['redemptionValue'], attribute['returnDate'], attribute['returnAmount'], attribute['purpose'] ] contract_address, abi = Contract.deploy_contract( 'IbetStraightBond', arguments, invoker['account_address'] ) # その他項目の更新 TokenContract = Contract.get_contract('IbetStraightBond', contract_address) TokenContract.functions.setTradableExchange(attribute['tradableExchange']). \ transact({'from': invoker['account_address'], 'gas': 4000000}) TokenContract.functions.setInterestRate(attribute['interestRate']). \ transact({'from': invoker['account_address'], 'gas': 4000000}) TokenContract.functions.setInterestPaymentDate(interestPaymentDate). \ transact({'from': invoker['account_address'], 'gas': 4000000}) TokenContract.functions.setMemo(attribute['memo']). \ transact({'from': invoker['account_address'], 'gas': 4000000}) TokenContract.functions.setContactInformation(attribute['contactInformation']). \ transact({'from': invoker['account_address'], 'gas': 4000000}) TokenContract.functions.setPrivacyPolicy(attribute['privacyPolicy']). \ transact({'from': invoker['account_address'], 'gas': 4000000}) TokenContract.functions.setPersonalInfoAddress(attribute['personalInfoAddress']). \ transact({'from': invoker['account_address'], 'gas': 4000000}) return {'address': contract_address, 'abi': abi}
def on_get(self, req, res): LOG.info('v2.token.CouponTokenAddresses') session = req.context["session"] if config.COUPON_TOKEN_ENABLED is False: raise NotSupportedError(method='GET', url=req.path) # Validation request_json = CouponTokens.validate(req) # TokenList-Contractへの接続 ListContract = Contract.get_contract( 'TokenList', config.TOKEN_LIST_CONTRACT_ADDRESS) # 取扱トークンリストを取得 available_tokens = session.query(Listing).\ filter(Listing.is_public == True).\ order_by(Listing.id).\ all() list_length = len(available_tokens) if request_json['cursor'] is not None and request_json['cursor'] > list_length: raise InvalidParameterError( "cursor parameter must be less than token list num") # パラメータを設定 cursor = request_json['cursor'] if cursor is None: cursor = list_length limit = request_json['limit'] if limit is None: limit = 10 token_list = [] count = 0 for i in reversed(range(0, cursor)): if count >= limit: break token_address = to_checksum_address(available_tokens[i].token_address) token = ListContract.functions.getTokenByAddress(token_address).call() if token[1] == 'IbetCoupon': # ibetCoupon以外は処理をスキップ # Token-Contractへの接続 TokenContract = Contract.get_contract("IbetCoupon", token_address) if TokenContract.functions.status().call(): # 取扱停止の場合は処理をスキップ token_list.append({"id": i, "token_address": token_address}) count += 1 self.on_success(res, token_list)
def exchange_regulator_service_contract(): deployer = eth_account['deployer'] web3.eth.defaultAccount = deployer['account_address'] contract_address, abi = Contract.deploy_contract( 'ExchangeRegulatorService', [], deployer['account_address']) exchange_regulator_service = Contract.get_contract('ExchangeRegulatorService', contract_address) web3.eth.defaultAccount = deployer['account_address'] exchange_regulator_service.functions.register(eth_account['issuer']['account_address'], False).\ transact({'from': deployer['account_address'], 'gas': 4000000}) exchange_regulator_service.functions.register(eth_account['trader']['account_address'], False).\ transact({'from': deployer['account_address'], 'gas': 4000000}) return {'address': contract_address, 'abi': abi}
def payment_gateway_contract(): deployer = eth_account['deployer'] agent = eth_account['agent'] web3.eth.defaultAccount = deployer['account_address'] contract_address, abi = Contract.deploy_contract( 'PaymentGateway', [], deployer['account_address']) contract = Contract.get_contract('PaymentGateway', contract_address) tx_hash = contract.functions.addAgent(agent['account_address']).transact( {'from': deployer['account_address'], 'gas': 4000000} ) web3.eth.waitForTransactionReceipt(tx_hash) return {'address': contract_address, 'abi': abi}
def get_token_detail(session, token_address: str, token_template: str, token_id: int = None): """ トークン詳細の取得 :param session: DB Session :param token_address: トークンアドレス :param token_template: トークンテンプレート :param token_id: シーケンスID(任意) :return: ShareToken(dict) """ if token_template == 'IbetShare': try: # Token-Contractへの接続 TokenContract = Contract.get_contract(token_template, token_address) # 取扱停止銘柄はリストに返さない if not TokenContract.functions.status().call(): return None sharetoken = ShareToken.get(session=session, token_address=token_address) sharetoken = sharetoken.__dict__ if token_id is not None: sharetoken['id'] = token_id return sharetoken except Exception as e: LOG.error(e) return None
def bond_untransferable(invoker, token): web3.eth.defaultAccount = invoker['account_address'] TokenContract = Contract.get_contract('IbetStraightBond', token['address']) tx_hash = TokenContract.functions.setTransferable(False). \ transact({'from': invoker['account_address'], 'gas': 4000000}) web3.eth.waitForTransactionReceipt(tx_hash)
def tokenlist_contract(): deployer = eth_account['deployer'] web3.eth.defaultAccount = deployer['account_address'] contract_address, abi = Contract.deploy_contract( 'TokenList', [], deployer['account_address']) return {'address': contract_address, 'abi': abi}
def on_get(self, req, res): LOG.info("v2.user.PersonalInfo") # Validation request_json = PersonalInfo.validate(req) # Get PersonalInfo contract if request_json["personal_info_address"] is not None: _personal_info_address = request_json["personal_info_address"] else: _personal_info_address = config.PERSONAL_INFO_CONTRACT_ADDRESS PersonalInfoContract = Contract.get_contract( contract_name="PersonalInfo", address=_personal_info_address) # Get registration status of personal information info = PersonalInfoContract.functions.personal_info( to_checksum_address(request_json["account_address"]), to_checksum_address(request_json["owner_address"])).call() if info[0] == config.ZERO_ADDRESS: response_json = { "account_address": request_json["account_address"], "owner_address": request_json["owner_address"], "registered": False } else: response_json = { "account_address": info[0], "owner_address": info[1], "registered": True } self.on_success(res, response_json)
def on_get(self, req, res): LOG.info("v2.user.PaymentAccount") request_json = PaymentAccount.validate(req) PaymentGatewayContract = Contract.get_contract( "PaymentGateway", config.PAYMENT_GATEWAY_CONTRACT_ADDRESS) # 口座登録・承認状況を参照 account_info = PaymentGatewayContract.functions.payment_accounts( to_checksum_address(request_json["account_address"]), to_checksum_address(request_json["agent_address"])).call() if account_info[0] == "0x0000000000000000000000000000000000000000": response_json = { "account_address": request_json["account_address"], "agent_address": request_json["agent_address"], "approval_status": 0 } else: response_json = { "account_address": account_info[0], "agent_address": account_info[1], "approval_status": account_info[3] } self.on_success(res, response_json)
def apply_for_transfer(tx_from: str, token_address: str, to: str, value: int): TokenContract = Contract.get_contract( contract_name="IbetShare", address=token_address ) TokenContract.functions.applyForTransfer(to, value, "").\ transact({"from": tx_from, "gas": gas_limit})
def on_post(self, req, res): LOG.info("v2.market_information.CouponLastPrice") if config.COUPON_TOKEN_ENABLED is False or config.IBET_CP_EXCHANGE_CONTRACT_ADDRESS is None: raise NotSupportedError(method="POST", url=req.path) request_json = CouponLastPrice.validate(req) ExchangeContract = Contract.get_contract( "IbetCouponExchange", config.IBET_CP_EXCHANGE_CONTRACT_ADDRESS) price_list = [] for token_address in request_json["address_list"]: try: last_price = ExchangeContract.functions. \ lastPrice(to_checksum_address(token_address)).call() except Exception as e: LOG.error(e) last_price = 0 price_list.append({ "token_address": token_address, "last_price": last_price }) self.on_success(res, price_list)
def set_transfer_approval_required(tx_from: str, token_address: str, required: bool): TokenContract = Contract.get_contract( contract_name="IbetShare", address=token_address ) TokenContract.functions.setTransferApprovalRequired(required).\ transact({"from": tx_from, "gas": gas_limit})
def issue(tx_from: str, args: Dict): web3.eth.defaultAccount = tx_from # issue arguments = [ args["name"], args["symbol"], args["issuePrice"], args["totalSupply"], args["dividends"], args["dividendRecordDate"], args["dividendPaymentDate"], args["cancellationDate"], args["principalValue"] ] contract_address, abi = Contract.deploy_contract( contract_name="IbetShare", args=arguments, deployer=tx_from ) # update TokenContract = Contract.get_contract( contract_name="IbetShare", address=contract_address ) if "tradableExchange" in args: TokenContract.functions.setTradableExchange(args["tradableExchange"]). \ transact({"from": tx_from, "gas": gas_limit}) if "personalInfoAddress" in args: TokenContract.functions.setPersonalInfoAddress(args["personalInfoAddress"]). \ transact({"from": tx_from, "gas": gas_limit}) if "contactInformation" in args: TokenContract.functions.setContactInformation(args["contactInformation"]). \ transact({"from": tx_from, "gas": gas_limit}) if "privacyPolicy" in args: TokenContract.functions.setPrivacyPolicy(args["privacyPolicy"]). \ transact({"from": tx_from, "gas": gas_limit}) if "memo" in args: TokenContract.functions.setMemo(args["memo"]). \ transact({"from": tx_from, "gas": gas_limit}) if "transferable" in args: TokenContract.functions.setTransferable(args["transferable"]). \ transact({"from": tx_from, "gas": gas_limit}) return {"address": contract_address, "abi": abi}
def register(tx_from: str, personal_info_address: str, link_address: str): web3.eth.defaultAccount = tx_from PersonalInfoContract = Contract.get_contract( contract_name="PersonalInfo", address=personal_info_address) encrypted_info = "some_encrypted_info" PersonalInfoContract.functions.\ register(link_address, encrypted_info). \ transact({"from": tx_from, "gas": 4000000})
def personalinfo_contract(): deployer = eth_account['deployer'] web3.eth.defaultAccount = deployer['account_address'] contract_address, abi = Contract.deploy_contract( 'PersonalInfo', [], deployer['account_address']) return {'address': contract_address, 'abi': abi}
def register_token_list(tx_from: str, token_address, token_list_contract_address): TokenListContract = Contract.get_contract( contract_name="TokenList", address=token_list_contract_address ) web3.eth.defaultAccount = tx_from TokenListContract.functions. \ register(token_address, "IbetShare"). \ transact({"from": tx_from, "gas": gas_limit})
def on_post(self, req, res): LOG.info("v2.token.Tokens(POST)") session = req.context["session"] # 入力値チェック request_json = self.validate(req) contract_address = request_json["contract_address"] # 既存レコードの存在チェック _listing = session.query(Listing). \ filter(Listing.token_address == contract_address). \ first() if _listing is not None: raise InvalidParameterError("contract_address already exist") _executable_contract = session.query(ExecutableContract). \ filter(ExecutableContract.contract_address == contract_address). \ first() if _executable_contract is not None: raise InvalidParameterError("contract_address already exist") # token情報をTokenListコントラクトから取得 ListContract = Contract.get_contract( 'TokenList', config.TOKEN_LIST_CONTRACT_ADDRESS) token = ListContract.functions.getTokenByAddress( contract_address).call() # contract_addressの有効性チェック if token[1] is None or token[1] not in self.available_token_template(): raise InvalidParameterError( "contract_address is invalid token address") owner_address = token[2] # 新規レコードの登録 is_public = request_json["is_public"] max_holding_quantity = request_json[ "max_holding_quantity"] if "max_holding_quantity" in request_json else None max_sell_amount = request_json[ "max_sell_amount"] if "max_sell_amount" in request_json else None listing = Listing() listing.token_address = contract_address listing.is_public = is_public listing.max_holding_quantity = max_holding_quantity listing.max_sell_amount = max_sell_amount listing.owner_address = owner_address session.add(listing) executable_contract = ExecutableContract() executable_contract.contract_address = contract_address session.add(executable_contract) session.commit() self.on_success(res)
def get_otc_complete_list(session, token_model, exchange_contract_name, exchange_contract_address, account_address): """List all orders that have been settled (DEX)""" # Get exchange contract exchange_address = to_checksum_address(exchange_contract_address) exchange_contract = Contract.get_contract(exchange_contract_name, exchange_address) # Filter agreement events (settlement completed) generated from account_address _agreement_events = session.query( Agreement.id, Agreement.order_id, Agreement.agreement_id, Agreement.agreement_timestamp, Agreement.settlement_timestamp, Agreement.buyer_address). \ filter(Agreement.exchange_address == exchange_address). \ filter(or_(Agreement.buyer_address == account_address, Agreement.seller_address == account_address)). \ filter(Agreement.status == AgreementStatus.DONE.value). \ all() complete_list = [] for (id, order_id, agreement_id, agreement_timestamp, settlement_timestamp, buyer_address) in _agreement_events: if settlement_timestamp is not None: settlement_timestamp_jp = settlement_timestamp.strftime( "%Y/%m/%d %H:%M:%S") else: settlement_timestamp_jp = "" order_book = exchange_contract.functions.getOrder(order_id).call() agreement = exchange_contract.functions.getAgreement( order_id, agreement_id).call() _complete = { "agreement": { "exchange_address": exchange_contract_address, "order_id": order_id, "agreement_id": agreement_id, "amount": agreement[1], "price": agreement[2], "is_buy": buyer_address == account_address, "agreement_timestamp": agreement_timestamp.strftime("%Y/%m/%d %H:%M:%S") }, "settlement_timestamp": settlement_timestamp_jp, "sort_id": id } if token_model is not None: token_detail = token_model.get( session=session, token_address=to_checksum_address(order_book[2])) _complete["token"] = token_detail.__dict__ complete_list.append(_complete) return complete_list
def bond_transfer_to_exchange(invoker, bond_exchange, bond_token, amount): web3.eth.defaultAccount = invoker['account_address'] TokenContract = Contract.get_contract( 'IbetStraightBond', bond_token['address']) tx_hash = TokenContract.functions.transfer(bond_exchange['address'], amount). \ transact({'from': invoker['account_address'], 'gas': 4000000}) web3.eth.waitForTransactionReceipt(tx_hash)
def register_exchange_regulator(invoker, exchange_regulator_service, account_address): web3.eth.defaultAccount = invoker['account_address'] ExchangeRegulatorService = \ Contract.get_contract('ExchangeRegulatorService', exchange_regulator_service['address']) tx_hash = ExchangeRegulatorService.functions.register(account_address, False). \ transact({'from': invoker['account_address'], 'gas': 4000000}) web3.eth.waitForTransactionReceipt(tx_hash)
def invalidate_coupon_token(invoker, coupon_token): web3.eth.defaultAccount = invoker['account_address'] CouponTokenContract = Contract.get_contract( 'IbetCoupon', coupon_token['address']) tx_hash = CouponTokenContract.functions. \ setStatus(False). \ transact({'from': invoker['account_address'], 'gas': 4000000}) web3.eth.waitForTransactionReceipt(tx_hash)
def consume_coupon_token(invoker, coupon_token, value): web3.eth.defaultAccount = invoker['account_address'] CouponTokenContract = Contract.get_contract( 'IbetCoupon', coupon_token['address']) tx_hash = CouponTokenContract.functions. \ consume(value). \ transact({'from': invoker['account_address'], 'gas': 4000000}) web3.eth.waitForTransactionReceipt(tx_hash)
def on_get(self, req, res): LOG.info('v2.token.MembershipTokens') session = req.context["session"] if config.MEMBERSHIP_TOKEN_ENABLED is False: raise NotSupportedError(method='GET', url=req.path) # Validation request_json = MembershipTokens.validate(req) # TokenList-Contractへの接続 ListContract = Contract.get_contract( 'TokenList', config.TOKEN_LIST_CONTRACT_ADDRESS) # 取扱トークンリストを取得 available_tokens = session.query(Listing).\ filter(Listing.is_public == True).\ order_by(Listing.id).\ all() list_length = len(available_tokens) if request_json['cursor'] is not None and request_json['cursor'] > list_length: raise InvalidParameterError("cursor parameter must be less than token list num") # パラメータを設定 cursor = request_json['cursor'] if cursor is None: cursor = list_length limit = request_json['limit'] if limit is None: limit = 10 token_list = [] count = 0 # TokenListを降順に調べる(登録が新しい順) for i in reversed(range(0, cursor)): if count >= limit: break # TokenList-Contractからトークンの情報を取得する token_address = to_checksum_address(available_tokens[i].token_address) token = ListContract.functions. \ getTokenByAddress(token_address).call() token_detail = MembershipTokenDetails.get_token_detail( session=session, token_id=i, token_address=token[0], token_template=token[1] ) if token_detail is not None: token_list.append(token_detail) count += 1 self.on_success(res, token_list)
def register_share_list(invoker, share_token, token_list): TokenListContract = Contract.get_contract( 'TokenList', token_list['address']) web3.eth.defaultAccount = invoker['account_address'] tx_hash = TokenListContract.functions.register( share_token['address'], 'IbetShare'). \ transact({'from': invoker['account_address'], 'gas': 4000000}) web3.eth.waitForTransactionReceipt(tx_hash)
def transfer_to_exchange(tx_from: str, exchange_address: str, token_address: str, amount: int): web3.eth.defaultAccount = tx_from TokenContract = Contract.get_contract( contract_name="IbetShare", address=token_address ) TokenContract.functions.transfer(exchange_address, amount). \ transact({"from": tx_from, "gas": gas_limit})
def register_share_reference_url(invoker, token, url_list): web3.eth.defaultAccount = invoker['account_address'] TokenContract = Contract.get_contract('IbetShare', token['address']) i = 0 for url in url_list: tx_hash = TokenContract.functions.setReferenceUrls(i, url). \ transact({'from': invoker['account_address'], 'gas': 4000000}) web3.eth.waitForTransactionReceipt(tx_hash) i = i + 1