PlatON Workshop 1 - Developer Tutorials | Using python SDK (Alaya) to build DApps

PlatON Grant Hackathon Workshop 1

Speaker:
Stephen Kusu_Founder of KusuConsult, PlatON Dev Ambassador

Theme:
Developer Tutorials | Using python SDK (Alaya) to build DApps
1, Introduction to Alaya
2, Introduction to Alaya development tools and in depth Introduction to the Alaya Python SDK
3, Use Case Instructions and Sample Tutorials of how Alaya can be used and how to use it.

Video:

Resources

Example code

from client_sdk_python import Web3, HTTPProvider
from client_sdk_python.eth import PlatON
from client_sdk_python.utils.transactions import get_block_gas_limit

rpc_url = 'http://47.241.91.2:6789'
chainId = 201030
hrp = 'apt'
opt_address, opt_private_key = 'atp1cl7kydvgfyvqsrg45xzmr4hdhxefdtkmnpul8p', "bb590e31b3fc650e2089eb795d57adaf08a20a36f6190c51868d051a95270992"
rec_address = 'atp1vjhjpnc2mfl0v7kkvdcmer4nza2xgc6l5483xj'


# Connect to the Alaya
w3 = Web3(HTTPProvider(rpc_url), chain_id=chainId)
platon = PlatON(w3)
print("Connected: ", w3.isConnected())
gas = get_block_gas_limit(w3) - 1
gasPrice = 1000000000

# contract bytecode and abi
abi = [{'constant': True, 'inputs': [{'internalType': 'address[]', 'name': 'arr', 'type': 'address[]'}], 'name': 'IuputArray',
        'outputs': [{'internalType': 'address[]', 'name': '', 'type': 'address[]'}], 'payable': False, 'stateMutability': 'view', 'type': 'function'},
       {'constant': True, 'inputs': [{'internalType': 'uint256', 'name': '', 'type': 'uint256'}], 'name': 'arr',
        'outputs': [{'internalType': 'address', 'name': '', 'type': 'address'}], 'payable': False, 'stateMutability': 'view', 'type': 'function'}]
bytecode = '608060405234801561001057600080fd5b506102b7806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806371e5ee5f1461003b578063e590cf7f146100a9575b600080fd5b6100676004803603602081101561005157600080fd5b81019080803590602001909291905050506101b6565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b61015f600480360360208110156100bf57600080fd5b81019080803590602001906401000000008111156100dc57600080fd5b8201836020820111156100ee57600080fd5b8035906020019184602083028401116401000000008311171561011057600080fd5b919080806020026020016040519081016040528093929190818152602001838360200280828437600081840152601f19601f8201169050808301925050505050505091929192905050506101f2565b6040518080602001828103825283818151815260200191508051906020019060200280838360005b838110156101a2578082015181840152602081019050610187565b505050509050019250505060405180910390f35b600081815481106101c357fe5b906000526020600020016000915054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60607324a42648378add5ba81c76b70caa84a9546279228260008151811061021657fe5b602002602001019073ffffffffffffffffffffffffffffffffffffffff16908173ffffffffffffffffffffffffffffffffffffffff168152505081905091905056fea265627a7a7231582095e2e313d166a0c2ce2537b858b388dfa0f631c0c8622519a5b282644d0682ef64736f6c63782c302e352e31332d646576656c6f702e323032302e352e31382b636f6d6d69742e33616239633638642e6d6f64005c'


# build transaction dict
def build_txn(from_address, to_address, value, data=''):
    return {"from": from_address,
            "nonce": platon.getTransactionCount(from_address),
            "gas": gas,
            "gasPrice": gasPrice,
            "value": value,
            "chainId": chainId,
            "data": data,
            "to": to_address,
            }


# send transaction dict.
def SendTxn(txn):
    signed_txn = platon.account.signTransaction(txn, private_key=opt_private_key, net_type=hrp)
    res = platon.sendRawTransaction(signed_txn.rawTransaction).hex()
    txn_receipt = platon.waitForTransactionReceipt(res)
    print("Tx hash: ", res, " receipt:", txn_receipt)
    return txn_receipt


# Send an wasm deploy transaction.
def wasm_deploy_by_signature(*args, **kwargs):
    encode_data = platon.contract(abi=abi, bytecode=bytecode)._encode_constructor_data(args, kwargs)
    SendTxn(build_txn(opt_address, rec_address, 0, encode_data))


# Send an transfer transaction.
def transfer_by_signature(from_address, to_address, value):
    return SendTxn(build_txn(from_address, to_address, value, ''))


# run.
wasm_deploy_by_signature()
transfer_by_signature(opt_address, rec_address, 100)

Workshop 2 Preview

:balloon: Topic:【Introduction to Contract Development based on PlatON Studio】
:timer: Time:12:00 Aug 17,2021 (UTC)
:movie_camera: Bilibili:哔哩哔哩直播
:video_camera: Youtube:PlatON/Alaya Development 101 : Issue your first token with PlatON Studio | Platon workshop 02 - YouTube

1 个赞