Skip to content

Commit

Permalink
add new example: ContractAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Zhang committed Jul 6, 2017
1 parent 8a6b633 commit 67fdd56
Show file tree
Hide file tree
Showing 19 changed files with 447 additions and 27 deletions.
5 changes: 3 additions & 2 deletions AgencyTransaction/AgencyTransaction.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="AntShares.SmartContract.Framework, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AntShares.SmartContract.Framework.2.0.0\lib\net40\AntShares.SmartContract.Framework.dll</HintPath>
<Reference Include="AntShares.SmartContract.Framework, Version=2.0.0.4, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\AntShares.SmartContract.Framework.2.0.0.4\lib\net40\AntShares.SmartContract.Framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -53,6 +53,7 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<None Include="AgencyTransaction.txt" />
<Content Include="AntShares.ILConvertPlugin.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
252 changes: 252 additions & 0 deletions AgencyTransaction/AgencyTransaction.txt
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
2 changes: 1 addition & 1 deletion AgencyTransaction/packages.config
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>
6 changes: 6 additions & 0 deletions AntShares.SmartContract.Contracts.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Domain", "Domain\Domain.csp
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StructExample", "StructExample\StructExample.csproj", "{6777CA72-89CB-4FDD-84C3-BC82A440CD58}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ContractAsset", "ContractAsset\ContractAsset.csproj", "{743F4206-82CC-4807-9189-AC6B8B64A563}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -39,6 +41,10 @@ Global
{6777CA72-89CB-4FDD-84C3-BC82A440CD58}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6777CA72-89CB-4FDD-84C3-BC82A440CD58}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6777CA72-89CB-4FDD-84C3-BC82A440CD58}.Release|Any CPU.Build.0 = Release|Any CPU
{743F4206-82CC-4807-9189-AC6B8B64A563}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{743F4206-82CC-4807-9189-AC6B8B64A563}.Debug|Any CPU.Build.0 = Debug|Any CPU
{743F4206-82CC-4807-9189-AC6B8B64A563}.Release|Any CPU.ActiveCfg = Release|Any CPU
{743F4206-82CC-4807-9189-AC6B8B64A563}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Binary file added ContractAsset/AntShares.ILConvertPlugin.dll
Binary file not shown.
53 changes: 53 additions & 0 deletions ContractAsset/ContractAsset.cs
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;
}
}
}
Loading

0 comments on commit 67fdd56

Please sign in to comment.