logo

Random Number

ACS6 - Random Number Provider Standard#

To generate a random number in your contract, use ACS6.

Interface#

To provide a random number based on input, implement this interface:

Methods#

Method NameRequest TypeResponse TypeDescription
GetRandomBytesgoogle.protobuf.BytesValuegoogle.protobuf.BytesValueGet the random number provided by the contract.

Usage#

Override the GetRandomBytes method to return a random number based on the given input. The logic for generating the random number is up to you. Ensure you return a BytesValue type so the caller can deserialize the output.

Implementation#

The simplest implementation:

1
public override BytesValue GetRandomBytes(BytesValue input)
2
{
3
var serializedInput = new GetRandomBytesInput();
4
serializedInput.MergeFrom(input.Value);
5
var value = new Hash();
6
value.MergeFrom(serializedInput.Value);
7
var randomHashFromContext = Context.GetRandomHash(value);
8
9
return new BytesValue
10
{
11
Value = serializedInput.Kind == 1
12
? new BytesValue {Value = randomHashFromContext.Value}.ToByteString()
13
: new Int64Value {Value = Context.ConvertHashToInt64(randomHashFromContext, 1, 10000)}.ToByteString()
14
};
15
}

Edited on: 15 July 2024 05:16:42 GMT+0