# Grant Application: TWAP Orders

**URL:** <https://forum.cow.fi/t/grant-application-twap-orders/1347>\
**Category:** Funded Grants\
**Tags:** complete, grant-funded\
**Created:** [December 24, 2022, 10:53am UTC](https://forum.cow.fi/t/grant-application-twap-orders/1347 "2022-12-24T10:53:16Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfw78](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.cow.fi/mfw78/32/1598_2.png) [@mfw78](https://forum.cow.fi/u/mfw78)\
**Post date:** [December 24, 2022, 10:53am UTC](https://forum.cow.fi/t/grant-application-twap-orders/1347/1 "2022-12-24T10:53:16Z")

</div>

# TWAP Orders

**Grant Title** : Time-Weighted Average Price (TWAP) Orders

**Author** :

- GitHub: [mfw78 (mfw78) · GitHub](https://github.com/mfw78) (see also [rndlabs · GitHub](https://github.com/rndlabs)).
- Gitcoin: [https://gitcoin.co/mfw78](https://gitcoin.co/mfw78)

**About You** :

- Experienced in technical systems analysis, design and implementation with a strong emphasis on risk management / gap analysis.
- Experienced technical writer in mission-critical, highly regulated, and technical industries.
- Actively independently developing with emerging decentralised technologies, including Swarm and Waku.

**Additional Links** :

- safe-cow: [GitHub - rndlabs/safe-cow: Gasless EIP-1271 token swaps on Cowswap](https://github.com/rndlabs/safe-cow) - implementing EIP-1271 signature aggregation for _Safe_ trading via Cowswap API.

**Grant category** : Protocol Order Flow / Developer tools

**Grant Description** :

_Problem_: Currently DEX aggregators / AMM marketplaces only provide _market_ and _limit_ order types. Conversely, on a TradFi exchange, more advanced order types are available, providing utility to traders.

_Solution_: By making use of [EIP-1271 smart contract orders](https://docs.cow.fi/tutorials/how-to-place-erc-1271-smart-contract-orders), implement more advanced order types, with this grant specifically targetting [TWAP](https://en.wikipedia.org/wiki/Time-weighted_average_price) (Time-weighted average price) orders.

**Grant Goals and impact** :

- Increase CoW protocol utility through DeFi-market unique order types (e.g dollar-cost averaging by use of TWAP orders).
- Increase concentration of orders by timing, leading to increased likelihood of CoWs.

**Milestones** :

Basic TWAP:

Basic TWAP logic would be implemented as multiple _fixed size orders_, with _constant time spacing_, enforcing a user-specified floor / ceiling price (sell / buy) as appropriate. Deliverables include:

1. Implement a user-upgradeable proxy contract with TWAP logic. This would allow users to upgrade their deployed proxy’s implementation contract, yielding gas savings on token approvals, and contract deployments.
2. Implement a factory contract for user’s to deploy contracts from (1).
3. Comprehensive test suite for contracts.
4. Example script for submitting TWAP orders off-chain to Cow protocol.
5. Documentation for integrators / developers.

**Grant Timeline** :

Complete by end of January 2023.

_Note: The above timelines do not make for allowances / timing associated with audit completion._

**Funding Request** :

$10,000. Half paid up-front ($5,000), with the remainder paid upon satisfactory completion of code as determined by the member(s) of the CoW team.

**Budget Breakdown** :

- $10k: development / labor cost.

This budget does _not_ include any costs associated with audit by a 3rd party. It is expected that CoW would undertake the necessary steps / cost for audit. Any cost overruns associated with meeting the standards of an external audit (additional testing / code fixes) would be paid out of the development cost.

**Gnosis Chain Address (to receive the grant)**:

0x070E0a700E36D303a0Ce3fe37976dD70974D7883

**Other Information** : As an active member of the CowDAO Grants Committee, I hereby refrain from voting / signing on this proposal so as to eliminate conflicts of interest. In determining the timelines associated with this Grant, I have taken into consideration workloads due to my position as a Committee member, 3rd party work, and allowed appropriate buffer to ensure the Grant Committee’s continuance.

**Referral** : N/A

**Terms and conditions** :

By applying for this grant, I agree to be bound by the [CowDAO Participation Agreement](https://cloudflare-ipfs.com/ipfs/Qmf9MYhcG2pFrDoVy13p6FWeVF4nG9HbJvRfYYbhazTCFe) and the [COWDAO Grant Terms and Conditions](https://cloudflare-ipfs.com/ipfs/QmePuufRisDpGrnaPYJo7By6sFRaGhWcoP7zQhS5FiJaT7)

---

<div class="post-metadata">

**Author:** ![netrunner.eth](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.cow.fi/netrunner.eth/32/252_2.png) [@netrunner.eth](https://forum.cow.fi/u/netrunner.eth)\
**Post date:** [December 24, 2022, 11:13am UTC](https://forum.cow.fi/t/grant-application-twap-orders/1347/2 "2022-12-24T11:13:49Z")

</div>

The TWAP advanced order will provide utility to traders and add further value to the CoW Protocol. I am signalling my support for this proposal.

---

<div class="post-metadata">

**Author:** ![Chim9](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.cow.fi/chim9/32/417_2.png) [@Chim9](https://forum.cow.fi/u/Chim9)\
**Post date:** [December 24, 2022, 12:17pm UTC](https://forum.cow.fi/t/grant-application-twap-orders/1347/3 "2022-12-24T12:17:46Z")

</div>

Great proposal, I’m supporting it!

---

<div class="post-metadata">

**Author:** ![middleway.eth](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.cow.fi/middleway.eth/32/12_2.png) [@middleway.eth](https://forum.cow.fi/u/middleway.eth)\
**Post date:** [December 26, 2022, 7:54am UTC](https://forum.cow.fi/t/grant-application-twap-orders/1347/4 "2022-12-26T07:54:32Z")

</div>

Great proposal @mfw78 !  
I think that this first example of ERC1271 based smart order is of great value to CoW Protocol and its users. Adding unique and differentiating functionality while increasing the chances for CoW within TWAP orders.

As we already have another grant approved aimed to eventually work on this use case, (golang client) I’d love to see some thought around the interface between those grants.  
Are there any TWAP related functions that would be useful to expose in the CoW Protocol SDK?

I would like to propose approval of the first phase outlined above (ASAP), with a soft commitment to approve the second phase upon satisfactory completion of phase one 🤝

(In addition, would love to get more details about the need and functionality that keeper bots will add to advanced TWAP orders)

---

<div class="post-metadata">

**Author:** ![mfw78](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.cow.fi/mfw78/32/1598_2.png) [@mfw78](https://forum.cow.fi/u/mfw78)\
**Post date:** [December 26, 2022, 8:52am UTC](https://forum.cow.fi/t/grant-application-twap-orders/1347/5 "2022-12-26T08:52:21Z")

</div>

Hi all,  
Thanks for the positive feedback.

> [@middleway.eth](#):
>
> As we already have another grant approved aimed to eventually work on this use case, (golang client) I’d love to see some thought around the interface between those grants.  
> Are there any TWAP related functions that would be useful to expose in the CoW Protocol SDK?

From my understanding, currently the golang client is a utility / wrapper for Cow’s API, and serves as a basis from which golang developers can integrate with the Cow services backend (such as auction batch retrieval, price quoting etc). For Phase 1 of this proposal, I wouldn’t foresee any specific functionality to expose (as all the orders would be submitted to the Cow backend API by the TWAP order, and these would then be visible to users of the golang client by polling for the batch auctions).

> [@middleway.eth](#):
>
> (In addition, would love to get more details about the need and functionality that keeper bots will add to advanced TWAP orders)

The biggest issue that I foresee with TWAP orders in general is that by placing them in a public order book, this leaks information to the market, presenting opportunity for the market to move against tranches of the TWAP order. In order to minimise this impact, I can think of a couple of approaches:

1. TWAP orders are held privately (off of the Cowswap public API), and only submitted on a just-in-time basis. This would primarily be the value proposition for a keeper bot, such that the bot is then able to introduce some time randomness / variable order sizes, without leaking this information well in advance (this assumes a self-soverign bot).
2. In order to increase the risk to those wanting to front-run a tranche of TWAP orders, the Cowswap Protocol could be modified to include some form of trusted randomness. This could be done by the solvers on a commit / reveal basis, or using external randomness providers, such as Chainlink VRF (though Chainlink VRF isn’t available yet on Gnosis Chain). TWAP orders could then be broken down into epochs, with the randomness forming jitter / noise as to when the TWAP trades in that epoch become executable.

Use of bots for (1) would be the solution requiring the least modification of the existing Cowswap protocol and wouldn’t place additional trust assumptions on the Cowswap protocol.

---

<div class="post-metadata">

**Author:** ![middleway.eth](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.cow.fi/middleway.eth/32/12_2.png) [@middleway.eth](https://forum.cow.fi/u/middleway.eth)\
**Post date:** [December 27, 2022, 9:52am UTC](https://forum.cow.fi/t/grant-application-twap-orders/1347/6 "2022-12-27T09:52:39Z")

</div>

Thanks for explaining!

I think that this kind of front running that you want to avoid will usually be done programmatically, in which case orders can be decoded from the TWAP contracts and even posting orders “just in time”, makes them public in the CoW Swap order book for enough time to front run them.

so although I agree front running (non-atomic) is a general concern, I think it should be evaluated based on evidence of actual activity and be addressed on the Protocol level if anything.

to add to this, I assume keeper bots will add additional complexity and cost overhead.

Based on the above I’m not sure there’s justification for implementing it.

But there are definitely other improvements that should be considered once the basic implementation is done!

As I mentioned, I propose to approve phase 1 and consider the details of phase 2 later on as we gather more insights.

---

<div class="post-metadata">

**Author:** ![mfw78](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.cow.fi/mfw78/32/1598_2.png) [@mfw78](https://forum.cow.fi/u/mfw78)\
**Post date:** [December 27, 2022, 10:01am UTC](https://forum.cow.fi/t/grant-application-twap-orders/1347/7 "2022-12-27T10:01:33Z")

</div>

Sure, the above points are salient. It certainly was / is the intention to structure it in a multi-phase approach so insights can be integrated / built upon.

I’m OK with Phase 1 approval, and defer to the Committee with respect to any voting / approval process to be undertaken per CowDAO / GrantsDAO processes.

---

<div class="post-metadata">

**Author:** ![middleway.eth](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.cow.fi/middleway.eth/32/12_2.png) [@middleway.eth](https://forum.cow.fi/u/middleway.eth)\
**Post date:** [December 27, 2022, 10:36am UTC](https://forum.cow.fi/t/grant-application-twap-orders/1347/8 "2022-12-27T10:36:13Z")

</div>

Can you please amend the OP saying that phase two is provided for information purposes and will be revised and approved separately? 🙏

Then I think you can proceed to be the first to use the new transparent grant approval flow using [cowgrants.eth snapshot space](https://snapshot.org/#/cowgrants.eth) 🥳

---

<div class="post-metadata">

**Author:** ![mfw78](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.cow.fi/mfw78/32/1598_2.png) [@mfw78](https://forum.cow.fi/u/mfw78)\
**Post date:** [December 27, 2022, 11:00am UTC](https://forum.cow.fi/t/grant-application-twap-orders/1347/9 "2022-12-27T11:00:11Z")

</div>

For the sake of clarity and to avoid ambiguous language, I have narrowed the scope of the grant to basic TWAP orders. It is envisioned that insights gained from this Grants Proposal will inform a future grant to expand functionality. For reference, expanded functionality in the original grant proposal included:

> Phase two (advanced features):
> 
> 1. Advanced implementation, of which the contract from phase (1) is capable of upgrading to. This implementation would use a Merkle-tree root hash to store an arbitrary amount of orders (including different assets, order sizes, validity times, and prices), optionally storing tokens in `GPv2RelayerVault` for gas efficiency.
> 2. Expand test suite and documentation for advanced implementation.
> 3. Write a keeper bot, written in `rust`, that supports day of week, size, and price modifiers, reducing the duration that pricing information is available in the public market.
> 4. Package keeper bot in `docker` container for ease of deployment in cloud environments.

## Grants Committee Snapshot

Given that overall, the feedback for this seems to be positive, I have created a snapshot proposal that you may review and cast your votes accordingly.

URL: [`https://snapshot.org/#/cowgrants.eth/proposal/0xe8e214a79b2682d9949b0314768ea128e39859144ad904a34df6396a48f06066`](https://snapshot.org/#/cowgrants.eth/proposal/0xe8e214a79b2682d9949b0314768ea128e39859144ad904a34df6396a48f06066)

**NOTE:** Above snapshot recreated due to lack of quorum. Seems most are on holidays!

---

<div class="post-metadata">

**Author:** ![mfw78](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.cow.fi/mfw78/32/1598_2.png) [@mfw78](https://forum.cow.fi/u/mfw78)\
**Post date:** [January 31, 2023, 11:05am UTC](https://forum.cow.fi/t/grant-application-twap-orders/1347/10 "2023-01-31T11:05:07Z")

</div>

To all those that are following, the grant proposal was passed with quorum, work commenced and has now been completed. The repository for the work is available at [GitHub - rndlabs/cowswap-twap-orders: 🐄🐮 Time-weighted average price orders for CoW Protocol](https://github.com/rndlabs/cowswap-twap-orders).

---

<div class="post-metadata">

**Author:** ![mfw78](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.cow.fi/mfw78/32/1598_2.png) [@mfw78](https://forum.cow.fi/u/mfw78)\
**Post date:** [February 5, 2023, 6:40am UTC](https://forum.cow.fi/t/grant-application-twap-orders/1347/11 "2023-02-05T06:40:13Z")

</div>

Hi,  
Work on this Grant Application is complete. [Audits](https://github.com/rndlabs/cowswap-twap-orders/blob/main/audits/Group0_CowTwapOrdersJan2023.pdf) have been passed and the contract has been deployed on Ethereum Mainnet, Gnosis Chain, and Goerli at address `0x87b52ed635df746ca29651581b4d87517aaa9a9f`.

The [repository](https://github.com/rndlabs/cowswap-twap-orders) contains source code for:

1. Deployed contracts.
2. A command-line utility to interact with a Safe with the configured TWAP fallback handler, to create and cancel TWAPs.
3. A tenderly web3 actions watchtower to relay the orders autonomously to the CoW API.

In addition, the `README.md` contains extensive documentation as to the architecture and method of use.

For the purposes of this grant, I consider all milestones / mandate to be satisfied.

---

<div class="post-metadata">

**Author:** ![mfw78](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.cow.fi/mfw78/32/1598_2.png) [@mfw78](https://forum.cow.fi/u/mfw78)\
**Post date:** [February 24, 2025, 9:11am UTC](https://forum.cow.fi/t/grant-application-twap-orders/1347/12 "2025-02-24T09:11:13Z")

</div>

Closed on February 24, 2025, 9:11am UTC


