Skip to content

Commit

Permalink
Merge pull request #88 from Concordium/deploy-module-example
Browse files Browse the repository at this point in the history
Deploy module example
  • Loading branch information
rasmus-kirk authored Feb 6, 2024
2 parents c5deb35 + 455a6b8 commit be6a44e
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 1 deletion.
7 changes: 7 additions & 0 deletions ConcordiumNetSdk.sln
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GetFinalizedBlocks", "examp
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Transations.UpdateContract", "examples\UpdateContract\Transations.UpdateContract.csproj", "{DBFBB7D1-E82D-4380-8263-B4B0AC3A6266}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DeployModule", "examples\DeployModule\DeployModule.csproj", "{D35681A3-04AE-41BA-86F3-3CF5369D6D97}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -233,6 +235,10 @@ Global
{DBFBB7D1-E82D-4380-8263-B4B0AC3A6266}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DBFBB7D1-E82D-4380-8263-B4B0AC3A6266}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DBFBB7D1-E82D-4380-8263-B4B0AC3A6266}.Release|Any CPU.Build.0 = Release|Any CPU
{D35681A3-04AE-41BA-86F3-3CF5369D6D97}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D35681A3-04AE-41BA-86F3-3CF5369D6D97}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D35681A3-04AE-41BA-86F3-3CF5369D6D97}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D35681A3-04AE-41BA-86F3-3CF5369D6D97}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -276,5 +282,6 @@ Global
{26417CD7-2897-47BA-BA9B-C4475187331A} = {FD2CDD9F-4650-4705-9CA2-98CC81F8891D}
{E2CC6AD7-98CE-41F5-8C66-AE8781F29C77} = {FD2CDD9F-4650-4705-9CA2-98CC81F8891D}
{DBFBB7D1-E82D-4380-8263-B4B0AC3A6266} = {FD2CDD9F-4650-4705-9CA2-98CC81F8891D}
{D35681A3-04AE-41BA-86F3-3CF5369D6D97} = {FD2CDD9F-4650-4705-9CA2-98CC81F8891D}
EndGlobalSection
EndGlobal
22 changes: 22 additions & 0 deletions examples/DeployModule/DeployModule.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Concordium.Sdk.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
</ItemGroup>

</Project>
78 changes: 78 additions & 0 deletions examples/DeployModule/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using CommandLine;
using Concordium.Sdk.Client;
using Concordium.Sdk.Types;
using Concordium.Sdk.Wallets;

// We disable these warnings since CommandLine needs to set properties in options
// but we don't want to give default values.
#pragma warning disable CS8618

namespace DeployModule;

internal sealed class DeployModuleOptions
{
[Option(
'k',
"keys",
HelpText = "Path to a file with contents that is in the Concordium browser wallet key export format.",
Required = true
)]
public string WalletKeysFile { get; set; }

[Option(HelpText = "URL representing the endpoint where the gRPC V2 API is served.",
Default = "http://node.testnet.concordium.com:20000/")]
public string Endpoint { get; set; }

[Option('m', "module-file", HelpText = "Path to the smart contract module.", Required = true)]
public string ModulePath { get; set; }
}

public static class Program
{
/// <summary>
/// Example demonstrating how to submit a deploy module transaction.
///
/// The example assumes you have your account key information stored
/// in the Concordium browser wallet key export format, and that a path
/// pointing to it is supplied to it from the command line.
/// </summary>
public static async Task Main(string[] args) =>
await Parser.Default
.ParseArguments<DeployModuleOptions>(args)
.WithParsedAsync(Run);

private static async Task Run(DeployModuleOptions o)
{
// Read the account keys from a file.
var walletData = File.ReadAllText(o.WalletKeysFile);
var account = WalletAccount.FromWalletKeyExportFormat(walletData);

// Construct the client.
var clientOptions = new ConcordiumClientOptions
{
Timeout = TimeSpan.FromSeconds(10)
};
using var client = new ConcordiumClient(new Uri(o.Endpoint), clientOptions);

// Create the transfer transaction.
var b = File.ReadAllBytes(o.ModulePath);
var versionedModule = VersionedModuleSourceFactory.FromFile(o.ModulePath);
var transferPayload = new Concordium.Sdk.Transactions.DeployModule(versionedModule);

// Prepare the transaction for signing.
var sender = account.AccountAddress;
var sequenceNumber = client.GetNextAccountSequenceNumber(sender).Item1;
var expiry = Expiry.AtMinutesFromNow(30);
var preparedTransfer = transferPayload.Prepare(sender, sequenceNumber, expiry);

// Sign the transaction using the account keys.
var signedTransfer = preparedTransfer.Sign(account);

// Submit the transaction.
var txHash = client.SendAccountTransaction(signedTransfer);

// Print the transaction hash.
Console.WriteLine($"Successfully submitted transfer transaction with hash {txHash}");
}
}

23 changes: 22 additions & 1 deletion src/Types/VersionedModuleSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ protected static bool GetSchemaFromWasmCustomSection(Module module, string entry
}
}

internal static class VersionedModuleSourceFactory
/// <summary>
/// Crates a VersiondModuleSource.
/// </summary>
public static class VersionedModuleSourceFactory
{
internal static VersionedModuleSource From(Grpc.V2.VersionedModuleSource versionedModuleSource) =>
versionedModuleSource.ModuleCase switch
Expand All @@ -127,6 +130,24 @@ internal static VersionedModuleSource From(Grpc.V2.VersionedModuleSource version
.ModuleCase)
};

/// <summary>
/// Create a cref="VersionedModuleSource" from a versioned WASM file.
/// </summary>
/// <param name="modulePath">The path to the versioned WASM file.</param>
/// <exception cref="DeserialException">The provided WASM module was unable to be parsed.</exception>
public static VersionedModuleSource FromFile(string modulePath)
{
var bytes = File.ReadAllBytes(modulePath);
if (TryDeserial(bytes, out var versionedModule))
{
return versionedModule.VersionedModuleSource;
}
else
{
throw new DeserialException(versionedModule.Error);
}
}

/// <summary>
/// Create a versioned module schema from a byte array.
/// </summary>
Expand Down

0 comments on commit be6a44e

Please sign in to comment.