zksync-sdk / zksync-python

Python 3.8 SDK for zkSync
MIT License
116 stars 64 forks source link

SyntaxError: 'await' outside function #43

Open lemons45 opened 2 years ago

lemons45 commented 2 years ago

Hi, having issues creating a wallet following the python tutorial, my code so far:

from web3 import Account, Web3, HTTPProvider
from zksync_sdk import ZkSyncProviderV01, HttpJsonRPCTransport, network, ZkSync, EthereumProvider, Wallet, ZkSyncSigner, EthereumSignerWeb3, ZkSyncLibrary

library = ZkSyncLibrary()
provider = ZkSyncProviderV01(provider=HttpJsonRPCTransport(network=network.rinkeby))
account = Account.from_key("XXX")
ethereum_signer = EthereumSignerWeb3(account=account)
contracts = await provider.get_contract_address()
w3 = Web3(HTTPProvider(endpoint_uri="https://rinkeby.infura.io/v3/XXX" ))
zksync = ZkSync(account=account, web3=w3,
                zksync_contract_address=contracts.main_contract)
ethereum_provider = EthereumProvider(w3, zksync)
signer = ZkSyncSigner.from_account(account, library, network.rinkeby.chain_id)
wallet = Wallet(ethereum_provider=ethereum_provider, zk_signer=signer,
                eth_signer=ethereum_signer, provider=provider)

getting this error:

contracts = await provider.get_contract_address()
                ^
SyntaxError: 'await' outside function

As far as I understand, the await line needs to be inside an async function, but I'm quite the python noob and didn't get it to work. Any help on how to get it running would be appreciated!

vyastrebovvareger commented 2 years ago

Hello. Async functions must be called from async methods or executed as a task by the event loop. This is an example that might be helpful for you @lemons45:

from web3 import Account, Web3, HTTPProvider
from zksync_sdk import ZkSyncProviderV01, HttpJsonRPCTransport, network, ZkSync, EthereumProvider, Wallet, ZkSyncSigner, \
    EthereumSignerWeb3, ZkSyncLibrary
import asyncio

def main():
    # env = TestEnv()

    library = ZkSyncLibrary()
    provider = ZkSyncProviderV01(provider=HttpJsonRPCTransport(network=network.rinkeby))
    account = Account.from_key("XXX")

    async def get_contract_address():
        ret = await provider.get_contract_address()
        return ret

    loop = asyncio.get_event_loop()
    task = loop.create_task(get_contract_address())
    loop.run_until_complete(task)
    contracts = task.result()

    w3 = Web3(HTTPProvider(endpoint_uri="https://rinkeby.infura.io/v3/XXX"))
    # Setup zksync contract interactor
    zksync = ZkSync(account=account, web3=w3,
                    zksync_contract_address=contracts.main_contract)
    # Create ethereum provider for interacting with ethereum node
    ethereum_provider = EthereumProvider(w3, zksync)
    ethereum_signer = EthereumSignerWeb3(account=account)

    # Initialize zksync signer, all creating options were described earlier
    signer = ZkSyncSigner.from_account(account, library, network.rinkeby.chain_id)
    # Initialize Wallet
    wallet = Wallet(ethereum_provider=ethereum_provider, zk_signer=signer,
                    eth_signer=ethereum_signer, provider=provider)

if __name__ == "__main__":
    main()