@@ -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 } ;
@@ -1552,6 +1549,11 @@ pub enum CustomTransactionError {
1552
1549
ColdkeyInSwapSchedule ,
1553
1550
StakeAmountTooLow ,
1554
1551
BalanceTooLow ,
1552
+ SubnetDoesntExist ,
1553
+ HotkeyAccountDoesntExist ,
1554
+ NotEnoughStakeToWithdraw ,
1555
+ RateLimitExceeded ,
1556
+ BadRequest ,
1555
1557
}
1556
1558
1557
1559
impl From < CustomTransactionError > for u8 {
@@ -1560,6 +1562,11 @@ impl From<CustomTransactionError> for u8 {
1560
1562
CustomTransactionError :: ColdkeyInSwapSchedule => 0 ,
1561
1563
CustomTransactionError :: StakeAmountTooLow => 1 ,
1562
1564
CustomTransactionError :: BalanceTooLow => 2 ,
1565
+ CustomTransactionError :: SubnetDoesntExist => 3 ,
1566
+ CustomTransactionError :: HotkeyAccountDoesntExist => 4 ,
1567
+ CustomTransactionError :: NotEnoughStakeToWithdraw => 5 ,
1568
+ CustomTransactionError :: RateLimitExceeded => 6 ,
1569
+ CustomTransactionError :: BadRequest => 255 ,
1563
1570
}
1564
1571
}
1565
1572
}
@@ -1602,6 +1609,41 @@ where
1602
1609
pub fn check_weights_min_stake ( who : & T :: AccountId , netuid : u16 ) -> bool {
1603
1610
Pallet :: < T > :: check_weights_min_stake ( who, netuid)
1604
1611
}
1612
+
1613
+ pub fn result_to_validity ( result : Result < ( ) , Error < T > > ) -> TransactionValidity {
1614
+ if let Err ( err) = result {
1615
+ match err {
1616
+ Error :: < T > :: AmountTooLow => Err ( InvalidTransaction :: Custom (
1617
+ CustomTransactionError :: StakeAmountTooLow . into ( ) ,
1618
+ )
1619
+ . into ( ) ) ,
1620
+ Error :: < T > :: SubnetNotExists => Err ( InvalidTransaction :: Custom (
1621
+ CustomTransactionError :: SubnetDoesntExist . into ( ) ,
1622
+ )
1623
+ . into ( ) ) ,
1624
+ Error :: < T > :: NotEnoughBalanceToStake => Err ( InvalidTransaction :: Custom (
1625
+ CustomTransactionError :: BalanceTooLow . into ( ) ,
1626
+ )
1627
+ . into ( ) ) ,
1628
+ Error :: < T > :: HotKeyAccountNotExists => Err ( InvalidTransaction :: Custom (
1629
+ CustomTransactionError :: HotkeyAccountDoesntExist . into ( ) ,
1630
+ )
1631
+ . into ( ) ) ,
1632
+ Error :: < T > :: NotEnoughStakeToWithdraw => Err ( InvalidTransaction :: Custom (
1633
+ CustomTransactionError :: NotEnoughStakeToWithdraw . into ( ) ,
1634
+ )
1635
+ . into ( ) ) ,
1636
+ _ => Err (
1637
+ InvalidTransaction :: Custom ( CustomTransactionError :: BadRequest . into ( ) ) . into ( ) ,
1638
+ ) ,
1639
+ }
1640
+ } else {
1641
+ Ok ( ValidTransaction {
1642
+ priority : Self :: get_priority_vanilla ( ) ,
1643
+ ..Default :: default ( )
1644
+ } )
1645
+ }
1646
+ }
1605
1647
}
1606
1648
1607
1649
impl < T : Config + Send + Sync + TypeInfo > sp_std:: fmt:: Debug for SubtensorSignedExtension < T > {
@@ -1646,7 +1688,10 @@ where
1646
1688
..Default :: default ( )
1647
1689
} )
1648
1690
} else {
1649
- Err ( InvalidTransaction :: Custom ( 1 ) . into ( ) )
1691
+ Err ( InvalidTransaction :: Custom (
1692
+ CustomTransactionError :: StakeAmountTooLow . into ( ) ,
1693
+ )
1694
+ . into ( ) )
1650
1695
}
1651
1696
}
1652
1697
Some ( Call :: reveal_weights { netuid, .. } ) => {
@@ -1658,7 +1703,10 @@ where
1658
1703
..Default :: default ( )
1659
1704
} )
1660
1705
} else {
1661
- Err ( InvalidTransaction :: Custom ( 2 ) . into ( ) )
1706
+ Err ( InvalidTransaction :: Custom (
1707
+ CustomTransactionError :: StakeAmountTooLow . into ( ) ,
1708
+ )
1709
+ . into ( ) )
1662
1710
}
1663
1711
}
1664
1712
Some ( Call :: batch_reveal_weights { netuid, .. } ) => {
@@ -1670,7 +1718,10 @@ where
1670
1718
..Default :: default ( )
1671
1719
} )
1672
1720
} else {
1673
- Err ( InvalidTransaction :: Custom ( 6 ) . into ( ) )
1721
+ Err ( InvalidTransaction :: Custom (
1722
+ CustomTransactionError :: StakeAmountTooLow . into ( ) ,
1723
+ )
1724
+ . into ( ) )
1674
1725
}
1675
1726
}
1676
1727
Some ( Call :: set_weights { netuid, .. } ) => {
@@ -1682,7 +1733,10 @@ where
1682
1733
..Default :: default ( )
1683
1734
} )
1684
1735
} else {
1685
- Err ( InvalidTransaction :: Custom ( 3 ) . into ( ) )
1736
+ Err ( InvalidTransaction :: Custom (
1737
+ CustomTransactionError :: StakeAmountTooLow . into ( ) ,
1738
+ )
1739
+ . into ( ) )
1686
1740
}
1687
1741
}
1688
1742
Some ( Call :: set_tao_weights { netuid, hotkey, .. } ) => {
@@ -1694,7 +1748,10 @@ where
1694
1748
..Default :: default ( )
1695
1749
} )
1696
1750
} else {
1697
- Err ( InvalidTransaction :: Custom ( 4 ) . into ( ) )
1751
+ Err ( InvalidTransaction :: Custom (
1752
+ CustomTransactionError :: StakeAmountTooLow . into ( ) ,
1753
+ )
1754
+ . into ( ) )
1698
1755
}
1699
1756
}
1700
1757
Some ( Call :: commit_crv3_weights { netuid, .. } ) => {
@@ -1706,40 +1763,88 @@ where
1706
1763
..Default :: default ( )
1707
1764
} )
1708
1765
} else {
1709
- Err ( InvalidTransaction :: Custom ( 7 ) . into ( ) )
1766
+ Err ( InvalidTransaction :: Custom (
1767
+ CustomTransactionError :: StakeAmountTooLow . into ( ) ,
1768
+ )
1769
+ . into ( ) )
1710
1770
}
1711
1771
}
1712
1772
Some ( Call :: add_stake {
1713
- hotkey : _ ,
1714
- netuid : _ ,
1773
+ hotkey,
1774
+ netuid,
1715
1775
amount,
1716
1776
} ) => {
1717
- // Check that amount parameter is at least the min stake
1718
- // also check the coldkey balance
1719
- let coldkey_balance = <<T as Config >:: Currency as fungible:: Inspect <
1720
- <T as frame_system:: Config >:: AccountId ,
1721
- > >:: reducible_balance (
1722
- who, Preservation :: Expendable , Fortitude :: Polite
1723
- ) ;
1724
- let min_amount =
1725
- DefaultMinStake :: < T > :: get ( ) . saturating_add ( DefaultStakingFee :: < T > :: get ( ) ) ;
1726
-
1727
- if * amount < min_amount {
1728
- InvalidTransaction :: Custom ( CustomTransactionError :: StakeAmountTooLow . into ( ) )
1729
- . into ( )
1730
- } else if coldkey_balance < min_amount {
1731
- InvalidTransaction :: Custom ( CustomTransactionError :: BalanceTooLow . into ( ) ) . into ( )
1732
- } else {
1733
- Ok ( ValidTransaction {
1734
- priority : Self :: get_priority_vanilla ( ) ,
1735
- ..Default :: default ( )
1736
- } )
1737
- }
1777
+ // Fully validate the user input
1778
+ Self :: result_to_validity ( Pallet :: < T > :: validate_add_stake (
1779
+ who, hotkey, * netuid, * amount,
1780
+ ) )
1781
+ }
1782
+ Some ( Call :: remove_stake {
1783
+ hotkey,
1784
+ netuid,
1785
+ amount_unstaked,
1786
+ } ) => {
1787
+ // Fully validate the user input
1788
+ Self :: result_to_validity ( Pallet :: < T > :: validate_remove_stake (
1789
+ who,
1790
+ hotkey,
1791
+ * netuid,
1792
+ * amount_unstaked,
1793
+ ) )
1794
+ }
1795
+ Some ( Call :: move_stake {
1796
+ origin_hotkey,
1797
+ destination_hotkey,
1798
+ origin_netuid,
1799
+ destination_netuid,
1800
+ alpha_amount,
1801
+ } ) => {
1802
+ // Fully validate the user input
1803
+ Self :: result_to_validity ( Pallet :: < T > :: validate_stake_transition (
1804
+ who,
1805
+ who,
1806
+ origin_hotkey,
1807
+ destination_hotkey,
1808
+ * origin_netuid,
1809
+ * destination_netuid,
1810
+ * alpha_amount,
1811
+ ) )
1812
+ }
1813
+ Some ( Call :: transfer_stake {
1814
+ destination_coldkey,
1815
+ hotkey,
1816
+ origin_netuid,
1817
+ destination_netuid,
1818
+ alpha_amount,
1819
+ } ) => {
1820
+ // Fully validate the user input
1821
+ Self :: result_to_validity ( Pallet :: < T > :: validate_stake_transition (
1822
+ who,
1823
+ destination_coldkey,
1824
+ hotkey,
1825
+ hotkey,
1826
+ * origin_netuid,
1827
+ * destination_netuid,
1828
+ * alpha_amount,
1829
+ ) )
1830
+ }
1831
+ Some ( Call :: swap_stake {
1832
+ hotkey,
1833
+ origin_netuid,
1834
+ destination_netuid,
1835
+ alpha_amount,
1836
+ } ) => {
1837
+ // Fully validate the user input
1838
+ Self :: result_to_validity ( Pallet :: < T > :: validate_stake_transition (
1839
+ who,
1840
+ who,
1841
+ hotkey,
1842
+ hotkey,
1843
+ * origin_netuid,
1844
+ * destination_netuid,
1845
+ * alpha_amount,
1846
+ ) )
1738
1847
}
1739
- Some ( Call :: remove_stake { .. } ) => Ok ( ValidTransaction {
1740
- priority : Self :: get_priority_vanilla ( ) ,
1741
- ..Default :: default ( )
1742
- } ) ,
1743
1848
Some ( Call :: register { netuid, .. } | Call :: burned_register { netuid, .. } ) => {
1744
1849
let registrations_this_interval =
1745
1850
Pallet :: < T > :: get_registrations_this_interval ( * netuid) ;
@@ -1748,7 +1853,10 @@ where
1748
1853
if registrations_this_interval >= ( max_registrations_per_interval. saturating_mul ( 3 ) )
1749
1854
{
1750
1855
// If the registration limit for the interval is exceeded, reject the transaction
1751
- return Err ( InvalidTransaction :: Custom ( 5 ) . into ( ) ) ;
1856
+ return Err ( InvalidTransaction :: Custom (
1857
+ CustomTransactionError :: RateLimitExceeded . into ( ) ,
1858
+ )
1859
+ . into ( ) ) ;
1752
1860
}
1753
1861
Ok ( ValidTransaction {
1754
1862
priority : Self :: get_priority_vanilla ( ) ,
0 commit comments