> For the complete documentation index, see [llms.txt](https://docs.gooddollar.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.gooddollar.org/previous-protocol-versions/protocol-v3/core-contracts-and-api/goodmarketmaker.md).

# GoodMarketMaker

### Events

#### BalancesUpdated

Emits when a change has occurred in a reserve balance, i.e. buy / sell will change the balance.

<table><thead><tr><th width="200.9737758180553">Parameter name</th><th>Annotation</th></tr></thead><tbody><tr><td>caller</td><td>The account who initiated the action.</td></tr><tr><td>reserveToken</td><td>The address of the reserve token.</td></tr><tr><td>amount</td><td>The incoming amount.</td></tr><tr><td>returnAmount</td><td>The return value.</td></tr><tr><td>totalSupply</td><td>The updated total supply.</td></tr><tr><td>reserveBalance</td><td>The updated reserve balance.</td></tr></tbody></table>

```
event BalancesUpdated(
    address indexed caller,
    address indexed reserveToken,
    uint256 amount,
    uint256 returnAmount,
    uint256 totalSupply,
    uint256 reserveBalance
);
```

#### ReserveRatioUpdated

Emits when the ratio changed. The caller should be the Avatar by definition.

<table><thead><tr><th width="200.9737758180553">Parameter name</th><th>Annotation</th></tr></thead><tbody><tr><td>caller</td><td>The address of the staker.</td></tr><tr><td>nom</td><td>Nominator of the ratio.</td></tr><tr><td>denom</td><td>Denominator of the ratio.</td></tr></tbody></table>

```
event ReserveRatioUpdated(address indexed caller, uint256 nom, uint256 denom);
```

### calculateNewReserveRatio

Calculates how much to decrease the reserve ratio for `_token` by the `reserveRatioDailyExpansion`.

| Parameter name | Annotation                                            |
| -------------- | ----------------------------------------------------- |
| \_token        | The reserve token to calculate the reserve ratio for. |

Returns: the new reserve ratio.

```
function calculateNewReserveRatio(ERC20 _token) public view returns (uint32);
```

### buyReturn

Calculates the buy return in G$ according to the given `_tokenAmount`.

| Parameter name | Annotation                               |
| -------------- | ---------------------------------------- |
| \_token        | The reserve token buying with.           |
| \_tokenAmount  | The amount of reserve token buying with. |

Returns: A number of G$ that should be given in exchange as calculated by the bonding curve.

```
function buyReturn(ERC20 _token, uint256 _tokenAmount);
```

### sellReturn

Calculates the sell return in `_token` according to the given `_gdAmount`.

| Parameter name | Annotation                               |
| -------------- | ---------------------------------------- |
| \_token        | The reserve token buying with.           |
| \_gdAmount     | The amount of reserve token buying with. |

Returns: A number of tokens that should be given in exchange as calculated by the bonding curve.

```
function sellReturn(ERC20 _token, uint256 _gdAmount);
```

### buy

Updates the `_token` bonding curve params. Emits `BalancesUpdated` with the new reserve token information.

| Parameter name | Annotation                               |
| -------------- | ---------------------------------------- |
| \_token        | The reserve token buying with.           |
| \_tokenAmount  | The amount of reserve token buying with. |

Returns: A number of G$ that will be given in exchange as calculated by the bonding curve.

```
function buy(ERC20 _token, uint256 _tokenAmount);
```

### sellWithContribution

Calculates the sell return with contribution in `_token` and update the bonding curve params. Emits `BalancesUpdated` with the new reserve token information.

| Parameter name         | Annotation                                                             |
| ---------------------- | ---------------------------------------------------------------------- |
| \_token                | The desired reserve token to have.                                     |
| \_gdAmount             | The amount of G$ that are sold.                                        |
| \_contributionGdAmount | The number of G$ tokens that will not be traded for the reserve token. |

Returns: A number of tokens that will be given in exchange as calculated by the bonding curve.

```
function sellWithContribution(
	ERC20 _token,
	uint256 _gdAmount,
	uint256 _contributionGdAmount
) public returns (uint256);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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.gooddollar.org/previous-protocol-versions/protocol-v3/core-contracts-and-api/goodmarketmaker.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.
