From 501f2e58cba6aa6a01de1a79f79d0a4853f148c5 Mon Sep 17 00:00:00 2001 From: Moody Salem Date: Thu, 16 May 2024 19:33:57 -0400 Subject: [PATCH] add metadata to the fungible staked token --- src/fungible_staked_token.cairo | 36 ++++++++++++++++++++++++++-- src/fungible_staked_token_test.cairo | 8 ++++--- src/interfaces/erc20.cairo | 8 +++++++ src/test/test_token.cairo | 7 +++++- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/src/fungible_staked_token.cairo b/src/fungible_staked_token.cairo index af9f770..c5ba3a2 100644 --- a/src/fungible_staked_token.cairo +++ b/src/fungible_staked_token.cairo @@ -29,7 +29,10 @@ pub mod FungibleStakedToken { use core::num::traits::zero::{Zero}; use core::option::{OptionTrait}; use core::traits::{Into, TryInto}; - use governance::interfaces::erc20::{IERC20, IERC20Dispatcher, IERC20DispatcherTrait}; + use governance::interfaces::erc20::{ + IERC20, IERC20Metadata, IERC20MetadataDispatcher, IERC20MetadataDispatcherTrait, + IERC20Dispatcher, IERC20DispatcherTrait + }; use governance::staker::{IStakerDispatcher, IStakerDispatcherTrait}; use starknet::{ get_caller_address, get_contract_address, get_block_timestamp, @@ -43,11 +46,18 @@ pub mod FungibleStakedToken { delegated_to: LegacyMap, balances: LegacyMap, allowances: LegacyMap<(ContractAddress, ContractAddress), u128>, + total_supply: u128, + name: felt252, + symbol: felt252, } #[constructor] - fn constructor(ref self: ContractState, staker: IStakerDispatcher) { + fn constructor( + ref self: ContractState, staker: IStakerDispatcher, name: felt252, symbol: felt252 + ) { self.staker.write(staker); + self.name.write(name); + self.symbol.write(symbol); } #[derive(starknet::Event, PartialEq, Debug, Drop)] @@ -106,8 +116,28 @@ pub mod FungibleStakedToken { } } + #[abi(embed_v0)] + impl FungibleStakedTokenERC20Metadata of IERC20Metadata { + fn name(self: @ContractState) -> felt252 { + self.name.read() + } + fn symbol(self: @ContractState) -> felt252 { + self.symbol.read() + } + fn decimals(self: @ContractState) -> u8 { + IERC20MetadataDispatcher { + contract_address: IStakerDispatcher { contract_address: self.get_staker() } + .get_token() + } + .decimals() + } + } + #[abi(embed_v0)] impl FungibleStakedTokenERC20 of IERC20 { + fn totalSupply(self: @ContractState) -> u256 { + self.total_supply.read().into() + } fn balanceOf(self: @ContractState, account: ContractAddress) -> u256 { self.balances.read(account).into() } @@ -198,6 +228,7 @@ pub mod FungibleStakedToken { staker.stake(self.delegated_to.read(caller)); self.balances.write(caller, self.balances.read(caller) + amount); + self.total_supply.write(self.total_supply.read() + amount); } fn deposit(ref self: ContractState) { @@ -214,6 +245,7 @@ pub mod FungibleStakedToken { let caller = get_caller_address(); self.balances.write(caller, self.balances.read(caller) - amount); + self.total_supply.write(self.total_supply.read() - amount); self.staker.read().withdraw_amount(self.delegated_to.read(caller), caller, amount); } diff --git a/src/fungible_staked_token_test.cairo b/src/fungible_staked_token_test.cairo index 062c8d4..b10d655 100644 --- a/src/fungible_staked_token_test.cairo +++ b/src/fungible_staked_token_test.cairo @@ -24,9 +24,11 @@ use starknet::{ }; -fn deploy(staker: IStakerDispatcher) -> IFungibleStakedTokenDispatcher { +fn deploy( + staker: IStakerDispatcher, name: felt252, symbol: felt252 +) -> IFungibleStakedTokenDispatcher { let mut constructor_args: Array = ArrayTrait::new(); - Serde::serialize(@(staker), ref constructor_args); + Serde::serialize(@(staker, name, symbol), ref constructor_args); let (address, _) = deploy_syscall( FungibleStakedToken::TEST_CLASS_HASH.try_into().unwrap(), 0, constructor_args.span(), true @@ -37,7 +39,7 @@ fn deploy(staker: IStakerDispatcher) -> IFungibleStakedTokenDispatcher { fn setup() -> (IStakerDispatcher, IERC20Dispatcher, IFungibleStakedTokenDispatcher) { let (staker, token) = setup_staker(1000000); - let fungible_staked_token = deploy(staker); + let fungible_staked_token = deploy(staker, 'Staked Token', 'vSTT'); (staker, token, fungible_staked_token) } diff --git a/src/interfaces/erc20.cairo b/src/interfaces/erc20.cairo index 34cdc22..ab7f16c 100644 --- a/src/interfaces/erc20.cairo +++ b/src/interfaces/erc20.cairo @@ -2,6 +2,7 @@ use starknet::{ContractAddress}; #[starknet::interface] pub(crate) trait IERC20 { + fn totalSupply(self: @TContractState) -> u256; fn balanceOf(self: @TContractState, account: ContractAddress) -> u256; fn allowance(self: @TContractState, owner: ContractAddress, spender: ContractAddress) -> u256; fn transfer(ref self: TContractState, recipient: ContractAddress, amount: u256) -> bool; @@ -10,3 +11,10 @@ pub(crate) trait IERC20 { ) -> bool; fn approve(ref self: TContractState, spender: ContractAddress, amount: u256) -> bool; } + +#[starknet::interface] +pub(crate) trait IERC20Metadata { + fn name(self: @TContractState) -> felt252; + fn symbol(self: @TContractState) -> felt252; + fn decimals(self: @TContractState) -> u8; +} diff --git a/src/test/test_token.cairo b/src/test/test_token.cairo index 888d82a..6cd0476 100644 --- a/src/test/test_token.cairo +++ b/src/test/test_token.cairo @@ -11,6 +11,7 @@ pub(crate) mod TestToken { struct Storage { balances: LegacyMap, allowances: LegacyMap<(ContractAddress, ContractAddress), u256>, + total_supply: u256, } #[derive(starknet::Event, PartialEq, Debug, Drop)] @@ -29,11 +30,15 @@ pub(crate) mod TestToken { #[constructor] fn constructor(ref self: ContractState, recipient: ContractAddress, amount: u256) { self.balances.write(recipient, amount); - self.emit(Transfer { from: Zero::zero(), to: recipient, value: amount }) + self.emit(Transfer { from: Zero::zero(), to: recipient, value: amount }); + self.total_supply.write(amount); } #[abi(embed_v0)] impl IERC20Impl of IERC20 { + fn totalSupply(self: @ContractState) -> u256 { + self.total_supply.read() + } fn balanceOf(self: @ContractState, account: ContractAddress) -> u256 { self.balances.read(account).into() }