The BlockAppsClient
Class is used to set the URLs for both the bloc-server
and strato-api
. Both are RESTful API's that the BlockAppsClient
abstracts
to native C# classes.
// Replace `bloc-url` and `strato-url with their respective URLs`
var blockAppsClient = new BlockAppsClient("http://bloc-url", "http://strato-url");
The BlockAppsClient
has 3 properties; UserManager
, BlockManager
,
ContractManager
. The following sections will describes how to use these in
detail.
The BlockManager
queries blocks on the blockchain. Currently it only supports
requesting blocks by number.
var BlockManager = blockAppsClient.BlockManager;
var block1 = await BlockManager.GetBlock(1);
The UserManager
- creates and gets users registered in the bloc-server.
var UserManager = blockAppsClient.UserManager;
Creates a user with a given username. An account is created with password
argument. This account is set as the
SigningAccount
property on the user instance.
var user = await UserManager.CreateUser("username");
Gets a user according to their username
var user = await UserManager.GetUser("username");
Gets a list of registered the usernames
var user = await UserManager.GetAllUserNames();
User
's own Account
's that are represent Ether accounts. They are
used to send Ether, create Contract, or call Contract methods.
var UserManager = blockAppsClient.UserManager;
var user = await UserManager.CreateUser("username","password");
Set the account that is bound to newly created contracts and that is used with the Send
method.
address
is the address of the account and password
is the accounts password.
user.SetSigningAccount("username","password");
Adds a new account to the user's Accounts
porperty.
var newAddress = await user.AddNewAccount("secretPassword");
Populates the Accounts
property with all the Accounts associated with the user.
await user.PopulateAccounts();
Sends the given value
of Ether to the toAddress
. The Ether is sent from the SigningAccount
.
var transaction = await user.Send("deadbeef", value);
Refreshes all the accounts in the Accounts
property.
await user.RefreshAllAccounts();
The BoundContractManager
creates and gets BoundContracts
. BoundContract
's
are Contract
's that are bound to an account that will sign all transactions
required for interacting with the contract on the chain.
var UserManager = blockAppsClient.UserManager;
var user = await UserManager.CreateUser("username","password");
Takes contract source and the name of the contract, and deploys it to the blockchain. Returns
an instance of the BoundContract
. The generic class is a model of the state
variables within the solidity contract.
public class SimpleStorageState
{
public int StoredData { get; set; }
}
var source = @"
contract SimpleStorage {
uint storedData;
function set(uint x) {
storedData = x;
}
function get() returns (uint retVal) {
return storedData;
}
}"
var contract = await user.BoundContractManager.CreateBoundContract<SimpleStorageState>(source, "SimpleStorage")
Creates an instance of a boundContract with a given name and address.
var contract = await user.BoundContractManager.GetBoundContract<SimpleStorageState>("SimpleStorage","deadbeef")
Gets all the Contracts of a given name and instantiates them as BoundContract
's
var listOfContracts = await user.BoundContractManager.GetBoundContractsWithName<SimpleStorageState>("SimpleStorage")
The BoundContract
class inherits the Contract
class. BoundContract
's
handle signing, holding the username, address, and password associated with
the account the contract is bound.
var UserManager = blockAppsClient.UserManager;
var user = await UserManager.CreateUser("username","password");
var boundContract = await user.BoundContractManager.GetBoundContract<SimpleStorageState>("SimpleStorage","deadbeef");
Set the signing account for the bound contract.
boundContract.SetSigningAccount("deadbeef", "beefy-password");
Calls the method of the contract with the given name and arguments. The value
parameter is the amount of ether that is sent to the contract in transaction to
call method.
var args = new Dictionary<string, string>();
args.Add("x", "2");
var resp = await boundContract.CallMethod("set", args, 3)