2021-10-26 14:48:25 +02:00
|
|
|
# standard imports
|
|
|
|
import json
|
2021-11-02 16:50:14 +01:00
|
|
|
import logging
|
2021-10-26 14:48:25 +02:00
|
|
|
import sys
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
import redis
|
|
|
|
from cic_eth.api.api_task import Api
|
2021-11-02 16:50:14 +01:00
|
|
|
from cic_eth.graphql.config import config
|
2021-10-26 14:48:25 +02:00
|
|
|
from graphene import (Boolean, Field, Float, Int, List, Mutation, ObjectType,
|
|
|
|
Schema, String)
|
|
|
|
|
2021-10-29 10:34:37 +02:00
|
|
|
chain_spec = config.get('CHAIN_SPEC')
|
|
|
|
redis_host = config.get('REDIS_HOST')
|
|
|
|
redis_port = config.get('REDIS_PORT')
|
|
|
|
redis_db = config.get('REDIS_DB')
|
|
|
|
celery_queue = config.get('CELERY_QUEUE')
|
2021-10-26 14:48:25 +02:00
|
|
|
|
2021-11-02 16:50:14 +01:00
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2021-10-26 14:48:25 +02:00
|
|
|
|
2021-10-29 10:34:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
def call(method, *args):
|
|
|
|
redis_channel = str(uuid.uuid4())
|
|
|
|
r = redis.Redis(redis_host, redis_port, redis_db)
|
|
|
|
ps = r.pubsub()
|
|
|
|
ps.subscribe(redis_channel)
|
|
|
|
ps.get_message() # Subscription Object
|
2021-11-01 11:56:12 +01:00
|
|
|
print(f"Channel init {redis_channel}")
|
2021-10-26 14:48:25 +02:00
|
|
|
print(args)
|
2021-11-02 16:50:14 +01:00
|
|
|
api = Api(
|
|
|
|
chain_spec,
|
|
|
|
queue=celery_queue,
|
|
|
|
callback_param='{}:{}:{}:{}'.format(
|
|
|
|
redis_host, redis_port, redis_db, redis_channel),
|
|
|
|
callback_task='cic_eth.callbacks.redis.redis',
|
|
|
|
callback_queue=celery_queue,
|
|
|
|
)
|
2021-10-26 14:48:25 +02:00
|
|
|
getattr(api, method)(*args)
|
|
|
|
|
|
|
|
ps.get_message() # returns None !?
|
|
|
|
try:
|
|
|
|
o = ps.get_message(timeout=config.get('REDIS_TIMEOUT'))
|
|
|
|
except TimeoutError as e:
|
|
|
|
sys.stderr.write(
|
|
|
|
'got no new address from cic-eth before timeout: {}\n'.format(e))
|
|
|
|
sys.exit(1)
|
|
|
|
ps.unsubscribe()
|
2021-11-02 16:50:14 +01:00
|
|
|
print(o)
|
2021-10-26 14:48:25 +02:00
|
|
|
m = json.loads(o['data'])
|
|
|
|
return m["result"]
|
|
|
|
|
|
|
|
|
|
|
|
class Token(ObjectType):
|
|
|
|
symbol = String()
|
|
|
|
address = String()
|
|
|
|
name = String()
|
|
|
|
decimals = Int()
|
|
|
|
|
|
|
|
|
|
|
|
class TokenBalance(ObjectType):
|
|
|
|
address = String()
|
|
|
|
converters = List(String)
|
2021-10-29 10:34:37 +02:00
|
|
|
balance_network = String(default_value="0")
|
|
|
|
balance_incoming = String(default_value="0")
|
|
|
|
balance_outgoing = String(default_value="0")
|
|
|
|
balance_available = String(default_value="0")
|
2021-10-26 14:48:25 +02:00
|
|
|
|
|
|
|
def resolve_balance_available(parent, info):
|
|
|
|
print(parent)
|
2021-10-29 10:34:37 +02:00
|
|
|
return str(int(parent.balance_network) + int(parent.balance_incoming) - int(parent.balance_outgoing))
|
2021-10-26 14:48:25 +02:00
|
|
|
|
|
|
|
|
2021-11-02 16:50:14 +01:00
|
|
|
|
2021-10-26 14:48:25 +02:00
|
|
|
class Query(ObjectType):
|
|
|
|
default_token = Field(Token)
|
|
|
|
|
|
|
|
def resolve_default_token(parent, info):
|
2021-11-02 16:50:14 +01:00
|
|
|
|
2021-10-26 14:48:25 +02:00
|
|
|
# always pass an object for `me` field
|
2021-11-02 16:50:14 +01:00
|
|
|
api = Api(
|
|
|
|
chain_spec,
|
|
|
|
queue=celery_queue,
|
|
|
|
)
|
|
|
|
task = api.default_token()
|
|
|
|
data = task.get()
|
2021-10-26 14:48:25 +02:00
|
|
|
print(data)
|
|
|
|
return Token(**data)
|
|
|
|
|
|
|
|
balance = Field(List(TokenBalance), address=String(),
|
2021-11-01 11:56:12 +01:00
|
|
|
token_symbol=String(), include_pending=Boolean())
|
2021-10-26 14:48:25 +02:00
|
|
|
|
2021-11-01 11:56:12 +01:00
|
|
|
def resolve_balance(root, info, address, token_symbol, include_pending=True):
|
2021-11-02 16:50:14 +01:00
|
|
|
api = Api(
|
|
|
|
chain_spec,
|
|
|
|
queue=celery_queue,
|
|
|
|
)
|
|
|
|
task = api.balance(address=address, token_symbol=token_symbol, include_pending=include_pending)
|
|
|
|
data = task.get() #api call('balance', address, token_symbol, include_pending)
|
|
|
|
log.debug(data)
|
|
|
|
print(data, file=sys.stdout)
|
2021-10-26 14:48:25 +02:00
|
|
|
#[{'address': '3ff776b6f888980def9d4220858803f9dc5e341e', 'converters': [], 'balance_network': 0}]
|
2021-10-29 10:34:37 +02:00
|
|
|
return map(lambda token: TokenBalance(
|
|
|
|
address=token.get("address"),
|
|
|
|
converters=token.get("converters"),
|
|
|
|
balance_network=str(token.get("balance_network") or "0"),
|
|
|
|
balance_incoming=str(token.get("balance_incoming") or "0"),
|
|
|
|
balance_outgoing=str(token.get("balance_outgoing") or "0"),
|
|
|
|
balance_available=str(token.get("balance_available" or "0"))
|
|
|
|
), data)
|
|
|
|
|
|
|
|
transactions = Field(String, address=String(
|
|
|
|
required=True), limit=Int(default_value=10))
|
|
|
|
|
|
|
|
def resolve_transactions(root, info, address, limit):
|
2021-11-02 16:50:14 +01:00
|
|
|
|
|
|
|
api = Api(
|
|
|
|
chain_spec,
|
|
|
|
queue=celery_queue,
|
|
|
|
)
|
|
|
|
task = api.list(address=address, limit=limit)
|
|
|
|
data = task.get()
|
2021-10-29 10:34:37 +02:00
|
|
|
print(data)
|
|
|
|
#[{'address': '3ff776b6f888980def9d4220858803f9dc5e341e', 'converters': [], 'balance_network': 0}]
|
|
|
|
return "test"
|
2021-10-26 14:48:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CreateAccount(Mutation):
|
|
|
|
address = String()
|
|
|
|
|
|
|
|
class Arguments:
|
|
|
|
password = String(required=True)
|
|
|
|
register = Boolean()
|
|
|
|
|
|
|
|
def mutate(root, info, password, register=True):
|
|
|
|
print(password, register)
|
2021-11-02 16:50:14 +01:00
|
|
|
api = Api(
|
|
|
|
chain_spec,
|
|
|
|
queue=celery_queue,
|
|
|
|
)
|
|
|
|
|
|
|
|
address = call('create_account',password, register)
|
2021-10-26 14:48:25 +02:00
|
|
|
return CreateAccount(address=f"0x{address}")
|
|
|
|
|
|
|
|
|
|
|
|
class Transfer(Mutation):
|
|
|
|
test = String(default_value="test")
|
|
|
|
|
|
|
|
class Arguments:
|
|
|
|
from_address = String(required=True)
|
|
|
|
to_address = String(required=True)
|
|
|
|
value = Int(required=True)
|
|
|
|
token_symbol = String(required=True)
|
|
|
|
|
|
|
|
def mutate(root, info, from_address,
|
|
|
|
to_address,
|
|
|
|
value,
|
|
|
|
token_symbol):
|
|
|
|
print(from_address, to_address, value, token_symbol)
|
2021-11-02 16:50:14 +01:00
|
|
|
api = Api(
|
|
|
|
chain_spec,
|
|
|
|
queue=celery_queue,
|
|
|
|
)
|
2021-10-29 10:34:37 +02:00
|
|
|
t = api.transfer(from_address, to_address,
|
|
|
|
int(value * (10**6)), token_symbol)
|
|
|
|
print(f"t {t}")
|
|
|
|
print(f"transfer {t.get_leaf()}")
|
|
|
|
print(f"transfer {t.successful()}")
|
|
|
|
return t.get()
|
2021-10-26 14:48:25 +02:00
|
|
|
# 0x0000000000000000000000000000000000000000
|
|
|
|
|
|
|
|
|
|
|
|
class TransferFrom(Mutation):
|
|
|
|
address = String()
|
|
|
|
|
|
|
|
class Arguments:
|
|
|
|
from_address = String(required=True)
|
|
|
|
to_address = String(required=True)
|
|
|
|
value = Int(required=True)
|
|
|
|
token_symbol = String(required=True)
|
|
|
|
spender_address = String(required=True)
|
|
|
|
|
|
|
|
def mutate(root, info, from_address,
|
|
|
|
to_address,
|
|
|
|
value,
|
|
|
|
token_symbol, spender_address):
|
2021-11-02 16:50:14 +01:00
|
|
|
api = Api(
|
|
|
|
chain_spec,
|
|
|
|
queue=celery_queue,
|
|
|
|
)
|
|
|
|
task = api.transfer_from(from_address=from_address,
|
|
|
|
to_address=to_address,
|
|
|
|
value=value,
|
|
|
|
token_symbol=token_symbol, spender_address=spender_address)
|
|
|
|
return task.get()
|
2021-10-26 14:48:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
class MyMutations(ObjectType):
|
|
|
|
create_account = CreateAccount.Field()
|
|
|
|
transfer = Transfer.Field()
|
|
|
|
|
|
|
|
|
|
|
|
schema = Schema(query=Query, mutation=MyMutations)
|
|
|
|
|
|
|
|
|
|
|
|
query_with_argument = """
|
|
|
|
{
|
|
|
|
defaultToken{
|
|
|
|
symbol
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
2021-10-29 10:34:37 +02:00
|
|
|
# result = schema.execute(query_with_argument)
|
|
|
|
# print(result)
|
2021-10-26 14:48:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
mutation_with_argument = """
|
|
|
|
mutation {
|
|
|
|
createAccount(password:"test"){
|
|
|
|
address
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
2021-10-29 14:10:56 +02:00
|
|
|
m_result = schema.execute(mutation_with_argument)
|
|
|
|
print(m_result.data)
|
|
|
|
address_1 = "0xde82da08c816865c24a2c9c76a2bcd44b5b94531" #m_result.data['createAccount']['address']
|
2021-10-26 14:48:25 +02:00
|
|
|
|
2021-10-29 14:10:56 +02:00
|
|
|
mutation_with_argument = """
|
|
|
|
mutation {
|
|
|
|
createAccount(password:"test"){
|
|
|
|
address
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
|
|
|
m_result = schema.execute(mutation_with_argument)
|
|
|
|
print(m_result)
|
|
|
|
address_2 = "0xaee24c8af3249b2d0a26769286623fff00556788" #m_result.data['createAccount']['address']
|
2021-10-26 14:48:25 +02:00
|
|
|
|
|
|
|
balance_query = """
|
2021-10-29 14:10:56 +02:00
|
|
|
query getBalance($address:String){
|
|
|
|
balance(address: $address, tokenSymbols:["COFE"]){
|
2021-10-26 14:48:25 +02:00
|
|
|
balanceNetwork
|
|
|
|
balanceIncoming
|
|
|
|
balanceOutgoing
|
|
|
|
balanceAvailable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
2021-10-29 14:10:56 +02:00
|
|
|
balance_query_result = schema.execute(
|
|
|
|
balance_query, variables={"address": "address_1"})
|
2021-10-29 10:34:37 +02:00
|
|
|
# print(balance_query_result)
|
2021-10-26 14:48:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
transfer_mutation = """
|
2021-10-29 14:10:56 +02:00
|
|
|
mutation transfer($from: String, $to: String) {
|
|
|
|
transfer(fromAddress : $from,
|
|
|
|
toAddress: $to
|
2021-10-29 10:34:37 +02:00
|
|
|
value: 20
|
2021-10-29 14:10:56 +02:00
|
|
|
tokenSymbol: "COFE" ){
|
|
|
|
test
|
2021-10-26 14:48:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
2021-10-29 14:10:56 +02:00
|
|
|
transfer_mutation_result = schema.execute(transfer_mutation, variables={
|
|
|
|
"from": address_1, "to": address_2})
|
2021-10-26 14:48:25 +02:00
|
|
|
print(transfer_mutation_result)
|
|
|
|
|
|
|
|
|
|
|
|
balance_query = """
|
|
|
|
query {
|
2021-10-29 10:34:37 +02:00
|
|
|
balance(address:"0xEb3907eCad74a0013c259D5874AE7f22DcBcC95C", tokenSymbols:["GFT"]){
|
2021-10-26 14:48:25 +02:00
|
|
|
balanceNetwork
|
|
|
|
balanceIncoming
|
|
|
|
balanceOutgoing
|
|
|
|
balanceAvailable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
2021-10-29 10:34:37 +02:00
|
|
|
# balance_query_result = schema.execute(balance_query)
|
|
|
|
# print(balance_query_result)
|
|
|
|
|
|
|
|
|
|
|
|
transactions_query = """
|
|
|
|
query {
|
|
|
|
transactions(address:"0xEb3907eCad74a0013c259D5874AE7f22DcBcC95C")
|
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
|
|
|
# transactions_query_result = schema.execute(transactions_query)
|
|
|
|
# print(transactions_query_result)
|