Contract deployment with Python

Below is an example of a contract deployment task, followed by an issuance task for the new contract:

from __future__ import print_function
import base64
import time
from pprint import pprint
# generated via https://github.com/OpenAPITools/openapi-generator from OpenAPI spec
import openapi_client
from openapi_client import Configuration, ContractDeployment, Issuance, Role
from openapi_client.rest import ApiException
URL = 'https://sandbox.cashlink-api.de/v1'
CLIENT_ID = '...'
CLIENT_SECRET = '...'
BACKUP_WALLET = '0x11Fd571Ec9A6ac5b3b546F1D4D57330A973317CA'
def authenticate():
''' Create an authenticated client instance. '''
configuration = Configuration()
configuration.host = URL
# Perform OAuth authentication
response, response_code, _ = openapi_client.ApiClient(configuration).call_api(
resource_path='/oauth/token',
method='POST',
body={
"grant_type": "client_credentials",
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
},
response_type='dict(str, str)'
)
assert response_code == 200
# Re-create client with access token
client = openapi_client.ApiClient(
configuration,
header_name='Authorization',
header_value=f'Bearer {response["access_token"]}'
)
return client
def main():
''' Deploy a contract and issue securities. '''
client = authenticate()
# Create an instance of the contracts API
api = openapi_client.ContractsApi(client)
# Create a deployment if none exists yet
deployments = api.list_deployments()
if deployments:
deployment = deployments[0]
else:
# Deploy a contract with managed issuer and approver roles
deployment = api.create_deployment(
x_idempotency_key=str(uuid4()),
contract_deployment=ContractDeployment(
asset_name='Awesome Asset',
asset_code='AWE',
initial_supply='1000000',
calculation_factor='100',
issuer=Role(wallet=BACKUP_WALLET, managed=True),
paper_contract_hash=base64.b64encode(b'a' * 32).decode(),
)
)
pprint(deployment)
print('Waiting for contract deployment...')
while True:
deployment = api.get_deployment(deployment.id)
if deployment.status == 'failed':
print('Deployment failed:')
pprint(deployment.error)
return
if deployment.status == 'succeeded':
break
time.sleep(5)
pprint(deployment)
contract = deployment.contract
print('Contract deployed at address ' + contract['address'])
# Issue securities
issuance = api.create_issuance(
x_idempotency_key=str(uuid4()),
id=deployment.contract['id'],
issuance=Issuance(
recipient='0x11Fd571Ec9A6ac5b3b546F1D4D57330A973317CA',
amount='1000',
)
)
print('Waiting for issuance...')
while True:
issuance = api.get_issuance(issuance.id)
if issuance.status in ['failed', 'unknown']:
print('Issuance might have failed:')
pprint(issuance.error)
return
if issuance.status == 'succeeded':
break
time.sleep(5)
pprint(issuance)
if __name__ == '__main__':
main()