Skip to content

Commit

Permalink
added discussions accounts factory to community contract (#88)
Browse files Browse the repository at this point in the history
This issue is supporting PR
[#645](NEAR-DevHub/neardevhub-bos#645)

@frolvlad @ailisp  I would like to get some feedback on my approach.

I assumed that in order to create
`discussions.${handle}.community.devhub.near` the
`${handle}.community.devhub.near` account needs to be an factory in
itself.


I added this image help describe what I did with some common
terminology.
<img width="1072" alt="Screenshot 2024-01-23 at 17 31 22"
src="https://github.com/NEAR-DevHub/neardevhub-contract/assets/28901891/7b0b1145-0828-4e32-9a1d-cb0ba32354f9">

I added a `set_discussions_community_socialdb` function so the user can
post on socialdb from the devhub.near contract. This uses
`get_devhub_discussions_account` which retrieves
`discussions.<handle>.community.devhub.near`.

To support that I added a `create_discussions_account` function to the
community contract. So the community contract is in essence a
discussions factory. I copied most of what was the community contract to
the discussions contract with some minor tweaks since it is a level down
in subaccounts.

In file `community/src/lib.rs` line 9 and 70 a PUBkey from devhub.near
is used to give control to make posts on socialDB.
Should this be devhub.near?
  • Loading branch information
Tguntenaar authored Feb 8, 2024
1 parent e80dc29 commit e8e8afe
Show file tree
Hide file tree
Showing 16 changed files with 3,307 additions and 58 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
uses: actions/checkout@v2
- uses: Swatinem/rust-cache@v1
- run: rustup target add wasm32-unknown-unknown
- name: Build discussions contract
run: cd discussions && ./build.sh
- name: Build community contract
run: cd community && ./build.sh
- name: Build community factory contract
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
uses: actions/checkout@v2
- uses: Swatinem/rust-cache@v1
- run: rustup target add wasm32-unknown-unknown
- name: Build discussions contract
run: cd discussions && ./build.sh
- name: Build community contract
run: cd community && ./build.sh
- name: Build community factory contract
Expand Down
8 changes: 4 additions & 4 deletions community-factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use near_sdk::borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::{env, near_bindgen, require, AccountId, Gas, NearToken, Promise};

const CODE: &[u8] = include_bytes!("../../res/devhub_community.wasm");
const INITIAL_BALANCE: NearToken = NearToken::from_near(2);
const INITIAL_BALANCE: NearToken = NearToken::from_near(4);
const PUBKEY_STR: &str = "ed25519:4deBAvg1S4MF7qe9GBDJwDCGLyyXtJa73JnMXwyG9vsB";

#[near_bindgen]
Expand All @@ -23,8 +23,8 @@ impl Contract {
"Can only be called from parent contract"
);
require!(
env::attached_deposit() == INITIAL_BALANCE,
"Require 2 NEAR to create community account"
env::attached_deposit() >= INITIAL_BALANCE,
"Require 4 NEAR to create community account"
);

let community_account_id: AccountId =
Expand All @@ -40,7 +40,7 @@ impl Contract {
"new".to_string(),
b"{}".to_vec(),
NearToken::from_near(0),
Gas::from_tgas(20),
Gas::from_tgas(50),
)
}
}
32 changes: 28 additions & 4 deletions community/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
mod social_db;

use crate::social_db::social_db_contract;
use near_sdk;
use near_sdk::borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::Gas;
use near_sdk::{env, near_bindgen, require, AccountId, NearToken, Promise};

const CODE: &[u8] = include_bytes!("../../res/devhub_discussions.wasm");
const INITIAL_BALANCE: NearToken = NearToken::from_near(4);
const PUBKEY_STR: &str = "ed25519:4deBAvg1S4MF7qe9GBDJwDCGLyyXtJa73JnMXwyG9vsB";

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, Default)]
#[borsh(crate = "near_sdk::borsh")]
pub struct Contract {}

#[near_bindgen]
impl Contract {
#[init]
pub fn new() -> Self {
#[payable]
pub fn new(&mut self) -> Promise {
social_db_contract()
.with_unused_gas_weight(1)
.with_attached_deposit(NearToken::from_near(1))
Expand All @@ -21,7 +26,8 @@ impl Contract {
None,
vec![env::current_account_id().to_string()],
);
Contract {}

self.create_discussions_account()
}

pub fn destroy(&mut self) {
Expand All @@ -41,4 +47,22 @@ impl Contract {
.expect("Community factory should be deployed on a child account")
.into()
}

pub fn create_discussions_account(&mut self) -> Promise {
let account_id: AccountId =
format!("discussions.{}", env::current_account_id()).parse().unwrap();

let pubkey = PUBKEY_STR.parse().unwrap();
Promise::new(account_id)
.create_account()
.add_full_access_key(pubkey)
.transfer(NearToken::from_near(2))
.deploy_contract(CODE.to_vec())
.function_call(
"new".to_string(),
b"{}".to_vec(),
NearToken::from_near(0),
Gas::from_tgas(20),
)
}
}
Loading

0 comments on commit e8e8afe

Please sign in to comment.