Skip to content

Commit 1d10dbf

Browse files
committed
Jettons: GetJettonAddressInfo -> GetWalletData, + GetJettonData
1 parent 0a13a5e commit 1d10dbf

File tree

2 files changed

+83
-21
lines changed

2 files changed

+83
-21
lines changed

TonLibDotNet.Demo/Samples/Recipes/Jettons.cs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
using System.Net;
2-
using Microsoft.Extensions.Logging;
3-
using TonLibDotNet.Cells;
1+
using Microsoft.Extensions.Logging;
42
using TonLibDotNet.Types;
53
using TonLibDotNet.Types.Msg;
64

75
namespace TonLibDotNet.Samples.Recipes
86
{
97
public class Jettons : ISample
108
{
11-
const string jettonMasterAddress = "EQBbX2khki4ynoYWgXqmc7_5Xlcley9luaHxoSE0-7R2whnK";
9+
// some testnet jetton
10+
const string jettonMinterAddressTestnet = "EQBbX2khki4ynoYWgXqmc7_5Xlcley9luaHxoSE0-7R2whnK";
1211

1312
// regular wallet, not jetton one!
14-
const string ownerWalletAddress = "EQAkEWzRLi1sw9AlaGDDzPvk2_F20hjpTjlvsjQqYawVmdT0";
13+
const string ownerWalletAddressTestnet = "EQAkEWzRLi1sw9AlaGDDzPvk2_F20hjpTjlvsjQqYawVmdT0";
14+
15+
// jUSDT
16+
const string jettonMinterAddressMainnet = "EQBynBO23ywHy_CgarY9NK9FTz0yDsG82PtcbSTQgGoXwiuA";
17+
18+
// regular wallet, not jetton one!
19+
const string ownerWalletAddressMainnet = "EQB3ncyBUTjZUA5EnFKR5_EnOMI9V1tTEAAPaiU71gc4TiUt";
1520

1621
// regular wallet, not jetton one!
17-
const string receiverWalletWallet = "EQC403uCzev_-2g8fNfFPOgr5xOxCoTrCX2gp6OMK6YDtARk";
22+
const string receiverWalletWalletTestnet = "EQC403uCzev_-2g8fNfFPOgr5xOxCoTrCX2gp6OMK6YDtARk";
1823

1924
private readonly ITonClient tonClient;
2025
private readonly ILogger logger;
@@ -27,30 +32,40 @@ public Jettons(ITonClient tonClient, ILogger<Jettons> logger)
2732

2833
public async Task Run(bool inMainnet)
2934
{
30-
if (inMainnet)
31-
{
32-
logger.LogWarning("Jettons() sample in Mainnet is disabled for safety reasons. Switch to testnet in Program.cs and try again.");
33-
return;
34-
}
35+
var jettonMinterAddress = inMainnet ? jettonMinterAddressMainnet : jettonMinterAddressTestnet;
36+
var ownerWalletAddress = inMainnet ? ownerWalletAddressMainnet : ownerWalletAddressTestnet;
3537

3638
await tonClient.InitIfNeeded();
3739

38-
var ownerJettonAddress = await TonRecipes.Jettons.GetWalletAddress(tonClient, jettonMasterAddress, ownerWalletAddress);
40+
var ownerJettonAddress = await TonRecipes.Jettons.GetWalletAddress(tonClient, jettonMinterAddress, ownerWalletAddress);
3941
logger.LogInformation("Jetton address for owner wallet {Wallet} is: {Address}", ownerWalletAddress, ownerJettonAddress);
4042

41-
var (bal, own, mst) = await TonRecipes.Jettons.GetJettonAddressInfo(tonClient, ownerJettonAddress);
43+
var wd = await TonRecipes.Jettons.GetWalletData(tonClient, ownerJettonAddress);
4244
logger.LogInformation("Info for Jetton address {Address}:", ownerJettonAddress);
43-
logger.LogInformation(" Balance: {Value}", bal);
44-
logger.LogInformation(" Owner: {Value}", own);
45-
logger.LogInformation(" Jett.Master: {Value}", mst);
45+
logger.LogInformation(" Balance: {Value}", wd.balance);
46+
logger.LogInformation(" Owner: {Value}", wd.ownerAddress);
47+
logger.LogInformation(" Jett.Master: {Value}", wd.jettonMinterAddress);
48+
logger.LogInformation(" Code: \r\n{Value}", wd.jettonWalletCode.DumpCells());
49+
50+
var jd = await TonRecipes.Jettons.GetJettonData(tonClient, jettonMinterAddress);
51+
logger.LogInformation("Info for Jetton Minter address {Address}:", jettonMinterAddress);
52+
logger.LogInformation(" Total supply:{Value}", jd.totalSupply);
53+
logger.LogInformation(" Mintable: {Value}", jd.mintable);
54+
logger.LogInformation(" Admin: {Value}", jd.adminAddress);
55+
logger.LogInformation(" Content: \r\n{Value}", jd.jettonContent.DumpCells());
56+
logger.LogInformation(" Wallet code: \r\n{Value}", jd.jettonWalletCode.DumpCells());
4657

47-
if (string.IsNullOrWhiteSpace(Program.TestMnemonic))
58+
if (inMainnet)
59+
{
60+
logger.LogWarning("Jettons transfer sample in Mainnet is disabled for safety reasons. Switch to testnet in Program.cs and try again.");
61+
}
62+
else if (string.IsNullOrWhiteSpace(Program.TestMnemonic))
4863
{
4964
logger.LogWarning("Actual mnemonic is not set, sending jettons code is skipped. Put mnemonic phrase in Prograg.cs and try again.");
5065
}
5166
else
5267
{
53-
var msg = TonRecipes.Jettons.CreateTransferMessage(ownerJettonAddress, 12345, 1_000_000_000, receiverWalletWallet, ownerWalletAddress, null, 0.01M, null);
68+
var msg = TonRecipes.Jettons.CreateTransferMessage(ownerJettonAddress, 12345, 1_000_000_000, receiverWalletWalletTestnet, ownerWalletAddress, null, 0.01M, null);
5469

5570
var inputKey = await tonClient.ImportKey(new ExportedKey(Program.TestMnemonic.Split(' ').ToList()));
5671

TonLibDotNet/Recipes/Tep74Recipes.cs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,18 @@ public async Task<string> GetWalletAddress(ITonClient tonClient, string jettonMi
6565
/// </summary>
6666
/// <param name="tonClient"><see cref="ITonClient"/> instance.</param>
6767
/// <param name="jettonAddress">Jetton address to obtain info for.</param>
68-
/// <returns>Information about specified Jetton address: balance, owner (user) wallet address, Jetton Minter contract address.</returns>
68+
/// <returns>
69+
/// <list type="table">
70+
/// <item><i>balance</i> - amount of jettons on wallet</item>
71+
/// <item><i>ownerAddress</i> - address of wallet owner</item>
72+
/// <item><i>jettonMinterAddress</i> - address of Jetton master-address</item>
73+
/// <item><i>jettonWalletCode</i> - code of this wallet</item>
74+
/// </list>
75+
/// </returns>
6976
/// <remarks>Jetton contract must be deployed and active (to execute get-method).</remarks>
7077
/// <exception cref="TonLibNonZeroExitCodeException" />
7178
/// <seealso href="https://github.com/ton-blockchain/token-contract/blob/main/ft/jetton-wallet.fc#L246">Source of 'get_wallet_address' method.</seealso>
72-
public async Task<(BigInteger balance, string ownerAddress, string jettonMinterAddress)> GetJettonAddressInfo(ITonClient tonClient, string jettonAddress)
79+
public async Task<(BigInteger balance, string ownerAddress, string jettonMinterAddress, Boc jettonWalletCode)> GetWalletData(ITonClient tonClient, string jettonAddress)
7380
{
7481
await tonClient.InitIfNeeded().ConfigureAwait(false);
7582

@@ -85,8 +92,48 @@ public async Task<string> GetWalletAddress(ITonClient tonClient, string jettonMi
8592
var balance = BigInteger.Parse(result.Stack[0].ToTvmNumberDecimal(), CultureInfo.InvariantCulture);
8693
var owner = result.Stack[1].ToTvmCell().ToBoc().RootCells[0].BeginRead().LoadAddressIntStd();
8794
var minter = result.Stack[2].ToTvmCell().ToBoc().RootCells[0].BeginRead().LoadAddressIntStd();
95+
var code = result.Stack[3].ToTvmCell().ToBoc();
8896

89-
return (balance, owner, minter);
97+
return (balance, owner, minter, code);
98+
}
99+
100+
/// <summary>
101+
/// Executes 'get_jetton_data' method on Jetton Minter contract, returns information about this jetton.
102+
/// </summary>
103+
/// <param name="tonClient"><see cref="ITonClient"/> instance.</param>
104+
/// <param name="jettonMinterAddress">Jetton Minter address to obtain info for.</param>
105+
/// <returns>
106+
/// <list type="table">
107+
/// <item><i>totalSupply</i> - the total number of issues jettons</item>
108+
/// <item><i>mintable</i> - flag which indicates whether number of jettons can increase</item>
109+
/// <item><i>adminAddress</i> - address of smart-contract which control Jetton</item>
110+
/// <item><i>jettonContent</i> - data in accordance to Token Data Standard #64</item>
111+
/// <item><i>jettonWalletCode</i> - code of wallet for that jetton</item>
112+
/// </list>
113+
/// </returns>
114+
/// <remarks>Jetton contract must be deployed and active (to execute get-method).</remarks>
115+
/// <exception cref="TonLibNonZeroExitCodeException" />
116+
/// <seealso href="https://github.com/ton-blockchain/token-contract/blob/main/ft/jetton-wallet.fc#L246">Source of 'get_wallet_address' method.</seealso>
117+
public async Task<(BigInteger totalSupply, bool mintable, string? adminAddress, Boc jettonContent, Boc jettonWalletCode)> GetJettonData(ITonClient tonClient, string jettonMinterAddress)
118+
{
119+
await tonClient.InitIfNeeded().ConfigureAwait(false);
120+
121+
var smc = await tonClient.SmcLoad(new Types.AccountAddress(jettonMinterAddress)).ConfigureAwait(false);
122+
123+
// (int total_supply, int mintable, slice admin_address, cell jetton_content, cell jetton_wallet_code)
124+
var result = await tonClient.SmcRunGetMethod(smc.Id, new MethodIdName("get_jetton_data")).ConfigureAwait(false);
125+
126+
await tonClient.SmcForget(smc.Id).ConfigureAwait(false);
127+
128+
TonLibNonZeroExitCodeException.ThrowIfNonZero(result.ExitCode);
129+
130+
var totalSupply = BigInteger.Parse(result.Stack[0].ToTvmNumberDecimal(), CultureInfo.InvariantCulture);
131+
var mintable = int.Parse(result.Stack[1].ToTvmNumberDecimal(), CultureInfo.InvariantCulture) != 0;
132+
var adminAddress = result.Stack[2].ToTvmCell().ToBoc().RootCells[0].BeginRead().TryLoadAddressIntStd();
133+
var jettonContent = result.Stack[3].ToTvmCell().ToBoc();
134+
var jettonWalletCode = result.Stack[4].ToTvmCell().ToBoc();
135+
136+
return (totalSupply, mintable, adminAddress, jettonContent, jettonWalletCode);
90137
}
91138

92139
/// <summary>

0 commit comments

Comments
 (0)