views.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from django.shortcuts import render
  2. from django.http import HttpResponse
  3. import json
  4. from pathlib import Path
  5. from erdpy import utils
  6. from django.views.decorators.csrf import csrf_protect, csrf_exempt
  7. from erdpy import cli_shared, testnet
  8. from erdpy.accounts import Account, Address
  9. from erdpy.contracts import CodeMetadata, SmartContract
  10. from erdpy.proxy.core import ElrondProxy
  11. # Create your views here.
  12. def index(request):
  13. return render(request, 'index.html')
  14. @csrf_exempt # disable this on deploy
  15. def deploy_contract_function(request):
  16. ##Arguments
  17. #--proxy
  18. #--pem
  19. proxy="https://testnet-gateway.elrond.com"
  20. pem_user="/home/ubuntu-test/Desktop/proj2/backend_dev/Market_Place_NFT/backend/wallet/users/sergiu.pem"
  21. response_data="asdas"
  22. proxy = ElrondProxy(proxy)
  23. network = proxy.get_network_config()
  24. user = Account(pem_file=pem_user)
  25. user.sync_nonce(proxy)
  26. bytecode_path = Path("/home/ubuntu-test/Desktop/proj2/project3/my_contract/output/my_contract.wasm").absolute()
  27. bytecode = utils.read_binary_file(bytecode_path).hex()
  28. code_metadata = CodeMetadata(upgradeable=True)
  29. contract = SmartContract(bytecode=bytecode, metadata=code_metadata)
  30. tx = contract.deploy(
  31. owner=user,
  32. arguments=[],
  33. gas_price=network.min_gas_price,
  34. gas_limit=60000000,
  35. value=0,
  36. chain=network.chain_id,
  37. version=network.min_tx_version
  38. )
  39. tx_on_network = tx.send_wait_result(proxy, 5000)
  40. write_logs_to_file(f"Contract address: {contract.address.bech32()}")
  41. write_logs_to_file(f"Deployment transaction: {tx_on_network.get_hash()}")
  42. return HttpResponse(
  43. json.dumps(f"Contract address: {contract.address.bech32()}"+f"Deployment transaction: {tx_on_network.get_hash()}"),
  44. content_type="application/json"
  45. )
  46. def write_logs_to_file(log_details):
  47. f = open("logfile.txt", "a")
  48. f.write(log_details)
  49. f.close()