Skip to content

Commit

Permalink
0.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
yourbuddyconner committed Jul 20, 2019
1 parent e3e92a2 commit 4872768
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 100 deletions.
113 changes: 55 additions & 58 deletions CodaClient.py
Expand Up @@ -34,7 +34,7 @@ def _send_query(self, query: str, variables: dict = {}) -> dict:
"""
return self._graphql_request(query, variables)

def _send_mutation(self, query: str, variables: dict) -> dict:
def _send_mutation(self, query: str, variables: dict = {}) -> dict:
"""Sends a mutation to the Coda Daemon's GraphQL Endpoint.
Arguments:
Expand Down Expand Up @@ -68,7 +68,11 @@ def _graphql_request(self, query: str, variables: dict = {}):
payload = {'query': query}
if variables:
payload = { **payload, 'variables': variables }
response = requests.post(self.endpoint, json=payload)

headers = {
"Accept": "application/json"
}
response = requests.post(self.endpoint, json=payload, headers=headers)
if response.status_code == 200:
return response.json()
else:
Expand Down Expand Up @@ -116,7 +120,6 @@ def get_daemon_status(self) -> dict:
blockchainLength
uptimeSecs
ledgerMerkleRoot
stagedLedgerHash
stateHash
peers
userCommandsSent
Expand All @@ -125,6 +128,8 @@ def get_daemon_status(self) -> dict:
consensusTimeNow
consensusTimeBestTip
consensusMechanism
confDir
commitId
consensusConfiguration {
delta
k
Expand All @@ -148,10 +153,10 @@ def get_daemon_version(self) -> dict:
dict -- Returns the "data" field of the JSON Response as a Dict.
"""
query = '''
{
version
}
'''
{
version
}
'''
res = self._send_query(query)
return res["data"]

Expand Down Expand Up @@ -252,48 +257,40 @@ def set_current_snark_worker(self, worker_pk: str, fee: str) -> dict:
res = self._send_query(query)
return res["data"]

def create_wallet(self, pk: str="", sk: str="") -> dict:
"""Creates a new wallet with the specified Public and Secret Keys.
def create_wallet(self) -> dict:
"""Creates a new wallet and returns the public key.
Arguments:
pk {str} -- The private key for the new wallet
sk {str} -- The secret key for the new wallet
N/A
Returns:
dict -- Returns the "data" field of the JSON Response as a Dict
"""
query = '''
mutation($publicKey:String, $privateKey:String){
addWallet(input: {
publicKey:$publicKey,
privateKey:$privateKey
}) {
mutation{
addWallet {
publicKey
}
}
'''
variables = {
"publicKey": pk,
"privateKey": sk
}
res = self._send_mutation(query, variables)
res = self._send_mutation(query)
return res["data"]

def send_payment(self, to_pk: str, from_pk: str, amount: str, fee: str, memo: str) -> dict:
def send_payment(self, to_pk: str, from_pk: str, amount: int, fee: int, memo: str) -> dict:
"""Send a payment from the specified wallet to the specified target wallet.
Arguments:
to_pk {str} -- The target wallet where funds should be sent
from_pk {str} -- The installed wallet which will finance the payment
amount {str} -- Tha amount of Coda to send
fee {str} -- The transaction fee that will be attached to the payment
to_pk {PublicKey} -- The target wallet where funds should be sent
from_pk {PublicKey} -- The installed wallet which will finance the payment
amount {UInt64} -- Tha amount of Coda to send
fee {UInt64} -- The transaction fee that will be attached to the payment
memo {str} -- A memo to attach to the payment
Returns:
dict -- Returns the "data" field of the JSON Response as a Dict
"""
query = '''
mutation($from:String!, $to:String!, $amount:String!, $fee:String!, $memo:String){
mutation($from:PublicKey!, $to:PublicKey!, $amount:UInt64!, $fee:UInt64!, $memo:String){
sendPayment(input: {
from:$from,
to:$to,
Expand Down Expand Up @@ -353,48 +350,48 @@ def get_pooled_payments(self, pk: str) -> dict:
res = self._send_query(query, variables)
return res["data"]

def listen_new_blocks(self, key: str):
def listen_new_blocks(self, pk: str):
"""Creates a subscription for new blocks created by a proposer using a particular private key.
Blocks until ctl+c
Arguments:
key {str} -- The key to use to filter blocks
pk {PublicKey} -- The public key to use to filter blocks
"""
query = '''
subscription($key:String){
newBlock(key:$key){
creator
stateHash
protocolState {
previousStateHash
blockchainState {
date
snarkedLedgerHash
stagedLedgerHash
}
},
transactions {
userCommands {
id
isDelegation
nonce
from
to
amount
fee
memo
}
feeTransfer {
recipient
fee
}
coinbase
subscription($pk:PublicKey){
newBlock(publicKey:$pk){
creator
stateHash
protocolState {
previousStateHash
blockchainState {
date
snarkedLedgerHash
stagedLedgerHash
}
},
transactions {
userCommands {
id
isDelegation
nonce
from
to
amount
fee
memo
}
feeTransfer {
recipient
fee
}
coinbase
}
}
}
'''
variables = {
"key": key
"pk": pk
}
asyncio.run(self._graphql_subscription(query, variables))

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -12,7 +12,7 @@

setup(
name='CodaClient',
version='0.0.5',
version='0.0.6',
python_requires='>=3.5',
description='A Python wrapper around the Coda Daemon GraphQL API.',
github='http://github.com/CodaProtocol/coda-python',
Expand Down
74 changes: 40 additions & 34 deletions tests/snapshots/snap_test_client.py
Expand Up @@ -13,8 +13,11 @@
'http://localhost:8304/graphql'
,),
{
'headers': {
'Accept': 'application/json'
},
'json': {
'query': 'query { daemonStatus { numAccounts blockchainLength uptimeSecs ledgerMerkleRoot stagedLedgerHash stateHash peers userCommandsSent runSnarkWorker proposePubkeys consensusTimeNow consensusTimeBestTip consensusMechanism consensusConfiguration { delta k c cTimesK slotsPerEpoch slotDuration epochDuration acceptableNetworkDelay } } }'
'query': 'query { daemonStatus { numAccounts blockchainLength uptimeSecs ledgerMerkleRoot stateHash peers userCommandsSent runSnarkWorker proposePubkeys consensusTimeNow consensusTimeBestTip consensusMechanism confDir commitId consensusConfiguration { delta k c cTimesK slotsPerEpoch slotDuration epochDuration acceptableNetworkDelay } } }'
}
}
,)
Expand All @@ -26,6 +29,9 @@
'http://localhost:8304/graphql'
,),
{
'headers': {
'Accept': 'application/json'
},
'json': {
'query': '{ version }'
}
Expand All @@ -39,6 +45,9 @@
'http://localhost:8304/graphql'
,),
{
'headers': {
'Accept': 'application/json'
},
'json': {
'query': '{ ownedWallets { publicKey } }'
}
Expand All @@ -52,6 +61,9 @@
'http://localhost:8304/graphql'
,),
{
'headers': {
'Accept': 'application/json'
},
'json': {
'query': '{ currentSnarkWorker{ key fee } }'
}
Expand All @@ -65,6 +77,9 @@
'http://localhost:8304/graphql'
,),
{
'headers': {
'Accept': 'application/json'
},
'json': {
'query': '{ syncStatus }'
}
Expand All @@ -78,78 +93,69 @@
'http://localhost:8304/graphql'
,),
{
'headers': {
'Accept': 'application/json'
},
'json': {
'query': '{ syncStatus }'
}
}
,)
]

snapshots['TestCodaClient.test_create_wallet_with_args 1'] = [
(
(
'http://localhost:8304/graphql'
,),
{
'json': {
'query': 'mutation($publicKey:String, $privateKey:String){ addWallet(input: { publicKey:$publicKey, privateKey:$privateKey }) { publicKey } }',
'variables': {
'privateKey': 'sk',
'publicKey': 'pk'
}
}
}
,)
]

snapshots['TestCodaClient.test_create_wallet_no_args 1'] = [
snapshots['TestCodaClient.test_send_payment 1'] = [
(
(
'http://localhost:8304/graphql'
,),
{
'headers': {
'Accept': 'application/json'
},
'json': {
'query': 'mutation($publicKey:String, $privateKey:String){ addWallet(input: { publicKey:$publicKey, privateKey:$privateKey }) { publicKey } }',
'query': 'mutation($from:PublicKey!, $to:PublicKey!, $amount:UInt64!, $fee:UInt64!, $memo:String){ sendPayment(input: { from:$from, to:$to, amount:$amount, fee:$fee, memo:$memo }) { payment { id, isDelegation, nonce, from, to, amount, fee, memo } } }',
'variables': {
'privateKey': '',
'publicKey': ''
'amount': 'amount',
'fee': 'fee',
'from': 'from_pk',
'memo': 'memo',
'to': 'to_pk'
}
}
}
,)
]

snapshots['TestCodaClient.test_send_payment 1'] = [
snapshots['TestCodaClient.test_get_wallet 1'] = [
(
(
'http://localhost:8304/graphql'
,),
{
'headers': {
'Accept': 'application/json'
},
'json': {
'query': 'mutation($from:String!, $to:String!, $amount:String!, $fee:String!, $memo:String){ sendPayment(input: { from:$from, to:$to, amount:$amount, fee:$fee, memo:$memo }) { payment { id, isDelegation, nonce, from, to, amount, fee, memo } } }',
'query': 'query($publicKey:PublicKey!){ wallet(publicKey:$publicKey) { publicKey balance { total unknown } nonce receiptChainHash delegate votingFor stakingActive privateKeyPath } }',
'variables': {
'amount': 'amount',
'fee': 'fee',
'from': 'from_pk',
'memo': 'memo',
'to': 'to_pk'
'publicKey': 'pk'
}
}
}
,)
]

snapshots['TestCodaClient.test_get_wallet 1'] = [
snapshots['TestCodaClient.test_create_wallet_no_args 1'] = [
(
(
'http://localhost:8304/graphql'
,),
{
'headers': {
'Accept': 'application/json'
},
'json': {
'query': 'query($publicKey:PublicKey!){ wallet(publicKey:$publicKey) { publicKey balance { total unknown } nonce receiptChainHash delegate votingFor stakingActive privateKeyPath } }',
'variables': {
'publicKey': 'pk'
}
'query': 'mutation{ addWallet { publicKey } }'
}
}
,)
Expand Down
7 changes: 0 additions & 7 deletions tests/test_client.py
Expand Up @@ -79,13 +79,6 @@ def test_set_current_snark_worker(self, mock_post, snapshot):
client = Client()
client.set_current_snark_worker("pk", "fee")
snapshot.assert_match(mock_post.call_args_list)

def test_create_wallet_with_args(self, mock_post, snapshot):
mock_post.return_value = self._mock_response(json_data={"data": "foo"})

client = Client()
client.create_wallet("pk", "sk")
snapshot.assert_match(mock_post.call_args_list)

def test_create_wallet_no_args(self, mock_post, snapshot):
mock_post.return_value = self._mock_response(json_data={"data": "foo"})
Expand Down

0 comments on commit 4872768

Please sign in to comment.