1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use async_trait::async_trait;
use codec::{Decode, Encode};
use substrate_subxt::balances::{Balances, BalancesEventsDecoder};
use substrate_subxt::system::{System, SystemEventsDecoder};
use substrate_subxt::{module, Call, Event, Runtime, SignedExtension, SignedExtra};
use sunshine_core::ChainClient;
use sunshine_identity_client::{Identity, IdentityEventsDecoder};

#[module]
pub trait Faucet: Identity + Balances + System {}

#[derive(Call, Clone, Debug, Eq, Encode, PartialEq)]
pub struct MintCall<'a, T: Faucet> {
    pub account: &'a <T as System>::AccountId,
}

#[derive(Clone, Debug, Decode, Eq, Event, PartialEq)]
pub struct MintedEvent<T: Faucet> {
    pub account: <T as System>::AccountId,
    pub amount: <T as Balances>::Balance,
}

#[async_trait]
pub trait FaucetClient<T: Runtime + Faucet>: ChainClient<T> {
    async fn mint(&self) -> Result<Option<MintedEvent<T>>, Self::Error>;
}

#[async_trait]
impl<T, C> FaucetClient<T> for C
where
    T: Runtime + Faucet,
    <<T::Extra as SignedExtra<T>>::Extra as SignedExtension>::AdditionalSigned: Send + Sync,
    C: ChainClient<T>,
{
    async fn mint(&self) -> Result<Option<MintedEvent<T>>, C::Error> {
        let account = self.chain_signer()?.account_id();
        let call = MintCall { account };
        let unsigned = self
            .chain_client()
            .create_unsigned(call, account, None)
            .await?;
        let decoder = self.chain_client().events_decoder::<MintCall<T>>();
        let event = self
            .chain_client()
            .submit_and_watch_extrinsic(unsigned, decoder)
            .await?
            .minted()?;
        Ok(event)
    }
}

#[cfg(test)]
mod tests {
    use test_client::faucet::FaucetClient;
    use test_client::mock::{test_node, AccountKeyring};
    use test_client::Client;

    #[async_std::test]
    async fn test_mint() {
        let (node, _node_tmp) = test_node();
        let (client, _client_tmp) = Client::mock(&node, AccountKeyring::Eve).await;
        client.mint().await.unwrap();
    }
}