-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathReadInfoFromSmartContracts.cs
66 lines (51 loc) · 2.48 KB
/
ReadInfoFromSmartContracts.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
using System.Text;
using Microsoft.Extensions.Logging;
using TonLibDotNet.Types.Smc;
using TonLibDotNet.Types;
namespace TonLibDotNet.Samples
{
public class ReadInfoFromSmartContracts : ISample
{
private readonly ITonClient tonClient;
private readonly ILogger logger;
public ReadInfoFromSmartContracts(ITonClient tonClient, ILogger<ReadInfoFromSmartContracts> logger)
{
this.tonClient = tonClient ?? throw new ArgumentNullException(nameof(tonClient));
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public Task Run(bool inMainnet)
{
return inMainnet ? RunOnMainnet() : RunOnTestnet();
}
protected async Task RunOnTestnet()
{
await tonClient.InitIfNeeded();
// https://ton-community.github.io/tutorials/02-contract/
const string adr = "EQAHI1vGuw7d4WG-CtfDrWqEPNtmUuKjKFEFeJmZaqqfWTvW";
var info = await tonClient.SmcLoad(new AccountAddress(adr));
_ = await tonClient.SmcGetCode(info.Id);
_ = await tonClient.SmcGetState(info.Id);
_ = await tonClient.SmcGetData(info.Id);
_ = await tonClient.SmcRunGetMethod(info.Id, new MethodIdName("counter"));
_ = await tonClient.SmcForget(info.Id);
}
protected async Task RunOnMainnet()
{
await tonClient.InitIfNeeded();
// https://tonapi.io/account/EQDendoireMDFMufOUzkqNpFIay83GnjV2tgGMbA64wA3siV
const string adr = "EQDendoireMDFMufOUzkqNpFIay83GnjV2tgGMbA64wA3siV"; // tonapi.ton domain NFT
var info = await tonClient.SmcLoad(adr);
var rr = await tonClient.SmcRunGetMethod(info.Id, new MethodIdName("get_domain"));
var boc = rr.Stack[0].ToBoc();
logger.LogInformation("Domain (expecting 'tonapi'): {Value}", Encoding.ASCII.GetString(boc.RootCells[0].Content));
// Check contract source code in expolorer to understand what get_nft_data() returns
rr = await tonClient.SmcRunGetMethod(info.Id, new MethodIdName("get_nft_data"));
boc = rr.Stack[3].ToBoc();
var slice = boc.RootCells[0].BeginRead();
var ads = slice.LoadAddressIntStd();
slice.EndRead();
logger.LogInformation("Owner (expecting 'EQCNdbNc28ZrcE3AKGDqK18-NFbcSzhTGaRPeEqnMIJiQsl_'): {Value}", ads);
_ = await tonClient.SmcForget(info.Id);
}
}
}