-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f39429
commit e139a96
Showing
24 changed files
with
1,124 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
contract/AElf.Contracts.MultiToken/AElf.Contracts.MultiToken.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
contract/AElf.Contracts.MultiToken/TokenContract_NFTHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Linq; | ||
|
||
namespace AElf.Contracts.MultiToken; | ||
|
||
public partial class TokenContract | ||
{ | ||
public enum SymbolType | ||
{ | ||
NFTCollection, | ||
NFT, | ||
TOKEN | ||
} | ||
|
||
private SymbolType GetCreateInputSymbolType(string symbol) | ||
{ | ||
var words = symbol.Split(TokenContractConstants.NFTSymbolSeparator); | ||
Assert(words[0].Length > 0 && words[0].All(IsValidCreateSymbolChar), "Invalid Symbol input"); | ||
if (words.Length == 1) return SymbolType.TOKEN; | ||
Assert(words.Length == 2 && words[1].Length > 0 && words[1].All(IsValidItemIdChar), "Invalid NFT Symbol input"); | ||
return words[1] == "0" ? SymbolType.NFTCollection : SymbolType.NFT; | ||
} | ||
|
||
private void AssertNFTCreateInput(CreateInput input) | ||
{ | ||
Assert(input.Decimals == 0, "NFT's decimals must be 0"); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
contract/AElf.Contracts.MultiToken/TokenContract_NFT_Actions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System.Linq; | ||
using AElf.CSharp.Core; | ||
using AElf.Sdk.CSharp; | ||
using AElf.Standards.ACS1; | ||
using AElf.Types; | ||
using Google.Protobuf.WellKnownTypes; | ||
|
||
namespace AElf.Contracts.MultiToken; | ||
|
||
public partial class TokenContract | ||
{ | ||
private Empty CreateNFTCollection(CreateInput input) | ||
{ | ||
AssertNFTCreateInput(input); | ||
return CreateToken(input, SymbolType.NFTCollection); | ||
} | ||
|
||
private Empty CreateNFTInfo(CreateInput input) | ||
{ | ||
AssertNFTCreateInput(input); | ||
var nftCollectionInfo = AssertNftCollectionExist(input.Symbol); | ||
input.IssueChainId = input.IssueChainId == 0 ? nftCollectionInfo.IssueChainId : input.IssueChainId; | ||
Assert(input.IssueChainId == nftCollectionInfo.IssueChainId, "NFT create ChainId must be collection's issue chainId"); | ||
Assert(Context.Sender == nftCollectionInfo.Issuer && nftCollectionInfo.Issuer == input.Issuer, "NFT issuer must be collection's issuer"); | ||
return CreateToken(input, SymbolType.NFT); | ||
} | ||
|
||
private void ChargeCreateFees() | ||
{ | ||
if (Context.Sender == Context.Origin) return; | ||
if (IsAddressInCreateWhiteList(Context.Sender)) return; | ||
|
||
var fee = GetCreateMethodFee(); | ||
Assert(fee != null, "not enough balance for create"); | ||
DoTransferFrom(Context.Sender, Context.Self, Context.Self, fee.Symbol, fee.BasicFee, ""); | ||
|
||
ModifyBalance(Context.Self, fee.Symbol, -fee.BasicFee); | ||
Context.Fire(new TransactionFeeCharged() | ||
{ | ||
Symbol = fee.Symbol, | ||
Amount = fee.BasicFee, | ||
ChargingAddress = Context.Self | ||
}); | ||
} | ||
|
||
private void DoTransferFrom(Address from, Address to, Address spender, string symbol, long amount, string memo) | ||
{ | ||
// First check allowance. | ||
var allowance = State.Allowances[from][spender][symbol]; | ||
if (allowance < amount) | ||
{ | ||
if (IsInWhiteList(new IsInWhiteListInput { Symbol = symbol, Address = spender }).Value) | ||
{ | ||
DoTransfer(from, to, symbol, amount, memo); | ||
DealWithExternalInfoDuringTransfer(new TransferFromInput() { From = from, To = to, Symbol = symbol, Amount = amount, Memo = memo }); | ||
return; | ||
} | ||
|
||
Assert(false, | ||
$"[TransferFrom]Insufficient allowance. Token: {symbol}; {allowance}/{amount}.\n" + | ||
$"From:{from}\tSpender:{spender}\tTo:{to}"); | ||
} | ||
|
||
DoTransfer(from, to, symbol, amount, memo); | ||
DealWithExternalInfoDuringTransfer(new TransferFromInput() { From = from, To = to, Symbol = symbol, Amount = amount, Memo = memo }); | ||
State.Allowances[from][spender][symbol] = allowance.Sub(amount); | ||
} | ||
|
||
private MethodFee GetCreateMethodFee() | ||
{ | ||
var fee = State.TransactionFees[nameof(Create)]; | ||
if (fee == null || fee.Fees.Count <= 0) return new MethodFee { Symbol = Context.Variables.NativeSymbol, BasicFee = 10000_00000000 }; | ||
return fee.Fees.FirstOrDefault(f => GetBalance(Context.Sender, f.Symbol) >= f.BasicFee); | ||
} | ||
|
||
private string GetNftCollectionSymbol(string inputSymbol) | ||
{ | ||
var symbol = inputSymbol; | ||
var words = symbol.Split(TokenContractConstants.NFTSymbolSeparator); | ||
const int tokenSymbolLength = 1; | ||
if (words.Length == tokenSymbolLength) return null; | ||
Assert(words.Length == 2 && words[1].All(IsValidItemIdChar), "Invalid NFT Symbol Input"); | ||
return symbol == $"{words[0]}-0" ? null : $"{words[0]}-0"; | ||
} | ||
|
||
private TokenInfo AssertNftCollectionExist(string symbol) | ||
{ | ||
var collectionSymbol = GetNftCollectionSymbol(symbol); | ||
if (collectionSymbol == null) return null; | ||
var collectionInfo = State.TokenInfos[collectionSymbol]; | ||
Assert(collectionInfo != null, "NFT collection not exist"); | ||
return collectionInfo; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.