-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXRC_721.pas
282 lines (238 loc) · 12.1 KB
/
XRC_721.pas
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
unit XRC_721;
interface
Uses
System.SysUtils,
Velthuis.BigIntegers,
web3.eth,
web3,
web3.eth.erc721,
web3.eth.erc20,
web3.utils,
web3.eth.types;
Type
TAsyncStringObject = reference to procedure(const res: string);
TWeb3Xdc721 = class;
IWeb3Xdc721 = interface
function TokenName(url : string; tokenAddress : string; callback : TAsyncStringObject) : string;
function TokenSymbol(url : string;tokenAddress: string; callback : TAsyncStringObject) : string;
function TokenURI(url : string; tokenAddress : string; tokenId: BigInteger; callback: TAsyncStringObject): string;
function TotalSupply(url : string; tokenAddress : string; callback: TAsyncStringObject): string;
function TokenByIndex(url : string; tokenAddress : string; index: UInt64; callback: TAsyncStringObject) : string;
function TokenOfOwnerByIndex(url : string; tokenAddress : string; ownerAddress: TAddress; index: UInt64; callback: TAsyncStringObject) : string;
function BalanceOf(url : string; tokenAddress : string; ownerAddress: string; callback: TAsyncStringObject): string;
function OwnerOf(url : string; tokenAddress : string; tokenId: UInt64; callback: TAsyncStringObject):string;
function Approve(url : string; aChain: TChain; ownerPrivateKey: TPrivateKey ; tokenAddress : string ; spenderAddress : TAddress ; tokenId : UInt64 ; callback : TAsyncStringObject ) : string;
function GetApproved(url : string; tokenAddress : string; tokenId: UInt64; callback: TAsyncStringObject): string;
function IsApprovedForAll(url : string; aChain: TChain; tokenAddress : string ; ownerAddress: TPrivateKey ; operatorAddress : TAddress ; callback : TAsyncStringObject ) : string;
function SetApprovalForAll(url : string; aChain: TChain; tokenAddress : string ; ownerPrivateKey: TPrivateKey ; operatorAddress : TAddress ; approved : boolean; callback : TAsyncStringObject ) : string;
function TransferFrom(url : string; aChain: TChain; spenderPrivateKey: TPrivateKey ; ownerAddress: TAddress; tokenAddress : string ; recipientAddress : TAddress ; tokenId : BigInteger ; callback : TAsyncStringObject ) : string;
function SafeTransferFrom(url : string; aChain: TChain; spenderPrivateKey: TPrivateKey ; ownerAddress: TAddress; tokenAddress : string ; recipientAddress : TAddress ; tokenId : BigInteger ; callback : TAsyncStringObject ) : string;
end;
TWeb3Xdc721 = class(TInterfacedObject, IWeb3Xdc721)
function TokenName(url : string; tokenAddress : string; callback : TAsyncStringObject) : string;
function TokenSymbol(url : string; tokenAddress: string; callback : TAsyncStringObject) : string;
function TokenURI(url : string; tokenAddress : string; tokenId: BigInteger; callback: TAsyncStringObject): string;
function TotalSupply(url : string; tokenAddress : string; callback: TAsyncStringObject): string;
function TokenByIndex(url : string; tokenAddress : string; index: UInt64; callback: TAsyncStringObject) : string;
function TokenOfOwnerByIndex(url : string; tokenAddress : string; ownerAddress: TAddress; index: UInt64; callback: TAsyncStringObject) : string;
function BalanceOf(url : string; tokenAddress : string; ownerAddress: string; callback: TAsyncStringObject): string;
function OwnerOf(url : string; tokenAddress : string; tokenId: UInt64; callback: TAsyncStringObject):string;
function Approve(url : string; aChain: TChain; ownerPrivateKey: TPrivateKey ; tokenAddress : string ; spenderAddress : TAddress ; tokenId : UInt64 ; callback : TAsyncStringObject ) : string;
function GetApproved(url : string; tokenAddress : string; tokenId: UInt64; callback: TAsyncStringObject): string;
function IsApprovedForAll(url : string; aChain: TChain; tokenAddress : string ; ownerAddress: TPrivateKey ; operatorAddress : TAddress ; callback : TAsyncStringObject ) : string;
function SetApprovalForAll(url : string; aChain: TChain; tokenAddress : string ; ownerPrivateKey: TPrivateKey ; operatorAddress : TAddress ; approved : boolean; callback : TAsyncStringObject ) : string;
function TransferFrom(url : string; aChain: TChain; spenderPrivateKey: TPrivateKey ; ownerAddress: TAddress; tokenAddress : string ; recipientAddress : TAddress ; tokenId : BigInteger ; callback : TAsyncStringObject ) : string;
function SafeTransferFrom(url : string; aChain: TChain; spenderPrivateKey: TPrivateKey ; ownerAddress: TAddress; tokenAddress : string ; recipientAddress : TAddress ; tokenId : BigInteger ; callback : TAsyncStringObject ) : string;
end;
implementation
function TWeb3Xdc721.TokenName(url : string; tokenAddress : string; callback : TAsyncStringObject) : string;
begin
var Client: IWeb3 := TWeb3.Create(url);
web3.eth.call(Client, tokenAddress, 'name()', [], procedure(tup: TTuple; err: IError)
begin
if Assigned(err) then
callback(err.Message)
else
callback(tup.ToString);
end);
end;
function TWeb3Xdc721.TokenSymbol(url : string; tokenAddress: string; callback: TAsyncStringObject): string;
begin
var Client: IWeb3 := TWeb3.Create(url);
web3.eth.call(Client, tokenAddress, 'symbol()', [], procedure(tup: TTuple; err: IError)
begin
if Assigned(err) then
callback(err.Message)
else
callback(tup.ToString);
end);
end;
function TWeb3Xdc721.TokenURI(url : string; tokenAddress : string; tokenId: BigInteger; callback: TAsyncStringObject): string;
begin
var Client: IWeb3 := TWeb3.Create(url);
web3.eth.call(Client, tokenAddress, 'tokenURI(uint256)', [web3.utils.toHex(tokenId)], procedure(tup: TTuple; err: IError)
begin
if Assigned(err) then
callback(err.Message)
else
callback(tup.ToString);
end);
end;
function TWeb3Xdc721.TotalSupply(url : string; tokenAddress : string; callback: TAsyncStringObject): string;
begin
var Client: IWeb3 := TWeb3.Create(url);
web3.eth.call(Client, tokenAddress, 'totalSupply()', [], procedure(qty : BigInteger; err : IError)
begin
if Assigned(err) then
callback(err.Message)
else
callback(qty.ToString);
end);
end;
function TWeb3Xdc721.TokenByIndex(url : string; tokenAddress : string; index: UInt64; callback: TAsyncStringObject) : string;
begin
var Client: IWeb3 := TWeb3.Create(url);
web3.eth.call(Client, tokenAddress, 'tokenByIndex(uint256)', [index], procedure(qty : BigInteger; err : IError)
begin
if Assigned(err) then
callback(err.Message)
else
callback(qty.ToString);
end);
end;
function TWeb3Xdc721.TokenOfOwnerByIndex(url : string; tokenAddress : string; ownerAddress: TAddress; index: UInt64; callback: TAsyncStringObject) : string;
begin
var Client: IWeb3 := TWeb3.Create(url);
web3.eth.call(Client, tokenAddress, 'tokenOfOwnerByIndex(address,uint256)', [ownerAddress, index], procedure(qty : BigInteger; err : IError)
begin
if Assigned(err) then
callback(err.Message)
else
callback(qty.ToString);
end);
end;
function TWeb3Xdc721.BalanceOf(url : string; tokenAddress : string; ownerAddress: string; callback: TAsyncStringObject): string;
begin
var Client: IWeb3 := TWeb3.Create(url);
web3.eth.call(Client, tokenAddress, 'balanceOf(address)', [ownerAddress], procedure(qty : BigInteger; err : IError)
begin
if Assigned(err) then
callback(err.Message)
else
callback(qty.ToString);
end);
end;
function TWeb3Xdc721.OwnerOf(url : string; tokenAddress : string; tokenId: UInt64; callback: TAsyncStringObject):string ;
begin
var Client: IWeb3 := TWeb3.Create(url);
web3.eth.call(Client, tokenAddress, 'ownerOf(uint256)', [tokenId], procedure(const hex: string; err: IError)
begin
if Assigned(err) then
callback(err.Message)
else
callback(TAddress.New(hex));
end);
end;
function TWeb3Xdc721.GetApproved(url : string; tokenAddress : string; tokenId: UInt64; callback: TAsyncStringObject): string;
begin
var Client: IWeb3 := TWeb3.Create(url);
web3.eth.call(Client, tokenAddress, 'getApproved(uint256)', [tokenId], procedure(const hex: string; err: IError)
begin
if Assigned(err) then
callback(err.Message)
else
callback(TAddress.New(hex));
end);
end;
function TWeb3Xdc721.IsApprovedForAll(url : string; aChain: TChain; tokenAddress : string ; ownerAddress: TPrivateKey ; operatorAddress : TAddress ; callback : TAsyncStringObject ) : string;
begin
var XRC721 := TERC721.Create(TWeb3.Create(aChain,url),tokenAddress);
try
XRC721.IsApprovedForAll(
ownerAddress,
operatorAddress,
procedure(bool : Boolean; err : IError)
begin
if Assigned(err) then
callback(err.Message)
else
callback(bool.ToString());
end);
finally
XRC721.Free;
end;
end;
function TWeb3Xdc721.TransferFrom(url : string; aChain: TChain; spenderPrivateKey: TPrivateKey ; ownerAddress: TAddress; tokenAddress : string ; recipientAddress : TAddress ; tokenId : BigInteger ; callback : TAsyncStringObject ) : string;
begin
var XRC721 := TERC20.Create(TWeb3.Create(aChain,url),tokenAddress); // RPC access to XDC
spenderPrivateKey.Address(procedure(addr: TAddress; err: IError)
begin
web3.eth.write(
XRC721.Client, spenderPrivateKey, XRC721.Contract,
'transferFrom(address,address,uint256)', [ownerAddress, recipientAddress, web3.utils.toHex(tokenId)],
procedure(hash : TTxHash; err : IError)
begin
if Assigned(err) then
callback(err.Message)
else
callback(string(hash));
end);
end);
end;
// edited.
function TWeb3Xdc721.SafeTransferFrom(url : string; aChain: TChain; spenderPrivateKey: TPrivateKey ; ownerAddress: TAddress; tokenAddress : string ; recipientAddress : TAddress ; tokenId : BigInteger ; callback : TAsyncStringObject ) : string;
begin
var XRC721 := TERC20.Create(TWeb3.Create(aChain,url),tokenAddress); // RPC access to XDC
spenderPrivateKey.Address(procedure(addr: TAddress; err: IError)
begin
web3.eth.write(
XRC721.Client, spenderPrivateKey, XRC721.Contract,
'transferFrom(address,address,uint256)', [ownerAddress, recipientAddress, web3.utils.toHex(tokenId)],
procedure(hash : TTxHash; err : IError)
begin
if Assigned(err) then
callback(err.Message)
else
callback(string(hash));
end);
end);
end;
function TWeb3Xdc721.Approve(url : string; aChain: TChain; ownerPrivateKey: TPrivateKey ; tokenAddress : string ; spenderAddress : TAddress ; tokenId : UInt64 ; callback : TAsyncStringObject ) : string;
begin
var XRC721 := TERC721.Create(TWeb3.Create(aChain,url),tokenAddress);
try
XRC721.Approve(
ownerPrivateKey, // from private key
spenderAddress, // to public key
tokenId, // 0.001 TST
procedure(rcpt: ITxReceipt; err: IError)
begin
if Assigned(err) then
callback(err.Message)
else
callback(string(rcpt.txHash));
end);
finally
XRC721.Free;
end;
end;
function TWeb3Xdc721.SetApprovalForAll(url : string; aChain: TChain; tokenAddress : string ; ownerPrivateKey: TPrivateKey ; operatorAddress : TAddress ; approved : boolean; callback : TAsyncStringObject ) : string;
begin
var XRC721 := TERC721.Create(TWeb3.Create(aChain,url),tokenAddress);
try
XRC721.SetApprovalForAll(
ownerPrivateKey,
operatorAddress,
approved,
procedure(rcpt : ITxReceipt; err : IError)
begin
if Assigned(err) then
callback(err.Message)
else
callback(string(rcpt.txHash));
end);
finally
XRC721.Free;
end;
end;
end.