linera_sdk/abis/wrapped_fungible.rs
1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! An ABI for applications that implement a wrapped (bridged) fungible token with Mint/Burn.
5
6use async_graphql::{Request, Response};
7pub use linera_base::identifiers::Account;
8use linera_base::{
9 abi::{ContractAbi, ServiceAbi},
10 data_types::Amount,
11 identifiers::{AccountOwner, ApplicationId, ChainId},
12};
13use linera_sdk_derive::GraphQLMutationRootInCrate;
14use serde::{Deserialize, Serialize};
15
16pub use super::fungible::{FungibleResponse, InitialState, InitialStateBuilder};
17
18/// Parameters for a wrapped fungible token backed by an EVM bridge.
19#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
20pub struct WrappedParameters {
21 /// Ticker symbol (e.g. "USDC")
22 pub ticker_symbol: String,
23 /// If set, only this account owner can sign mint operations
24 pub minter: Option<AccountOwner>,
25 /// If set, minting and auto-burning are restricted to this chain
26 pub mint_chain_id: Option<ChainId>,
27 /// The ERC-20 token address on the source EVM chain
28 pub evm_token_address: [u8; 20],
29 /// The EVM chain ID of the source chain (e.g. 8453 for Base)
30 pub evm_source_chain_id: u64,
31 /// If set, only this application can call Mint (via cross-app call)
32 pub bridge_app_id: Option<ApplicationId>,
33}
34
35/// Event emitted when tokens are auto-burned on the bridge chain.
36/// The relayer observes these on the "burns" stream and forwards
37/// to EVM to release the corresponding ERC-20 tokens.
38#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
39pub struct BurnEvent {
40 /// The Ethereum address to receive the unlocked ERC-20 tokens
41 pub target: [u8; 20],
42 /// Amount of tokens burned
43 pub amount: Amount,
44}
45
46/// Operations for the wrapped fungible token application.
47#[derive(Debug, Deserialize, Serialize, GraphQLMutationRootInCrate)]
48pub enum WrappedFungibleOperation {
49 /// Requests an account balance.
50 Balance {
51 /// Owner to query the balance for
52 owner: AccountOwner,
53 },
54 /// Requests this fungible token's ticker symbol.
55 TickerSymbol,
56 /// Approve the transfer of tokens
57 Approve {
58 /// Owner to transfer from
59 owner: AccountOwner,
60 /// The spender account
61 spender: AccountOwner,
62 /// Maximum amount to be transferred
63 allowance: Amount,
64 },
65 /// Transfers tokens from a (locally owned) account to a (possibly remote) account.
66 Transfer {
67 /// Owner to transfer from
68 owner: AccountOwner,
69 /// Amount to be transferred
70 amount: Amount,
71 /// Target account to transfer the amount to
72 target_account: Account,
73 },
74 /// Transfers tokens from a (locally owned) account to a (possibly remote) account by using the allowance.
75 TransferFrom {
76 /// Owner to transfer from
77 owner: AccountOwner,
78 /// The spender of the amount.
79 spender: AccountOwner,
80 /// Amount to be transferred
81 amount: Amount,
82 /// Target account to transfer the amount to
83 target_account: Account,
84 },
85 /// Same as `Transfer` but the source account may be remote. Depending on its
86 /// configuration, the target chain may take time or refuse to process
87 /// the message.
88 Claim {
89 /// Source account to claim amount from
90 source_account: Account,
91 /// Amount to be claimed
92 amount: Amount,
93 /// Target account to claim the amount into
94 target_account: Account,
95 },
96 /// Mints new tokens to a target account. Only the authorized minter can call this.
97 Mint {
98 /// Account to receive the minted tokens
99 target_account: Account,
100 /// Amount of tokens to mint
101 amount: Amount,
102 },
103 /// Burns tokens from an account. Rejected by the contract — burning happens
104 /// automatically when tokens are transferred to an Address20 on the bridge chain.
105 Burn {
106 /// Account owner whose tokens to burn
107 owner: AccountOwner,
108 /// Amount of tokens to burn
109 amount: Amount,
110 },
111}
112
113/// ABI for the wrapped fungible token application.
114pub struct WrappedFungibleTokenAbi;
115
116impl ContractAbi for WrappedFungibleTokenAbi {
117 type Operation = WrappedFungibleOperation;
118 type Response = FungibleResponse;
119}
120
121impl ServiceAbi for WrappedFungibleTokenAbi {
122 type Query = Request;
123 type QueryResponse = Response;
124}