@@ -12,10 +12,7 @@ use frame_support::{
12
12
dispatch:: { self , DispatchInfo , DispatchResult , DispatchResultWithPostInfo , PostDispatchInfo } ,
13
13
ensure,
14
14
pallet_macros:: import_section,
15
- traits:: {
16
- tokens:: { fungible, Fortitude , Preservation } ,
17
- IsSubType ,
18
- } ,
15
+ traits:: { tokens:: fungible, IsSubType } ,
19
16
} ;
20
17
21
18
use codec:: { Decode , Encode } ;
@@ -707,11 +704,17 @@ pub mod pallet {
707
704
#[ pallet:: type_value]
708
705
/// Default minimum stake.
709
706
/// 500k rao matches $0.25 at $500/TAO
710
- /// Also used as staking fee
711
707
pub fn DefaultMinStake < T : Config > ( ) -> u64 {
712
708
500_000
713
709
}
714
710
711
+ #[ pallet:: type_value]
712
+ /// Default staking fee.
713
+ /// 500k rao matches $0.25 at $500/TAO
714
+ pub fn DefaultStakingFee < T : Config > ( ) -> u64 {
715
+ 500_000
716
+ }
717
+
715
718
#[ pallet:: type_value]
716
719
/// Default unicode vector for tau symbol.
717
720
pub fn DefaultUnicodeVecU8 < T : Config > ( ) -> Vec < u8 > {
@@ -1554,13 +1557,25 @@ pub enum CallType {
1554
1557
pub enum CustomTransactionError {
1555
1558
ColdkeyInSwapSchedule ,
1556
1559
StakeAmountTooLow ,
1560
+ BalanceTooLow ,
1561
+ SubnetDoesntExist ,
1562
+ HotkeyAccountDoesntExist ,
1563
+ NotEnoughStakeToWithdraw ,
1564
+ RateLimitExceeded ,
1565
+ BadRequest ,
1557
1566
}
1558
1567
1559
1568
impl From < CustomTransactionError > for u8 {
1560
1569
fn from ( variant : CustomTransactionError ) -> u8 {
1561
1570
match variant {
1562
1571
CustomTransactionError :: ColdkeyInSwapSchedule => 0 ,
1563
1572
CustomTransactionError :: StakeAmountTooLow => 1 ,
1573
+ CustomTransactionError :: BalanceTooLow => 2 ,
1574
+ CustomTransactionError :: SubnetDoesntExist => 3 ,
1575
+ CustomTransactionError :: HotkeyAccountDoesntExist => 4 ,
1576
+ CustomTransactionError :: NotEnoughStakeToWithdraw => 5 ,
1577
+ CustomTransactionError :: RateLimitExceeded => 6 ,
1578
+ CustomTransactionError :: BadRequest => 255 ,
1564
1579
}
1565
1580
}
1566
1581
}
@@ -1603,6 +1618,41 @@ where
1603
1618
pub fn check_weights_min_stake ( who : & T :: AccountId , netuid : u16 ) -> bool {
1604
1619
Pallet :: < T > :: check_weights_min_stake ( who, netuid)
1605
1620
}
1621
+
1622
+ pub fn result_to_validity ( result : Result < ( ) , Error < T > > ) -> TransactionValidity {
1623
+ if let Err ( err) = result {
1624
+ match err {
1625
+ Error :: < T > :: AmountTooLow => Err ( InvalidTransaction :: Custom (
1626
+ CustomTransactionError :: StakeAmountTooLow . into ( ) ,
1627
+ )
1628
+ . into ( ) ) ,
1629
+ Error :: < T > :: SubnetNotExists => Err ( InvalidTransaction :: Custom (
1630
+ CustomTransactionError :: SubnetDoesntExist . into ( ) ,
1631
+ )
1632
+ . into ( ) ) ,
1633
+ Error :: < T > :: NotEnoughBalanceToStake => Err ( InvalidTransaction :: Custom (
1634
+ CustomTransactionError :: BalanceTooLow . into ( ) ,
1635
+ )
1636
+ . into ( ) ) ,
1637
+ Error :: < T > :: HotKeyAccountNotExists => Err ( InvalidTransaction :: Custom (
1638
+ CustomTransactionError :: HotkeyAccountDoesntExist . into ( ) ,
1639
+ )
1640
+ . into ( ) ) ,
1641
+ Error :: < T > :: NotEnoughStakeToWithdraw => Err ( InvalidTransaction :: Custom (
1642
+ CustomTransactionError :: NotEnoughStakeToWithdraw . into ( ) ,
1643
+ )
1644
+ . into ( ) ) ,
1645
+ _ => Err (
1646
+ InvalidTransaction :: Custom ( CustomTransactionError :: BadRequest . into ( ) ) . into ( ) ,
1647
+ ) ,
1648
+ }
1649
+ } else {
1650
+ Ok ( ValidTransaction {
1651
+ priority : Self :: get_priority_vanilla ( ) ,
1652
+ ..Default :: default ( )
1653
+ } )
1654
+ }
1655
+ }
1606
1656
}
1607
1657
1608
1658
impl < T : Config + Send + Sync + TypeInfo > sp_std:: fmt:: Debug for SubtensorSignedExtension < T > {
@@ -1647,7 +1697,10 @@ where
1647
1697
..Default :: default ( )
1648
1698
} )
1649
1699
} else {
1650
- Err ( InvalidTransaction :: Custom ( 1 ) . into ( ) )
1700
+ Err ( InvalidTransaction :: Custom (
1701
+ CustomTransactionError :: StakeAmountTooLow . into ( ) ,
1702
+ )
1703
+ . into ( ) )
1651
1704
}
1652
1705
}
1653
1706
Some ( Call :: reveal_weights { netuid, .. } ) => {
@@ -1659,7 +1712,10 @@ where
1659
1712
..Default :: default ( )
1660
1713
} )
1661
1714
} else {
1662
- Err ( InvalidTransaction :: Custom ( 2 ) . into ( ) )
1715
+ Err ( InvalidTransaction :: Custom (
1716
+ CustomTransactionError :: StakeAmountTooLow . into ( ) ,
1717
+ )
1718
+ . into ( ) )
1663
1719
}
1664
1720
}
1665
1721
Some ( Call :: batch_reveal_weights { netuid, .. } ) => {
@@ -1671,7 +1727,10 @@ where
1671
1727
..Default :: default ( )
1672
1728
} )
1673
1729
} else {
1674
- Err ( InvalidTransaction :: Custom ( 6 ) . into ( ) )
1730
+ Err ( InvalidTransaction :: Custom (
1731
+ CustomTransactionError :: StakeAmountTooLow . into ( ) ,
1732
+ )
1733
+ . into ( ) )
1675
1734
}
1676
1735
}
1677
1736
Some ( Call :: set_weights { netuid, .. } ) => {
@@ -1683,7 +1742,10 @@ where
1683
1742
..Default :: default ( )
1684
1743
} )
1685
1744
} else {
1686
- Err ( InvalidTransaction :: Custom ( 3 ) . into ( ) )
1745
+ Err ( InvalidTransaction :: Custom (
1746
+ CustomTransactionError :: StakeAmountTooLow . into ( ) ,
1747
+ )
1748
+ . into ( ) )
1687
1749
}
1688
1750
}
1689
1751
Some ( Call :: set_tao_weights { netuid, hotkey, .. } ) => {
@@ -1695,7 +1757,10 @@ where
1695
1757
..Default :: default ( )
1696
1758
} )
1697
1759
} else {
1698
- Err ( InvalidTransaction :: Custom ( 4 ) . into ( ) )
1760
+ Err ( InvalidTransaction :: Custom (
1761
+ CustomTransactionError :: StakeAmountTooLow . into ( ) ,
1762
+ )
1763
+ . into ( ) )
1699
1764
}
1700
1765
}
1701
1766
Some ( Call :: commit_crv3_weights { netuid, .. } ) => {
@@ -1707,38 +1772,91 @@ where
1707
1772
..Default :: default ( )
1708
1773
} )
1709
1774
} else {
1710
- Err ( InvalidTransaction :: Custom ( 7 ) . into ( ) )
1775
+ Err ( InvalidTransaction :: Custom (
1776
+ CustomTransactionError :: StakeAmountTooLow . into ( ) ,
1777
+ )
1778
+ . into ( ) )
1711
1779
}
1712
1780
}
1713
1781
Some ( Call :: add_stake {
1714
- hotkey : _ ,
1715
- netuid : _ ,
1782
+ hotkey,
1783
+ netuid,
1716
1784
amount_staked,
1717
1785
} ) => {
1718
- // Check that amount parameter is at least the min stake
1719
- // also check the coldkey balance
1720
- let coldkey_balance = <<T as Config >:: Currency as fungible:: Inspect <
1721
- <T as frame_system:: Config >:: AccountId ,
1722
- > >:: reducible_balance (
1723
- who, Preservation :: Expendable , Fortitude :: Polite
1724
- ) ;
1725
-
1726
- if ( * amount_staked < DefaultMinStake :: < T > :: get ( ) )
1727
- || ( coldkey_balance < DefaultMinStake :: < T > :: get ( ) )
1728
- {
1729
- InvalidTransaction :: Custom ( CustomTransactionError :: StakeAmountTooLow . into ( ) )
1730
- . into ( )
1731
- } else {
1732
- Ok ( ValidTransaction {
1733
- priority : Self :: get_priority_vanilla ( ) ,
1734
- ..Default :: default ( )
1735
- } )
1736
- }
1786
+ // Fully validate the user input
1787
+ Self :: result_to_validity ( Pallet :: < T > :: validate_add_stake (
1788
+ who,
1789
+ hotkey,
1790
+ * netuid,
1791
+ * amount_staked,
1792
+ ) )
1793
+ }
1794
+ Some ( Call :: remove_stake {
1795
+ hotkey,
1796
+ netuid,
1797
+ amount_unstaked,
1798
+ } ) => {
1799
+ // Fully validate the user input
1800
+ Self :: result_to_validity ( Pallet :: < T > :: validate_remove_stake (
1801
+ who,
1802
+ hotkey,
1803
+ * netuid,
1804
+ * amount_unstaked,
1805
+ ) )
1806
+ }
1807
+ Some ( Call :: move_stake {
1808
+ origin_hotkey,
1809
+ destination_hotkey,
1810
+ origin_netuid,
1811
+ destination_netuid,
1812
+ alpha_amount,
1813
+ } ) => {
1814
+ // Fully validate the user input
1815
+ Self :: result_to_validity ( Pallet :: < T > :: validate_stake_transition (
1816
+ who,
1817
+ who,
1818
+ origin_hotkey,
1819
+ destination_hotkey,
1820
+ * origin_netuid,
1821
+ * destination_netuid,
1822
+ * alpha_amount,
1823
+ ) )
1824
+ }
1825
+ Some ( Call :: transfer_stake {
1826
+ destination_coldkey,
1827
+ hotkey,
1828
+ origin_netuid,
1829
+ destination_netuid,
1830
+ alpha_amount,
1831
+ } ) => {
1832
+ // Fully validate the user input
1833
+ Self :: result_to_validity ( Pallet :: < T > :: validate_stake_transition (
1834
+ who,
1835
+ destination_coldkey,
1836
+ hotkey,
1837
+ hotkey,
1838
+ * origin_netuid,
1839
+ * destination_netuid,
1840
+ * alpha_amount,
1841
+ ) )
1842
+ }
1843
+ Some ( Call :: swap_stake {
1844
+ hotkey,
1845
+ origin_netuid,
1846
+ destination_netuid,
1847
+ alpha_amount,
1848
+ } ) => {
1849
+ // Fully validate the user input
1850
+ Self :: result_to_validity ( Pallet :: < T > :: validate_stake_transition (
1851
+ who,
1852
+ who,
1853
+ hotkey,
1854
+ hotkey,
1855
+ * origin_netuid,
1856
+ * destination_netuid,
1857
+ * alpha_amount,
1858
+ ) )
1737
1859
}
1738
- Some ( Call :: remove_stake { .. } ) => Ok ( ValidTransaction {
1739
- priority : Self :: get_priority_vanilla ( ) ,
1740
- ..Default :: default ( )
1741
- } ) ,
1742
1860
Some ( Call :: register { netuid, .. } | Call :: burned_register { netuid, .. } ) => {
1743
1861
let registrations_this_interval =
1744
1862
Pallet :: < T > :: get_registrations_this_interval ( * netuid) ;
@@ -1747,7 +1865,10 @@ where
1747
1865
if registrations_this_interval >= ( max_registrations_per_interval. saturating_mul ( 3 ) )
1748
1866
{
1749
1867
// If the registration limit for the interval is exceeded, reject the transaction
1750
- return Err ( InvalidTransaction :: Custom ( 5 ) . into ( ) ) ;
1868
+ return Err ( InvalidTransaction :: Custom (
1869
+ CustomTransactionError :: RateLimitExceeded . into ( ) ,
1870
+ )
1871
+ . into ( ) ) ;
1751
1872
}
1752
1873
Ok ( ValidTransaction {
1753
1874
priority : Self :: get_priority_vanilla ( ) ,
0 commit comments