Skip to content

Contract Deployment

While, in general, we would recommend deploying your smart contracts via the Builder, we recognize that there are some use cases where deploying a smart contract from Unity (or a Made-With-Unity application) is useful.

Contract deployment involves sending a transaction, which is done via an asynchronous Task. You can use await when calling WaaSWallet.DeployContract from within an async Task if you wish to obtain the ContractDeploymentReturn object directly. Or, you can take the recommended approach which is to setup handler functions for the WaaSWallet.OnDeployContractComplete and WaaSWallet.OnDeployContractFailed events and call the WaaSWallet.DeployContract method from anywhere (without await).

WaaSWallet.DeployContract is essentially a wrapper for a very special WaaSWallet.SendTransaction call and therefore, you can expect to still receive the WaaSWallet.OnSendTransactionComplete or WaaSWallet.OnSendTransactionFailed events in addition.

public void OnDeployContractCompleteHandler(SuccessfulContractDeploymentReturn result) {
    Address newlyDeployedContractAddress = result.DeployedContractAddress;

    // Do something
}

public void OnDeployContractFailedHandler(FailedContractDeploymentReturn result) {
    // Do something
}

public void OnWaaSWalletCreatedHander(WaaSWallet wallet) {
    wallet.OnDeployContractComplete += OnDeployContractCompleteHandler;
    wallet.OnDeployContractFailed += OnDeployContractFailedHandler;
}

If you're unfamiliar with working with events in Unity, check out this great Reddit post!

To deploy a contract you'll need to first compile your smart contract code into bytecode and add the bytecode as a hexadecimal string in one of your C# scripts.

To deploy a smart contract, you can use this code snippet:

string bytecode = "Here you'll paste your compiled bytecode"
_wallet.DeployContract(Chain.Polygon, bytecode);