-
Notifications
You must be signed in to change notification settings - Fork 3
/
Woolong.cs
executable file
·154 lines (125 loc) · 5.34 KB
/
Woolong.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
using System.ComponentModel;
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
using System.Numerics;
namespace Woolong
{
public class Woolong : FunctionCode
{
/// <summary>
/// This is the NEP5 example token: Woolong.
/// It will generate 1 new Woolong every time the contract is invoked.
///
/// Note the parameter list below:
/// Parameter List: 05
/// Return List: 05
///
/// 参数:05
/// 返回值:05
/// </summary>
/// <param name="method">
/// The NEP5 Method being invoked.
/// 所调用的 NEP5 方法
/// </param>
/// <param name="args">
/// Optional input parameters used by the NEP5 functions.
/// NEP5 方法的参数
/// </param>
public static object Main(string method, params object[] args)
{
string name = "Woolong";
string symbol = "WNG";
BigInteger decimals = 0;
var lllwvlvwlll = new byte[] { 3, 84, 174, 73, 130, 33, 4, 108, 102,
110, 254, 187, 174, 233, 189, 14, 180,
130, 52, 105, 201, 142, 116, 132, 148,
169, 42, 113, 243, 70, 177, 166, 97 };
if (method == "deploy") return Deploy(lllwvlvwlll);
Grow(lllwvlvwlll);
if (method == "totalSupply") return Storage.Get(Storage.CurrentContext, "supply");
if (method == "name") return name;
if (method == "symbol") return symbol;
if (method == "decimals") return decimals;
if (method == "balanceOf") return Storage.Get(Storage.CurrentContext, (byte[]) args[1]);
//Verify that the originator is honest.
//确认交易者诚实
if (!Runtime.CheckWitness((byte[]) args[0])) return false;
if (method == "transfer") return Transfer((byte[]) args[0], (byte[]) args[1], BytesToInt((byte[]) args[2]));
return false;
}
/// <summary>
/// Generate 1 Woolong every time the contract is invoked.
/// </summary>
private static void Grow(byte[] lllwvlvwlll)
{
var value = BytesToInt(Storage.Get(Storage.CurrentContext, lllwvlvwlll)) + 1;
Storage.Put(Storage.CurrentContext, lllwvlvwlll, IntToBytes(value));
var supply = BytesToInt(Storage.Get(Storage.CurrentContext, "supply")) + 1;
Storage.Put(Storage.CurrentContext, "supply", IntToBytes(supply));
}
/// <summary>
/// Deploy the token with 1 supply
/// </summary>
/// <param name="lllwvlvwlll">
/// My account...
/// </param>
/// <returns></returns>
private static bool Deploy(byte[] lllwvlvwlll)
{
BigInteger initSupply = 1;
Storage.Put(Storage.CurrentContext, lllwvlvwlll, IntToBytes(initSupply));
Storage.Put(Storage.CurrentContext, "supply", IntToBytes(initSupply));
return true;
}
/// <summary>
/// Transfer a balance to another account.
/// 转帐
/// </summary>
/// <param name="originator">
/// The contract invoker.
/// 合约调用者的公钥
/// </param>
/// <param name="to">
/// The account to transfer to.
/// 转帐目标
/// </param>
/// <param name="amount">
/// The amount to transfer.
/// 转账数量
/// </param>
/// <returns>
/// Transaction Successful?
/// 交易是否成功,布尔值
/// </returns>
private static bool Transfer(byte[] originator, byte[] to, BigInteger amount)
{
//Get the account value of the source and destination accounts.
//获取源和目标账户的余额
var originatorValue = Storage.Get(Storage.CurrentContext, originator);
var targetValue = Storage.Get(Storage.CurrentContext, to);
BigInteger nOriginatorValue = BytesToInt(originatorValue) - amount;
BigInteger nTargetValue = BytesToInt(targetValue) + amount;
//If the transaction is valid, proceed.
//如果交易有效,继续
if (nOriginatorValue >= 0 &&
amount >= 0)
{
Storage.Put(Storage.CurrentContext, originator, IntToBytes(nOriginatorValue));
Storage.Put(Storage.CurrentContext, to, IntToBytes(nTargetValue));
Runtime.Notify("Transfer Successful", originator, to, amount, Blockchain.GetHeight());
return true;
}
return false;
}
private static byte[] IntToBytes(BigInteger value)
{
byte[] buffer = value.ToByteArray();
return buffer;
}
private static BigInteger BytesToInt(byte[] array)
{
var buffer = new BigInteger(array);
return buffer;
}
}
}