linera_sdk/abis/
fungible.rs1use std::collections::BTreeMap;
7
8use async_graphql::{InputObject, Request, Response, SimpleObject};
9use linera_base::{
10 abi::{ContractAbi, ServiceAbi},
11 data_types::Amount,
12 identifiers::{AccountOwner, ChainId},
13};
14use linera_sdk_derive::GraphQLMutationRootInCrate;
15use serde::{Deserialize, Serialize};
16
17pub struct FungibleTokenAbi;
19
20impl ContractAbi for FungibleTokenAbi {
21 type Operation = Operation;
22 type Response = FungibleResponse;
23}
24
25impl ServiceAbi for FungibleTokenAbi {
26 type Query = Request;
27 type QueryResponse = Response;
28}
29
30#[derive(Debug, Deserialize, Serialize, GraphQLMutationRootInCrate)]
32pub enum Operation {
33 Balance {
35 owner: AccountOwner,
37 },
38 TickerSymbol,
40 Transfer {
42 owner: AccountOwner,
44 amount: Amount,
46 target_account: Account,
48 },
49 Claim {
53 source_account: Account,
55 amount: Amount,
57 target_account: Account,
59 },
60}
61
62#[derive(Debug, Deserialize, Serialize, Default)]
64pub enum FungibleResponse {
65 #[default]
67 Ok,
68 Balance(Amount),
70 TickerSymbol(String),
72}
73
74#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
76pub struct InitialState {
77 pub accounts: BTreeMap<AccountOwner, Amount>,
79}
80
81#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
83pub struct Parameters {
84 pub ticker_symbol: String,
86}
87
88impl Parameters {
89 pub fn new(ticker_symbol: &str) -> Self {
91 let ticker_symbol = ticker_symbol.to_string();
92 Self { ticker_symbol }
93 }
94}
95
96#[derive(
98 Clone,
99 Copy,
100 Debug,
101 Deserialize,
102 Eq,
103 Ord,
104 PartialEq,
105 PartialOrd,
106 Serialize,
107 SimpleObject,
108 InputObject,
109)]
110#[graphql(input_name = "FungibleAccount")]
111pub struct Account {
112 pub chain_id: ChainId,
114 pub owner: AccountOwner,
116}
117
118#[derive(Debug, Default)]
120pub struct InitialStateBuilder {
121 account_balances: BTreeMap<AccountOwner, Amount>,
123}
124
125impl InitialStateBuilder {
126 pub fn with_account(mut self, account: AccountOwner, balance: impl Into<Amount>) -> Self {
128 self.account_balances.insert(account, balance.into());
129 self
130 }
131
132 pub fn build(&self) -> InitialState {
135 InitialState {
136 accounts: self.account_balances.clone(),
137 }
138 }
139}