> For the complete documentation index, see [llms.txt](https://docs.gnoswap.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.gnoswap.io/contracts/gov/staker/delegation.gno.md).

# delegation.gno

{% embed url="<https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno>" %}

## NewDelegationTree

```go
func NewDelegationTree() *bptree.BPTree
```

NewDelegationTree creates an empty tree for delegatee-to-delegation-ID mappings.

#### Return Values

| Name             | Type            | Description                                          |
| ---------------- | --------------- | ---------------------------------------------------- |
| `*bptree.BPTree` | \*bptree.BPTree | empty tree configured for delegation lookup entries. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L196)

## NewUserDelegationTree

```go
func NewUserDelegationTree() *bptree.BPTree
```

NewUserDelegationTree creates an empty tree for user delegation mappings.

#### Return Values

| Name             | Type            | Description                                        |
| ---------------- | --------------- | -------------------------------------------------- |
| `*bptree.BPTree` | \*bptree.BPTree | empty tree configured for user delegation entries. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L203)

## Types

### Delegation

```go
type Delegation struct
```

Delegation represents a delegation between two addresses

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L32)

#### Methods

**AddWithdraw**

```go
func (d *Delegation) AddWithdraw(
	withdraw DelegationWithdraw
)
```

AddWithdraw appends a withdrawal record to the delegation history.

#### Parameters

| Name       | Type               | Description                                                          |
| ---------- | ------------------ | -------------------------------------------------------------------- |
| `withdraw` | DelegationWithdraw | withdrawal record to append, including its amount and unlock timing. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L146)

**Clone**

```go
func (d *Delegation) Clone() *Delegation
```

Clone returns a deep copy of the delegation and its withdrawal slice.

#### Return Values

| Name          | Type         | Description                                         |
| ------------- | ------------ | --------------------------------------------------- |
| `*Delegation` | \*Delegation | copied delegation, or nil when the receiver is nil. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L171)

**CollectedAmount**

```go
func (d *Delegation) CollectedAmount() int64
```

CollectedAmount returns the amount already collected from undelegation.

#### Return Values

| Name    | Type  | Description                                 |
| ------- | ----- | ------------------------------------------- |
| `int64` | int64 | token amount released from this delegation. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L116)

**CreatedAt**

```go
func (d *Delegation) CreatedAt() int64
```

CreatedAt returns the Unix timestamp recorded when the delegation was created.

#### Return Values

| Name    | Type  | Description                         |
| ------- | ----- | ----------------------------------- |
| `int64` | int64 | creation timestamp in Unix seconds. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L97)

**DelegateFrom**

```go
func (d *Delegation) DelegateFrom() address
```

DelegateFrom returns the address that supplied the delegated tokens.

#### Return Values

| Name      | Type    | Description                                   |
| --------- | ------- | --------------------------------------------- |
| `address` | address | delegator address recorded on the delegation. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L85)

**DelegateTo**

```go
func (d *Delegation) DelegateTo() address
```

DelegateTo returns the address receiving the delegated voting power.

#### Return Values

| Name      | Type    | Description                                   |
| --------- | ------- | --------------------------------------------- |
| `address` | address | delegatee address recorded on the delegation. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L91)

**ID**

```go
func (d *Delegation) ID() int64
```

Basic getters ID returns the unique identifier of the delegation record.

#### Return Values

| Name    | Type  | Description                                 |
| ------- | ----- | ------------------------------------------- |
| `int64` | int64 | delegation identifier assigned at creation. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L79)

**SetCollectedAmount**

```go
func (d *Delegation) SetCollectedAmount(
	amount int64
)
```

SetCollectedAmount replaces the amount already collected from this delegation.

#### Parameters

| Name     | Type  | Description                                       |
| -------- | ----- | ------------------------------------------------- |
| `amount` | int64 | released token amount in the smallest token unit. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L138)

**SetUnDelegateAmount**

```go
func (d *Delegation) SetUnDelegateAmount(
	amount int64
)
```

Setters SetUnDelegateAmount replaces the amount currently in undelegation lockup.

#### Parameters

| Name     | Type  | Description                                                  |
| -------- | ----- | ------------------------------------------------------------ |
| `amount` | int64 | token amount awaiting collection in the smallest token unit. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L130)

**SetWithdraw**

```go
func (d *Delegation) SetWithdraw(
	index int,
	withdraw DelegationWithdraw
)
```

SetWithdraw replaces one withdrawal record at the supplied slice index.

#### Parameters

| Name       | Type               | Description                                          |
| ---------- | ------------------ | ---------------------------------------------------- |
| `index`    | int                | zero-based index of the withdrawal entry to replace. |
| `withdraw` | DelegationWithdraw | replacement withdrawal record.                       |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L155)

**SetWithdraws**

```go
func (d *Delegation) SetWithdraws(
	withdraws []DelegationWithdraw
)
```

SetWithdraws replaces the complete withdrawal history.

#### Parameters

| Name        | Type                  | Description                                      |
| ----------- | --------------------- | ------------------------------------------------ |
| `withdraws` | \[]DelegationWithdraw | withdrawal records to store for this delegation. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L163)

**TotalDelegatedAmount**

```go
func (d *Delegation) TotalDelegatedAmount() int64
```

Amount getters TotalDelegatedAmount returns the current amount still actively delegated.

#### Return Values

| Name    | Type  | Description                                               |
| ------- | ----- | --------------------------------------------------------- |
| `int64` | int64 | active delegated token amount in the smallest token unit. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L104)

**UnDelegatedAmount**

```go
func (d *Delegation) UnDelegatedAmount() int64
```

UnDelegatedAmount returns the amount moved into undelegation lockup.

#### Return Values

| Name    | Type  | Description                                               |
| ------- | ----- | --------------------------------------------------------- |
| `int64` | int64 | token amount awaiting collection after its lockup period. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L110)

**Withdraws**

```go
func (d *Delegation) Withdraws() []DelegationWithdraw
```

Withdraws getters Withdraws returns the delegation's withdrawal records.

#### Return Values

| Name                   | Type                  | Description                                     |
| ---------------------- | --------------------- | ----------------------------------------------- |
| `[]DelegationWithdraw` | \[]DelegationWithdraw | withdrawal entries tracked for this delegation. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L123)

#### Constructors

* `func NewDelegation(id int64, delegateFrom address, delegateTo address, delegateAmount int64, createdHeight int64, createdAt int64) *Delegation` ([source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L56))

### DelegationType

```go
type DelegationType string
```

DelegationType represents the type of delegation operation

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L6)

#### Methods

**IsDelegate**

```go
func (d DelegationType) IsDelegate() bool
```

IsDelegate reports whether the operation type is DelegateType.

#### Return Values

| Name   | Type | Description                                         |
| ------ | ---- | --------------------------------------------------- |
| `bool` | bool | true only when d represents a delegation operation. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L23)

**IsUnDelegate**

```go
func (d DelegationType) IsUnDelegate() bool
```

IsUnDelegate reports whether the operation type is UnDelegateType.

#### Return Values

| Name   | Type | Description                                            |
| ------ | ---- | ------------------------------------------------------ |
| `bool` | bool | true only when d represents an undelegation operation. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L29)

**String**

```go
func (d DelegationType) String() string
```

String returns the textual operation name represented by the delegation type.

#### Return Values

| Name     | Type   | Description                                                         |
| -------- | ------ | ------------------------------------------------------------------- |
| `string` | string | "DELEGATE", "UNDELEGATE", or the underlying value for another type. |

[source](https://github.com/gnoswap-labs/gnoswap/blob/d9d34bd345a36c3eedaccf491c6615b0ac4168cd/contract/r/gnoswap/gov/staker/delegation.gno#L17)
