Skip to content

Commit 74841ed

Browse files
committed
Added menu to demo app to choose sample to run
1 parent 46c5052 commit 74841ed

12 files changed

+151
-67
lines changed

TonLibDotNet.Demo/ISample.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace TonLibDotNet
2+
{
3+
public interface ISample
4+
{
5+
public Task Run(bool inMainnet);
6+
}
7+
}

TonLibDotNet.Demo/Program.cs

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ namespace TonLibDotNet
77
{
88
public static class Program
99
{
10-
private const bool useMainnet = true;
10+
// You will need actual mnemonic and address with some coins to run tests like SendTon, SendJetton etc.
11+
// These tests have safeguards and will not run on mainnet.
12+
// But anyway, double check that you are using testnet and what tests are uncommented before putting actual seed phrase here!!!
13+
public const string TestAddress = "EQAkEWzRLi1sw9AlaGDDzPvk2_F20hjpTjlvsjQqYawVmdT0";
14+
public const string TestMnemonic = ""; // put space-delimited mnemonic words here
15+
16+
// Some tests need mainnet (e.g. domains), some will run only in testnet (e.g. sending coins).
17+
public const bool UseMainnet = true;
1118

1219
private const string DirectoryForKeys = "D:/Temp/keys";
1320

@@ -20,44 +27,32 @@ public static async Task Main(string[] args)
2027
{
2128
services.Configure<TonOptions>(o =>
2229
{
23-
o.UseMainnet = useMainnet;
30+
o.UseMainnet = UseMainnet;
2431
o.LogTextLimit = 500; // Set to 0 to see full requests/responses
2532
o.VerbosityLevel = 0;
2633
o.Options.KeystoreType = new KeyStoreTypeDirectory(DirectoryForKeys);
2734
});
35+
36+
services.AddHostedService<SamplesRunner>();
37+
2838
services.AddSingleton<ITonClient, TonClient>();
2939

30-
services.AddTransient<GeneralNetworkInfo>();
31-
services.AddTransient<KeysAndMnemonics>();
32-
services.AddTransient<AccountBalanceAndTransactions>();
33-
services.AddTransient<LibraryExtensibility>();
34-
services.AddTransient<SendTon>();
35-
services.AddTransient<ResolveDomains>();
36-
services.AddTransient<ReadInfoFromSmartContracts>();
37-
services.AddTransient<BocAndCells>();
38-
services.AddTransient<DomainAuctionInfo>();
40+
services.AddTransient<ISample, GeneralNetworkInfo>();
41+
services.AddTransient<ISample, KeysAndMnemonics>();
42+
services.AddTransient<ISample, AccountBalanceAndTransactions>();
43+
services.AddTransient<ISample, LibraryExtensibility>();
44+
services.AddTransient<ISample, SendTon>();
45+
services.AddTransient<ISample, ResolveDomains>();
46+
services.AddTransient<ISample, ReadInfoFromSmartContracts>();
47+
services.AddTransient<ISample, BocAndCells>();
48+
services.AddTransient<ISample, DomainAuctionInfo>();
3949
});
4050

4151
/// Add types from current assembly (see <see cref="LibraryExtensibility"/> class for more info).
4252
TonClient.RegisterAssembly(typeof(Program).Assembly);
4353

4454
var app = builder.Build();
45-
46-
await app.Services.GetRequiredService<ITonClient>().InitIfNeeded();
47-
48-
// Feel free to comment unneeded calls below
49-
await app.Services.GetRequiredService<GeneralNetworkInfo>().Run();
50-
await app.Services.GetRequiredService<KeysAndMnemonics>().Run();
51-
await app.Services.GetRequiredService<AccountBalanceAndTransactions>().Run();
52-
await app.Services.GetRequiredService<LibraryExtensibility>().Run();
53-
await app.Services.GetRequiredService<SendTon>().Run(useMainnet);
54-
await app.Services.GetRequiredService<ResolveDomains>().Run(useMainnet);
55-
await app.Services.GetRequiredService<ReadInfoFromSmartContracts>().Run(useMainnet);
56-
await app.Services.GetRequiredService<BocAndCells>().Run(useMainnet);
57-
await app.Services.GetRequiredService<DomainAuctionInfo>().Run(useMainnet);
58-
59-
// Loggers need some time to flush data to screen/console.
60-
await Task.Delay(TimeSpan.FromSeconds(1));
55+
await app.RunAsync();
6156
}
6257
}
6358
}

