-
Notifications
You must be signed in to change notification settings - Fork 0
/
pawnshop.cs
146 lines (114 loc) · 4.44 KB
/
pawnshop.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
using System;
using Com.Expload;
[Program]
class Pawnshop {
/* Gametoken balances - imitation of the */
Mapping<Bytes, int> balances = new Mapping<Bytes, int>();
/* Rare items from the game */
Mapping<Bytes, int> userGameItem = new Mapping<Bytes, int>();
Mapping<int, int> gameItemPrice = new Mapping<int, int>();
Mapping<int, String> gameItemName = new Mapping<int, String>();
Mapping<int, Bytes> gameItemOwner = new Mapping<int, Bytes>();
Mapping<int, Bytes> gameItemUser = new Mapping<int, Bytes>();
//////////////////////////////////////////////
/////////////////////// Constructor
public bool initHasNotBeenLaunched = true;
public int GameItemsTotal = 0;
public int maxPricePawnshopPercent = 70;
/* Contract admin */
public Bytes ContractAdmin = Info.Sender();
public void initContract()
{
if (initHasNotBeenLaunched)
{
ContractAdmin = Info.Sender();
/* Mine some tokens to contract Admin for simulation purposes */
_mintGametoken(150000000);
}
initHasNotBeenLaunched = false;
}
//////////////////////////////////////////////
/////////////////////// Pawnshop methods
public void initiatePawnTransaction(int _tokenId)
{
Bytes owner = gameItemOwner.getDefault(_tokenId, Bytes.EMPTY);
if (Info.Sender() != owner) return;
int willReturnMoney = maxPricePawnshopPercent * gameItemPrice.getDefault(_tokenId, 0);
if (willReturnMoney <= 0) return;
if (balances.getDefault(ContractAdmin, 0) < willReturnMoney) return;
balances.put(ContractAdmin, balances.getDefault(ContractAdmin, 0) - willReturnMoney);
balances.put(owner, balances.getDefault(owner, 0) + willReturnMoney);
gameItemOwner.put(_tokenId, ContractAdmin);
}
public void finishPawnTransaction(int _tokenId)
{
Bytes sender = Info.Sender();
int willReturnMoney = maxPricePawnshopPercent * gameItemPrice.getDefault(_tokenId, 0);
if (balances.getDefault(sender, 0) < willReturnMoney) return;
balances.put(ContractAdmin, balances.getDefault(ContractAdmin, 0) + willReturnMoney);
balances.put(sender, balances.getDefault(sender, 0) - willReturnMoney);
gameItemOwner.put(_tokenId, sender);
}
public void changePawnshopPercent(int _newPercent)
{
if (Info.Sender() == ContractAdmin) {
maxPricePawnshopPercent = _newPercent;
}
}
//////////////////////////////////////////////
/////////////////////// Getters
public int gameItemOf(Bytes _itemOwner)
{
return userGameItem.getDefault(_itemOwner, 0);
}
public int balanceOf(Bytes _tokenOwner)
{
return balances.getDefault(_tokenOwner, 0);
}
public Bytes getContractAdmin() {
return ContractAdmin;
}
public Bytes getGameItemOwner(int _tokenId) {
return gameItemOwner.getDefault(_tokenId, Bytes.EMPTY);
}
public Bytes getGameItemUser(int _tokenId) {
return gameItemUser.getDefault(_tokenId, Bytes.EMPTY);
}
//////////////////////////////////////////////
/////////////////////// Transfers
public void transfer(Bytes _to, int _tokens)
{
if (_tokens <= 0) return;
if (balances.getDefault(Info.Sender(), 0) < _tokens) return;
balances.put(Info.Sender(), balances.getDefault(Info.Sender(), 0) - _tokens);
balances.put(_to, balances.getDefault(_to, 0) + _tokens);
}
public void transferOwnership(int _tokenId, Bytes _to)
{
if (Info.Sender() != gameItemOwner.getDefault(_tokenId, Bytes.EMPTY))
return;
if(_tokenId == 0) return;
if(_to == Bytes.EMPTY) return;
gameItemOwner.put(_tokenId, _to);
}
//////////////////////////////////////////////
/////////////////////// Minting for simulation purposes
public void mintGametoken(int _tokens)
{
_mintGametoken(_tokens);
}
private void _mintGametoken(int _tokens)
{
balances.put(Info.Sender(), _tokens);
}
public void mintGameItem()
{
GameItemsTotal += 1;
userGameItem.put(Info.Sender(), GameItemsTotal);
gameItemName.put(GameItemsTotal, "Very Cool Sword");
gameItemPrice.put(GameItemsTotal, 1000);
gameItemOwner.put(GameItemsTotal, Info.Sender());
gameItemUser.put(GameItemsTotal, Info.Sender());
}
}
class MainClass { public static void Main() {} }