Skip to content

Latest commit

 

History

History
180 lines (136 loc) · 3.85 KB

README.org

File metadata and controls

180 lines (136 loc) · 3.85 KB

Waves Sandbox deploy contract

Prerequisites

  • Deployed Waves Enterprise Node (version 1.4.0 has been tested).

Init parameters

SENDER=
KEYPAIR_PASSWORD=
NODE_URL=http://localhost:6862

Contract code

contract.py

import json
import os
import requests
import sys


def find_param_value(params, name):
    for param in params:
        if param['key'] == name: return param['value']
    return None


def print_success(results):
    print(json.dumps(results, separators=(',', ':')))


def print_error(message):
    print(message)
    sys.exit(3)


def get_value(contract_id):
    node = os.environ['NODE_API']
    if not node:
        print_error("Node REST API address is not defined")
    token = os.environ["API_TOKEN"]
    if not token:
        print_error("Node API token is not defined")
    headers = {'X-Contract-Api-Token': token}
    url = '{0}/internal/contracts/{1}/sum'.format(node, contract_id)
    r = requests.get(url, verify=False, timeout=2, headers=headers)
    data = r.json()
    return data['value']


if __name__ == '__main__':
    command = os.environ['COMMAND']
    if command == 'CALL':
        contract_id = json.loads(os.environ['TX'])['contractId']
        value = get_value(contract_id)
        print_success([{
            "key": "sum",
            "type": "integer",
            "value": value + 1}])
    elif command == 'CREATE':
        print_success([{
            "key": "sum",
            "type": "integer",
            "value": 0}])
    else:
        print_error("Unknown command {0}".format(command))

run.sh

#!/bin/sh

python contract.py

Dockerfile

FROM python:alpine3.8
RUN pip install requests
ADD contract.py /
ADD run.sh /
RUN chmod +x run.sh
CMD exec /bin/sh -c "trap : TERM INT; (while true; do sleep 1000; done) & wait"

Build contract

docker build -t test-contract src

Set tag

docker image tag test-contract registry:5000/test-contract

Push image to the registry

docker push registry:5000/test-contract

Get image ID

IMAGE_ID=`docker inspect registry:5000/test-contract | grep Id | cut -d'"' -f 4 | cut -d":" -f 2`
echo $IMAGE_ID

Sign the create smart-contract transaction

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'X-Contract-Api-Token' -d "    { \
        \"fee\": 100000000, \
        \"image\": \"test-contract:latest\", \
        \"imageHash\": \"$IMAGE_ID\", \
        \"contractName\": \"test-contract\", \
        \"sender\": \"$SENDER\", \
        \"password\": \"$KEYPAIR_PASSWORD\", \
        \"params\": [], \
        \"type\": 103, \
        \"version\": 1 \
    }" "$NODE_URL/transactions/signAndBroadcast"

Find the transaction ID

Id field from JSON result.

TXID=

Check TXID in blockchain

curl $NODE_URL/transactions/info/$TXID

Set CONTRACT_ID

CONTRACT_ID=$TXID

Call contract method

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'X-Contract-Api-Token' -d "{ \
    \"contractId\": \"$CONTRACT_ID\", \
    \"fee\": 10, \
    \"sender\": \"$SENDER\", \
    \"password\": \"$KEYPAIR_PASSWORD\", \
    \"type\": 104, \
    \"version\": 1, \
    \"params\": [ \
        { \
            \"type\": \"integer\", \
            \"key\": \"a\", \
            \"value\": 4 \
        } \
    ] \
}" "$NODE_URL/transactions/signAndbroadcast"

Get result

curl $NODE_URL/contracts/$CONTRACT_ID

References