TonLibDotNet.Demo/Samples/AccountBalanceAndTransactions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace TonLibDotNet.Samples
55
{
6-
public class AccountBalanceAndTransactions
6+
public class AccountBalanceAndTransactions : ISample
77
{
88
private readonly ITonClient tonClient;
99
private readonly ILogger logger;
@@ -16,7 +16,7 @@ public AccountBalanceAndTransactions(ITonClient tonClient, ILogger<AccountBalanc
1616
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
1717
}
1818

19-
public async Task Run()
19+
public async Task Run(bool inMainnet)
2020
{
2121
await tonClient.InitIfNeeded();
2222

TonLibDotNet.Demo/Samples/BocAndCells.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace TonLibDotNet.Samples
66
{
7-
public class BocAndCells
7+
public class BocAndCells : ISample
88
{
99
private readonly ITonClient tonClient;
1010
private readonly ILogger logger;
@@ -15,11 +15,11 @@ public BocAndCells(ITonClient tonClient, ILogger<BocAndCells> logger)
1515
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
1616
}
1717

18-
public async Task Run(bool useMainnet)
18+
public async Task Run(bool inMainnet)
1919
{
20-
if (!useMainnet)
20+
if (!inMainnet)
2121
{
22-
logger.LogWarning("BocAndCells() must be run in Mainnet to work correctly.");
22+
logger.LogWarning("BocAndCells() must be run in Mainnet to work correctly. Switch to testnet in Program.cs and try again.");
2323
return;
2424
}
2525

TonLibDotNet.Demo/Samples/DomainAuctionInfo.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace TonLibDotNet.Samples
66
{
7-
public class DomainAuctionInfo
7+
public class DomainAuctionInfo : ISample
88
{
99
private readonly ITonClient tonClient;
1010
private readonly ILogger logger;
@@ -15,11 +15,11 @@ public DomainAuctionInfo(ITonClient tonClient, ILogger<DomainAuctionInfo> logger
1515
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
1616
}
1717

18-
public async Task Run(bool useMainnet)
18+
public async Task Run(bool inMainnet)
1919
{
20-
if (!useMainnet)
20+
if (!inMainnet)
2121
{
22-
logger.LogWarning("GetDomainAuctionInfo() demo in Testnet is disabled, because I don't know valid DNS Contract addresses");
22+
logger.LogWarning("GetDomainAuctionInfo() sample in Testnet is disabled, because I don't know valid DNS Contract addresses. Switch to mainnet in Program.cs and try again.");
2323
return;
2424
}
2525

TonLibDotNet.Demo/Samples/GeneralNetworkInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace TonLibDotNet.Samples
44
{
5-
public class GeneralNetworkInfo
5+
public class GeneralNetworkInfo : ISample
66
{
77
private readonly ITonClient tonClient;
88
private readonly ILogger logger;
@@ -13,7 +13,7 @@ public GeneralNetworkInfo(ITonClient tonClient, ILogger<GeneralNetworkInfo> logg
1313
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
1414
}
1515

16-
public async Task Run()
16+
public async Task Run(bool inMainnet)
1717
{
1818
await tonClient.InitIfNeeded();
1919

TonLibDotNet.Demo/Samples/KeysAndMnemonics.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace TonLibDotNet.Samples
44
{
5-
public class KeysAndMnemonics
5+
public class KeysAndMnemonics : ISample
66
{
77
private readonly ITonClient tonClient;
88
private readonly ILogger logger;
@@ -13,7 +13,7 @@ public KeysAndMnemonics(ITonClient tonClient, ILogger<KeysAndMnemonics> logger)
1313
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
1414
}
1515

16-
public async Task Run()
16+
public async Task Run(bool inMainnet)
1717
{
1818
await tonClient.InitIfNeeded();
1919

TonLibDotNet.Demo/Samples/LibraryExtensibility.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace TonLibDotNet.Samples
77
{
8-
public class LibraryExtensibility
8+
public class LibraryExtensibility : ISample
99
{
1010
private readonly ITonClient tonClient;
1111
private readonly ILogger logger;
@@ -16,7 +16,7 @@ public LibraryExtensibility(ITonClient tonClient, ILogger<LibraryExtensibility>
1616
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
1717
}
1818

19-
public async Task Run()
19+
public async Task Run(bool inMainnet)
2020
{
2121
await tonClient.InitIfNeeded();
2222

TonLibDotNet.Demo/Samples/ReadInfoFromSmartContracts.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace TonLibDotNet.Samples
77
{
8-
public class ReadInfoFromSmartContracts
8+
public class ReadInfoFromSmartContracts : ISample
99
{
1010
private readonly ITonClient tonClient;
1111
private readonly ILogger logger;
@@ -16,9 +16,9 @@ public ReadInfoFromSmartContracts(ITonClient tonClient, ILogger<ReadInfoFromSmar
1616
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
1717
}
1818

19-
public Task Run(bool mainnet)
19+
public Task Run(bool inMainnet)
2020
{
21-
return mainnet ? RunOnMainnet() : RunOnTestnet();
21+
return inMainnet ? RunOnMainnet() : RunOnTestnet();
2222
}
2323

2424
protected async Task RunOnTestnet()

TonLibDotNet.Demo/Samples/ResolveDomains.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace TonLibDotNet.Samples
66
{
7-
public class ResolveDomains
7+
public class ResolveDomains : ISample
88
{
99
private readonly ITonClient tonClient;
1010
private readonly ILogger logger;
@@ -22,11 +22,11 @@ public ResolveDomains(ITonClient tonClient, ILogger<ResolveDomains> logger)
2222
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
2323
}
2424

25-
public async Task Run(bool useMainnet)
25+
public async Task Run(bool inMainnet)
2626
{
27-
if (!useMainnet)
27+
if (!inMainnet)
2828
{
29-
logger.LogWarning("ResolveDomains() demo in Testnet is disabled, because I don't know valid DNS Contract addresses");
29+
logger.LogWarning("ResolveDomains() sample in Testnet is disabled, because I don't know valid DNS Contract addresses. Switch to mainnet in Program.cs and try again.");
3030
return;
3131
}
3232

0 commit comments

Comments
 (0)