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 SequenceWallet.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 SequenceWallet.OnDeployContractComplete
and SequenceWallet.OnDeployContractFailed
events and call the SequenceWallet.DeployContract
method from anywhere (without await).
SequenceWallet.DeployContract
is essentially a wrapper for a very special SequenceWallet.SendTransaction
call and therefore, you can expect to still receive the SequenceWallet.OnSendTransactionComplete
or SequenceWallet.OnSendTransactionFailed
events in addition.
public void OnDeployContractCompleteHandler(SuccessfulContractDeploymentReturn result) {
Address newlyDeployedContractAddress = result.DeployedContractAddress;
// Do something
}
public void OnDeployContractFailedHandler(FailedContractDeploymentReturn result) {
// Do something
}
public void OnWalletCreatedHander(SequenceWallet 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);