-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contract1.cs
258 lines (223 loc) · 9.25 KB
/
Contract1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
using Neo.SmartContract.Framework.Services.System;
using System;
using System.Numerics;
using System.ComponentModel;
namespace NepSwap
{
//public class AssetObject{};
public class NepSwapClass : SmartContract
{
public static AssetObject[] assetList;
//static List<string> orderList = new List<string>();
//public static List<AssetObject> assetList;
private static readonly byte[] neo_asset_id = {
155, 124, 255, 218, 166, 116, 190, 174, 15, 147, 14, 190, 96, 133, 175, 144, 147, 229,
254, 86, 179, 74, 92, 34, 12, 205, 207, 110, 252, 51, 111, 197 };
private static readonly byte[] contractHash = { 2, 133, 234, 182, 95, 74, 1, 38, 228, 184, 91, 78, 93, 139, 126,
48, 58, 255, 126, 251, 54, 13, 89, 95, 46, 49, 137, 187, 144, 72, 122, 213, 170 };
//[DisplayName("transfer")]
//public static event Action<byte[], byte[], BigInteger> Transferred;
//[DisplayName("refund")]
//public static event Action<byte[], BigInteger> Refund;
public class OrderObject
{
public byte[] user;
public BigInteger price;
public BigInteger amount;
}
public class AccountObject
{
public byte[] address;
public BigInteger amount;
}
public class AssetObject
{
public string name;
public ulong lastPrice;
public string[] waitList;
public AccountObject[] accountBag; //only temporary solution, for several dozens of users
public OrderObject[] asksBook;
public OrderObject[] bidsBook;
public AssetObject() {
waitList = new string[]{ "testUser", "testAmont" };
}
}
private static void Initialise()
{
//assetList = { }
assetList[0] = (
new AssetObject
{
name = "NEO",
lastPrice = 0
});
}
public static ulong getPrice(string asset)
{
if (asset == "NEO")
return assetList[0].lastPrice;
else // next update: multi asset compatibility
return 0;
}
// Can't accept multiple assests at once
// will return false and refund, unless it returns true somewhere
public static bool depositAsset()
{
Transaction tx = (Transaction)ExecutionEngine.ScriptContainer;
TransactionOutput[] outputs = tx.GetOutputs();
ulong value = 0; BigInteger previousValue;
foreach (TransactionOutput output in outputs)
{
// To do: next update: switch, enaums, multi-asset support
if (output.ScriptHash == GetReceiver() && output.AssetId == neo_asset_id)
{
if (output.ScriptHash != null)
{
value += (ulong)output.Value;
foreach (AccountObject array in assetList[0].accountBag)
{
if (array.address == output.ScriptHash && value > 0)
{
previousValue = array.amount; //if (!assetList[0].accountBag.TryGetValue(output.ScriptHash, out previousValue)) { }
array.amount += value; //assetList[0].accountBag.TryUpdate(output.ScriptHash, value, previousValue);
return true;
}
}
}
}
}
return false; // wrong asset or amount
}
public static bool withdrawAsset (string name, ulong quantity )
{
Transaction tx = (Transaction)ExecutionEngine.ScriptContainer;
TransactionOutput[] outputs = tx.GetOutputs();
Account account = Blockchain.GetAccount(contractHash);
byte[] sender = GetSender();
//BigInteger previousValue;
foreach (TransactionOutput output in outputs)
{
foreach (AccountObject array in assetList[0].accountBag)
{
if (array.address == sender)
// if user has this amount in AccountBag and if Contract has this amount
if (array.amount < quantity )
if (quantity < (ulong)account.GetBalance(neo_asset_id))
{
ulong contribute_value = GetContributeValue();
if (contribute_value > 0 && sender.Length != 0)
{
array.amount -= quantity; //update accountBag record
//Refund(sender, contribute_value); //contract.sendTo(asset, quantity)
return true; //Next Update: Refund / Withdraw / Transfer asset back
}
} else
{
//updateWaitList(asset, quantity)
return false;
}
}
}
return false;
}
/* ********** PSEUDO CODE *********************************************
updateOrderBook( asset, price, quantity, duration, user )
{
var buy[] = new Array(); // in descending order
var sell[] = new Array(); // in ascending order
for each asset in orders
{
for each item in orders[asset]
var lastPrice = getPrice( asset );
if orders[assets][item].price <= lastPrice
{
buy[item].push( item );
} else if orders[assets][item].price > lastPrice
{
sell[item].push( item );
}
}
}
postOrder ( asset, price, quantity, duration )
{
if account.asset.initialValue > quantity
account.depositAsset( asset, quantity )
if orders[account][asset].price != price
contracts.orders.add ( transaction.user.last.asset, transaction.user.last,price,
transaction.user.last.quantity, transaction.user, duration ) //updateOrderBook( asset, price, quantity, duration, user )
else
print ( not enough balance )
}
updateAsset ( asset, user, quantity )
{
if (contract.balance.asset.quantity > quantity) {
contract.sendTo( asset, user, quantity );
} else {
if !(queue.contains(account, asset))
{
contract.updateWaitList(asset, quantity)
}
}
}
trade ( asset, price, user, quantity ... )
{
if account.asset.initialValue > quantity
{
account.depositAsset( asset, quantity );
contract.sendTo( asset, user, quantity );
} else {
print ( not enough @asset );
}
}
globalUpdate ()
{
for each account (has * asset)
if account has balance 0f asset
if asset != NEO or asset != GAS
get account.initialValue
if asset >< initialValue
update account.initialValue
send NEO * (price) to account from contract
}
****************** END OF PSEUDO CODE *********************************************
*/
public static bool Main()
{
Initialise();
return true;
}
// Methods from Neo NEP1 Template
private static ulong GetContributeValue()
{
Transaction tx = (Transaction)ExecutionEngine.ScriptContainer;
TransactionOutput[] outputs = tx.GetOutputs();
ulong value = 0;
// get the total amount of Neo
foreach (TransactionOutput output in outputs)
{
if (output.ScriptHash == GetReceiver() && output.AssetId == neo_asset_id)
{
value += (ulong)output.Value;
}
}
return value;
}
private static byte[] GetSender()
{
Transaction tx = (Transaction)ExecutionEngine.ScriptContainer;
TransactionOutput[] reference = tx.GetReferences();
// refund will proceed via depositAsset method
foreach (TransactionOutput output in reference)
{
if (output.AssetId == neo_asset_id) return output.ScriptHash;
}
return new byte[0];
}
private static byte[] GetReceiver()
{
return ExecutionEngine.ExecutingScriptHash;
}
}
}