Getting Started

Orbs Contracts

Gamma in Depth

API Reference

This page is used to describe all the additional APIs which exists as part of the Contract SDK.

Env

To use the

`Env`

package, you need to import it:1

import (

2

"github.com/orbs-network/orbs-contract-sdk/go/sdk/v1/env"

3

)

Copied!

It provides the following functions:

GetBlockHeight

Used to get the block height which will be the one this transaction be a part of

1

GetBlockHeight() uint64

Copied!

It returns the block height in decimal values:

GetBlockTimestamp

Used to get the block timestamp (creation time) which will be the one this transaction be a part of:

1

GetBlockTimestamp() uint64

Copied!

It returns the block timestamp representing the Unix epoch in nanoseconds.

GetVirtualChainId

Used to get the virtual chain id this contract is executing on.

1

GetVirtualChainId() uint32

Copied!

It returns the virtual chain id in decimal values.

Safemath

To use the

`Safemath`

package, you need to import it:1

import (

2

"github.com/orbs-network/orbs-contract-sdk/go/sdk/v1/safemath/safeuint256"

3

"github.com/orbs-network/orbs-contract-sdk/go/sdk/v1/safemath/safeuint64"

4

"github.com/orbs-network/orbs-contract-sdk/go/sdk/v1/safemath/safeuint32"

5

)

Copied!

It gives the ability to work with 32, 64, and 256 bit unsigned integers in a 'safe' way meaning there is an overflow/underflow and illegal operation protection around the arithmetic operation done.

The API is uniform between the different variants and consist of the following functions:

Add

Adds two numbers and returns the result. The numbers are validated so that they do not overflow after the addition.

1

Add(x *big.Int, y *big.Int) *big.Int

2

Add(x uint32, y uint32) uint32

3

Add(x uint64, y uint64) uint64

Copied!

Sub

Subtract

`y`

from `x`

. The result is validated, so if it becomes negative, an error is returned.1

Sub(x *big.Int, y *big.Int) *big.Int

2

Sub(x uint32, y uint32) uint32

3

Sub(x uint64, y uint64) uint64

Copied!

Mul

Multiplies the values and returns the new number. The result is validated so that it does not overflow.

1

Mul(x *big.Int, y *big.Int) *big.Int

2

Mul(x uint32, y uint32) uint32

3

Mul(x uint64, y uint64) uint64

Copied!

Div

Performs an integer division of

`x`

by `y`

. The remainder is discarded as it should in integer arithmetic, and the input (`y`

) is validated not to be zero.1

Div(x *big.Int, y *big.Int) *big.Int

2

Div(x uint32, y uint32) uint32

3

Div(x uint64, y uint64) uint64

Copied!

Mod

Performs the modulo operation of

`x`

by `y`

. The input is validated to be valid (meaning `y`

is not zero). The resulting number is the reminder.1

Mod(x *big.Int, y *big.Int) *big.Int

2

Mod(x uint32, y uint32) uint32

3

Mod(x uint64, y uint64) uint64

Copied!

Validate

Only relevant for

`safeuint256`

The function validates that the number is not negative, and did not overflow. If the validation fails, the code panics with the appropriate message.

1

Validate(n *big.Int)

Copied!

Note that

`validate`

is called automatically on each operation, meaning after using `Add`

or `Sub`

or any operation of the safemath library, so there is no need to call it manually.Orbs Contracts - Previous

Calling Ethereum contract

Next - Orbs Contracts

Limitations of Orbs Contracts

Last modified 1yr ago

Copy link