CSharpSmartContractContext

This class represents the transaction execution context in a smart contract. An instance of this class is present in the base class for smart contracts (Context property). It provides access to properties and methods useful for implementing the logic in smart contracts.

Properties

StateProvider

public IStateProvider StateProvider

Provides access to the underlying state provider.

ChainId

public int ChainId

The chain id of the chain on which the contract is currently running.

TransactionId

public Hash TransactionId

The ID of the transaction that's currently executing.

Sender

public Address Sender

The Sender of the transaction that is executing.

Self

public Address Self

the address of the contract currently being executed. This changes for every transaction and inline transaction.

Origin

public Address Origin

The address of the sender (signer) of the transaction being executed. It’s type is an AElf address. It corresponds to the From field of the transaction. This value never changes, even for nested inline calls. This means that when you access this property in your contract, it’s value will be the entity that created the transaction (user or smart contract through an inline call)

CurrentHeight

public long CurrentHeight

The height of the block that contains the transaction currently executing.

CurrentBlockTime

public Timestamp CurrentBlockTime

The time included in the current blocks header.

PreviousBlockHash

public Hash PreviousBlockHash

the hash of the block that precedes the current in the blockchain structure.

Variables

public ContextVariableDictionary Variables

Provides access to variable of the bridge.

Methods

LogDebug

public void LogDebug(Func<string> func)

Application logging - when writing a contract it is useful to be able to log some elements in the applications log file to simplify development. Note that these logs are only visible when the node executing the transaction is build in debug mode.

FireLogEvent

public void FireLogEvent(LogEvent logEvent)

This method is used to produce logs that can be found in the transaction result after execution.

RecoverPublicKey

public byte[] RecoverPublicKey()

Recovers the public key of the transaction Sender.

GetPreviousBlockTransactions

public List<Transaction> GetPreviousBlockTransactions()

Returns the transaction included in the previous block (previous to the one currently executing).

VerifySignature

public bool VerifySignature(Transaction tx)

Returns wether or not the given transaction is well formed and the signature is correct.

Call T

public T Call<T>(Address address, string methodName, ByteString args) where T : IMessage<T>, new()

Calls a method on another contract. This method takes the three following parameters:

  • address: the address of the contract you're seeking to interact with.

  • methodName: the name of method you want to call.

  • args: the input arguments for calling that method. This is usually generated from the protobuf definition of the input type.

SendInline

public void SendInline(Address toAddress, string methodName, ByteString args)

Sends an inline transaction to another contract. This method takes the three following parameters:

  • address: the address of the contract you're seeking to interact with.

  • methodName: the name of method you want to invoke.

  • args: the input arguments for calling that method. This is usually generated from the protobuf definition of the input type.

SendVirtualInline

public void SendVirtualInline(Hash fromVirtualAddress, Address toAddress, string methodName, ByteString args)

Sends an inline transaction to another contract. This method takes the three following parameters:

  • address: the address of the contract you're seeking to interact with.

  • methodName: the name of method you want to invoke.

  • args: the input arguments for calling that method. This is usually generated from the protobuf definition of the input type.

SendVirtualInlineBySystemContract

public void SendVirtualInlineBySystemContract(Hash fromVirtualAddress, Address toAddress, string methodName, ByteString args)

Sends an inline transactions from a system contracts.

ConvertVirtualAddressToContractAddress

public Address ConvertVirtualAddressToContractAddress(Hash virtualAddress)

Converts a virtual address to the contract's address.

ConvertVirtualAddressToContractAddressWithContractHashName

public Address ConvertVirtualAddressToContractAddressWithContractHashName(Hash virtualAddress)

Converts a virtual address to the contract's address.

GetZeroSmartContractAddress

public Address GetZeroSmartContractAddress()

This method returns the address of the Genesis contract (smart contract zero) of the current chain.

GetZeroSmartContractAddress

public Address GetZeroSmartContractAddress(int chainId)

This method returns the address of the Genesis contract (smart contract zero) of the specified chain.

GetContractAddressByName

public Address GetContractAddressByName(Hash hash)

It's sometimes useful to get the address of a system contract. The input is a hash of the system contracts name. These hashes are easily accessible through the constants in the SmartContractConstants.cs file of the C# SDK.

GetSystemContractNameToAddressMapping

public IReadOnlyDictionary<Hash, Address> GetSystemContractNameToAddressMapping()

Get the mapping that associates the system contract addresses and their hash.

EncryptMessage

public byte[] EncryptMessage(byte[] receiverPublicKey, byte[] plainMessage)

Encrypts a message with the given public key.

DecryptMessage

public byte[] DecryptMessage(byte[] senderPublicKey, byte[] cipherMessage)

Decrypts a message with the given public key.