Advertisement
AGTGreg

Klaytn use smart contract

May 20th, 2021
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. def swap(klaytn_api, public_key, eth_amount, usdt_amount):
  2.     """ Uses the KlaySwap Protocol smart contract
  3.    (address: 0xc6a2ad8cc6e4a7e08fc37cc5954be07d499e7654) to make a swap of
  4.    ETH vs USDT.
  5.    """
  6.    
  7.     # This is the web3.py client.
  8.     w3py = Web3Client(provider=MAINNET, public_key=public_key)
  9.    
  10.     trade_contract = w3py.w3.eth.contract(
  11.         abi=w3py.swap_contract_ABI, address=w3py.ksp_contract_address)
  12.    
  13.     # Get the encoded hash of the contract's function and its parameters.
  14.     contract_data = w3py.trade_contract.encodeABI(
  15.         fn_name="exchangeKctNeg",
  16.         args=[
  17.             w3py.kusdt_contract_address,
  18.             usdt_amount,
  19.             w3py.keth_contract_address,
  20.             eth_amount,
  21.             []
  22.         ]
  23.     )
  24.    
  25.     # Construct a TxTypeSmartContractExecution object for Klaytn.
  26.     sc_tx = {
  27.         "type": '0x30',
  28.         "nonce": klaytn_api.account.get_transaction_count(),
  29.         "gasPrice": klaytn_api.configuration.gas_price(),
  30.         "gas": w3py.w3.toHex(5000000),
  31.         "from": public_key,
  32.         "to": w3py.kethusdt_contract_address,
  33.         "data": contract_data
  34.     }
  35.    
  36.     # Run the contract on Klaytn and get the result.
  37.     result = klaytn_api.transaction.call(sc_tx)
  38.     return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement