# Manage Wallets And Earnings

{% hint style="info" %}
The revenue is split between the [admin](https://docs.dataunions.org/main-concepts/roles-and-responsibilities/admin), the [members](https://docs.dataunions.org/main-concepts/roles-and-responsibilities/member) and the [protocol beneficiary](https://docs.dataunions.org/main-concepts/roles-and-responsibilities/protocol-beneficiary) (Data Union DAO).
{% endhint %}

### Revenue Distribution

The revenue distribution is by default defined by two variables in the smart contract:

#### Protocol fee ([read more](https://docs.dataunions.org/main-concepts/roles-and-responsibilities/protocol-beneficiary)):

Takes **1%** of data union revenue, governed by the Data Union DAO, earnings get allocated to the protocolBeneficiary address.

**Admin fee (**[**read more**](https://docs.dataunions.org/main-concepts/roles-and-responsibilities/admin)**):**

Takes **x%** of data union revenue, can be changed at any time by the admin (previous earnings are not affected when changed), earnings get allocated to the admin/owner address

**Individual member earnings:**

Is calculated as follows:&#x20;

<mark style="color:green;">`totalMemberEarnings`</mark>` ``=  revenue - (revenue * (adminFee + protocolFee))`

`individualMemberEarnings =`` `<mark style="color:green;">`totalMemberEarnings`</mark>` ``/ allActiveMembers`

{% hint style="info" %}
Read how you can handle uneven payments to members [here](#weighted-payments)
{% endhint %}

### Refresh Revenue

You need to **manually refresh the revenue** if you **don't use the Streamr Marketplace** as your revenue source **or you don't use a ERC677 token** .

Whenever your data union contract receives token aka revenue you need to refresh it after the transaction before users can withdraw or see their earnings.

```typescript
const tx = await dataUnion.refreshRevenue();
```

The revenue then gets refreshed after every withdraw call.&#x20;

### Check Withdrawable Earnings

Members can **check their earnings** (minus earlier withdrawals) like this:

```typescript
const amount = await dataUnion.getWithdrawableEarnings();
```

### Withdraw Earnings

```typescript
const tx = await dataUnion.withdrawAll();
```

## Weighted Payments

You can individually assign weights to members if you don't want them to get rewarded equally.

{% hint style="warning" %} <mark style="color:red;">Don't change the weights too often</mark> as this will result in defeating the purpose of saving transactions/transaction fees. Try to minimize changing the weight factor as good as you can.
{% endhint %}

**Example:**

| Address | Weight | Earnings |
| ------- | ------ | -------- |
| 0x1234  | 1      | 2000     |
| 0x4321  | 1      | 2000     |
| 0xabcd  | 0.5    | 1000     |

**Calculation:**\ <mark style="color:blue;">`totalWeight`</mark> = 2.5\ <mark style="color:orange;">`receivedRevenue`</mark> = 5000 token

<mark style="color:green;">`baseEarnings`</mark> = <mark style="color:orange;">`receivedRevenue`</mark> / <mark style="color:blue;">`totalWeight`</mark> = <mark style="color:orange;">5000</mark> / <mark style="color:blue;">2.5</mark> = 2000

**Earnings for each member:**

addr1 = weight \* <mark style="color:green;">`baseEarnings`</mark> = 1 \* <mark style="color:green;">2000</mark> = 2000

addr2 = weight \* <mark style="color:green;">`baseEarnings`</mark> = 1 \* <mark style="color:green;">2000</mark> = 2000

addr3 = weight \* <mark style="color:green;">`baseEarnings`</mark> = 0.5 \* <mark style="color:green;">2000</mark> = 1000

### Add Members With Weights

Learn more about adding members [here](https://docs.dataunions.org/roles-and-responsibilities/joinpart-agents#add-and-remove-members)

```typescript
const tx = await dataUnion.addMembersWithWeights(
      ['0x1234', '0x4321', '0xabcd'],
      [1, 1, 0.5]
);
```

### Set Member Weights

```typescript
const tx = await dataUnion.setMemberWeights(
      ['0x1234', '0xabcd'],
      [0.2, 0.4]
    );
```

## Member wallet Management

**Members are identified by an Ethereum address.** The **address is used to authenticate** the member by **signing requests with their associated private key**.

{% hint style="info" %}
There are **two ways to enable users to interact** with the data union infrastructure:

* **Manage the** **member wallets yourself** by generating and storing the private key
* Enable your users to **connect their own wallet and join via a** [**join server** ](https://docs.dataunions.org/main-concepts/join-server)
  {% endhint %}

#### Manage member wallets yourself

If your users don't have crypto wallets you can **generate a wallet address yourself** and store it securely on a server:

```typescript
import { DataUnionClient } from '@dataunions/client';
const wallet = DataUnionClient.generateEthereumAccount();
```

The **data-producing application** can generate the keys when it is launched for the first time, and <mark style="color:red;">**store the information locally**</mark> or the user may connect their existing wallet. The **private key** should always stay on the device and it should <mark style="color:red;">**never be sent over the internet**</mark>.

**Storing the private key in encrypted form is recommended**. One way to approach this is to encrypt it using a user-defined password. If the key is stored encrypted, the key needs to be decrypted when the application starts. The decrypted version should be kept in memory, as it is continuously needed for signing data.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.dataunions.org/main-concepts/manage-wallets-and-earnings.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
