Skip to content

Commit

Permalink
Discord identity optimizations
Browse files Browse the repository at this point in the history
Users can now set their own connections
  • Loading branch information
ismellike committed Dec 18, 2024
1 parent e0f5366 commit d08efc2
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 67 deletions.
153 changes: 115 additions & 38 deletions contracts/arena-discord-identity/schema/arena-discord-identity.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,38 @@
"type": "object",
"required": [
"addr",
"discord_id",
"username"
"discord_profile"
],
"properties": {
"addr": {
"type": "string"
},
"avatar_hash": {
"type": [
"string",
"null"
]
},
"discord_profile": {
"$ref": "#/definitions/DiscordProfile"
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"set_connections"
],
"properties": {
"set_connections": {
"type": "object",
"required": [
"connections"
],
"properties": {
"connections": {
"type": [
"array",
"null"
],
"type": "array",
"items": {
"$ref": "#/definitions/DiscordConnection"
}
},
"discord_id": {
"$ref": "#/definitions/Uint64"
},
"username": {
"type": "string"
}
},
"additionalProperties": false
Expand Down Expand Up @@ -222,6 +227,29 @@
},
"additionalProperties": false
},
"DiscordProfile": {
"type": "object",
"required": [
"user_id",
"username"
],
"properties": {
"avatar_hash": {
"type": [
"string",
"null"
]
},
"user_id": {
"$ref": "#/definitions/Uint64"
},
"username": {
"description": "The discord username",
"type": "string"
}
},
"additionalProperties": false
},
"Expiration": {
"description": "Expiration represents a point in time when some event happens. It can compare with a BlockInfo and will return is_expired() == true once the condition is hit (and for every block in the future)",
"oneOf": [
Expand Down Expand Up @@ -294,10 +322,10 @@
{
"type": "object",
"required": [
"user_id"
"discord_profile"
],
"properties": {
"user_id": {
"discord_profile": {
"type": "object",
"required": [
"addr"
Expand Down Expand Up @@ -333,6 +361,27 @@
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"discord_connections"
],
"properties": {
"discord_connections": {
"type": "object",
"required": [
"addr"
],
"properties": {
"addr": {
"type": "string"
}
},
"additionalProperties": false
}
},
"additionalProperties": false
},
{
"description": "Query the contract's ownership information",
"type": "object",
Expand Down Expand Up @@ -390,6 +439,52 @@
}
}
},
"discord_connections": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Array_of_DiscordConnection",
"type": "array",
"items": {
"$ref": "#/definitions/DiscordConnection"
},
"definitions": {
"DiscordConnection": {
"type": "object",
"required": [
"key",
"username"
],
"properties": {
"key": {
"description": "The type of service connection",
"type": "string"
},
"username": {
"description": "The service's connection username",
"type": "string"
}
},
"additionalProperties": false
}
}
},
"discord_profile": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Nullable_Uint64",
"anyOf": [
{
"$ref": "#/definitions/Uint64"
},
{
"type": "null"
}
],
"definitions": {
"Uint64": {
"description": "A thin wrapper around u64 that is using strings for JSON encoding/decoding, such that the full u64 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.\n\n# Examples\n\nUse `from` to create instances of this and `u64` to get the value out:\n\n``` # use cosmwasm_std::Uint64; let a = Uint64::from(42u64); assert_eq!(a.u64(), 42);\n\nlet b = Uint64::from(70u32); assert_eq!(b.u64(), 70); ```",
"type": "string"
}
}
},
"ownership": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Ownership_for_String",
Expand Down Expand Up @@ -484,24 +579,6 @@
"type": "string"
}
}
},
"user_id": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Nullable_Uint64",
"anyOf": [
{
"$ref": "#/definitions/Uint64"
},
{
"type": "null"
}
],
"definitions": {
"Uint64": {
"description": "A thin wrapper around u64 that is using strings for JSON encoding/decoding, such that the full u64 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.\n\n# Examples\n\nUse `from` to create instances of this and `u64` to get the value out:\n\n``` # use cosmwasm_std::Uint64; let a = Uint64::from(42u64); assert_eq!(a.u64(), 42);\n\nlet b = Uint64::from(70u32); assert_eq!(b.u64(), 70); ```",
"type": "string"
}
}
}
}
}
44 changes: 24 additions & 20 deletions contracts/arena-discord-identity/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use cw2::{ensure_from_older_version, set_contract_version};
use cw_ownable::assert_owner;

use crate::{
msg::{DiscordProfile, ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg},
state::{discord_identity, FAUCET_AMOUNT},
msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg},
state::{discord_identity, DISCORD_CONNECTIONS, FAUCET_AMOUNT},
ContractError,
};

