Skip to content

Commit dda8976

Browse files
committed
also make near social work on both mainnet and testnet
1 parent 7834bd9 commit dda8976

File tree

6 files changed

+36
-22
lines changed

6 files changed

+36
-22
lines changed

community/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod social_db;
22

3-
use crate::social_db::{ext_social_db, SOCIAL_DB};
3+
use crate::social_db::social_db_contract;
44
use near_sdk::borsh::{BorshDeserialize, BorshSerialize};
55
use near_sdk::{env, near_bindgen, require, AccountId, NearToken, Promise};
66

@@ -13,7 +13,7 @@ pub struct Contract {}
1313
impl Contract {
1414
#[init]
1515
pub fn new() -> Self {
16-
ext_social_db::ext(SOCIAL_DB.into())
16+
social_db_contract()
1717
.with_unused_gas_weight(1)
1818
.with_attached_deposit(NearToken::from_near(1))
1919
.grant_write_permission(

community/src/social_db.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use near_sdk::serde_json::Value;
2-
use near_sdk::{ext_contract, AccountId, PublicKey};
3-
4-
pub const SOCIAL_DB: &near_sdk::AccountIdRef = near_sdk::AccountIdRef::new_or_panic("social.near");
2+
use near_sdk::{ext_contract, AccountId, PublicKey, env};
53

64
#[ext_contract(ext_social_db)]
75
pub trait SocialDB {
@@ -13,3 +11,12 @@ pub trait SocialDB {
1311
keys: Vec<String>,
1412
);
1513
}
14+
15+
pub fn social_db_contract() -> ext_social_db::SocialDBExt {
16+
let social_db: AccountId = if env::current_account_id().to_string().ends_with("testnet") {
17+
"v1.social08.testnet"
18+
} else {
19+
"social.near"
20+
}.parse().unwrap();
21+
ext_social_db::ext(social_db)
22+
}

src/lib.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::access_control::AccessControl;
1515
use community::*;
1616
use post::*;
1717

18-
use crate::social_db::{ext_social_db, SOCIAL_DB};
18+
use crate::social_db::social_db_contract;
1919
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
2020
use near_sdk::collections::{LookupMap, UnorderedMap, Vector};
2121
use near_sdk::require;
@@ -527,7 +527,7 @@ impl Contract {
527527
require!(community.handle == handle, "Community handle cannot be changed");
528528
require!(env::prepaid_gas() >= UPDATE_COMMUNITY_GAS, "Require at least 30 Tgas");
529529
self.communities.insert(&handle, &community);
530-
ext_social_db::ext(SOCIAL_DB.parse().unwrap()).with_unused_gas_weight(1).set(json!({
530+
social_db_contract().with_unused_gas_weight(1).set(json!({
531531
get_devhub_community_account(&community.handle)
532532
: {
533533
"profile": {
@@ -546,9 +546,9 @@ impl Contract {
546546
.expect("Only community admins and hub moderators can set community Social DB");
547547

548548
require!(env::prepaid_gas() >= ADD_COMMUNITY_ANNOUNCEMENT_GAS, "Require at least 30 Tgas");
549-
ext_social_db::ext(SOCIAL_DB.parse().unwrap())
549+
social_db_contract()
550550
.with_unused_gas_weight(1)
551-
.set(json!({ get_devhub_community_account(&handle) : data }));
551+
.set(json!({ get_devhub_community_account(&handle): data }));
552552
}
553553

554554
pub fn delete_community(&mut self, handle: CommunityHandle) {
@@ -564,11 +564,9 @@ impl Contract {
564564
self.communities.remove(&community.handle);
565565

566566
require!(env::prepaid_gas() >= DELETE_COMMUNITY_GAS, "Require at least 30 Tgas");
567-
ext_devhub_community::ext(
568-
get_devhub_community_account(&community.handle).parse().unwrap()
569-
)
570-
.with_unused_gas_weight(1)
571-
.destroy();
567+
ext_devhub_community::ext(get_devhub_community_account(&community.handle).parse().unwrap())
568+
.with_unused_gas_weight(1)
569+
.destroy();
572570
}
573571

574572
pub fn set_featured_communities(&mut self, handles: Vec<CommunityHandle>) {

src/notify.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::social_db::{ext_social_db, SOCIAL_DB};
1+
use crate::social_db::social_db_contract;
22
use crate::PostId;
33
use near_sdk::serde_json::json;
44
use near_sdk::{env, AccountId, Promise};
@@ -44,7 +44,7 @@ pub fn notify_mentions(text: &str, post_id: PostId) {
4444
}));
4545
}
4646

47-
ext_social_db::ext(SOCIAL_DB.parse().unwrap())
47+
social_db_contract()
4848
.with_static_gas(env::prepaid_gas() / 4)
4949
.with_attached_deposit(env::attached_deposit())
5050
.set(json!({
@@ -69,7 +69,7 @@ pub fn notify_edit(post_id: PostId, post_author: AccountId) -> Promise {
6969
}
7070

7171
fn notify(post_id: PostId, post_author: AccountId, action: &str) -> Promise {
72-
ext_social_db::ext(SOCIAL_DB.parse().unwrap())
72+
social_db_contract()
7373
.with_static_gas(env::prepaid_gas() / 4)
7474
.with_attached_deposit(env::attached_deposit())
7575
.set(json!({

src/repost.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::post::{get_post_description, Post, PostBody};
2-
use crate::social_db::{ext_social_db, SOCIAL_DB};
2+
use crate::social_db::social_db_contract;
33
use near_sdk::serde_json::json;
44
use near_sdk::{env, AccountId, Promise};
55

@@ -47,7 +47,7 @@ fn repost_internal(post: Post, contract_address: AccountId) -> near_sdk::serde_j
4747
}
4848

4949
pub fn repost(post: Post) -> Promise {
50-
ext_social_db::ext(SOCIAL_DB.parse().unwrap())
50+
social_db_contract()
5151
.with_static_gas(env::prepaid_gas() / 3)
5252
.with_attached_deposit(env::attached_deposit())
5353
.set(repost_internal(post, env::current_account_id()))

src/social_db.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1-
use near_sdk::ext_contract;
1+
use near_sdk::{ext_contract, AccountId, env};
22
use near_sdk::serde_json::Value;
33

4-
pub const SOCIAL_DB: &str = "social.near";
5-
64
#[ext_contract(ext_social_db)]
75
pub trait SocialDB {
86
fn set(&mut self, data: Value);
97
}
8+
9+
pub fn social_db_contract() -> ext_social_db::SocialDBExt {
10+
let social_db: AccountId = if env::current_account_id().to_string().ends_with("testnet") {
11+
"v1.social08.testnet"
12+
} else {
13+
"social.near"
14+
}
15+
.parse()
16+
.unwrap();
17+
ext_social_db::ext(social_db)
18+
}

0 commit comments

Comments
 (0)