OSDN Git Service

add faucet
authorChengcheng Zhang <943420582@qq.com>
Tue, 2 Apr 2019 10:57:16 +0000 (18:57 +0800)
committerChengcheng Zhang <943420582@qq.com>
Tue, 2 Apr 2019 10:57:16 +0000 (18:57 +0800)
app/api/__init__.py
app/api/resources.py
app/model/faucet.py [new file with mode: 0644]

index 058d7e4..ff93d4f 100644 (file)
@@ -36,6 +36,8 @@ from app.api.resources import Get_Gm_Address
 from app.api.resources import Get_Gm_New_Key
 from app.api.resources import Get_Gm_New_Address
 from app.api.resources import Decode_Raw_Tx
+from app.api.resources import Get_Testnet_Coins
+from app.api.resources import Get_Gm_Testnet_Coins
 
 
 blueprint = Blueprint('api', __name__, url_prefix='/api/v1')
@@ -75,4 +77,6 @@ api.add_resource(Get_Gm_P2WPKH_Program, '/get_gm_P2WPKH_program')
 api.add_resource(Get_Gm_Address, '/get_gm_address')
 api.add_resource(Get_Gm_New_Key, '/get_gm_new_key')
 api.add_resource(Get_Gm_New_Address, '/get_gm_new_address')
-api.add_resource(Decode_Raw_Tx, '/decode_raw_tx')
\ No newline at end of file
+api.add_resource(Decode_Raw_Tx, '/decode_raw_tx')
+api.add_resource(Get_Testnet_Coins, '/get_testnet_coins')
+api.add_resource(Get_Gm_Testnet_Coins, '/get_gm_testnet_coins')
\ No newline at end of file
index bee1ff3..1f0f6e3 100644 (file)
@@ -36,6 +36,8 @@ from app.model.receiver import get_gm_address
 from app.model.key_gm import get_gm_new_key
 from app.model.receiver import get_gm_new_address
 from app.model.transaction import decode_raw_tx
+from app.model.faucet import get_testnet_coins
+from app.model.faucet import get_gm_testnet_coins
 
 
 parser = reqparse.RequestParser()
@@ -57,6 +59,7 @@ parser.add_argument('network_str', type=str)
 parser.add_argument('raw_transaction_str', type=str)
 parser.add_argument('address_str', type=str)
 parser.add_argument('s', type=str)
+parser.add_argument('receiver_str', type=str)
 
 class Hello(Resource):
 
@@ -368,4 +371,20 @@ class Decode_Raw_Tx(Resource):
         network = args.get('network_str')
         raw_transaction = args.get('raw_transaction_str')
         tx = decode_raw_tx(raw_transaction, network)
-        return tx
\ No newline at end of file
+        return tx
+
+class Get_Testnet_Coins(Resource):
+
+    def post(self):
+        args = parser.parse_args()
+        receiver = args.get('receiver_str')
+        tx_id = get_testnet_coins(receiver)
+        return tx_id
+
+class Get_Gm_Testnet_Coins(Resource):
+
+    def post(self):
+        args = parser.parse_args()
+        receiver = args.get('receiver_str')
+        tx_id = get_gm_testnet_coins(receiver)
+        return tx_id
\ No newline at end of file
diff --git a/app/model/faucet.py b/app/model/faucet.py
new file mode 100644 (file)
index 0000000..c32789b
--- /dev/null
@@ -0,0 +1,90 @@
+import requests
+import json
+
+def get_testnet_coins(receiver_str):
+    transaction_dict = {
+        "actions":[
+            {
+                "account_id":"0KN9JNBA00A02",
+                "amount":1020000000,
+                "asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
+                "type":"spend_account",
+                "use_unconfirmed":True
+            },
+            {
+                "amount":1000000000,
+                "asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
+                "address":receiver_str,
+                "type":"control_address"
+            }
+        ],
+        "ttl":0,
+        "time_range":0
+    }
+    transaction_json = json.dumps(transaction_dict)
+    headers = {
+        "content-type": "application/json",
+        "accept": "application/json"
+    }
+    build_url = "http://127.0.0.1:9888/build-transaction"
+    response = requests.post(build_url, data=transaction_json).json()
+    built_transaction_dict = {
+        "password": "12345",
+        "transaction": response['data']
+    }
+    built_transaction_json = json.dumps(built_transaction_dict)
+    sign_url = "http://127.0.0.1:9888/sign-transaction"
+    response = requests.post(sign_url, headers=headers, data=built_transaction_json).json()
+    signed_transaction_dict = {
+        "raw_transaction": response['data']['transaction']['raw_transaction']
+    }
+    signed_transaction_json = json.dumps(signed_transaction_dict)
+    submit_url = "http://127.0.0.1:9888/submit-transaction"
+    response = requests.post(submit_url, headers=headers, data=signed_transaction_json).json()
+    return {
+        "tx_id": response['data']['tx_id']
+    }
+
+def get_gm_testnet_coins(receiver_str):
+    transaction_dict = {
+        "actions":[
+            {
+                "account_id":"0KN9JNBA00A02",
+                "amount":1020000000,
+                "asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
+                "type":"spend_account",
+                "use_unconfirmed":True
+            },
+            {
+                "amount":1000000000,
+                "asset_id":"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
+                "address":receiver_str,
+                "type":"control_address"
+            }
+        ],
+        "ttl":0,
+        "time_range":0
+    }
+    transaction_json = json.dumps(transaction_dict)
+    headers = {
+        "content-type": "application/json",
+        "accept": "application/json"
+    }
+    build_url = "http://127.0.0.1:9889/build-transaction"
+    response = requests.post(build_url, data=transaction_json).json()
+    built_transaction_dict = {
+        "password": "12345",
+        "transaction": response['data']
+    }
+    built_transaction_json = json.dumps(built_transaction_dict)
+    sign_url = "http://127.0.0.1:9889/sign-transaction"
+    response = requests.post(sign_url, headers=headers, data=built_transaction_json).json()
+    signed_transaction_dict = {
+        "raw_transaction": response['data']['transaction']['raw_transaction']
+    }
+    signed_transaction_json = json.dumps(signed_transaction_dict)
+    submit_url = "http://127.0.0.1:9889/submit-transaction"
+    response = requests.post(submit_url, headers=headers, data=signed_transaction_json).json()
+    return {
+        "tx_id": response['data']['tx_id']
+    }