Introducing gTrade v8: Diamond Refactor and Smart Contract Integration

Gains Network
Gains Network
Published in
6 min readMay 24, 2024

Hello gTraders!

This announcement is to share the exciting release of v8 — a major milestone in the growth & development of the gTrade protocol!

With v8, smart contracts are now able to interact directly with trading contracts, opening the door to a new set of use cases where other protocols integrate trading. Equally as important, protocol contracts were refactored for nearly endless scalability. This will allow for faster feature releases going forward. And don’t worry, trader UX was of course also improved!

Let’s get into it.

Trader UX Improvements

The following improvements have been made to the protocol. All traders immediately benefit from them:

Unlimited positions per pair

Traders can now open as many positions as they want on a single trading pair, providing more flexibility and control over their trading strategies. This is a significant improvement from the previous limit, which restricted the number of positions per pair to 3.

No max collateral limit

The maximum collateral limit has been removed, allowing traders to open as large of a position as they can afford, as long as it’s within the open interest limits of the market. This change provides traders with greater capital efficiency and the ability to take larger collateral positions when they see fit.

Single 1-Click Trading (1CT) delegation for all collaterals

Traders now only need to delegate 1CT once for all collaterals, simplifying the process and reducing the number of transactions required. This streamlines the user experience and makes it easier for traders to manage their trades.

Leverage settable up to 3 decimals

Leverage can now be set with precision up to 3 decimal places, giving traders more granular control over their risk management. This feature allows for finer position sizes, and unlocks more control over trade modifications in the near future with partials release.

Trades openable with ETH

Traders can now open trades directly with native tokens, such as ETH, which are automatically converted and wrapped in a single transaction. This eliminates the need for manual token conversions and simplifies the trading process, saving time and effort. This is coming to the frontend very soon!

~15% lower transaction fees (gas)

Transaction fees have been reduced by approximately 15% due to gas optimizations in the smart contracts. This means traders will incur lower costs when opening, closing, or managing their positions, resulting in improved profitability and capital efficiency.

These UX enhancements collectively provide a more seamless, flexible, and cost-effective trading experience for all users of the gTrade protocol.

Smart Contract Trading

Other smart contracts can now interact with the gTrade trading contracts. This opens the door for creating new products and strategies where trading functions are called within other protocols’ functions.

What does that mean? Before, only Externally Owned Accounts (EOA) were able to interact with the protocol, meaning transactions submitted to the blockchain had to directly call gTrade functions. Now, other functions can include gTrade contracts within their call patterns.

While gTrade trades are not a classic DeFi primitive, the more protocols use them and the deeper they are within call stacks, the more they start to look like it.

Developers and potential partners are encouraged to explore the possibilities enabled by v8. Reach out in the Gains Network Discord to discuss collaborating. More than 5 projects are currently integrating with gTrade, stay tuned for details on these early partners.

Diamond Pattern Refactor

The Diamond Pattern is an architectural design pattern that includes:

1. Diamond Proxy Contract

  • Main contract for user and contract interaction
  • Delegates function calls to Facets using delegate call

2. Facets (Logic Contracts)

  • Contain the actual business logic
  • Each facet holds functions for a single part of the application logic

3. Diamond Storage

  • Shared storage structure to avoid collisions and maintain consistency
  • All facets write to the same storage, defined by the Diamond Contract

The v8 update’s Diamond pattern refactor brings:

Improved scalability by extending functionality with new facets

The Diamond Pattern allows for the seamless addition of new functionalities through the implementation of new facets. This means that the gTrade protocol can easily scale and introduce new features without the need for complex contract migrations or upgrades, ensuring a future-proof and adaptable architecture.

Cost-effectiveness by reducing the need for multiple large contracts

By segregating the protocol’s logic into smaller, focused facets, the Diamond Pattern reduces the need for deploying and maintaining multiple large contracts. This results in lower deployment and maintenance costs, as well as more efficient use of blockchain resources.

Enhanced upgradeability and modularity

The Diamond Pattern enables the gTrade protocol to be highly upgradeable and modular. Individual facets can be updated or replaced without affecting the entire system, allowing for seamless improvements and bugfixes. This modularity also facilitates the development and testing of new features in isolation before integrating them into the main protocol.

Optimized storage facet with cleaner code, safety checks, and improved getters

The storage facet has been optimized to provide a more robust and efficient storage solution. The code has been refactored for better readability and maintainability, while additional safety checks have been implemented to ensure data integrity. Moreover, improved getter functions have been introduced to facilitate easier and more secure access to stored data.

Cleaner interface separation for facet types, functions, events, and errors

The Diamond Pattern promotes a cleaner separation of concerns by organizing the protocol’s interface into distinct facet types, functions, events, and errors. This separation enhances the overall clarity and maintainability of the codebase, making it easier for developers to navigate and understand the protocol’s structure.

Clean pattern for adding and updating features with isolated code

The Diamond Pattern provides a clean and structured approach for adding and updating features within the gTrade protocol. New functionalities can be implemented as separate facets, allowing for isolated code development and testing. This isolation minimizes the risk of unintended side effects and makes it easier to manage and maintain the protocol’s codebase over time.

Simplified interaction model with a single contract across collaterals

The v8 update simplifies the user interaction model by consolidating all collateral-related operations into a single contract across collaterals. This means that users can interact with a single contract address for all their trading needs, streamlining the user experience and reducing the potential for confusion or errors.

Ongoing on-chain access to historical trades and orders

With the Diamond Pattern refactor, the gTrade protocol now maintains a complete on-chain history of all trades and orders. This data is stored in a gas-efficient manner and can be easily accessed by users, developers, and analytics platforms. Having a comprehensive and immutable record of trading activity enhances transparency, facilitates auditing, and enables the development of advanced trading tools and strategies.

Globally unique IDs for all trades and pending orders

The v8 update introduces globally unique identifiers (IDs) for all trades and pending orders within the gTrade protocol. These IDs are assigned in a deterministic and sequential manner, ensuring that each trade and order can be uniquely identified across the entire system. This feature simplifies the tracking and management of trades and orders, facilitates efficient indexing and querying, and enables seamless integration with external systems and tools.

The Diamond Pattern allows for more scalable and manageable functionality development, with logic distributed among different contracts accessible through a single interface.

Natspec Documentation

The v8 update introduces comprehensive Natspec documentation for the gTrade smart contracts, accessible at: https://gains-network.gitbook.io/docs-home/developer/technical-reference

Natspec provides human-readable descriptions of contract functions, parameters, and return values, serving as a valuable resource for developers. It promotes transparency, developer empowerment, and user accessibility.

The Natspec documentation helps developers understand the protocol’s functionality, facilitates the development of secure applications, and can be used to generate user-friendly interfaces and guides.

The Diamond Pattern and Natspec documentation in the v8 update lay a solid foundation for the future growth and development of the gTrade protocol, enabling it to adapt and evolve rapidly to meet the needs of the decentralized trading ecosystem.

Closing

The v8 release is a testament to the hard work and dedication put into continuously improving and evolving the gTrade protocol. With the introduction of smart contract trading, the refactored architecture for enhanced scalability, and the significant trader UX improvements, gTrade is well-positioned to accelerate its growth and cement its position as a leading perp dex.

There is excitement about the new opportunities and use cases that v8 enables, and the gTrade team looks forward to collaborating with developers and partners to push the boundaries of what’s possible in decentralized trading. As always, the commitment remains to provide users with the best possible trading experience and drive innovation in the DeFi space.

Thank you for the continued support, and happy trading!

--

--