In-depth analysis of the Sputnik DAO platform factory contract: design implementation and security mechanisms

robot
Abstract generation in progress

Rust Smart Contracts Development Diary (10-2): Sputnik DAO Factory Contract Analysis

This article will provide an in-depth interpretation of the design and implementation of the Sputnik DAO platform factory model (sputnikdao-factory).

1. Sputnik-DAO Factory Contract

Sputnik-DAO adopts a creation-based factory design pattern to achieve the unified creation and management of the decentralized autonomous organization (DAO) on the platform.

The source code repository for the corresponding contract is located at:

2. Introduction to DAPP Module Functions

The DAPP page of the Sputnik DAO platform displays the created DAO instance object (Sputnikdaov2 contract ). As of March 2022, the most active DAO on the platform is news.sputnik-dao.near, with 3051 proposals either voting or completed.

All DAO instance contracts created on the Sputnik DAO platform are uniformly deployed under its sub-accounts by the sputnik-dao.near account ( and the sputnikdao-factory contract ), for example:

  • pcp.sputnik-dao.near
  • test-dao-bro.sputnik-dao.near
  • blaqkstereo.sputnik-dao.near

Organizations can publicly call the create() method of the sputnikdao-factory smart contract in the NEAR mainnet to create new DAO instances.

3. Interpretation of sputnikdao-factory Contract Code

3.1 Create DAO

The contract status of sputnikdao-factory mainly consists of two parts:

rust pub struct SputnikDAOFactory { factory_manager: FactoryManager, daos: UnorderedSet, }

  • factory_manager: Provides methods to create/delete/update DAO instances.
  • daos: records the NEAR account address of the created DAO instance

create() method definition:

rust #[payable] pub fn create(&mut self, name: AccountId, args: Base64VecU8) { let account_id: AccountId = format!('{}.{}', name, env::current_account_id()) .parse() .unwrap(); let callback_args = serde_json::to_vec(\u0026json!({ 'account_id': account_id, 'attached_deposit': U128(env::attached_deposit()), 'predecessor_account_id': env::predecessor_account_id() })) .expect('Failed to serialize'); self.factory_manager.create_contract( self.get_default_code_hash(), account_id, 'new', &args.0, 'on_create', &callback_args, ); }

The specific implementation of factory_manager.create_contract:

rust pub fn create_contract( &self, code_hash: Base58CryptoHash, account_id: AccountId, new_method: &str, args: [u8], callback_method: &str, callback_args: &[u8], ) { // Load contract code // Create Promise // Create Account // Transfer // Deploy contract
// Call the initialization function // Set Callback }

on_create callback function:

rust #[private] pub fn on_create( &mut self, account_id: AccountId, attached_deposit: U128, predecessor_account_id: AccountId, bool { if near_sdk::is_promise_success)( { self.daos.insert)\u0026account_id(; true } else { Promise::new)predecessor_account_id(.transfer)attached_deposit.0(; false } }

![])https://img-cdn.gateio.im/webp-social/moments-8b462e3ac0fd55e990921d21d66e3d42.webp(

) 3.2 Update DAO

update###( method:

rust pub fn update)&self, account_id: AccountId, code_hash: Base58CryptoHash( { let caller_id = env::predecessor_account_id)(; assert!) caller_id == self.get_owner(( || caller_id == account_id, 'Must be updated by the factory owner or the DAO itself' ); assert!) self.daos.contains(&account_id(, 'Must be contract created by factory' ); self.factory_manager .update_contract)account_id, code_hash, 'update'(; }

![])https://img-cdn.gateio.im/webp-social/moments-a8d69d504693c5c14767aed9244a090a.webp(

4. Sputnik-DAO Factory Contract Security Analysis

Mainly ensure security from the following aspects:

  • Permission control: the view method does not modify state variables
  • The privileged function can only be executed by the owner.
  • Reasonable error handling mechanism

![])https://img-cdn.gateio.im/webp-social/moments-729b65b1335931b4645ff5a9981d125b.webp(

![])https://img-cdn.gateio.im/webp-social/moments-074c1aa0b7f9adfa5780a29c73654fc0.webp(

![])https://img-cdn.gateio.im/webp-social/moments-aebf6315f45abba98b7345259ffe8b5d.webp(

![])https://img-cdn.gateio.im/webp-social/moments-a3a84d139b8950498848a5cafbdfea83.webp(

![])https://img-cdn.gateio.im/webp-social/moments-46f1a89001b11b13b1a8d1b484d4877d.webp(

View Original
This page may contain third-party content, which is provided for information purposes only (not representations/warranties) and should not be considered as an endorsement of its views by Gate, nor as financial or professional advice. See Disclaimer for details.
  • Reward
  • 7
  • Share
Comment
0/400
AltcoinOraclevip
· 12h ago
degens sleeping on sputnik... their factory model is literally web3 governance 2.0
Reply0
not_your_keysvip
· 12h ago
This design is pretty hardcore, wow.
View OriginalReply0
ImpermanentPhilosophervip
· 12h ago
Brain-dead modular development, love it.
View OriginalReply0
PensionDestroyervip
· 12h ago
The factory contract looks reliable.
View OriginalReply0
ImaginaryWhalevip
· 12h ago
This contract has something special.
View OriginalReply0
InfraVibesvip
· 12h ago
Domestic products can also do this trap.
View OriginalReply0
BanklessAtHeartvip
· 12h ago
The security of factory contracts is indeed crucial.
View OriginalReply0
Trade Crypto Anywhere Anytime
qrCode
Scan to download Gate app
Community
English
  • 简体中文
  • English
  • Tiếng Việt
  • 繁體中文
  • Español
  • Русский
  • Français (Afrique)
  • Português (Portugal)
  • Bahasa Indonesia
  • 日本語
  • بالعربية
  • Українська
  • Português (Brasil)