github.com/hashgraph/hedera-sdk-go/v2@v2.48.0/examples/precompile_example/FeeHelper.sol (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  pragma solidity >=0.5.0 <0.9.0;
     3  pragma experimental ABIEncoderV2;
     4  
     5  // This file was copied from github.com/hashgraph/hedera-smart-contracts on Sep 27 2023
     6  
     7  import "./IHederaTokenService.sol";
     8  
     9  abstract contract FeeHelper {
    10      function createFixedHbarFee(int64 amount, address feeCollector)
    11          internal
    12          pure
    13          returns (IHederaTokenService.FixedFee memory fixedFee)
    14      {
    15          fixedFee.amount = amount;
    16          fixedFee.useHbarsForPayment = true;
    17          fixedFee.feeCollector = feeCollector;
    18      }
    19  
    20      function createFixedTokenFee(
    21          int64 amount,
    22          address tokenId,
    23          address feeCollector
    24      ) internal pure returns (IHederaTokenService.FixedFee memory fixedFee) {
    25          fixedFee.amount = amount;
    26          fixedFee.tokenId = tokenId;
    27          fixedFee.feeCollector = feeCollector;
    28      }
    29  
    30      function createFixedSelfDenominatedFee(int64 amount, address feeCollector)
    31          internal
    32          pure
    33          returns (IHederaTokenService.FixedFee memory fixedFee)
    34      {
    35          fixedFee.amount = amount;
    36          fixedFee.useCurrentTokenForPayment = true;
    37          fixedFee.feeCollector = feeCollector;
    38      }
    39  
    40      function createFractionalFee(
    41          int64 numerator,
    42          int64 denominator,
    43          bool netOfTransfers,
    44          address feeCollector
    45      )
    46          internal
    47          pure
    48          returns (IHederaTokenService.FractionalFee memory fractionalFee)
    49      {
    50          fractionalFee.numerator = numerator;
    51          fractionalFee.denominator = denominator;
    52          fractionalFee.netOfTransfers = netOfTransfers;
    53          fractionalFee.feeCollector = feeCollector;
    54      }
    55  
    56      function createFractionalFeeWithMinAndMax(
    57          int64 numerator,
    58          int64 denominator,
    59          int64 minimumAmount,
    60          int64 maximumAmount,
    61          bool netOfTransfers,
    62          address feeCollector
    63      )
    64          internal
    65          pure
    66          returns (IHederaTokenService.FractionalFee memory fractionalFee)
    67      {
    68          fractionalFee.numerator = numerator;
    69          fractionalFee.denominator = denominator;
    70          fractionalFee.minimumAmount = minimumAmount;
    71          fractionalFee.maximumAmount = maximumAmount;
    72          fractionalFee.netOfTransfers = netOfTransfers;
    73          fractionalFee.feeCollector = feeCollector;
    74      }
    75  
    76      function createFractionalFeeWithLimits(
    77          int64 numerator,
    78          int64 denominator,
    79          int64 minimumAmount,
    80          int64 maximumAmount,
    81          bool netOfTransfers,
    82          address feeCollector
    83      )
    84          internal
    85          pure
    86          returns (IHederaTokenService.FractionalFee memory fractionalFee)
    87      {
    88          fractionalFee.numerator = numerator;
    89          fractionalFee.denominator = denominator;
    90          fractionalFee.minimumAmount = minimumAmount;
    91          fractionalFee.maximumAmount = maximumAmount;
    92          fractionalFee.netOfTransfers = netOfTransfers;
    93          fractionalFee.feeCollector = feeCollector;
    94      }
    95  
    96      function createRoyaltyFeeWithoutFallback(
    97          int64 numerator,
    98          int64 denominator,
    99          address feeCollector
   100      ) internal pure returns (IHederaTokenService.RoyaltyFee memory royaltyFee) {
   101          royaltyFee.numerator = numerator;
   102          royaltyFee.denominator = denominator;
   103          royaltyFee.feeCollector = feeCollector;
   104      }
   105  
   106      function createRoyaltyFeeWithHbarFallbackFee(
   107          int64 numerator,
   108          int64 denominator,
   109          int64 amount,
   110          address feeCollector
   111      ) internal pure returns (IHederaTokenService.RoyaltyFee memory royaltyFee) {
   112          royaltyFee.numerator = numerator;
   113          royaltyFee.denominator = denominator;
   114          royaltyFee.amount = amount;
   115          royaltyFee.useHbarsForPayment = true;
   116          royaltyFee.feeCollector = feeCollector;
   117      }
   118  
   119      function createRoyaltyFeeWithTokenDenominatedFallbackFee(
   120          int64 numerator,
   121          int64 denominator,
   122          int64 amount,
   123          address tokenId,
   124          address feeCollector
   125      ) internal pure returns (IHederaTokenService.RoyaltyFee memory royaltyFee) {
   126          royaltyFee.numerator = numerator;
   127          royaltyFee.denominator = denominator;
   128          royaltyFee.amount = amount;
   129          royaltyFee.tokenId = tokenId;
   130          royaltyFee.feeCollector = feeCollector;
   131      }
   132  
   133      function createNAmountFixedFeesForHbars(
   134          uint8 numberOfFees,
   135          int64 amount,
   136          address feeCollector
   137      ) internal pure returns (IHederaTokenService.FixedFee[] memory fixedFees) {
   138          fixedFees = new IHederaTokenService.FixedFee[](numberOfFees);
   139  
   140          for (uint8 i = 0; i < numberOfFees; i++) {
   141              IHederaTokenService.FixedFee
   142                  memory fixedFee = createFixedFeeForHbars(amount, feeCollector);
   143              fixedFees[i] = fixedFee;
   144          }
   145      }
   146  
   147      function createSingleFixedFeeForToken(
   148          int64 amount,
   149          address tokenId,
   150          address feeCollector
   151      ) internal pure returns (IHederaTokenService.FixedFee[] memory fixedFees) {
   152          fixedFees = new IHederaTokenService.FixedFee[](1);
   153          IHederaTokenService.FixedFee memory fixedFee = createFixedFeeForToken(
   154              amount,
   155              tokenId,
   156              feeCollector
   157          );
   158          fixedFees[0] = fixedFee;
   159      }
   160  
   161      function createFixedFeesForToken(
   162          int64 amount,
   163          address tokenId,
   164          address firstFeeCollector,
   165          address secondFeeCollector
   166      ) internal pure returns (IHederaTokenService.FixedFee[] memory fixedFees) {
   167          fixedFees = new IHederaTokenService.FixedFee[](1);
   168          IHederaTokenService.FixedFee memory fixedFee1 = createFixedFeeForToken(
   169              amount,
   170              tokenId,
   171              firstFeeCollector
   172          );
   173          IHederaTokenService.FixedFee memory fixedFee2 = createFixedFeeForToken(
   174              2 * amount,
   175              tokenId,
   176              secondFeeCollector
   177          );
   178          fixedFees[0] = fixedFee1;
   179          fixedFees[0] = fixedFee2;
   180      }
   181  
   182      function createSingleFixedFeeForHbars(int64 amount, address feeCollector)
   183          internal
   184          pure
   185          returns (IHederaTokenService.FixedFee[] memory fixedFees)
   186      {
   187          fixedFees = new IHederaTokenService.FixedFee[](1);
   188          IHederaTokenService.FixedFee memory fixedFee = createFixedFeeForHbars(
   189              amount,
   190              feeCollector
   191          );
   192          fixedFees[0] = fixedFee;
   193      }
   194  
   195      function createSingleFixedFeeForCurrentToken(
   196          int64 amount,
   197          address feeCollector
   198      ) internal pure returns (IHederaTokenService.FixedFee[] memory fixedFees) {
   199          fixedFees = new IHederaTokenService.FixedFee[](1);
   200          IHederaTokenService.FixedFee
   201              memory fixedFee = createFixedFeeForCurrentToken(
   202                  amount,
   203                  feeCollector
   204              );
   205          fixedFees[0] = fixedFee;
   206      }
   207  
   208      function createSingleFixedFeeWithInvalidFlags(
   209          int64 amount,
   210          address feeCollector
   211      ) internal pure returns (IHederaTokenService.FixedFee[] memory fixedFees) {
   212          fixedFees = new IHederaTokenService.FixedFee[](1);
   213          IHederaTokenService.FixedFee
   214              memory fixedFee = createFixedFeeWithInvalidFlags(
   215                  amount,
   216                  feeCollector
   217              );
   218          fixedFees[0] = fixedFee;
   219      }
   220  
   221      function createSingleFixedFeeWithTokenIdAndHbars(
   222          int64 amount,
   223          address tokenId,
   224          address feeCollector
   225      ) internal pure returns (IHederaTokenService.FixedFee[] memory fixedFees) {
   226          fixedFees = new IHederaTokenService.FixedFee[](1);
   227          IHederaTokenService.FixedFee
   228              memory fixedFee = createFixedFeeWithTokenIdAndHbars(
   229                  amount,
   230                  tokenId,
   231                  feeCollector
   232              );
   233          fixedFees[0] = fixedFee;
   234      }
   235  
   236      function createFixedFeesWithAllTypes(
   237          int64 amount,
   238          address tokenId,
   239          address feeCollector
   240      ) internal pure returns (IHederaTokenService.FixedFee[] memory fixedFees) {
   241          fixedFees = new IHederaTokenService.FixedFee[](3);
   242          IHederaTokenService.FixedFee
   243              memory fixedFeeForToken = createFixedFeeForToken(
   244                  amount,
   245                  tokenId,
   246                  feeCollector
   247              );
   248          IHederaTokenService.FixedFee
   249              memory fixedFeeForHbars = createFixedFeeForHbars(
   250                  amount * 2,
   251                  feeCollector
   252              );
   253          IHederaTokenService.FixedFee
   254              memory fixedFeeForCurrentToken = createFixedFeeForCurrentToken(
   255                  amount * 4,
   256                  feeCollector
   257              );
   258          fixedFees[0] = fixedFeeForToken;
   259          fixedFees[1] = fixedFeeForHbars;
   260          fixedFees[2] = fixedFeeForCurrentToken;
   261      }
   262  
   263      function createFixedFeeForToken(
   264          int64 amount,
   265          address tokenId,
   266          address feeCollector
   267      ) internal pure returns (IHederaTokenService.FixedFee memory fixedFee) {
   268          fixedFee.amount = amount;
   269          fixedFee.tokenId = tokenId;
   270          fixedFee.feeCollector = feeCollector;
   271      }
   272  
   273      function createFixedFeeForHbars(int64 amount, address feeCollector)
   274          internal
   275          pure
   276          returns (IHederaTokenService.FixedFee memory fixedFee)
   277      {
   278          fixedFee.amount = amount;
   279          fixedFee.useHbarsForPayment = true;
   280          fixedFee.feeCollector = feeCollector;
   281      }
   282  
   283      function createFixedFeeForCurrentToken(int64 amount, address feeCollector)
   284          internal
   285          pure
   286          returns (IHederaTokenService.FixedFee memory fixedFee)
   287      {
   288          fixedFee.amount = amount;
   289          fixedFee.useCurrentTokenForPayment = true;
   290          fixedFee.feeCollector = feeCollector;
   291      }
   292  
   293      //Used for negative scenarios
   294      function createFixedFeeWithInvalidFlags(int64 amount, address feeCollector)
   295          internal
   296          pure
   297          returns (IHederaTokenService.FixedFee memory fixedFee)
   298      {
   299          fixedFee.amount = amount;
   300          fixedFee.useHbarsForPayment = true;
   301          fixedFee.useCurrentTokenForPayment = true;
   302          fixedFee.feeCollector = feeCollector;
   303      }
   304  
   305      //Used for negative scenarios
   306      function createFixedFeeWithTokenIdAndHbars(
   307          int64 amount,
   308          address tokenId,
   309          address feeCollector
   310      ) internal pure returns (IHederaTokenService.FixedFee memory fixedFee) {
   311          fixedFee.amount = amount;
   312          fixedFee.tokenId = tokenId;
   313          fixedFee.useHbarsForPayment = true;
   314          fixedFee.feeCollector = feeCollector;
   315      }
   316  
   317      function getEmptyFixedFees()
   318          internal
   319          pure
   320          returns (IHederaTokenService.FixedFee[] memory fixedFees)
   321      {}
   322  
   323      function createNAmountFractionalFees(
   324          uint8 numberOfFees,
   325          int64 numerator,
   326          int64 denominator,
   327          bool netOfTransfers,
   328          address feeCollector
   329      )
   330          internal
   331          pure
   332          returns (IHederaTokenService.FractionalFee[] memory fractionalFees)
   333      {
   334          fractionalFees = new IHederaTokenService.FractionalFee[](numberOfFees);
   335  
   336          for (uint8 i = 0; i < numberOfFees; i++) {
   337              IHederaTokenService.FractionalFee
   338                  memory fractionalFee = createFractionalFee(
   339                      numerator,
   340                      denominator,
   341                      netOfTransfers,
   342                      feeCollector
   343                  );
   344              fractionalFees[i] = fractionalFee;
   345          }
   346      }
   347  
   348      function createSingleFractionalFee(
   349          int64 numerator,
   350          int64 denominator,
   351          bool netOfTransfers,
   352          address feeCollector
   353      )
   354          internal
   355          pure
   356          returns (IHederaTokenService.FractionalFee[] memory fractionalFees)
   357      {
   358          fractionalFees = new IHederaTokenService.FractionalFee[](1);
   359          IHederaTokenService.FractionalFee
   360              memory fractionalFee = createFractionalFee(
   361                  numerator,
   362                  denominator,
   363                  netOfTransfers,
   364                  feeCollector
   365              );
   366          fractionalFees[0] = fractionalFee;
   367      }
   368  
   369      function createSingleFractionalFeeWithLimits(
   370          int64 numerator,
   371          int64 denominator,
   372          int64 minimumAmount,
   373          int64 maximumAmount,
   374          bool netOfTransfers,
   375          address feeCollector
   376      )
   377          internal
   378          pure
   379          returns (IHederaTokenService.FractionalFee[] memory fractionalFees)
   380      {
   381          fractionalFees = new IHederaTokenService.FractionalFee[](1);
   382          IHederaTokenService.FractionalFee
   383              memory fractionalFee = createFractionalFeeWithLimits(
   384                  numerator,
   385                  denominator,
   386                  minimumAmount,
   387                  maximumAmount,
   388                  netOfTransfers,
   389                  feeCollector
   390              );
   391          fractionalFees[0] = fractionalFee;
   392      }
   393  
   394      function getEmptyFractionalFees()
   395          internal
   396          pure
   397          returns (IHederaTokenService.FractionalFee[] memory fractionalFees)
   398      {
   399          fractionalFees = new IHederaTokenService.FractionalFee[](0);
   400      }
   401  
   402      function createNAmountRoyaltyFees(
   403          uint8 numberOfFees,
   404          int64 numerator,
   405          int64 denominator,
   406          address feeCollector
   407      )
   408          internal
   409          pure
   410          returns (IHederaTokenService.RoyaltyFee[] memory royaltyFees)
   411      {
   412          royaltyFees = new IHederaTokenService.RoyaltyFee[](numberOfFees);
   413  
   414          for (uint8 i = 0; i < numberOfFees; i++) {
   415              IHederaTokenService.RoyaltyFee memory royaltyFee = createRoyaltyFee(
   416                  numerator,
   417                  denominator,
   418                  feeCollector
   419              );
   420              royaltyFees[i] = royaltyFee;
   421          }
   422      }
   423  
   424      function getEmptyRoyaltyFees()
   425          internal
   426          pure
   427          returns (IHederaTokenService.RoyaltyFee[] memory royaltyFees)
   428      {
   429          royaltyFees = new IHederaTokenService.RoyaltyFee[](0);
   430      }
   431  
   432      function createSingleRoyaltyFee(
   433          int64 numerator,
   434          int64 denominator,
   435          address feeCollector
   436      )
   437          internal
   438          pure
   439          returns (IHederaTokenService.RoyaltyFee[] memory royaltyFees)
   440      {
   441          royaltyFees = new IHederaTokenService.RoyaltyFee[](1);
   442  
   443          IHederaTokenService.RoyaltyFee memory royaltyFee = createRoyaltyFee(
   444              numerator,
   445              denominator,
   446              feeCollector
   447          );
   448          royaltyFees[0] = royaltyFee;
   449      }
   450  
   451      function createSingleRoyaltyFeeWithFallbackFee(
   452          int64 numerator,
   453          int64 denominator,
   454          int64 amount,
   455          address tokenId,
   456          bool useHbarsForPayment,
   457          address feeCollector
   458      )
   459          internal
   460          pure
   461          returns (IHederaTokenService.RoyaltyFee[] memory royaltyFees)
   462      {
   463          royaltyFees = new IHederaTokenService.RoyaltyFee[](1);
   464  
   465          IHederaTokenService.RoyaltyFee
   466              memory royaltyFee = createRoyaltyFeeWithFallbackFee(
   467                  numerator,
   468                  denominator,
   469                  amount,
   470                  tokenId,
   471                  useHbarsForPayment,
   472                  feeCollector
   473              );
   474          royaltyFees[0] = royaltyFee;
   475      }
   476  
   477      function createRoyaltyFeesWithAllTypes(
   478          int64 numerator,
   479          int64 denominator,
   480          int64 amount,
   481          address tokenId,
   482          address feeCollector
   483      )
   484          internal
   485          pure
   486          returns (IHederaTokenService.RoyaltyFee[] memory royaltyFees)
   487      {
   488          royaltyFees = new IHederaTokenService.RoyaltyFee[](3);
   489          IHederaTokenService.RoyaltyFee
   490              memory royaltyFeeWithoutFallback = createRoyaltyFee(
   491                  numerator,
   492                  denominator,
   493                  feeCollector
   494              );
   495          IHederaTokenService.RoyaltyFee
   496              memory royaltyFeeWithFallbackHbar = createRoyaltyFeeWithFallbackFee(
   497                  numerator,
   498                  denominator,
   499                  amount,
   500                  address(0x0),
   501                  true,
   502                  feeCollector
   503              );
   504          IHederaTokenService.RoyaltyFee
   505              memory royaltyFeeWithFallbackToken = createRoyaltyFeeWithFallbackFee(
   506                  numerator,
   507                  denominator,
   508                  amount,
   509                  tokenId,
   510                  false,
   511                  feeCollector
   512              );
   513          royaltyFees[0] = royaltyFeeWithoutFallback;
   514          royaltyFees[1] = royaltyFeeWithFallbackHbar;
   515          royaltyFees[2] = royaltyFeeWithFallbackToken;
   516      }
   517  
   518      function createRoyaltyFee(
   519          int64 numerator,
   520          int64 denominator,
   521          address feeCollector
   522      ) internal pure returns (IHederaTokenService.RoyaltyFee memory royaltyFee) {
   523          royaltyFee.numerator = numerator;
   524          royaltyFee.denominator = denominator;
   525          royaltyFee.feeCollector = feeCollector;
   526      }
   527  
   528      function createRoyaltyFeeWithFallbackFee(
   529          int64 numerator,
   530          int64 denominator,
   531          int64 amount,
   532          address tokenId,
   533          bool useHbarsForPayment,
   534          address feeCollector
   535      ) internal pure returns (IHederaTokenService.RoyaltyFee memory royaltyFee) {
   536          royaltyFee.numerator = numerator;
   537          royaltyFee.denominator = denominator;
   538          royaltyFee.amount = amount;
   539          royaltyFee.tokenId = tokenId;
   540          royaltyFee.useHbarsForPayment = useHbarsForPayment;
   541          royaltyFee.feeCollector = feeCollector;
   542      }
   543  }