-
Notifications
You must be signed in to change notification settings - Fork 135
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
Erik Zhang
committed
Jul 6, 2017
1 parent
8a6b633
commit 67fdd56
Showing
19 changed files
with
447 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
//public static bool Main(byte[] agent, byte[] assetId, byte[] valueId, byte[] client, bool way, BigInteger price, byte[] signature) | ||
|
||
//if (VerifySignature(client, signature)) return true; | ||
PUSH 6 | ||
PICK | ||
PUSH 4 | ||
ROLL | ||
CHECKSIG | ||
JMPIFNOT S01 | ||
PUSH true | ||
TOALTSTACK | ||
JMP CLEAR | ||
|
||
//if (!VerifySignature(agent, signature)) return false; | ||
S01: | ||
PUSH 5 | ||
ROLL | ||
SWAP | ||
CHECKSIG | ||
JMPIF S02 | ||
PUSH false | ||
TOALTSTACK | ||
JMP CLEAR | ||
|
||
//set inputId and outputId | ||
S02: | ||
PUSH 2 | ||
PICK | ||
JMPIF S03 | ||
SWAP | ||
|
||
//BigInteger inputSum = 0, outputSum = 0; | ||
S03: | ||
PUSH 0 | ||
PUSH 0 | ||
|
||
//TransactionOutput[] references = ((Transaction)ExecutionEngine.ScriptContainer).GetReferences(); | ||
SYSCALL System.ExecutionEngine.GetScriptContainer | ||
SYSCALL AntShares.Transaction.GetReferences | ||
|
||
//foreach (TransactionOutput reference in references) | ||
//{ | ||
|
||
//initialize loop | ||
DUP | ||
ARRAYSIZE | ||
PUSH 0 | ||
|
||
//exit test | ||
LOOP1: | ||
OVER | ||
OVER | ||
NUMEQUAL | ||
JMPIF S04 | ||
|
||
//set reference | ||
PUSH 2 | ||
PICK | ||
OVER | ||
PICKITEM | ||
|
||
//if (reference.ScriptHash.Equals(ExecutionEngine.EntryScriptHash)) | ||
//{ | ||
DUP | ||
SYSCALL AntShares.Output.GetScriptHash | ||
SYSCALL System.ExecutionEngine.GetEntryScriptHash | ||
EQUAL | ||
JMPIFNOT LOOP1S1 | ||
|
||
//if (!reference.AssetId.Equals(inputId)) | ||
DUP | ||
SYSCALL AntShares.Output.GetAssetId | ||
PUSH 7 | ||
PICK | ||
EQUAL | ||
JMPIF LOOP1S2 | ||
|
||
//return false; | ||
PUSH false | ||
TOALTSTACK | ||
JMP CLEAR | ||
|
||
//else | ||
LOOP1S2: | ||
|
||
//inputSum += reference.Value; | ||
DUP | ||
SYSCALL AntShares.Output.GetValue | ||
PUSH 5 | ||
PICK | ||
ADD | ||
PUSH 5 | ||
XSWAP | ||
DROP | ||
//} | ||
|
||
LOOP1S1: | ||
DROP | ||
INC | ||
JMP LOOP1 | ||
S04: | ||
DROP | ||
DROP | ||
DROP | ||
//} | ||
|
||
//TransactionOutput[] outputs = ((Transaction)ExecutionEngine.ScriptContainer).GetOutputs(); | ||
SYSCALL System.ExecutionEngine.GetScriptContainer | ||
SYSCALL AntShares.Transaction.GetOutputs | ||
|
||
//foreach (TransactionOutput output in outputs) | ||
//{ | ||
|
||
//initialize loop | ||
DUP | ||
ARRAYSIZE | ||
PUSH 0 | ||
|
||
//exit test | ||
LOOP2: | ||
OVER | ||
OVER | ||
NUMEQUAL | ||
JMPIF S05 | ||
|
||
//set output | ||
PUSH 2 | ||
PICK | ||
OVER | ||
PICKITEM | ||
|
||
//if (output.ScriptHash.Equals(ExecutionEngine.EntryScriptHash)) | ||
//{ | ||
DUP | ||
SYSCALL AntShares.Output.GetScriptHash | ||
SYSCALL System.ExecutionEngine.GetEntryScriptHash | ||
EQUAL | ||
JMPIFNOT LOOP2S1 | ||
|
||
//if (output.AssetId.Equals(inputId)) | ||
DUP | ||
SYSCALL AntShares.Output.GetAssetId | ||
PUSH 7 | ||
PICK | ||
EQUAL | ||
JMPIFNOT LOOP2S2 | ||
|
||
//inputSum -= output.Value; | ||
PUSH 4 | ||
PICK | ||
OVER | ||
SYSCALL AntShares.Output.GetValue | ||
SUB | ||
PUSH 5 | ||
XSWAP | ||
DROP | ||
JMP LOOP2S1 | ||
|
||
//else if (output.AssetId.Equals(outputId)) | ||
LOOP2S2: | ||
DUP | ||
SYSCALL AntShares.Output.GetAssetId | ||
PUSH 8 | ||
PICK | ||
EQUAL | ||
JMPIFNOT LOOP2S1 | ||
|
||
//outputSum += output.Value; | ||
PUSH 5 | ||
PICK | ||
OVER | ||
SYSCALL AntShares.Output.GetValue | ||
ADD | ||
PUSH 6 | ||
XSWAP | ||
DROP | ||
//} | ||
|
||
LOOP2S1: | ||
DROP | ||
INC | ||
JMP LOOP2 | ||
S05: | ||
DROP | ||
DROP | ||
DROP | ||
//} | ||
|
||
//if (inputSum <= 0) return true; | ||
DUP | ||
PUSH 0 | ||
LTE | ||
JMPIFNOT S06 | ||
PUSH true | ||
TOALTSTACK | ||
JMP CLEAR | ||
|
||
//if (way) | ||
//{ | ||
S06: | ||
PUSH 4 | ||
ROLL | ||
JMPIFNOT S07 | ||
|
||
//if (outputSum * 100000000 < inputSum * price) return false; | ||
PUSH 4 | ||
ROLL | ||
MUL | ||
SWAP | ||
PUSH 100000000 | ||
MUL | ||
GT | ||
JMPIFNOT S08 | ||
PUSH false | ||
TOALTSTACK | ||
JMP CLEAR | ||
//} | ||
|
||
//else | ||
//{ | ||
S07: | ||
|
||
//if (inputSum * 100000000 > outputSum * price) return false; | ||
PUSH 100000000 | ||
MUL | ||
SWAP | ||
PUSH 4 | ||
ROLL | ||
MUL | ||
GT | ||
JMPIFNOT S08 | ||
PUSH false | ||
TOALTSTACK | ||
JMP CLEAR | ||
//} | ||
|
||
//return true; | ||
S08: | ||
DROP | ||
DROP | ||
PUSH true | ||
RET | ||
|
||
//clear stack and return | ||
CLEAR: | ||
DEPTH | ||
JMPIFNOT EXIT | ||
DROP | ||
JMP CLEAR | ||
EXIT: | ||
FROMALTSTACK | ||
RET |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="AntShares.SmartContract.Framework" version="2.0.0" targetFramework="net462" /> | ||
<package id="AntShares.SmartContract.Framework" version="2.0.0.4" targetFramework="net462" /> | ||
</packages> |
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
Binary file not shown.
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,53 @@ | ||
using AntShares.SmartContract.Framework; | ||
using AntShares.SmartContract.Framework.Services.AntShares; | ||
using AntShares.SmartContract.Framework.Services.System; | ||
using System.Numerics; | ||
|
||
namespace AntShares.SmartContract | ||
{ | ||
public class ContractAsset : FunctionCode | ||
{ | ||
public static object Main(string operation, params object[] args) | ||
{ | ||
switch (operation) | ||
{ | ||
case "create": | ||
return Create((string)args[0], (BigInteger)args[1], (byte[])args[2]); | ||
case "transfer": | ||
return Transfer((byte[])args[0], (byte[])args[1], (BigInteger)args[2]); | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
public static byte[] Create(string name, BigInteger amount, byte[] owner) | ||
{ | ||
Contract contract = Blockchain.GetContract(ExecutionEngine.ExecutingScriptHash); | ||
byte[] script = contract.Script.Concat(name.AsByteArray()); | ||
contract = Contract.Create(script, new byte[] { 5, 16 }, 5, true, name, "1.0.0-preview1", owner.AsString(), "", name); | ||
Storage.Put(contract.StorageContext, owner, amount.ToByteArray()); | ||
return Hash160(contract.Script); | ||
} | ||
|
||
public static bool Transfer(byte[] from, byte[] to, BigInteger value) | ||
{ | ||
if (value <= 0) return false; | ||
if (from.Length != 33 || to.Length != 33) return false; | ||
if (!Runtime.CheckWitness(from)) return false; | ||
byte[] data = Storage.Get(Storage.CurrentContext, from); | ||
if (data == null) return false; | ||
BigInteger available = new BigInteger(data); | ||
if (available < value) return false; | ||
available -= value; | ||
Storage.Put(Storage.CurrentContext, from, available.ToByteArray()); | ||
data = Storage.Get(Storage.CurrentContext, to); | ||
if (data == null) | ||
available = 0; | ||
else | ||
available = new BigInteger(data); | ||
available += value; | ||
Storage.Put(Storage.CurrentContext, to, available.ToByteArray()); | ||
return true; | ||
} | ||
} | ||
} |
Oops, something went wrong.