Skip to content

Commit

Permalink
Merge pull request #3555 from AElfProject/feature/multi-approve-and-a…
Browse files Browse the repository at this point in the history
…lias

Update MultiToken Contract: Approve for all tokens and setting token alias
  • Loading branch information
eanzhao authored May 21, 2024
2 parents e67aeca + a40cb23 commit 0d78fc6
Show file tree
Hide file tree
Showing 15 changed files with 1,496 additions and 88 deletions.
3 changes: 3 additions & 0 deletions contract/AElf.Contracts.MultiToken/TokenContractConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public static class TokenContractConstants
public const string LockCallbackExternalInfoKey = "aelf_lock_callback";
public const string UnlockCallbackExternalInfoKey = "aelf_unlock_callback";
public const string LogEventExternalInfoKey = "aelf_log_event";
public const string TokenAliasExternalInfoKey = "aelf_token_alias";
public const int DELEGATEE_MAX_COUNT = 24;
public const char NFTSymbolSeparator = '-';
public const int NFTSymbolMaxLength = 30;
Expand All @@ -24,4 +25,6 @@ public static class TokenContractConstants
public const string SeedExpireTimeExternalInfoKey = "__seed_exp_time";
public const string NftCreateChainIdExternalInfoKey = "__nft_create_chain_id";
public const int DefaultMaxBatchApproveCount = 100;
public const char AllSymbolIdentifier = '*';

}
8 changes: 8 additions & 0 deletions contract/AElf.Contracts.MultiToken/TokenContractState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public partial class TokenContractState : ContractState
public StringState NativeTokenSymbol { get; set; }

public StringState ChainPrimaryTokenSymbol { get; set; }

/// <summary>
/// WARNING: Use GetTokenInfo & SetTokenInfo to operate TokenInfos
/// due to token symbol alias feature.
/// </summary>
public MappedState<string, TokenInfo> TokenInfos { get; set; }
public MappedState<string, bool> InsensitiveTokenExisting { get; set; }
public MappedState<string, string> SymbolSeedMap { get; set; }
Expand Down Expand Up @@ -68,4 +73,7 @@ public partial class TokenContractState : ContractState
public SingletonState<bool> TokenIssuerAndOwnerModificationDisabled { get; set; }

public SingletonState<int> MaxBatchApproveCount { get; set; }

// Alias -> Actual Symbol
public MappedState<string, string> SymbolAliasMap { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,12 @@ private bool CheckOrganizationExist(AuthorityInfo authorityInfo)
private void AssertValidFeeToken(string symbol, long amount)
{
AssertValidSymbolAndAmount(symbol, amount);
if (State.TokenInfos[symbol] == null)
var tokenInfo = GetTokenInfo(symbol);
if (tokenInfo == null)
{
throw new AssertionException("Token is not found");
Assert(State.TokenInfos[symbol].IsBurnable, $"Token {symbol} cannot set as method fee.");
}
Assert(tokenInfo.IsBurnable, $"Token {symbol} cannot set as method fee.");
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public override ResourceInfo GetResourceInfo(Transaction txn)
{
WritePaths =
{
GetPath(nameof(TokenContractState.Allowances), args.From.ToString(), txn.From.ToString(),
args.Symbol),
GetPath(nameof(TokenContractState.Balances), args.From.ToString(), args.Symbol),
GetPath(nameof(TokenContractState.Balances), args.To.ToString(), args.Symbol),
GetPath(nameof(TokenContractState.LockWhiteLists), args.Symbol, txn.From.ToString())
Expand All @@ -57,7 +55,7 @@ public override ResourceInfo GetResourceInfo(Transaction txn)
GetPath(nameof(TokenContractState.TransactionFeeFreeAllowancesSymbolList))
}
};

AddPathForAllowance(resourceInfo, args.From.ToString(), txn.From.ToString(), args.Symbol);
AddPathForTransactionFee(resourceInfo, txn.From.ToString(), txn.MethodName);
AddPathForDelegatees(resourceInfo, txn.From, txn.To, txn.MethodName);
AddPathForTransactionFeeFreeAllowance(resourceInfo, txn.From);
Expand All @@ -70,6 +68,18 @@ public override ResourceInfo GetResourceInfo(Transaction txn)
}
}

private void AddPathForAllowance(ResourceInfo resourceInfo, string from, string spender, string symbol)
{
resourceInfo.WritePaths.Add(GetPath(nameof(TokenContractState.Allowances), from, spender, symbol));
resourceInfo.WritePaths.Add(GetPath(nameof(TokenContractState.Allowances), from, spender,
GetAllSymbolIdentifier()));
var symbolType = GetSymbolType(symbol);
if (symbolType == SymbolType.Nft || symbolType == SymbolType.NftCollection)
{
resourceInfo.WritePaths.Add(GetPath(nameof(TokenContractState.Allowances), from, spender,
GetNftCollectionAllSymbolIdentifier(symbol)));
}
}

private void AddPathForTransactionFee(ResourceInfo resourceInfo, string from, string methodName)
{
Expand Down
Loading

0 comments on commit 0d78fc6

Please sign in to comment.