Expand Down Expand Up @@ -44,17 +44,21 @@ pub fn execute(
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
if info.sender != env.contract.address {
if !matches!(msg, ExecuteMsg::SetConnections { .. }) && info.sender != env.contract.address {
assert_owner(deps.storage, &info.sender)?;
}

match msg {
ExecuteMsg::SetConnections { connections } => {
let discord_profile = discord_identity().load(deps.storage, &info.sender)?;

DISCORD_CONNECTIONS.save(deps.storage, discord_profile.user_id.u64(), &connections)?;

Ok(Response::new().add_attribute("action", "set_connections"))
}
ExecuteMsg::SetProfile {
addr,
discord_id,
username,
avatar_hash,
connections,
discord_profile,
} => {
let discord_identity = discord_identity();
let user = deps.api.addr_validate(&addr)?;
Expand All @@ -63,7 +67,7 @@ pub fn execute(
&& discord_identity
.idx
.discord_id
.prefix(discord_id.u64())
.prefix(discord_profile.user_id.u64())
.range(deps.storage, None, None, Order::Descending)
.collect::<StdResult<Vec<_>>>()?
.is_empty()
Expand All @@ -84,22 +88,13 @@ pub fn execute(
}
}

discord_identity.save(
deps.storage,
&user,
&DiscordProfile {
user_id: discord_id,
username,
avatar_hash,
connections,
},
)?;
discord_identity.save(deps.storage, &user, &discord_profile)?;

Ok(Response::new()
.add_messages(msgs)
.add_attribute("action", "set_profile")
.add_attribute("address", user)
.add_attribute("discord_id", discord_id.to_string()))
.add_attribute("discord_id", discord_profile.user_id.to_string()))
}
ExecuteMsg::SetFaucetAmount { amount } => {
FAUCET_AMOUNT.save(deps.storage, &amount)?;
Expand Down Expand Up @@ -130,7 +125,7 @@ pub fn execute(
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::UserId { addr } => {
QueryMsg::DiscordProfile { addr } => {
let addr = deps.api.addr_validate(&addr)?;

to_json_binary(&discord_identity().may_load(deps.storage, &addr)?)
Expand All @@ -143,6 +138,15 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
.keys(deps.storage, None, None, Order::Descending)
.collect::<StdResult<Vec<_>>>()?,
),
QueryMsg::DiscordConnections { addr } => {
let addr = deps.api.addr_validate(&addr)?;
let discord_id = discord_identity().load(deps.storage, &addr)?.user_id;
to_json_binary(
&DISCORD_CONNECTIONS
.may_load(deps.storage, discord_id.u64())?
.unwrap_or_default(),
)
}
QueryMsg::Ownership {} => to_json_binary(&cw_ownable::get_ownership(deps.storage)?),
}
}
Expand Down
13 changes: 7 additions & 6 deletions contracts/arena-discord-identity/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ pub struct InstantiateMsg {
pub enum ExecuteMsg {
SetProfile {
addr: String,
discord_id: Uint64,
username: String,
avatar_hash: Option<String>,
connections: Option<Vec<DiscordConnection>>,
discord_profile: DiscordProfile,
},
SetConnections {
connections: Vec<DiscordConnection>,
},
SetFaucetAmount {
amount: Coin,
Expand All @@ -29,9 +29,11 @@ pub enum ExecuteMsg {
#[derive(QueryResponses, cw_orch::QueryFns)]
pub enum QueryMsg {
#[returns(Option<Uint64>)]
UserId { addr: String },
DiscordProfile { addr: String },
#[returns(Vec<cosmwasm_std::Addr>)]
ConnectedWallets { discord_id: Uint64 },
#[returns(Vec<DiscordConnection>)]
DiscordConnections { addr: String },
}

#[cw_serde]
Expand All @@ -45,7 +47,6 @@ pub struct DiscordProfile {
/// The discord username
pub username: String,
pub avatar_hash: Option<String>,
pub connections: Option<Vec<DiscordConnection>>,
}

#[cw_serde]
Expand Down
5 changes: 3 additions & 2 deletions contracts/arena-discord-identity/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_std::{Addr, Coin};
use cw_storage_plus::{Index, IndexList, IndexedMap, Item, MultiIndex};
use cw_storage_plus::{Index, IndexList, IndexedMap, Item, Map, MultiIndex};

use crate::msg::DiscordProfile;
use crate::msg::{DiscordConnection, DiscordProfile};

pub struct DiscordProfileIndexes<'a> {
pub discord_id: MultiIndex<'a, u64, DiscordProfile, &'a Addr>,
Expand All @@ -26,4 +26,5 @@ pub fn discord_identity<'a>() -> IndexedMap<'a, &'a Addr, DiscordProfile, Discor
IndexedMap::new("discord_identity", indexes)
}

pub const DISCORD_CONNECTIONS: Map<u64, Vec<DiscordConnection>> = Map::new("discord_connections");
pub const FAUCET_AMOUNT: Item<Coin> = Item::new("faucet_amount");
2 changes: 1 addition & 1 deletion scripts/state.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"code_ids": {
"arena_competition_enrollment": 2654,
"arena_core": 2217,
"arena_discord_identity": 2759,
"arena_discord_identity": 2819,
"arena_escrow": 2216,
"arena_group": 2224,
"arena_league_module": 2219,
Expand Down

0 comments on commit d08efc2

Please sign in to comment.