-
Notifications
You must be signed in to change notification settings - Fork 2
/
SEAS.cs
383 lines (352 loc) · 15 KB
/
SEAS.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
using Neo.SmartContract.Framework.Services.System;
using System;
using System.ComponentModel;
using System.Numerics;
namespace SEAS
{
public class SEAS : SmartContract
{
//Token Settings
public static string Name() => "Shares of SEA";
public static string Symbol() => "SEAS";
public static readonly byte[] Owner = "AUkVH4k8gPowAEpvQVAmNEkriX96CrKzk9".ToScriptHash();
public static byte Decimals() => 0;
private const ulong factor = 1; //decided by Decimals()
private const uint bonus_start_height = 2000000;
private const uint bonus_end_height = 17000000;//bonus_start_height + 1500 0000
private static readonly byte[] SEAC_CONTRACT = "SEAC".AsByteArray();
public delegate object NEP5Contract(string method, object[] args);
private static readonly byte INVOCATION_TRANSACTION_TYPE = 0xd1;
//ICO Settings
private static readonly byte[] BYTE1 = { 0 };
private static readonly byte[] BYTE2 = { 0, 0 };
private static readonly byte[] BYTE3 = { 0, 0, 0 };
private static readonly byte[] BYTE4 = { 0, 0, 0, 0 };
private static readonly byte[] BYTE5 = { 0, 0, 0, 0, 0 };
private static readonly byte[] BYTE6 = { 0, 0, 0, 0, 0, 0 };
private static readonly byte[] BYTE7 = { 0, 0, 0, 0, 0, 0, 0 };
private static readonly byte[] BYTE8 = { 0, 0, 0, 0, 0, 0, 0, 0 };
private static readonly byte[] GOD = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 };
private static readonly byte[] AssetId = { 234, 81, 101, 147, 88, 115, 123, 111, 43, 52, 78, 224, 151, 17, 212, 56, 208, 61, 117, 214, 234, 127, 72, 121, 32, 36, 165, 82, 142, 160, 183, 187 };//testnet
//private static readonly byte[] AssetId = { 212, 79, 241, 19, 187, 199, 206, 203, 163, 223, 13, 211, 61, 76, 202, 195, 16, 193, 103, 15, 214, 81, 150, 19, 136, 242, 73, 194, 107, 99, 233, 48 };//mainnet
private const ulong total_amount = 100000000 * factor; // total token amount
public static BigInteger TotalSupply() => total_amount;
[DisplayName("transfer")]
public static event Action<byte[], byte[], BigInteger> Transferred;
public static Object Main(string operation, params object[] args)
{
if (Runtime.Trigger == TriggerType.Verification)
{
Transaction tx = (Transaction)ExecutionEngine.ScriptContainer;
var type = tx.Type;
if (type != INVOCATION_TRANSACTION_TYPE) return false;
bool result = CheckSender();
if (!result) return false;
ulong contribute_value = GetContributeValue();
if (contribute_value <= 0) return false;
return true;
}
else if (Runtime.Trigger == TriggerType.Application)
{
if (operation == "deploy")
{
if (args.Length != 1) return false;
byte[] contract = (byte[])args[0];
return Deploy(contract);
}
if (operation == "mintTokens") return MintTokens();
if (operation == "totalSupply") return TotalSupply();
if (operation == "name") return Name();
if (operation == "symbol") return Symbol();
if (operation == "transfer")
{
if (args.Length != 3) return false;
byte[] from = (byte[])args[0];
byte[] callscript = ExecutionEngine.CallingScriptHash;
byte[] to = (byte[])args[1];
BigInteger value = (BigInteger)args[2];
return Transfer(from, to, value, callscript);
}
if (operation == "balanceOf")
{
if (args.Length != 1) return 0;
byte[] account = (byte[])args[0];
return BalanceOf(account);
}
if (operation == "decimals") return Decimals();
}
else if (Runtime.Trigger == TriggerType.VerificationR) //Backward compatibility, refusing to accept other assets
{
var currentHash = ExecutionEngine.ExecutingScriptHash;
var tx = ExecutionEngine.ScriptContainer as Transaction;
foreach (var output in tx.GetOutputs())
{
if (output.ScriptHash == currentHash && output.AssetId.AsBigInteger() != AssetId.AsBigInteger())
return false;
}
return true;
}
return false;
}
// initialization parameters, only once
// 初始化参数
public static bool Deploy(byte[] contract)
{
if (contract.Length != 20) return false;
if (!Runtime.CheckWitness(Owner)) return false;
byte[] seac_contract = Storage.Get(Storage.CurrentContext, SEAC_CONTRACT);
if (seac_contract.Length != 0) return false;
Storage.Put(Storage.CurrentContext, SEAC_CONTRACT, contract);
return true;
}
private static BigInteger GetBalance(byte[] info)
{
if (info == null)
{
throw new InvalidOperationException("The parameter address SHOULD NOT be null.");
}
if (info.Length != 16)
{
throw new InvalidOperationException("The parameter info's length SHOULD = 16.");
}
return info.Range(0,8).AsBigInteger();
}
private static BigInteger GetHeight(byte[] info)
{
if (info == null)
{
throw new InvalidOperationException("The parameter address SHOULD NOT be null.");
}
if (info.Length != 16)
{
throw new InvalidOperationException("The parameter info's length SHOULD = 16.");
}
return info.Range(8, 8).AsBigInteger();
}
private static byte[] GetAssetInfo(byte[] address)
{
if (address.Length != 20) return null;
byte[] assetInfo = Storage.Get(Storage.CurrentContext, address); //0.1
if (assetInfo.Length != 16) return null;
return assetInfo;
}
private static byte[] GetFixed8(BigInteger value)
{
byte[] tmp = value.AsByteArray();
if (tmp.Length > 8)
{
throw new InvalidOperationException("The parameter value's length SHOULD <= 8.");
}
if (tmp.Length == 7)
{
byte[] tmp8 = tmp.Concat(BYTE1);
return tmp8;
}
if (tmp.Length == 6)
{
byte[] tmp8 = tmp.Concat(BYTE2);
return tmp8;
}
if (tmp.Length == 5)
{
byte[] tmp8 = tmp.Concat(BYTE3);
return tmp8;
}
if (tmp.Length == 4)
{
byte[] tmp8 = tmp.Concat(BYTE4);
return tmp8;
}
if (tmp.Length == 3)
{
byte[] tmp8 = tmp.Concat(BYTE5);
return tmp8;
}
if (tmp.Length == 2)
{
byte[] tmp8 = tmp.Concat(BYTE6);
return tmp8;
}
if (tmp.Length == 1)
{
byte[] tmp8 = tmp.Concat(BYTE7);
return tmp8;
}
if (tmp.Length == 0)
{
return BYTE8;
}
return tmp;
}
private static void SetAssetInfo(byte[] address, BigInteger balance, BigInteger height)
{
if (balance <= 0)
{
Storage.Delete(Storage.CurrentContext, address); //0.1
}
else
{
byte[] b8 = GetFixed8(balance);
byte[] h8 = GetFixed8(height);
byte[] bh16 = b8.Concat(h8);
Storage.Put(Storage.CurrentContext, address, bh16); //1
}
}
public static bool MintTokens()
{
byte[] to = GetSender();
if (to.Length != 20) return false;
if (!Runtime.CheckWitness(to)) return false;
BigInteger token = (BigInteger)GetContributeValue();
if (token <= 0) return false;
BigInteger value = token / 100000000;
byte[] seac_contract = Storage.Get(Storage.CurrentContext, SEAC_CONTRACT);
if (seac_contract.Length != 20) return false;
BigInteger current_height = Blockchain.GetHeight();
BigInteger to_value = 0;
BigInteger to_bonus = 0;
BigInteger to_bonus_height = 0;
to_bonus = ComputeBonus(current_height, value, 0);
byte[] to_result = GetAssetInfo(to);
if (to_result != null)
{
to_value = GetBalance(to_result);
to_bonus_height = GetHeight(to_result);
}
to_bonus += ComputeBonus(current_height, to_value, to_bonus_height);
if (to_bonus > 0)
{
bool result = ShareBonus(GOD, 0, to, to_bonus, seac_contract);
if (!result) return false;
}
BigInteger new_to_value = to_value + value;
SetAssetInfo(to, new_to_value, current_height);
Transferred(GOD, to, value);
return true;
}
// function that is always called when someone wants to transfer tokens.
// 流转token调用
public static bool Transfer(byte[] from, byte[] to, BigInteger value, byte[] callscript)
{
if (from.Length != 20) return false;
if (to.Length != 20) return false;
if (value <= 0) return false;
if (!Runtime.CheckWitness(from) && from.AsBigInteger() != callscript.AsBigInteger()) return false;
byte[] seac_contract = Storage.Get(Storage.CurrentContext, SEAC_CONTRACT);
if (seac_contract.Length != 20) return false;
BigInteger current_height = Blockchain.GetHeight();
BigInteger from_value = 0;
BigInteger from_bonus = 0;
BigInteger from_bonus_height = 0;
BigInteger to_value = 0;
BigInteger to_bonus = 0;
BigInteger to_bonus_height = 0;
byte[] from_result = GetAssetInfo(from);
if (from_result == null) return false;
from_value = GetBalance(from_result);
from_bonus_height = GetHeight(from_result);
if (from_value < value) return false;
from_bonus = ComputeBonus(current_height, from_value, from_bonus_height);
byte[] to_result = GetAssetInfo(to);
if (to_result != null)
{
to_value = GetBalance(to_result);
to_bonus_height = GetHeight(to_result);
}
if (from != to) to_bonus = ComputeBonus(current_height, to_value, to_bonus_height);
if (from_bonus > 0 || to_bonus > 0)
{
bool result = ShareBonus(from, from_bonus, to, to_bonus, seac_contract);
if (!result) return false;
}
if (from == to)
{
SetAssetInfo(from, from_value, current_height);
return true;
}
BigInteger new_from_value = from_value - value;
SetAssetInfo(from, new_from_value, current_height);
BigInteger new_to_value = to_value + value;
SetAssetInfo(to, new_to_value, current_height);
Transferred(from, to, value);
return true;
}
public static BigInteger ComputeBonus(BigInteger current_height, BigInteger value, BigInteger start_height)
{
if (value <= 0) return 0;
if (current_height <= bonus_start_height) return 0;
if (start_height < bonus_start_height) start_height = bonus_start_height;
if (start_height >= bonus_end_height) return 0;
if (current_height > bonus_end_height) current_height = bonus_end_height;
if (current_height <= start_height) return 0;
BigInteger b = (current_height - start_height) * 6 * value;
return b;
}
public static bool ShareBonus(byte[] from, BigInteger from_bonus, byte[] to, BigInteger to_bonus, byte[] seac_contract)
{
var bonus_args = new object[] { from, from_bonus, to, to_bonus };
var contract = (NEP5Contract)seac_contract.ToDelegate();
bool result = (bool)contract("bonus", bonus_args);
return result;
}
// get the account balance of another account with address
// 根据地址获取token的余额
public static BigInteger BalanceOf(byte[] address)
{
byte[] result = GetAssetInfo(address);
if (result == null) return 0;
return GetBalance(result);
}
// check whether asset is gseas and get sender script hash
private static byte[] GetSender()
{
Transaction tx = (Transaction)ExecutionEngine.ScriptContainer;
TransactionOutput[] reference = tx.GetReferences();
// you can choice refund or not refund
foreach (TransactionOutput output in reference)
{
if (output.AssetId == AssetId) return output.ScriptHash;
}
return new byte[] { };
}
private static bool CheckSender()
{
Transaction tx = (Transaction)ExecutionEngine.ScriptContainer;
TransactionOutput[] reference = tx.GetReferences();
ulong count = 0;
foreach (TransactionOutput output in reference)
{
if (output.AssetId == AssetId)
{
if (output.ScriptHash == GetReceiver()) return false;
count += 1;
}
}
if (count < 1) return false;
return true;
}
// get smart contract script hash
private static byte[] GetReceiver()
{
return ExecutionEngine.ExecutingScriptHash;
}
// get all you contribute global seas amount
private static ulong GetContributeValue()
{
Transaction tx = (Transaction)ExecutionEngine.ScriptContainer;
TransactionOutput[] outputs = tx.GetOutputs();
ulong value = 0;
// 获取转入智能合约地址的申一股总量
foreach (TransactionOutput output in outputs)
{
if (output.ScriptHash == GetReceiver() && output.AssetId == AssetId)
{
value += (ulong)output.Value;
}
}
return value;
}
}
}