use std::collections::BTreeMap;
use async_graphql::{InputObject, Request, Response, SimpleObject};
use linera_base::{
abi::{ContractAbi, ServiceAbi},
data_types::Amount,
identifiers::{AccountOwner, ChainId},
};
use linera_sdk_derive::GraphQLMutationRootInCrate;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize, GraphQLMutationRootInCrate)]
pub enum NativeFungibleOperation {
Balance {
owner: AccountOwner,
},
TickerSymbol,
Transfer {
owner: AccountOwner,
amount: Amount,
target_account: Account,
},
Claim {
source_account: Account,
amount: Amount,
target_account: Account,
},
}
pub struct NativeFungibleTokenAbi;
impl ContractAbi for NativeFungibleTokenAbi {
type Operation = NativeFungibleOperation;
type Response = FungibleResponse;
}
impl ServiceAbi for NativeFungibleTokenAbi {
type Query = Request;
type QueryResponse = Response;
}
#[derive(Debug, Deserialize, Serialize, GraphQLMutationRootInCrate)]
pub enum FungibleOperation {
Balance {
owner: AccountOwner,
},
TickerSymbol,
Approve {
owner: AccountOwner,
spender: AccountOwner,
allowance: Amount,
},
Transfer {
owner: AccountOwner,
amount: Amount,
target_account: Account,
},
TransferFrom {
owner: AccountOwner,
spender: AccountOwner,
amount: Amount,
target_account: Account,
},
Claim {
source_account: Account,
amount: Amount,
target_account: Account,
},
}
pub struct FungibleTokenAbi;
impl ContractAbi for FungibleTokenAbi {
type Operation = FungibleOperation;
type Response = FungibleResponse;
}
impl ServiceAbi for FungibleTokenAbi {
type Query = Request;
type QueryResponse = Response;
}
#[derive(Debug, Deserialize, Serialize, Default)]
pub enum FungibleResponse {
#[default]
Ok,
Balance(Amount),
TickerSymbol(String),
}
#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
pub struct InitialState {
pub accounts: BTreeMap<AccountOwner, Amount>,
}
#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
pub struct Parameters {
pub ticker_symbol: String,
}
impl Parameters {
pub fn new(ticker_symbol: &str) -> Self {
let ticker_symbol = ticker_symbol.to_string();
Self { ticker_symbol }
}
}
#[derive(
Clone,
Copy,
Debug,
Deserialize,
Eq,
Ord,
PartialEq,
PartialOrd,
Serialize,
SimpleObject,
InputObject,
)]
#[graphql(input_name = "FungibleAccount")]
pub struct Account {
pub chain_id: ChainId,
pub owner: AccountOwner,
}
#[derive(Debug, Default)]
pub struct InitialStateBuilder {
account_balances: BTreeMap<AccountOwner, Amount>,
}
impl InitialStateBuilder {
pub fn with_account(mut self, account: AccountOwner, balance: impl Into<Amount>) -> Self {
self.account_balances.insert(account, balance.into());
self
}
pub fn build(&self) -> InitialState {
InitialState {
accounts: self.account_balances.clone(),
}
}
}