Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
Support reward activity host
Browse files Browse the repository at this point in the history
  • Loading branch information
kikakkz committed Dec 8, 2023
1 parent 80023c7 commit 682badd
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 18 deletions.
12 changes: 12 additions & 0 deletions activity/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,17 @@ impl Activity {
Ok(())
}

async fn reward_activity_host(&mut self, activity_id: u64) -> Result<(), ActivityError> {
let call = foundation::ApplicationCall::Reward {
reward_user: None,
reward_type: foundation::RewardType::Activity,
activity_id: Some(activity_id),
};
self.call_application(true, Self::foundation_app_id()?, &call, vec![])
.await?;
Ok(())
}

async fn _finalize(&mut self, activity_id: u64) -> Result<(), ActivityError> {
self.finalize(activity_id).await?;
let activity = self.activity(activity_id).await?;
Expand Down Expand Up @@ -347,6 +358,7 @@ impl Activity {
)
.await?;
}
self.reward_activity_host(activity_id).await?;
Ok(())
}
}
1 change: 1 addition & 0 deletions activity/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ impl Activity {
}
activity.winners = winners;
activity.finalized = true;
self.activities.insert(&activity_id, activity)?;
Ok(())
}
}
1 change: 0 additions & 1 deletion feed/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ impl Feed {
let call = foundation::ApplicationCall::Reward {
reward_user: Some(author),
reward_type: foundation::RewardType::Publish,
amount: None,
activity_id: None,
};
self.call_application(true, Self::foundation_app_id()?, &call, vec![])
Expand Down
7 changes: 1 addition & 6 deletions foundation/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ impl Contract for Foundation {
Message::Reward {
reward_user,
reward_type,
amount,
activity_id,
} => {
let reward_user = match reward_type {
Expand All @@ -129,16 +128,14 @@ impl Contract for Foundation {
Some(user) => user,
None => return Err(ContractError::InvalidUser),
};
self.reward(_reward_user, reward_type, amount, activity_id)
.await?;
self.reward(_reward_user, reward_type, activity_id).await?;
let dest =
Destination::Subscribers(ChannelName::from(SUBSCRIPTION_CHANNEL.to_vec()));
Ok(ExecutionResult::default().with_authenticated_message(
dest,
Message::Reward {
reward_user,
reward_type,
amount,
activity_id,
},
))
Expand Down Expand Up @@ -207,14 +204,12 @@ impl Contract for Foundation {
ApplicationCall::Reward {
reward_user,
reward_type,
amount,
activity_id,
} => ExecutionResult::default().with_authenticated_message(
system_api::current_application_id().creation.chain_id,
Message::Reward {
reward_user,
reward_type,
amount,
activity_id,
},
),
Expand Down
5 changes: 1 addition & 4 deletions foundation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ pub enum ApplicationCall {
// Activity: sender must be the activity host
reward_user: Option<Owner>,
reward_type: RewardType,
// For activity we have amount, for other type amount is determined by foundation
amount: Option<Amount>,
activity_id: Option<u64>,
},
ActivityRewards {
Expand Down Expand Up @@ -104,10 +102,9 @@ pub enum Message {
// Review: sender is the reward user (the reviewer)
// Author: sender is not the reward user but the reviewer
// Activity: sender must be the activity host
// TODO: for activity host reward we should review it
reward_user: Option<Owner>,
reward_type: RewardType,
// For activity we have amount, for other type amount is determined by foundation
amount: Option<Amount>,
activity_id: Option<u64>,
},
ActivityRewards {
Expand Down
5 changes: 2 additions & 3 deletions foundation/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,14 @@ impl Foundation {
pub(crate) async fn reward_activity(
&mut self,
reward_user: Owner,
amount: Amount,
activity_id: u64,
) -> Result<(), StateError> {
// TODO: check who can reward activity here
let balance = match self.activity_lock_funds.get(&activity_id).await? {
Some(balance) => balance,
None => return Err(StateError::InsufficientBalance),
};
let amount = Amount::from_tokens(50);
if balance.le(&amount) {
return Err(StateError::InsufficientBalance);
}
Expand Down Expand Up @@ -207,12 +207,11 @@ impl Foundation {
&mut self,
reward_user: Owner,
reward_type: RewardType,
amount: Option<Amount>,
activity_id: Option<u64>,
) -> Result<(), StateError> {
match reward_type {
RewardType::Activity => {
self.reward_activity(reward_user, amount.unwrap(), activity_id.unwrap())
self.reward_activity(reward_user, activity_id.unwrap())
.await
}
RewardType::Publish => self.reward_author(reward_user).await,
Expand Down
1 change: 0 additions & 1 deletion review/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,6 @@ impl Review {
let call = foundation::ApplicationCall::Reward {
reward_user: None,
reward_type: foundation::RewardType::Review,
amount: None,
activity_id: None,
};
self.call_application(true, Self::foundation_app_id()?, &call, vec![])
Expand Down
29 changes: 26 additions & 3 deletions webui/src/components/ActivityContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,16 @@ import { useCollectionStore } from 'src/stores/collection'
import { useActivityStore, JoinType } from 'src/stores/activity'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from 'src/stores/user'
import { provideApolloClient, useMutation } from '@vue/apollo-composable'
import gql from 'graphql-tag'
import { getClientOptions } from 'src/apollo'
import { ApolloClient } from '@apollo/client/core'

import ActivityVote from './ActivityVote.vue'
import { targetChain } from 'src/stores/chain'

const options = /* await */ getClientOptions(/* {app, router ...} */)
const apolloClient = new ApolloClient(options)

const collection = useCollectionStore()
const splitter = ref(200)
Expand Down Expand Up @@ -231,7 +239,7 @@ const votable = () => {
}

const voteEnd = () => {
return (activity.value?.voteEndAt || 0) < Date.now()
return ((activity.value?.voteEndAt || 0) < Date.now() && !activity.value?.finalized) || true
}

const router = useRouter()
Expand All @@ -255,8 +263,23 @@ const onVoteClick = () => {
})
}

const onFinalizeClick = () => {
// TODO
const onFinalizeClick = async () => {
const { mutate, onDone, onError } = provideApolloClient(apolloClient)(() => useMutation(gql`
mutation finalize($activityId: Int!) {
finalize(activityId: $activityId)
}
`))
onDone(() => {
// TODO
})
onError((error) => {
console.log(error)
})
await mutate({
activityId: parseInt(activityId.value.toString()),
endpoint: 'activity',
chainId: targetChain.value
})
}

</script>
Expand Down

0 comments on commit 682badd

Please sign in to comment.