Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Commit

Permalink
Create crowdfunding, for real
Browse files Browse the repository at this point in the history
  • Loading branch information
metachris committed Mar 1, 2018
1 parent 062124e commit 365422b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
21 changes: 14 additions & 7 deletions imusify-crowdfunding-api/api.py
Expand Up @@ -2,9 +2,9 @@
"""
Imusify crowdfunding blockchain middleware
Example usage (with "123" as valid API token):
Example usage (with "123" as valid API token), on the privnet:
NEO_REST_API_TOKEN="123" ./imusify-crowdfunding-api/api.py
NEO_REST_API_TOKEN="123" ./imusify-crowdfunding-api/api.py -p
Example API calls:
Expand All @@ -18,7 +18,7 @@
$ curl -vvv -X GET -H "Authorization: Bearer 123" localhost:8080/imuBalance/AK2nJJpJr6o664CWJKi1QRXjqeic2zRp8y
# Create a crowdfunding
$ curl -vvv -X POST -H "Authorization: Bearer 123" -d '{ "memberAddresses": [] }' localhost:8080/crowdfunding/create
$ curl -vvv -X POST -H "Authorization: Bearer 123" -d '{ "memberAddresses": ["AaLCDGQMuKdpNCkSVWVLry8MyvaXhbAXGW", "AW5C5LZjyr1jBQuZZg3kAmmri6ic32yH2q"] }' localhost:8080/crowdfunding/create
"""
import os
Expand Down Expand Up @@ -46,7 +46,7 @@


# Set the hash of your contract here:
SMART_CONTRACT_HASH = "95ed79af690e274ad6c5594c4496daf72f5832b6"
SMART_CONTRACT_HASH = "0xde43c873ef3d0b23e24eef87cc70a8837a4ba387"

# Default REST API port is 8080, and can be overwritten with an env var:
API_PORT = os.getenv("NEO_REST_API_PORT", 8080)
Expand Down Expand Up @@ -218,9 +218,16 @@ def create_crowdfunding(request):
request.setResponseCode(400)
return build_error(STATUS_ERROR_JSON, "Address not 34 characters")

# TODO: put into queue, invoke smart contract. this is only a mock response
# Create the crowdfunding address
private_key = bytes(Random.get_random_bytes(32))
key = KeyPair(priv_key=private_key)
crowdfunding_address = key.GetAddress()

# Put into queue and invoke smart contract
imuSmartContract.add_invoke("crowdfunding_create", crowdfunding_address, *memberAddresses)

return {
"crowdfundingAddress": "AKadKVhU43qfaLW3JGmK9MoAJ4VNp1oCdu"
"crowdfundingAddress": crowdfunding_address
}


Expand Down Expand Up @@ -260,7 +267,7 @@ def main():
NodeLeader.Instance().Start()

# Disable smart contract events for external smart contracts
settings.set_log_smart_contract_events(False)
settings.set_log_smart_contract_events(True)
logger.info("Using network: %s" % settings.net_name)

# Start a thread with custom code
Expand Down
10 changes: 7 additions & 3 deletions imusify-crowdfunding-api/imusmartcontract.py
Expand Up @@ -2,12 +2,14 @@
Interface to call smart contract methods
"""
import time
import json
import threading

from queue import Queue
from logzero import logger
from twisted.internet import task

from neo.Wallets.utils import to_aes_key
from neo.Settings import settings
from neo.Core.Blockchain import Blockchain
from neo.contrib.smartcontract import SmartContract
Expand Down Expand Up @@ -114,7 +116,7 @@ def run(self):
def open_wallet(self):
""" Open a wallet. Needed for invoking contract methods. """
assert self.wallet is None
self.wallet = UserWallet.Open(self.wallet_path, self.wallet_pass)
self.wallet = UserWallet.Open(self.wallet_path, to_aes_key(self.wallet_pass))
self.wallet.ProcessBlocks()
self._walletdb_loop = task.LoopingCall(self.wallet.ProcessBlocks)
self._walletdb_loop.start(1)
Expand Down Expand Up @@ -173,7 +175,6 @@ def invoke_method(self, method_name, *args):
if self.tx_in_progress:
raise Exception("Transaction already in progress (%s)" % self.tx_in_progress.Hash.ToString())


time.sleep(3)
logger.info("wallet synced. checking if gas is available...")

Expand All @@ -186,4 +187,7 @@ def invoke_method(self, method_name, *args):
tx, fee, results, num_ops = TestInvokeContract(self.wallet, _args)
if not tx:
raise Exception("TestInvokeContract failed")
logger.info("TestInvoke result: %s", str(results))

# logger.info("TestInvoke result: %s", str(results))
logger.info("TestInvoke done, invoking now...")
result = InvokeContract(self.wallet, tx, fee)
Binary file modified imusify-crowdfunding-api/neo-privnet.wallet
Binary file not shown.

0 comments on commit 365422b

Please sign in to comment.