Skip to content

Commit

Permalink
Merging transfer and transferAPP
Browse files Browse the repository at this point in the history
  • Loading branch information
陈志同 committed Sep 11, 2018
1 parent b055adb commit d293d65
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 42 deletions.
41 changes: 3 additions & 38 deletions NeoContract/CGAS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ public static object Main(string method, object[] args)
if (method == "totalSupply") return TotalSupply();

if (method == "transfer") return Transfer((byte[])args[0], (byte[])args[1], (BigInteger)args[2], callscript);

if (method == "transferAPP") return TransferAPP((byte[])args[0], (byte[])args[1], (BigInteger)args[2], callscript);
}
else if (Runtime.Trigger == TriggerType.VerificationR) //Backward compatibility, refusing to accept other assets
{
Expand Down Expand Up @@ -163,7 +161,7 @@ public static bool MintTokens()
{
if (input.AssetId.AsBigInteger() == AssetId.AsBigInteger())
sender = sender ?? input.ScriptHash;
//SGAS address as inputs is not allowed
//CGAS address as inputs is not allowed
if (input.ScriptHash.AsBigInteger() == ExecutionEngine.ExecutingScriptHash.AsBigInteger())
return false;
}
Expand Down Expand Up @@ -288,42 +286,9 @@ private static bool Transfer(byte[] from, byte[] to, BigInteger amount, byte[] c
throw new InvalidOperationException("The parameter amount MUST be greater than 0.");
if (ExecutionEngine.EntryScriptHash.AsBigInteger() != callscript.AsBigInteger())
return false;
if (!IsPayable(to) || !Runtime.CheckWitness(from)/*0.2*/)
return false;
StorageMap asset = Storage.CurrentContext.CreateMap(nameof(asset));
var fromAmount = asset.Get(from).AsBigInteger(); //0.1
if (fromAmount < amount)
if (!IsPayable(to))
return false;
if (from == to)
return true;

//Reduce payer balances
if (fromAmount == amount)
asset.Delete(from); //0.1
else
asset.Put(from, fromAmount - amount); //1

//Increase the payee balance
var toAmount = asset.Get(to).AsBigInteger(); //0.1
asset.Put(to, toAmount + amount); //1

SetTxInfo(from, to, amount);
Transferred(from, to, amount);
return true;
}
#if DEBUG
[DisplayName("transferAPP")] //Only for ABI file
public static bool TransferAPP(byte[] from, byte[] to, BigInteger amount) => true;
#endif
//Methods of actual execution
private static bool TransferAPP(byte[] from, byte[] to, BigInteger amount, byte[] callscript)
{
//Check parameters
if (from.Length != 20 || to.Length != 20)
throw new InvalidOperationException("The parameters from and to SHOULD be 20-byte addresses.");
if (amount <= 0)
throw new InvalidOperationException("The parameter amount MUST be greater than 0.");
if (!IsPayable(to) || from.AsBigInteger() != callscript.AsBigInteger())
if (!Runtime.CheckWitness(from) && from.AsBigInteger() != callscript.AsBigInteger()) /*0.2*/
return false;
StorageMap asset = Storage.CurrentContext.CreateMap(nameof(asset));
var fromAmount = asset.Get(from).AsBigInteger(); //0.1
Expand Down
4 changes: 2 additions & 2 deletions Tools/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ static class Program
static void Main(string[] args)
{
//address 2 script hash
Console.WriteLine(Neo.Wallets.Wallet.ToScriptHash("AK4LdT5ZXR9DQZjfk5X6Xy79mE8ad8jKAW"));
Console.WriteLine(Neo.Wallets.Wallet.ToScriptHash("Ae8AD6Rc3cvQapqttJcUTj9ULfLi2tLHmc"));
//script hash 2 address
Console.WriteLine(Neo.Wallets.Wallet.ToAddress(new UInt160("0x9121e89e8a0849857262d67c8408601b5e8e0524".Remove(0, 2).HexToBytes().Reverse().ToArray())));
Console.WriteLine(Neo.Wallets.Wallet.ToAddress(new UInt160("0x505663a29d83663a838eee091249abd167e928f5".Remove(0, 2).HexToBytes().Reverse().ToArray())));

//hex string 2 string
Console.WriteLine("7472616e73666572".HexToString());
Expand Down
4 changes: 2 additions & 2 deletions UnitTests/CGASTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace UnitTests
{
public static class CGASTest
{
static readonly UInt160 SgasAddress = Wallet.ToScriptHash("AK4LdT5ZXR9DQZjfk5X6Xy79mE8ad8jKAW");
static readonly UInt160 ScriptHash = new UInt160("0x9121e89e8a0849857262d67c8408601b5e8e0524".Remove(0, 2).HexToBytes().Reverse().ToArray());
static readonly UInt160 SgasAddress = Wallet.ToScriptHash("Ae8AD6Rc3cvQapqttJcUTj9ULfLi2tLHmc");
static readonly UInt160 ScriptHash = new UInt160("0x505663a29d83663a838eee091249abd167e928f5".Remove(0, 2).HexToBytes().Reverse().ToArray());
static readonly UInt160 User = Wallet.ToScriptHash("AJd31a8rYPEBkY1QSxpsGy8mdU4vTYTD4U");
static readonly byte[] UserScript = "2103ad1d70f140d84a90ad4491cdf175fa64bfa9287a006e8cbd8f8db8500b5205baac".HexToBytes();

Expand Down

0 comments on commit d293d65

Please sign in to comment.