Spaces:
Sleeping
Sleeping
File size: 183,906 Bytes
788c6a1 |
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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 |
# **TVM Solidity API**
TVM Solidity compiler expands Solidity language with different API functions to facilitate TVM contract development.
When deploying contracts, you should use the latest released version of Solidity. Apart from exceptional cases, only the latest version receives security fixes. Furthermore, breaking changes, as well as new features, are introduced regularly. We currently use a 0.y.z version number to indicate this fast pace of change.
## Table of Contents
* [Compiler version](#compiler-version)
* [TVM specific types](#tvm-specific-types)
* [TVM units](#tvm-units)
* [TvmCell](#tvmcell)
* [constructing TvmCell](#constructing-tvmcell)
* [\<TvmCell\>.depth()](#tvmcelldepth)
* [\<TvmCell\>.dataSize()](#tvmcelldatasize)
* [\<TvmCell\>.dataSizeQ()](#tvmcelldatasizeq)
* [\<TvmCell\>.toSlice()](#tvmcelltoslice)
* [\<TvmCell\>.exoticToSlice()](#tvmcellexotictoslice)
* [\<TvmCell\>.loadExoticCell() and \<TvmCell\>.loadExoticCellQ()](#tvmcellloadexoticcell-and-tvmcellloadexoticcellq)
* [TvmSlice](#tvmslice)
* [\<TvmSlice\>.empty(), \<TvmSlice\>.bitEmpty() and \<TvmSlice\>.refEmpty()](#tvmsliceempty-tvmslicebitempty-and-tvmslicerefempty)
* [\<TvmSlice\>.size()](#tvmslicesize)
* [\<TvmSlice\>.bits()](#tvmslicebits)
* [\<TvmSlice\>.refs()](#tvmslicerefs)
* [\<TvmSlice\>.dataSize()](#tvmslicedatasize)
* [\<TvmSlice\>.dataSizeQ()](#tvmslicedatasizeq)
* [\<TvmSlice\>.depth()](#tvmslicedepth)
* [\<TvmSlice\>.hasNBits(), \<TvmSlice\>.hasNRefs() and \<TvmSlice\>.hasNBitsAndRefs()](#tvmslicehasnbits-tvmslicehasnrefs-and-tvmslicehasnbitsandrefs)
* [\<TvmSlice\>.compare()](#tvmslicecompare)
* [\<TvmSlice\>.startsWith()](#tvmslicestartswith)
* [\<TvmSlice\>.startsWithOne()](#tvmslicestartswithone)
* [TvmSlice load primitives](#tvmslice-load-primitives)
* [\<TvmSlice\>.load()](#tvmsliceload)
* [\<TvmSlice\>.loadQ()](#tvmsliceloadq)
* [\<TvmSlice\>.loadRef()](#tvmsliceloadref)
* [\<TvmSlice\>.loadRefAsSlice()](#tvmsliceloadrefasslice)
* [\<TvmSlice\>.loadInt() and \<TvmSlice\>.loadIntQ()](#tvmsliceloadint-and-tvmsliceloadintq)
* [\<TvmSlice\>.loadUint() and \<TvmSlice\>.loadUintQ()](#tvmsliceloaduint-and-tvmsliceloaduintq)
* [Load little-endian integers](#load-little-endian-integers)
* [\<TvmSlice\>.loadSlice() and \<TvmSlice\>.loadSliceQ()](#tvmsliceloadslice-and-tvmsliceloadsliceq)
* [\<TvmSlice\>.skip()](#tvmsliceskip)
* [\<TvmSlice\>.loadZeroes(), \<TvmSlice\>.loadOnes() and \<TvmSlice\>.loadSame()](#tvmsliceloadzeroes-tvmsliceloadones-and-tvmsliceloadsame)
* [TvmSlice preload primitives](#tvmslice-preload-primitives)
* [\<TvmSlice\>.preload()](#tvmslicepreload)
* [\<TvmSlice\>.preloadQ()](#tvmslicepreloadq)
* [\<TvmSlice\>.preloadRef()](#tvmslicepreloadref)
* [\<TvmSlice\>.preloadInt() and \<TvmSlice\>.preloadIntQ()](#tvmslicepreloadint-and-tvmslicepreloadintq)
* [\<TvmSlice\>.preloadUint() and \<TvmSlice\>.preloadUintQ()](#tvmslicepreloaduint-and-tvmslicepreloaduintq)
* [Preload little-endian integers](#preload-little-endian-integers)
* [\<TvmSlice\>.preloadSlice() and \<TvmSlice\>.preloadSliceQ()](#tvmslicepreloadslice-and-tvmslicepreloadsliceq)
* [TvmBuilder](#tvmbuilder)
* [\<TvmBuilder\>.toSlice()](#tvmbuildertoslice)
* [\<TvmBuilder\>.toCell()](#tvmbuildertocell)
* [\<TvmBuilder\>.toExoticCell()](#tvmbuildertoexoticcell)
* [\<TvmBuilder\>.size()](#tvmbuildersize)
* [\<TvmBuilder\>.bits()](#tvmbuilderbits)
* [\<TvmBuilder\>.refs()](#tvmbuilderrefs)
* [\<TvmBuilder\>.remBits()](#tvmbuilderrembits)
* [\<TvmBuilder\>.remRefs()](#tvmbuilderremrefs)
* [\<TvmBuilder\>.remBitsAndRefs()](#tvmbuilderrembitsandrefs)
* [\<TvmBuilder\>.depth()](#tvmbuilderdepth)
* [\<TvmBuilder\>.store()](#tvmbuilderstore)
* [\<TvmBuilder\>.storeQ()](#tvmbuilderstoreq)
* [\<TvmBuilder\>.storeZeroes(), \<TvmBuilder\>.storeOnes() and \<TvmBuilder\>.storeSame()](#tvmbuilderstorezeroes-tvmbuilderstoreones-and-tvmbuilderstoresame)
* [\<TvmBuilder\>.storeInt()](#tvmbuilderstoreint)
* [\<TvmBuilder\>.storeUint()](#tvmbuilderstoreuint)
* [Store little-endian integers](#store-little-endian-integers)
* [\<TvmBuilder\>.storeRef()](#tvmbuilderstoreref)
* [StringBuilder](#stringbuilder)
* [\<StringBuilder\>.append()](#stringbuilderappend)
* [\<StringBuilder\>.toString()](#stringbuildertostring)
* [optional(T)](#optionalt)
* [constructing an optional](#constructing-an-optional)
* [\<optional(T)\>.hasValue()](#optionalthasvalue)
* [\<optional(T)\>.get()](#optionaltget)
* [\<optional(T)\>.getOr()](#optionaltgetor)
* [\<optional(T)\>.getOrDefault()](#optionaltgetordefault)
* [\<optional(T)\>.set()](#optionaltset)
* [Keyword `null`](#keyword-null)
* [variant](#variant)
* [variant.isUint()](#variantisuint)
* [variant.toUint()](#varianttouint)
* [vector(T)](#vectort)
* [\<vector(T)\>.push()](#vectortpush)
* [\<vector(T)\>.pop()](#vectortpop)
* [\<vector(T)\>.last()](#vectortlast)
* [\<vector(T)\>.operator[]](#vectortoperator)
* [\<vector(T)\>.length()](#vectortlength)
* [\<vector(T)\>.empty()](#vectortempty)
* [stack(T)](#stackt)
* [\<stack(T)\>.push()](#stacktpush)
* [\<stack(T)\>.pop()](#stacktpop)
* [\<stack(T)\>.top()](#stackttop)
* [\<stack(T)\>.empty()](#stacktempty)
* [\<stack(T)\>.sort()](#stacktsort)
* [\<stack(T)\>.reverse()](#stacktreverse)
* [TVM specific control structures](#tvm-specific-control-structures)
* [Range-based for loop](#range-based-for-loop)
* [repeat](#repeat)
* [try-catch](#try-catch)
* [unchecked block](#unchecked-block)
* [Changes and extensions in Solidity types](#changes-and-extensions-in-solidity-types)
* [Integers](#integers)
* [\<Integer\>.cast()](#integercast)
* [bitSize() and uBitSize()](#bitsize-and-ubitsize)
* [Quiet arithmetic](#quiet-arithmetic)
* [qintN and quintN](#qintn-and-quintn)
* [qbool](#qbool)
* [Keyword NaN](#keyword-nan)
* [\<T\>.isNaN()](#tisnan)
* [\<T\>.get()](#tget)
* [\<T\>.getOr()](#tgetor)
* [\<T\>.getOrDefault()](#tgetordefault)
* [\<T\>.toOptional()](#ttooptional)
* [varint and varuint](#varint-and-varuint)
* [struct](#struct)
* [struct constructor](#struct-constructor)
* [\<struct\>.unpack()](#structunpack)
* [Arrays](#arrays)
* [Array literals](#array-literals)
* [Creating new arrays](#creating-new-arrays)
* [\<array\>.empty()](#arrayempty)
* [bytesN](#bytesn)
* [bytes](#bytes)
* [\<bytes\>.empty()](#bytesempty)
* [\<bytes\>.operator[]](#bytesoperator)
* [\<bytes\> slice](#bytes-slice)
* [\<bytes\>.length](#byteslength)
* [\<bytes\>.dataSize()](#bytesdatasize)
* [\<bytes\>.dataSizeQ()](#bytesdatasizeq)
* [\<bytes\>.append()](#bytesappend)
* [bytes conversion](#bytes-conversion)
* [string](#string)
* [\<string\>.empty()](#stringempty)
* [\<string\>.byteLength()](#stringbytelength)
* [\<string\>.substr()](#stringsubstr)
* [\<string\>.append()](#stringappend)
* [\<string\>.operator+](#stringoperator)
* [\<string\>.find() and \<string\>.findLast()](#stringfind-and-stringfindlast)
* [\<string\>.dataSize()](#stringdatasize)
* [\<string\>.dataSizeQ()](#stringdatasizeq)
* [\<string\>.toUpperCase()` and \<string\>.toLowerCase()](#stringtouppercase-and-stringtolowercase)
* [format()](#format)
* [stoi()](#stoi)
* [string conversion](#string-conversion)
* [address](#address)
* [Object creating](#object-creating)
* [constructor()](#constructor)
* [address.makeAddrStd()](#addressmakeaddrstd)
* [address.addrNone](#addressaddrnone)
* [address.makeAddrExtern()](#addressmakeaddrextern)
* [Members](#members)
* [\<address\>.wid](#addresswid)
* [\<address\>.value](#addressvalue)
* [\<address\>.balance](#addressbalance)
* [\<address\>.currencies](#addresscurrencies)
* [Functions](#functions)
* [\<address\>.getType()](#addressgettype)
* [\<address\>.isStdZero()](#addressisstdzero)
* [\<address\>.isStdAddrWithoutAnyCast()](#addressisstdaddrwithoutanycast)
* [\<address\>.isExternZero()](#addressisexternzero)
* [\<address\>.isNone()](#addressisnone)
* [\<address\>.unpack()](#addressunpack)
* [\<address\>.transfer()](#addresstransfer)
* [mapping](#mapping)
* [Keyword `emptyMap`](#keyword-emptymap)
* [\<mapping\>.operator[]](#mappingoperator)
* [\<mapping\>.at()](#mappingat)
* [\<mapping\>.min() and \<mapping\>.max()](#mappingmin-and-mappingmax)
* [\<mapping\>.next() and \<mapping\>.prev()](#mappingnext-and-mappingprev)
* [\<mapping\>.nextOrEq() and \<mapping\>.prevOrEq()](#mappingnextoreq-and-mappingprevoreq)
* [\<mapping\>.delMin() and \<mapping\>.delMax()](#mappingdelmin-and-mappingdelmax)
* [\<mapping\>.fetch()](#mappingfetch)
* [\<mapping\>.exists()](#mappingexists)
* [\<mapping\>.empty()](#mappingempty)
* [\<mapping\>.replace()](#mappingreplace)
* [\<mapping\>.add()](#mappingadd)
* [\<mapping\>.getSet()](#mappinggetset)
* [\<mapping\>.getAdd()](#mappinggetadd)
* [\<mapping\>.getDel()](#mappinggetdel)
* [\<mapping\>.getReplace()](#mappinggetreplace)
* [\<mapping\>.keys() \<mapping\>.values()](#mappingkeys-mappingvalues)
* [Fixed point number](#fixed-point-number)
* [Function type](#function-type)
* [User-defined type](#user-defined-type)
* [require, revert](#require-revert)
* [require](#require)
* [revert](#revert)
* [Libraries](#libraries)
* [Function call via library name](#function-call-via-library-name)
* [Function call via object](#function-call-via-object)
[Free function call via object](#free-function-call-via-object)
* [Pragmas](#pragmas)
* [pragma tvm-solidity](#pragma-tvm-solidity)
* [pragma copyleft](#pragma-copyleft)
* [pragma ignoreIntOverflow](#pragma-ignoreintoverflow)
* [pragma AbiHeader](#pragma-abiheader)
* [pragma msgValue](#pragma-msgvalue)
* [pragma upgrade func/oldsol](#pragma-upgrade-funcoldsol)
* [State variables](#state-variables)
* [Decoding state variables](#decoding-state-variables)
* [Keyword `constant`](#keyword-constant)
* [Keyword `static`](#keyword-static)
* [Keyword `nostorage`](#keyword-nostorage)
* [Keyword `public`](#keyword-public)
* [Special contract functions](#special-contract-functions)
* [receive](#receive)
* [fallback](#fallback)
* [onBounce](#onbounce)
* [onTickTock](#onticktock)
* [onCodeUpgrade](#oncodeupgrade)
* [afterSignatureCheck](#aftersignaturecheck)
* [Function specifiers](#function-specifiers)
* [Function mutability: pure, view and default](#function-mutability-pure-view-and-default)
* [Keyword inline](#keyword-inline)
* [Assembly](#assembly)
* [functionID()](#functionid)
* [externalMsg and internalMsg](#externalmsg-and-internalmsg)
* [Events and return](#events-and-return)
* [emit](#emit)
* [return](#return)
* [External function calls](#external-function-calls)
* [Delete variables](#delete-variables)
* [API functions and members](#api-functions-and-members)
* [Type information](#type-information)
* [**msg** namespace](#msg-namespace)
* [msg.sender](#msgsender)
* [msg.value](#msgvalue)
* [msg.currencies](#msgcurrencies)
* [msg.pubkey()](#msgpubkey)
* [msg.isInternal, msg.isExternal and msg.isTickTock](#msgisinternal-msgisexternal-and-msgisticktock)
* [msg.createdAt](#msgcreatedat)
* [msg.data](#msgdata)
* [msg.forwardFee](#msgforwardfee)
* [msg.importFee](#msgimportFee)
* [msg.body](#msgbody)
* [msg.hasStateInit](#msghasstateinit)
* [**tvm** namespace](#tvm-namespace)
* [TVM instructions](#tvm-instructions)
* [tvm.accept()](#tvmaccept)
* [tvm.setGasLimit()](#tvmsetgaslimit)
* [tvm.buyGas()](#tvmbuygas)
* [tvm.commit()](#tvmcommit)
* [tvm.rawCommit()](#tvmrawcommit)
* [tvm.getData()](#tvmgetdata)
* [tvm.setData()](#tvmsetdata)
* [tvm.log()](#tvmlog)
* [tvm.hexdump() and tvm.bindump()](#tvmhexdump-and-tvmbindump)
* [tvm.setcode()](#tvmsetcode)
* [tvm.configParam()](#tvmconfigparam)
* [tvm.rawConfigParam()](#tvmrawconfigparam)
* [tvm.rawReserve()](#tvmrawreserve)
* [tvm.initCodeHash()](#tvminitcodehash)
* [Hashing and cryptography](#hashing-and-cryptography)
* [tvm.hash()](#tvmhash)
* [tvm.checkSign()](#tvmchecksign)
* [Deploy contract from contract](#deploy-contract-from-contract)
* [Deploy via new](#deploy-via-new)
* [`stateInit` option usage](#stateinit-option-usage)
* [`code` option usage](#code-option-usage)
* [Other deploy options](#other-deploy-options)
* [Deploy via \<address\>.transfer()](#deploy-via-addresstransfer)
* [Deploy the contract with no constructor](#deploy-the-contract-with-no-constructor)
* [New contract address problem](#new-contract-address-problem)
* [Misc functions from `tvm`](#misc-functions-from-tvm)
* [tvm.code()](#tvmcode)
* [tvm.pubkey()](#tvmpubkey)
* [tvm.setPubkey()](#tvmsetpubkey)
* [tvm.setCurrentCode()](#tvmsetcurrentcode)
* [tvm.resetStorage()](#tvmresetstorage)
* [tvm.exit() and tvm.exit1()](#tvmexit-and-tvmexit1)
* [tvm.sendrawmsg()](#tvmsendrawmsg)
* [**bls** namespace](#bls-namespace)
* [bls.verify()](#blsverify)
* [bls.aggregate()](#blsaggregate)
* [bls.fastAggregateVerify()](#blsfastaggregateverify)
* [bls.aggregateVerify()](#blsaggregateverify)
* [bls.g1Zero() and bls.g2Zero()](#blsg1zero-and-blsg2zero)
* [bls.g1IsZero() and bls.g2IsZero()](#blsg1iszero-and-blsg2iszero)
* [bls.g1Add() and bls.g2Add()](#blsg1add-and-blsg2add)
* [bls.g1Sub() and bls.g2Sub()](#blsg1sub-and-blsg2sub)
* [bls.g1Neg() and bls.g2Neg()](#blsg1neg-and-blsg2neg)
* [bls.g1Mul() and bls.g2Mul()](#blsg1mul-and-blsg2mul)
* [bls.g1InGroup() and bls.g2InGroup()](#blsg1ingroup-and-blsg2ingroup)
* [bls.r()](#blsr)
* [bls.g1MultiExp() and bls.g2MultiExp()](#blsg1multiexp-and-blsg2multiexp)
* [**math** namespace](#math-namespace)
* [math.min() math.max()](#mathmin-mathmax)
* [math.minmax()](#mathminmax)
* [math.abs()](#mathabs)
* [math.modpow2()](#mathmodpow2)
* [math.divr() math.divc()](#mathdivr-mathdivc)
* [math.divmod()](#mathdivmod)
* [math.muldiv() math.muldivr() math.muldivc()](#mathmuldiv-mathmuldivr-mathmuldivc)
* [math.muldivmod()](#mathmuldivmod)
* [math.mulmod()](#mathmulmod)
* [math.sign()](#mathsign)
* [**tx** namespace](#tx-namespace)
* [tx.logicaltime](#txlogicaltime)
* [tx.storageFee](#txstoragefee)
* [**block** namespace](#block-namespace)
* [block.timestamp](#blocktimestamp)
* [block.logicaltime](#blocklogicaltime)
* [**rnd** namespace](#rnd-namespace)
* [rnd.next](#rndnext)
* [rnd.getSeed](#rndgetseed)
* [rnd.setSeed](#rndsetseed)
* [rnd.shuffle](#rndshuffle)
* [**abi** namespace](#abi-namespace)
* [abi.encode(), abi.decode()](#abiencode-abidecode)
* [abi.encodeData()](#abiencodedata)
* [abi.encodeOldDataInit()](#abiencodeolddatainit)
* [abi.decodeData()](#abidecodedata)
* [abi.encodeStateInit()](#abiencodestateinit)
* [abi.stateInitHash()](#abistateinithash)
* [abi.encodeBody()](#abiencodebody)
* [abi.decodeFunctionParams()](#abidecodefunctionparams)
* [abi.codeSalt()](#abicodesalt)
* [abi.setCodeSalt()](#abisetcodesalt)
* [abi.functionId()](#abifunctionid)
* [abi.encodeIntMsg()](#abiencodeintmsg)
* [**gosh** namespace](#gosh-namespace)
* [gosh.diff and gosh.zipDiff](#goshdiff-and-goshzipdiff)
* [gosh.applyPatch, gosh.applyPatchQ, gosh.applyZipPatch, gosh.applyZipPatchQ, gosh.applyZipBinPatch and gosh.applyZipBinPatchQ](#goshapplypatch-goshapplypatchq-goshapplyzippatch-goshapplyzippatchq-goshapplyzipbinpatch-and-goshapplyzipbinpatchq)
* [gosh.zip and gosh.unzip](#goshzip-and-goshunzip)
* [gosh.mintshell and gosh.mintshellq](#goshmintshell-and-goshmintshellq)
* [gosh.sendtodappconfig](#goshsendtodappconfig)
* [gosh.getavailablebalance](#goshgetavailablebalance)
* [gosh.burnecc](#goshburnecc)
* [gosh.cnvrtshellq](#goshcnvrtshellq)
* [gosh.mintecc](#goshmintecc)
* [Exponentiation](#exponentiation)
* [selfdestruct](#selfdestruct)
* [sha256](#sha256)
* [gasToValue](#gastovalue)
* [valueToGas](#valuetogas)
* [gasleft](#gasleft)
* [TVM capabilities](#tvm-capabilities)
* [TVM exception codes](#tvm-exception-codes)
* [Solidity runtime errors](#solidity-runtime-errors)
* [Division and rounding](#division-and-rounding)
* [Contract execution](#contract-execution)
* [Gas optimization hints](#gas-optimization-hints)
## Detailed description
### Compiler version
TVM Solidity compiler adds its current version to the generated code. This version can be obtained:
1) using [tvm_linker](https://github.com/everx-labs/TVM-linker#2-decoding-of-boc-messages-prepared-externally) from a `*.tvc` file:
```bash
tvm_linker decode --tvm <tvc-file>
```
2) using [ever-cli](https://github.com/everx-labs/ever-cli#48-decode-commands) from a `*.boc` file, a `*.tvc` file, or a network account:
```bash
ever-cli decode tvc [--tvc] [--boc] <input>
```
### TVM specific types
TVM Solidity compiler expands functionality of some existing types and adds several new TVM specific types: TvmCell, TvmSlice and TvmBuilder. Full description of these types can be found in [TVM][1] and [Blockchain][2] specifications.
#### TVM units
A literal number can take a suffix to specify a subdenomination of currency, where numbers without a postfix are assumed to be nanoevers.
```TVMSolidity
uint a0 = 1 nano; // a0 == 1 == 1e-9 ever
uint a1 = 1 nanoever; // a1 == 1 == 1e-9 ever
uint a3 = 1 ever; // a3 == 1 000 000 000 (1e9)
uint a4 = 1 Ever; // a4 == 1 000 000 000 (1e9)
uint a5 = 1 micro; // a5 == 1 000 == 1e-6 ever
uint a6 = 1 microever; // a6 == 1 000 == 1e-6 ever
uint a7 = 1 milli; // a7 == 1 000 000 == 1e-3 ever
uint a8 = 1 milliever; // a8 == 1 000 000 == 1e-3 ever
uint a9 = 1 kiloever; // a9 == 1 000 000 000 000 (1e12) == 1e3 ever
uint a10 = 1 kEver; // a10 == 1 000 000 000 000 (1e12) == 1e3 ever
uint a11 = 1 megaever; // a11 == 1 000 000 000 000 000 (1e15) == 1e6 ever
uint a12 = 1 MEver; // a12 == 1 000 000 000 000 000 (1e15) == 1e6 ever
uint a13 = 1 gigaever; // a13 == 1 000 000 000 000 000 000 (1e18) == 1e9 ever
uint a14 = 1 GEver; // a14 == 1 000 000 000 000 000 000 (1e18) == 1e9 ever
```
```TVMSolidity
uint a0 = 1 nano; // a0 == 1 == 1e-9 ever
uint a1 = 1 nanovmshell; // a1 == 1 == 1e-9 ever
uint a3 = 1 vmshell; // a3 == 1 000 000 000 (1e9)
uint a4 = 1 VMShell; // a4 == 1 000 000 000 (1e9)
uint a5 = 1 micro; // a5 == 1 000 == 1e-6 ever
uint a6 = 1 microvmshell; // a6 == 1 000 == 1e-6 ever
uint a7 = 1 milli; // a7 == 1 000 000 == 1e-3 ever
uint a8 = 1 millivmshell; // a8 == 1 000 000 == 1e-3 ever
uint a9 = 1 kilovmshell; // a9 == 1 000 000 000 000 (1e12) == 1e3 ever
uint a10 = 1 kVMShell; // a10 == 1 000 000 000 000 (1e12) == 1e3 ever
uint a11 = 1 megavmshell; // a11 == 1 000 000 000 000 000 (1e15) == 1e6 ever
uint a12 = 1 MVMShell; // a12 == 1 000 000 000 000 000 (1e15) == 1e6 ever
uint a13 = 1 gigavmshell; // a13 == 1 000 000 000 000 000 000 (1e18) == 1e9 ever
uint a14 = 1 GVMShell; // a14 == 1 000 000 000 000 000 000 (1e18) == 1e9 ever
```
```TVMSolidity
uint a0 = 1 nano; // a0 == 1
uint a1 = 1 nanoton; // a1 == 1
uint a2 = 1 nTon; // a2 == 1
uint a3 = 1 ton; // a3 == 1 000 000 000 (1e9)
uint a4 = 1 Ton; // a4 == 1 000 000 000 (1e9)
uint a5 = 1 micro; // a5 == 1 000
uint a6 = 1 microton; // a6 == 1 000
uint a7 = 1 milli; // a7 == 1 000 000
uint a8 = 1 milliton; // a8 == 1 000 000
uint a9 = 1 kiloton; // a9 == 1 000 000 000 000 (1e12)
uint a10 = 1 kTon; // a10 == 1 000 000 000 000 (1e12)
uint a11 = 1 megaton; // a11 == 1 000 000 000 000 000 (1e15)
uint a12 = 1 MTon; // a12 == 1 000 000 000 000 000 (1e15)
uint a13 = 1 gigaton; // a13 == 1 000 000 000 000 000 000 (1e18)
uint a14 = 1 GTon; // a14 == 1 000 000 000 000 000 000 (1e18)
```
#### TvmCell
`TvmCell` represents *TVM cell* ([TVM][1] - 1.1.3). The compiler defines the following
operators and functions to work with this type:
Comparison operators:
`==`, `!=` (evaluate to `bool`)
##### constructing TvmCell
To create empty cell use `TvmCell()`. Example:
```TVMSolidity
TvmCell cell = ...;
if (cell == TvmCell()) { // check whether `cell` is empty
}
```
##### \<TvmCell\>.depth()
```TVMSolidity
<TvmCell>.depth() returns (uint16);
```
Returns the depth **d** of the `TvmCell` **c**. If **c** has no references, then **d** = 0;
otherwise **d** is equal to one plus the maximum of depths of cells referred to from **c**.
If **c** is a Null instead of a Cell, returns zero.
#### \<TvmCell\>.dataSize()
```TVMSolidity
<TvmCell>.dataSize(uint n) returns (uint /*cells*/, uint /*bits*/, uint /*refs*/);
```
Returns the number of distinct cells, data bits in the distinct cells and
cell references in the distinct cells. If number of the distinct cells
exceeds `n+1`, then a cell overflow [exception](#tvm-exception-codes) is thrown.
This function is a wrapper for the `CDATASIZE` opcode ([TVM][1] - A.11.7).
If `CapFastStorageStatBugfix` and `CapFastStorageStat` are set, then calculate number of total (not distinct) cells and bits. For example:
```TVMSolidity
TvmBuilder b2;
b2.storeOnes(5);
TvmBuilder b;
b.storeOnes(10);
b.storeRef(b2);
b.storeRef(b2);
b.storeRef(b2);
TvmCell cell = b.toCell();
(uint cells, uint bits, uint refs) = cell.dataSize(100);
// If `CapFastStorageStatBugfix` and `CapFastStorageStat` are set
// cells == 4
// bits == 10 + 3 * 5
// refs == 3
// Otherwise:
// cells == 2
// bits == 5 + 10
// refs == 3
```
#### \<TvmCell\>.dataSizeQ()
```TVMSolidity
<TvmCell>.dataSizeQ(uint n) returns (optional(uint /*cells*/, uint /*bits*/, uint /*refs*/));
```
Returns the number of distinct cells, data bits in the distinct cells and
cell references in the distinct cells. If number of the distinct cells
exceeds `n+1`, then this function returns an `optional` that has no value.
This function is a wrapper for the `CDATASIZEQ` opcode ([TVM][1] - A.11.7).
##### \<TvmCell\>.toSlice()
```TVMSolidity
<TvmCell>.toSlice() returns (TvmSlice);
```
Converts a `TvmCell` to `TvmSlice`.
If the cell is exotic, then the cell is automatically loaded and converted to `TvmSlice`. For example:
```
TvmCell cellProof = ...;
TvmBuilder b;
b.store(
uint8(3), // type of MerkleProof exotic cell
tvm.hash(cellProof),
cellProof.depth(),
cellProof
);
TvmCell merkleProof = b.toExoticCell();
TvmSlice s = merkleProof.toSlice();
// `s` has the same data and references as `cellProof`
```
If you want load the cell as is, then see [\<TvmCell\>.exoticToSlice()](#tvmcellexotictoslice).
##### \<TvmCell\>.exoticToSlice()
```TVMSolidity
<TvmCell>.exoticToSlice() returns (TvmSlice, bool)
```
Converts an ordinary or exotic cell into a `TvmSlice`, as if it were an ordinary cell. A flag is returned indicating whether
`TvmCell` is exotic. If that be the case, its type can later be deserialized from the first eight bits of `TvmSlice`.
Example:
```TVMSolidity
TvmCell cellProof = ...;
TvmBuilder b;
b.store(
uint8(3), // type of MerkleProof exotic cell
tvm.hash(cellProof),
cellProof.depth(),
cellProof
);
{
// convert builder to exotic cell
TvmCell merkleProof = b.toExoticCell();
(TvmSlice s, bool isExotic) = merkleProof.exoticToSlice();
// isExotic == true
uint8 flag = s.load(uint8); // flag == 3
}
{
// convert builder to ordinary cell
TvmCell cell = b.toCell();
(TvmSlice s, bool isExotic) = cell.exoticToSlice();
// isExotic == false
uint8 flag = s.load(uint8); // flag == 3
}
```
See also:
* [\<TvmBuilder\>.toExoticCell()](#tvmbuildertoexoticcell)
#### \<TvmCell\>.loadExoticCell() and \<TvmCell\>.loadExoticCellQ()
```TVMSolidity
(1)
<TvmCell>.loadExoticCell() returns (TvmCell)
(2)
<TvmCell>.loadExoticCellQ() returns (TvmCell cell, bool ok)
```
(1) Loads an exotic cell and returns an ordinary cell. If the cell is already ordinary, does nothing. If it cannot be loaded, throws an exception. It is wrapper for opcode `XLOAD`.
(2) Same as (1) but if it cannot be loaded, does not throw exception and `ok` is equal to `false`. It is wrapper for opcode `XLOADQ`.
Example:
```TVMSolidity
TvmCell cellProof = ...;
TvmBuilder b;
b.store(
uint8(3), // type of MerkleProof exotic cell
tvm.hash(cellProof),
cellProof.depth(),
cellProof
);
TvmCell cell = merkleProof.loadExoticCell(); // cell == cellProof
(TvmCell cell, bool ok) = merkleProof.loadExoticCellQ();
// cell == cellProof
// ok == true
```
#### TvmSlice
`TvmSlice` represents *TVM cell slice* ([TVM][1] - 1.1.3). The compiler defines the following
operators and functions to work with this type:
Comparison operators:
`<=`, `<`, `==`, `!=`, `>=`, `>` (evaluate to bool).
**Note:** only data bits from the root cells are compared. References are ignored.
String literals can be converted to `TvmSlice`:
```TVMSolidity
TvmSlice s = "0189abef_";
```
`TvmSlice` can be converted to `bytes`. It costs at least 500 gas units.
##### \<TvmSlice\>.empty(), \<TvmSlice\>.bitEmpty() and \<TvmSlice\>.refEmpty()
```TVMSolidity
(1)
<TvmSlice>.empty() returns (bool);
(2)
<TvmSlice>.bitEmpty() returns (bool);
3()
<TvmSlice>.refEmpty() returns (bool);
```
(1)
Checks whether the `TvmSlice` contains no data bits and no cell references.
(2)
Checks whether the `TvmSlice` contains no data bits.
(3)
Checks whether the `TvmSlice` contains no cell references.
##### \<TvmSlice\>.size()
```TVMSolidity
<TvmSlice>.size() returns (uint16 /*bits*/, uint8 /*refs*/);
```
Returns the number of data bits and references in the `TvmSlice`.
##### \<TvmSlice\>.bits()
```TVMSolidity
<TvmSlice>.bits() returns (uint16);
```
Returns the number of data bits in the `TvmSlice`.
##### \<TvmSlice\>.refs()
```TVMSolidity
<TvmSlice>.refs() returns (uint8);
```
Returns the number of references in the `TvmSlice`.
#### \<TvmSlice\>.dataSize()
```TVMSolidity
<TvmSlice>.dataSize(uint n) returns (uint /*cells*/, uint /*bits*/, uint /*refs*/);
```
Returns the number of distinct cells, data bits in the distinct cells and
cell references in the distinct cells. If number of the distinct cells
exceeds `n+1`, then a cell overflow [exception](#tvm-exception-codes) is thrown.
Note that the returned `count of distinct cells` does not take into
account the cell that contains the slice itself.
This function is a wrapper for `SDATASIZE` opcode ([TVM][1] - A.11.7).
#### \<TvmSlice\>.dataSizeQ()
```TVMSolidity
<TvmSlice>.dataSizeQ(uint n) returns (optional(uint /*cells*/, uint /*bits*/, uint /*refs*/));
```
Returns the number of distinct cells, data bits in the distinct cells and
cell references in the distinct cells. If number of the distinct cells
exceeds `n+1`, then this function returns an `optional` that has no value.
Note that the returned `count of distinct cells` does not take into
account the cell that contains the slice itself.
This function is a wrapper for `SDATASIZEQ` opcode ([TVM][1] - A.11.7).
##### \<TvmSlice\>.depth()
```TVMSolidity
<TvmSlice>.depth() returns (uint16);
```
Returns the depth of `TvmSlice`. If the `TvmSlice` has no references, then 0 is returned,
otherwise function result is one plus the maximum of depths of the cells referred to from the slice.
##### \<TvmSlice\>.hasNBits(), \<TvmSlice\>.hasNRefs() and \<TvmSlice\>.hasNBitsAndRefs()
```TVMSolidity
<TvmSlice>.hasNBits(uint10 bits) returns (bool);
<TvmSlice>.hasNRefs(uint2 refs) returns (bool);
<TvmSlice>.hasNBitsAndRefs(uint10 bits, uint2 refs) returns (bool);
```
Checks whether the `TvmSlice` contains the specified amount of data bits and references.
##### \<TvmSlice\>.compare()
```TVMSolidity
<TvmSlice>.compare(TvmSlice other) returns (int2);
```
Lexicographically compares the `slice` and `other` data bits of the root slices and returns result as an integer:
* 1 - `slice` > `other`
* 0 - `slice` == `other`
* -1 - `slice` < `other`
##### \<TvmSlice\>.startsWith()
```TVMSolidity
<TvmSlice>.startsWith(TvmSlice prefix) returns (bool);
```
Checks whether `prefix` is a prefix of `TvmSlice`.
##### \<TvmSlice\>.startsWithOne()
```TVMSolidity
<TvmSlice>.startsWithOne() returns (bool);
```
Checks whether the first bit of `TvmSlice` is a one.
##### TvmSlice load primitives
All `load*` functions below modify the `TvmSlice` object. If you wants to load second reference from the `TvmSlice`, you should load the first one with [\<TvmSlice\>.loadRef()](#tvmsliceloadref) and then load the reference you need. The same rule is applied to data bits. To load bits from 2 to 10 positions, you should load or skip first two bits.
###### \<TvmSlice\>.load()
```TVMSolidity
<TvmSlice>.load(TypeA, TypeB, ...) returns (TypeA /*a*/, TypeB /*b*/, ...);
```
Sequentially loads values of the specified types from the `TvmSlice`.
Supported types: `uintN`, `intN`, `bytesN`, `bool`, `ufixedMxN`, `fixedMxN`, `address`, `contract`,
`TvmCell`, `bytes`, `string`, `mapping`, `array`, `optional` and
`struct`. Example:
```TVMSolidity
TvmSlice slice = ...;
(uint8 a, uint16 b) = slice.load(uint8, uint16);
(uint16 num0, uint32 num1, address addr) = slice.load(uint16, uint32, address);
```
See also: [\<TvmBuilder\>.store()](#tvmbuilderstore).
**Note**: if all the argument types can't be loaded from the slice a cell underflow [exception](#tvm-exception-codes) is thrown.
###### \<TvmSlice\>.loadQ()
```TVMSolidity
<TvmSlice>.loadQ(TypeA, TypeB, ...) returns (optional(TypeA, TypeB, ...));
```
Sequentially decodes values of the specified types from the `TvmSlice`
if the `TvmSlice` holds sufficient data for all specified types. Otherwise, returns `null`.
```TVMSolidity
TvmSlice slice = ...;
optional(uint) a = slice.loadQ(uint);
optional(uint8, uint16) b = slice.loadQ(uint8, uint16);
```
See also: [\<TvmBuilder\>.store()](#tvmbuilderstore).
###### \<TvmSlice\>.loadRef()
```TVMSolidity
<TvmSlice>.loadRef() returns (TvmCell);
```
Loads a cell from the `TvmSlice` reference.
###### \<TvmSlice\>.loadRefAsSlice()
```TVMSolidity
<TvmSlice>.loadRefAsSlice() returns (TvmSlice);
```
Loads a cell from the `TvmSlice` reference and converts it into a `TvmSlice`.
###### \<TvmSlice\>.loadInt() and \<TvmSlice\>.loadIntQ()
```TVMSolidity
(1)
<TvmSlice>.loadInt(uint9 bitSize) returns (int);
(2)
<TvmSlice>.loadIntQ(uint9 bitSize) returns (optional(int));
```
(1) Loads a signed integer with the given **bitSize** from the `TvmSlice`.
(2) Loads a signed integer with the given **bitSize** from the `TvmSlice` if `TvmSlice` contains it. Otherwise, returns `null`.
###### \<TvmSlice\>.loadUint() and \<TvmSlice\>.loadUintQ()
```TVMSolidity
(1)
<TvmSlice>.loadUint(uint9 bitSize) returns (uint);
(2)
<TvmSlice>.loadUintQ(uint9 bitSize) returns (optional(uint));
```
(1) Loads an unsigned integer with the given **bitSize** from the `TvmSlice`.
(2) Loads an unsigned integer with the given **bitSize** from the `TvmSlice` if `TvmSlice` contains it. Otherwise, returns `null`.
###### Load little-endian integers
```TVMSolidity
(1)
<TvmSlice>.loadIntLE2() returns (int16)
<TvmSlice>.loadIntLE4() returns (int32)
<TvmSlice>.loadIntLE8() returns (int64)
<TvmSlice>.loadUintLE2() returns (uint16)
<TvmSlice>.loadUintLE4() returns (uint32)
<TvmSlice>.loadUintLE8() returns (uint64)
(2)
<TvmSlice>.loadIntLE4Q() returns (optional(int32))
<TvmSlice>.loadIntLE8Q() returns (optional(int64))
<TvmSlice>.loadUintLE4Q() returns (optional(uint32))
<TvmSlice>.loadUintLE8Q() returns (optional(uint64))
```
(1) Loads the little-endian integer from `TvmSlice`.
(2) Same as (1) but returns `null` if it's impossible to load the integer.
###### \<TvmSlice\>.loadSlice() and \<TvmSlice\>.loadSliceQ()
```TVMSolidity
(1)
<TvmSlice>.loadSlice(uint10 bits) returns (TvmSlice);
(2)
<TvmSlice>.loadSlice(uint10 bits, uint2 refs) returns (TvmSlice);
(3)
<TvmSlice>.loadSliceQ(uint10 bits) returns (optional(TvmSlice));
(4)
<TvmSlice>.loadSliceQ(uint10 bits, uint2 refs) returns (optional(TvmSlice));
```
(1) Loads the first `bits` bits from `TvmSlice`.
(2) Loads the first `bits` bits and `refs` references from `TvmSlice`.
(3) and (4) are same as (1) and (2) but return `optional` type.
###### \<TvmSlice\>.skip()
```TVMSolidity
<TvmSlice>.skip(uint10 bits);
<TvmSlice>.skip(uint10 bits, uint3 refs);
```
Skips the first `bits` bits and `refs` references from the `TvmSlice`.
###### \<TvmSlice\>.loadZeroes(), \<TvmSlice\>.loadOnes() and \<TvmSlice\>.loadSame()
```TVMSolidity
(1)
<TvmSlice>.loadZeroes() returns (uint10 n);
(2)
<TvmSlice>.loadOnes() returns (uint10 n);
(3)
<TvmSlice>.loadSame(uint1 value) returns (uint10 n);
```
(1) Returns the count `n` of leading zero bits in `TvmSlice`, and removes these bits from `TvmSlice`.
(2) Returns the count `n` of leading one bits in `TvmSlice`, and removes these bits from `TvmSlice`.
(3) Returns the count `n` of leading bits equal to `0 β€ value β€ 1` in `TvmSlice`, and removes these bits from `TvmSlice`.
See also: [\<TvmBuilder\>.storeZeroes(), \<TvmBuilder\>.storeOnes() and \<TvmBuilder\>.storeSame()](#tvmbuilderstorezeroes-tvmbuilderstoreones-and-tvmbuilderstoresame).
##### TvmSlice preload primitives
All `preload*` functions below don't modify the `TvmSlice` object.
###### \<TvmSlice\>.preload()
```TVMSolidity
<TvmSlice>.preload(TypeA, TypeB, ...) returns (TypeA /*a*/, TypeB /*b*/, ...);
```
Same as [\<TvmSlice\>.load()](#tvmsliceload) but doesn't modify `TvmSlice`.
###### \<TvmSlice\>.preloadQ()
```TVMSolidity
<TvmSlice>.preloadQ(TypeA, TypeB, ...) returns (optional(TypeA, TypeB, ...));
```
Same as [\<TvmSlice\>.loadQ()](#tvmsliceloadq) but doesn't modify `TvmSlice`.
###### \<TvmSlice\>.preloadRef()
```TVMSolidity
(1)
<TvmSlice>.preloadRef() returns (TvmCell);
(2)
<TvmSlice>.preloadRef(uint2 index) returns (TvmCell);
```
(1) Returns the first cell reference of `TvmSlice`.
(2) Returns the `index` cell reference of `TvmSlice`, where `0 β€ index β€ 3`.
###### \<TvmSlice\>.preloadInt() and \<TvmSlice\>.preloadIntQ()
```TVMSolidity
(1)
<TvmSlice>.preloadInt(uint9 bitSize) returns (int);
(2)
<TvmSlice>.preloadIntQ(uint9 bitSize) returns (optional(int));
```
Same as [\<TvmSlice\>.loadInt() and \<TvmSlice\>.loadIntQ()](#tvmsliceloadint-and-tvmsliceloadintq) but doesn't modify `TvmSlice`.
###### \<TvmSlice\>.preloadUint() and \<TvmSlice\>.preloadUintQ()
```TVMSolidity
(1)
<TvmSlice>.preloadUint(uint9 bitSize) returns (uint);
(2)
<TvmSlice>.preloadUintQ(uint9 bitSize) returns (optional(uint));
```
Same as [\<TvmSlice\>.loadUint() and \<TvmSlice\>.loadUintQ()](#tvmsliceloaduint-and-tvmsliceloaduintq) but doesn't modify `TvmSlice`.
###### Preload little-endian integers
```TVMSolidity
<TvmSlice>.preloadIntLE4() returns (int32)
<TvmSlice>.preloadIntLE8() returns (int64)
<TvmSlice>.preloadUintLE4() returns (uint32)
<TvmSlice>.preloadUintLE8() returns (uint64)
<TvmSlice>.preloadIntLE4Q() returns (optional(int32))
<TvmSlice>.preloadIntLE8Q() returns (optional(int64))
<TvmSlice>.preloadUintLE4Q() returns (optional(uint32))
<TvmSlice>.preloadUintLE8Q() returns (optional(uint64))
```
Same as [Load little-endian integers](#load-little-endian-integers) but doesn't modify `TvmSlice`.
###### \<TvmSlice\>.preloadSlice() and \<TvmSlice\>.preloadSliceQ()
```TVMSolidity
(1)
<TvmSlice>.preloadSlice(uint10 bits) returns (TvmSlice);
(2)
<TvmSlice>.preloadSlice(uint10 bits, uint refs) returns (TvmSlice);
(3)
<TvmSlice>.preloadSliceQ(uint10 bits) returns (optional(TvmSlice));
(4)
<TvmSlice>.preloadSliceQ(uint10 bits, uint4 refs) returns (optional(TvmSlice));
```
Same as [\<TvmSlice\>.loadSlice() and \<TvmSlice\>.loadSliceQ()](#tvmsliceloadslice-and-tvmsliceloadsliceq) but doesn't modify `TvmSlice`.
#### TvmBuilder
`TvmBuilder` represents *TVM cell builder* ([TVM][1] - 1.1.3). TVM Solidity compiler defines the following
functions to work with this type:
##### \<TvmBuilder\>.toSlice()
```TVMSolidity
<TvmBuilder>.toSlice() returns (TvmSlice);
```
Converts a `TvmBuilder` into `TvmSlice`.
##### \<TvmBuilder\>.toCell()
```TVMSolidity
<TvmBuilder>.toCell() returns (TvmCell);
```
Converts a `TvmBuilder` into `TvmCell`.
##### \<TvmBuilder\>.toExoticCell()
```TVMSolidity
<TvmBuilder>.toExoticCell() returns (TvmCell);
```
Creates an exotic cell from `TvmBuilder`. It is wrapper for opcodes `TRUE ENDXC`.
Examples:
```TVMSolidity
TvmCell cellProof = getCell();
TvmBuilder b;
b.store(
uint8(3), // type of MerkleProof exotic cell
tvm.hash(cellProof),
cellProof.depth(),
cellProof
);
TvmCell merkleProof = b.toExoticCell();
```
##### \<TvmBuilder\>.size()
```TVMSolidity
<TvmBuilder>.size() returns (uint16 /*bits*/, uint8 /*refs*/);
```
Returns the number of data bits and references already stored in the `TvmBuilder`.
##### \<TvmBuilder\>.bits()
```TVMSolidity
<TvmBuilder>.bits() returns (uint16);
```
Returns the number of data bits already stored in the `TvmBuilder`.
##### \<TvmBuilder\>.refs()
```TVMSolidity
<TvmBuilder>.refs() returns (uint8);
```
Returns the number of references already stored in the `TvmBuilder`.
##### \<TvmBuilder\>.remBits()
```TVMSolidity
<TvmBuilder>.remBits() returns (uint16);
```
Returns the number of data bits that can still be stored in the `TvmBuilder`.
##### \<TvmBuilder\>.remRefs()
```TVMSolidity
<TvmBuilder>.remRefs() returns (uint8);
```
Returns the number of references that can still be stored in the `TvmBuilder`.
##### \<TvmBuilder\>.remBitsAndRefs()
```TVMSolidity
<TvmBuilder>.remBitsAndRefs() returns (uint16 /*bits*/, uint8 /*refs*/);
```
Returns the number of data bits and references that can still be stored in the `TvmBuilder`.
##### \<TvmBuilder\>.depth()
```TVMSolidity
<TvmBuilder>.depth() returns (uint16);
```
Returns the depth of `TvmBuilder`. If no cell references are stored
in the builder, then 0 is returned; otherwise function result is one plus the maximum of
depths of cells referred to from the builder.
##### \<TvmBuilder\>.store()
```TVMSolidity
<TvmBuilder>.store(/*list_of_values*/);
```
Stores the list of values into the `TvmBuilder`.
Internal representation of the stored data:
* `uintN`/`intN`/`bytesN` - stored as an N-bit string.
For example, `uint8(100), int16(-3), bytes2(0xaabb)` stored as `0x64fffdaabb`.
* `bool` - stored as a binary zero for `false` or a binary one for `true`. For example,
`true, false, true` stored as `0xb_`.
* `ufixedMxN`/`fixedMxN` - stored as an M-bit string.
* `address`/`contract` - stored according to the [TL-B scheme][3] of `MsgAddress`.
* `TvmCell`/`bytes`/`string` - stored as a cell in reference.
* `TvmSlice`/`TvmBuilder` - all data bits and references of the `TvmSlice` or the `TvmBuilder`
are appended to the `TvmBuilder`, not in a reference as `TvmCell`. To store `TvmSlice`/`TvmBuilder` in
the references use [\<TvmBuilder\>.storeRef()](#tvmbuilderstoreref).
* `mapping` - stored according to the [TL-B scheme][3] of `HashmapE`: if map is
empty, then stored as a binary zero, otherwise as a binary one and the dictionary `Hashmap` in a reference.
* `array` - stored as a 32 bit value - size of the array and a `HashmapE` that contains all values of
the array.
* `optional` - stored as a binary zero if the `optional` doesn't contain value. Otherwise, stored as
a binary one and the cell with serialized value in a reference.
* `struct` - stored in the order of its members in the builder. Make sure the entire struct fits into the
builder.
**Note:** there is no gap or offset between two consecutive data assets stored in the `TvmBuilder`.
See [TVM][1] to read about notation for bit strings.
Example:
```TVMSolidity
uint8 a = 11;
int16 b = 22;
TvmBuilder builder;
builder.store(a, b, uint(33));
```
See also: [\<TvmSlice\>.load()](#tvmsliceload).
##### \<TvmBuilder\>.storeQ()
```TVMSolidity
<TvmBuilder>.storeQ(T value) returns (bool ok);
```
Same as [\<TvmBuilder\>.store()](#tvmbuilderstore) but returns the success flag. It does not throw exceptions.
Supported types:
* `uintN`/`intN`/`bytesN`
* `bool`
* `ufixedMxN`/`fixedMxN`
* `address`/`contract`
* `TvmCell`/`bytes`/`string`
* `TvmSlice`/`TvmBuilder`
##### \<TvmBuilder\>.storeZeroes(), \<TvmBuilder\>.storeOnes() and \<TvmBuilder\>.storeSame()
```TVMSolidity
(1)
<TvmBuilder>.storeZeroes(uint10 n);
(2)
<TvmBuilder>.storeOnes(uint10 n);
(3)
<TvmBuilder>.storeSame(uint10 n, uint1 value);
```
(1) Stores `n` binary zeroes into the `TvmBuilder`.
(2) Stores `n` binary ones into the `TvmBuilder`.
(3) Stores `n` binary `value`s (0 β€ value β€ 1) into the `TvmBuilder`.
See also: [\<TvmSlice\>.loadZeroes(), \<TvmSlice\>.loadOnes() and \<TvmSlice\>.loadSame()](#tvmsliceloadzeroes-tvmsliceloadones-and-tvmsliceloadsame).
##### \<TvmBuilder\>.storeInt()
```TVMSolidity
<TvmBuilder>.storeInt(int257 value, uint9 bitSize);
```
Stores a signed integer **value** with given **bitSize** in the `TvmBuilder`.
##### \<TvmBuilder\>.storeUint()
```TVMSolidity
<TvmBuilder>.storeUint(uint256 value, uint9 bitSize);
```
Stores an unsigned integer **value** with given **bitSize** in the `TvmBuilder`.
##### Store little-endian integers
```TVMSolidity
<TvmBuilder>.storeIntLE2(int16)
<TvmBuilder>.storeIntLE4(int32)
<TvmBuilder>.storeIntLE8(int64)
<TvmBuilder>.storeUintLE2(uint16)
<TvmBuilder>.storeUintLE4(uint32)
<TvmBuilder>.storeUintLE8(uint64)
```
Stores the little-endian integer.
##### \<TvmBuilder\>.storeRef()
```TVMSolidity
<TvmBuilder>.storeRef(TvmBuilder b);
<TvmBuilder>.storeRef(TvmCell c);
<TvmBuilder>.storeRef(TvmSlice s);
```
Stores `TvmBuilder b`/`TvmCell c`/`TvmSlice s` in the reference of the `TvmBuilder`.
#### StringBuilder
A mutable sequence of characters. `StringBuilder` allows creating a string from `bytes1` and `string` in a gas-efficient way. Example:
```TVMSolidity
StringBuilder b;
b.append(bytes1("-"));
b.append(bytes1("0"), 10);
b.append("1234");
string s = b.toString(); // s == "-00000000001234"
```
##### \<StringBuilder\>.append()
```TVMSolidity
(1)
<StringBuilder>.append(bytes1);
(2)
<StringBuilder>.append(bytes1, uint31 n);
(3)
<StringBuilder>.append(string);
```
(1) Appends `bytes1` to the sequence.
(2) Appends `bytes1` `n` times to the sequence.
(3) Appends `string` to the sequence.
##### \<StringBuilder\>.toString()
```TVMSolidity
<StringBuilder>.toString();
```
Returns a string representing the data in this sequence.
#### optional(T)
The template optional type manages an optional contained value, i.e. a value that may or may not be present.
##### constructing an optional
There are many ways to set a value:
```TVMSolidity
optional(uint) opt;
opt.set(11); // just sets value
opt = 22; // just sets value, too
opt.get() = 33; // if 'opt' has value, then set value. Otherwise throws an exception.
optional(uint) another = 44;
opt = another;
```
##### \<optional(T)\>.hasValue()
```TVMSolidity
<optional(T)>.hasValue() returns (bool);
```
Checks whether the `optional` contains a value.
##### \<optional(T)\>.get()
```TVMSolidity
<optional(T)>.get() returns (T);
```
Returns the contained value, if the `optional` contains one. Otherwise, throws an exception.
##### \<optional(T)\>.getOr()
```TVMSolidity
<optional(T)>.getOr(T default) returns (T);
```
Returns the contained value, if the `optional` contains one. Otherwise, returns `default`.
##### \<optional(T)\>.getOrDefault()
```TVMSolidity
<optional(T)>.getOrDefault() returns (T);
```
Returns the contained value, if the `optional` contains one. Otherwise, returns the default value for `T` type.
##### \<optional(T)\>.set()
```TVMSolidity
<optional(T)>.set(Type value);
```
Replaces content of the `optional` with **value**.
##### Keyword `null`
Keyword `null` is a constant that is used to indicate an optional type with uninitialized state.
Example:
```TVMSolidity
optional(uint) x = 123;
x = null; // reset value
```
#### variant
The `variant` type acts like a union for the most common solidity data types. Supported only `uint` so far.
#### variant.isUint()
```TVMSolidity
<variant>.isUint() returns (bool)
```
Checks whether `<variant>` holds `uint` type.
#### variant.toUint()
Converts `<variant>` to `uint` type if it's possible. Otherwise, throws an exception with code `77`.
#### vector(T)
`vector(T)` is a template container type for storing values of a same type, pretty much like [dynamic-sized array](#arrays).
Two major differences are that `vector(T)`:
1. is much more efficient than a [dynamic-sized array](#arrays);
2. has a lifespan of a smart-contract execution, so it can't be neither passed nor returned as an external function call parameter, nor stored in a state variable.
**Note:** `vector` implementation based on `TVM Tuple` type, and it has a limited length of 255 values.
Example:
```TVMSolidity
vector(int) arr;
arr.push(100); // arr == [100]
arr.push(200); // arr == [100, 200]
arr.push(300); // arr == [100, 200, 300]
uint8 len = arr.length(); // len == 3
int value1 = arr[1]; // value1 == 200
arr[1] = 222; // arr == [100, 222, 300]
int last = arr.last(); // last == 300, arr == [100, 222, 300]
last = arr.pop(); // last == 300, arr == [100, 222]
last = arr.pop(); // last == 222, arr == [100]
last = arr.pop(); // last == 100, arr == []
bool isEmpty = arr.empty(); // isEmpty == true
```
##### \<vector(T)\>.push()
```TVMSolidity
<vector(T)>.push(T element);
```
Appends **element** to the `vector`.
##### \<vector(T)\>.pop()
```TVMSolidity
<vector(T)>.pop() returns (T);
```
Pops the last value from the `vector` and returns it.
##### \<vector(T)\>.last()
```TVMSolidity
<vector(T)>.last() returns (T);
```
Returns the last value from the `vector`.
##### \<vector(T)\>.operator[]
```TVMSolidity
<vector(T)>.operator[](uint index) returns (T);
```
Returns the value located at the `index` position. If `index` is not within the range of the container, an exception is thrown.
##### \<vector(T)\>.length()
```TVMSolidity
<vector(T)>.length() returns (uint8);
```
Returns length of the `vector`.
##### \<vector(T)\>.empty()
```TVMSolidity
<vector(T)>.empty() returns (bool);
```
Checks whether the `vector` is empty.
##### stack(T)
`stack` represents a last-in-first-out (LIFO) stack of items. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty.
```TVMSolidity
stack(int) st;
st.push(100);
st.push(200);
bool isEmpty = st.empty(); // isEmpty == false
int item = st.top(); // item == 200, st == [100, 200]
st.top() += 25; // st == [100, 225]
item = st.top(); // item == 225, st == [100, 225]
item = st.pop(); // item == 225, st == [100]
item = st.pop(); // item == 100, st == []
isEmpty = st.empty(); // isEmpty == true
```
##### \<stack(T)\>.push()
```TVMSolidity
<stack(T)>.push(T item)
```
Pushes an item onto the top of this stack.
##### \<stack(T)\>.pop()
```TVMSolidity
<stack(T)>.pop() returns (T)
```
Removes the item at the top of this stack and returns that item as the value of this function.
##### \<stack(T)\>.top()
```TVMSolidity
<stack(T)>.top() returns (ref T)
```
Returns reference at the item at the top of this stack without removing it from the stack. Example:
```TVMSolidity
stack(int) st;
st.push(200);
st.top() += 25; // st == [225]
int item = st.top(); // item = 225, st == [225]
```
##### \<stack(T)\>.empty()
```TVMSolidity
<stack(T)>.empty() returns (bool)
```
Checks whether the `stack` is empty.
##### \<stack(T)\>.sort()
```TVMSolidity
<stack(T)>.sort(function(Type, Type) internal pure returns(bool) isLess)
```
Sorts the specified stack into ascending order. Example:
```TVMSolidity
struct Point {
int x;
int y;
}
function less(Point a, Point b) private pure returns(bool) {
return a.x < b.x || a.x == b.x && a.y < b.y;
}
function testPoints() public pure {
stack(Point) st;
st.push(Point(20, 40));
st.push(Point(10, 10));
st.push(Point(20, 30));
st.sort(less);
Point p;
p = st.pop(); // p == Point(10, 10)
p = st.pop(); // p == Point(20, 30)
p = st.pop(); // p == Point(20, 40)
}
```
##### \<stack(T)\>.reverse()
```TVMSolidity
<stack(T)>.reverse()
```
Reverses the order of the elements in the specified stack. Example:
```TVMSolidity
stack(int) st;
st.push(100);
st.push(200);
st.push(300);
int value = st.top(); // value == 300
st.reverse();
value = st.pop(); // value == 100
value = st.pop(); // value == 200
value = st.pop(); // value == 300
```
### TVM specific control structures
### Range-based for loop
Executes a `for` loop over a range. Used as a more readable equivalent to the traditional `for` loop
operating over a range of values, such as all elements in an array or mapping.
```TVMSolidity
for ( range_declaration : range_expression ) loop_statement
```
`range_expression` is calculated only once, and the result is copied. Then iteration goes over the copy of
the array or mapping.
```TVMSolidity
uint[] arr = ...;
uint sum = 0;
for (uint val : arr) { // iteration over the array
sum += val;
}
bytes byteArray = "Hello!";
for (byte b : byteArray) { // iteration over the byte array
}
mapping(uint32 => uint) map = ...;
uint keySum = 0;
uint valueSum = 0;
for ((uint32 key, uint value) : map) { // iteration over the mapping
keySum += key;
valueSum += value;
}
```
Key or value can be omitted if you iterate over a mapping:
```TVMSolidity
mapping(uint32 => uint) map = ...;
uint keySum = 0;
for ((uint32 key, ) : map) { // value is omitted
keySum += key;
}
uint valueSum = 0;
for ((, uint value) : map) { // key is omitted
valueSum += value;
}
```
#### repeat
```TVMSolidity
repeat (exression) statement
```
Allows repeating the block of code several times. `exression` evaluates only one time.
```TVMSolidity
uint a = 0;
repeat(10) {
++a;
}
// a == 10
```
#### try-catch
[Capabilities](#tvm-capabilities) required: `CapsTvmBugfixes2022`.
The `try` statement allows you to define a block of code to be tested for errors while it is executed. The
`catch` statement allows you to define a block of code to be executed, if an error occurs in the try block.
`catch` block gets two parameters of type variant and uint16, which contain exception argument and code respectively.
Example:
```TVMSolidity
TvmBuilder builder;
uint c = 0;
try {
c = a + b;
require(c != 42, 100, 22);
require(c != 43, 100, 33);
builder.store(c);
} catch (variant value, uint16 errorCode) {
uint errorValue;
if (value.isUint()) {
errorValue = value.toUint();
}
if (errorCode == 100) {
if (errorValue == 22) {
// it was line: `require(c != 42, 100, 22);`
} else if (errorValue == 33) {
// it was line: `require(c != 43, 100, 33);`
}
} else if (errorCode == 8) {
// Cell overflow
// It was line: `builder.store(c);`
} else if (errorCode == 4) {
// Integer overflow
// It was line: `c = a + b;`
}
}
```
You can pass either of the parameters:
```TVMSolidity
uint c;
try {
c = a + b;
} catch (variant , uint16 errorCode) {
if (errorCode == 4) {
}
}
```
Or you can pass all parameters:
```TVMSolidity
uint c;
try {
c = a + b;
} catch {
}
```
#### unchecked block
```TVMSolidity
unchecked {
/* */
}
```
Turns off reverting on over- and underflow for arithmetic operations in `unchecked` block. It is up to programmer to control range of integer types if they use `unchecked` blocks. For example:
```TVMSolidity
function f(uint8 a, uint8 b) pure private returns (uint8) {
unchecked {
uint8 c = a - b;
return c;
}
}
function g(uint8 a, uint8 b) pure private returns (uint8) {
uint8 c = a - b;
return c;
}
```
The call to `f(2, 3)` will return `-1`, while `g(2, 3)` will cause a failing assertion.
See also: [pragma ignoreIntOverflow](#pragma-ignoreintoverflow).
### Changes and extensions in Solidity types
#### Integers
`int` / `uint`: Signed and unsigned integers of various sizes.
Keywords `uint1` to `uint256` in steps of 1 (unsigned of 1 up to 256 bits) and `int1` to `int257`. `uint` and `int` are aliases for `uint256` and `int257`, respectively.
Operators:
* Comparison: `<=`, `<`, `==`, `!=`, `>=`, `>` (evaluate to `bool`)
* Bit operators: `&`, `|`, `^` (bitwise exclusive or), `~` (bitwise negation)
* Shift operators: `<<` (left shift), `>>` (right shift)
* Arithmetic operators: `+`, `-`, unary `-`, `*`, `/`, `%` (modulo), `**` (exponentiation)
##### \<Integer\>.cast()
```TVMSolidity
<Integer>.cast(T) returns (T)
```
Convert `<Integer>` to T type. `T` is integer type. Type of `<Integer>` and `T` must have same sign or bit-size. Never throws an exception. For example:
```TVMSolidity
uint8 a = 255;
uint4 b = a.cast(uint4); // b == 15
uint8 a = 255;
int8 b = a.cast(int8); // b == -1
uint8 a = 255;
int4 b = a.cast(uint4).cast(int4); // b == -1
uint8 a = 255;
// int4 b = a.cast(int4); // compilation fail
```
**Note:** conversion via `T(x)` throws an exception if `x` does not fit into `T` type. For example:
```TVMSolidity
uint8 a = 10;
uint4 b = uint4(a); // OK, a == 10
uint8 a = 100;
uint4 b = uint4(a); // throws an exception because type(uint4).max == 15
int8 a = -1;
uint8 b = uint8(a); // throws an exception because type(uint8).min == 0
```
##### bitSize() and uBitSize()
```TVMSolidity
bitSize(int x) returns (uint16)
uBitSize(uint x) returns (uint16)
```
`bitSize` computes the smallest `c` β₯ 0 such that `x` fits into a `c`-bit signed integer
(β2<sup>cβ1</sup> β€ x < 2<sup>cβ1</sup>).
`uBitSize` computes the smallest `c` β₯ 0 such that `x` fits into a `c`-bit unsigned integer
(0 β€ x < 2<sup>c</sup>).
Example:
```TVMSolidity
uint16 s = bitSize(12); // s == 5
uint16 s = bitSize(1); // s == 2
uint16 s = bitSize(-1); // s == 1
uint16 s = bitSize(0); // s == 0
uint16 s = uBitSize(10); // s == 4
uint16 s = uBitSize(1); // s == 1
uint16 s = uBitSize(0); // s == 0
```
#### Quiet arithmetic
Operations with `qintN` / `quintN` return `NaN` instead of throwing integer overflow exceptions if the results do not fit in type, or if one of their arguments is a `NaN`. Default value for `qintN` / `quintN` is `0`, for `qbool` - `false`.
##### qintN and quintN
`qint` / `quint`: Signed and unsigned integers of various sizes.
Keywords `quint1` to `quint256` in steps of 1 (unsigned of 1 up to 256 bits) and `qint1` to `qint257`. `quint` and `qint` are aliases for `quint256` and `qint257`, respectively.
Operators:
* Comparison: `<=`, `<`, `==`, `!=`, `>=`, `>` (evaluate to [qbool](#qbool))
* Bit operators: `&`, `|`, `^` (bitwise exclusive or), `~` (bitwise negation)
* Shift operators: `<<` (left shift), `>>` (right shift)
* Arithmetic operators: `+`, `-`, unary `-`, `*`, `/`, `%` (modulo), `**` (exponentiation)
If one operand of bitwise βorβ (`|`) is equal to β1, the result
is always β1, even if the other argument is a `NaN`;
If one operand of bitwise βandβ (`&`) is equal to 0, the result
is always 0, even if the other argument is a `NaN`;
If one operand of `==` (`!=`) is equal to `NaN`, the result
is always `NaN`.
All [math namespace](#math-namespace) functions support quiet arithmetic.
##### qbool
`qbool` can have 3 values: `true`, `false` and `NaN`.
Operators:
* `!` (logical negation)
* `&&` (logical conjunction, βandβ)
* `||` (logical disjunction, βorβ)
* `==` (equality)
* `!=` (inequality)
The operators `||` and `&&` apply the common short-circuiting rules. This means that in the expression `f(x) || g(y)`, if `f(x)` evaluates to true, `g(y)` will not be evaluated even if it may have side-effects.
If one operand of logical βorβ (`|`) is equal to `true` , the result
is always `true`, even if the other argument is a `NaN`;
If one operand of logical βandβ (`&`) is equal to `false`, the result
is always `false`, even if the other argument is a `NaN`;
If one operand of `==` (`!=`) is equal to `NaN`, the result
is always `NaN`.
```TVMSolidity
function f(quint32 a, quint32 b, quint32 c, quint32 d) private {
qbool less = a * b < c * d;
if (less.isNaN()) {
// ...
} else {
bool l = less.get();
// ...
}
}
```
##### Keyword NaN
The `NaN` constant returns a nan (Not a Number) value. This value is not a legal number. Example:
```TVMSolidity
qint32 x = NaN;
qbool y = NaN;
```
##### \<T\>.isNaN()
```TVMSolidity
<T>.isNaN() returns (bool)
```
Checks whether `<T>` is `NaN`. `T` is `qintN`, `quintN` or `qbool`. Example:
```TVMSolidity
function checkOverflow(quint32 a, quint32 b) private pure returns(bool) {
quint32 s = a + b;
return s.isNaN();
}
```
##### \<T\>.get()
```TVMSolidity
<T>.get() returns (T2)
```
Returns "non-quiet" integer. If `<T>` is `NaN`, then throws an exception. `T` is `qintN`, `quintN` or `qbool`. Example:
```TVMSolidity
function f(quint32 a, quint32 b) private pure {
quint32 s = a + b;
if (!s.isNaN()) {
uint32 ss = s.get();
// ...
}
}
```
##### \<T\>.getOr()
```TVMSolidity
<T>.getOr(T2 default) returns (T2)
```
Returns "non-quiet" integer. If `<T>` is `NaN`, then returns `default`. `T` is `qintN`, `quintN` or `qbool`. Example:
```TVMSolidity
function f(quint32 a, quint32 b) private pure {
quint32 s = a + b;
uint32 ss = s.getOr(42); // ss is equal to `a + b` or 42
// ...
}
```
##### \<T\>.getOrDefault()
```TVMSolidity
<T>.getOrDefault() returns (T2)
```
Returns "non-quiet" integer. If `<T>` is `NaN`, then returns default value. `T` is `qintN`, `quintN` or `qbool`. Example:
```TVMSolidity
function f(quint32 a, quint32 b) private pure {
quint32 s = a + b;
uint32 ss = s.getOrDefault(); // ss is equal to `a + b` or 0
// ...
}
```
##### \<T\>.toOptional()
```TVMSolidity
<T>.toOptional() returns (optional(T2))
```
Returns optional integer. If `<T>` is `NaN`, then returns [null](#keyword-null). `T` is `qintN`, `quintN` or `qbool`. Example:
```TVMSolidity
function f(quint32 a, quint32 b) private pure {
quint32 s = a + b;
optional(uint32) ss = s.toOptional(); // ss is equal to `a + b` or null
// ...
}
```
#### varint and varuint
`varint`/`varint16`/`varint32`/`varuint`/`varuint16`/`coins`/`varuint32` are kinds of [Integer](#integers)
types. But they are serialized/deserialized according to [their TLB schemes](https://github.com/ton-blockchain/ton/blob/master/crypto/block/block.tlb#L112).
These schemes are effective if you want to store or send integers, and they usually have small size.
Use these types only if you are sure.
`varint` is equal to `varint32`, `varint32` - `int248` , `varint16` - `int120`.
`varuint` is equal to `varuint32`, `varuint32` - `uint248` , `varuint16` - `uint120`.
`coins` is an alias for `varuint16`.
Example:
```TVMSolidity
mapping(uint => varint) m_map; // use `varint` as mapping value only if values have small size
m_map[10] = 15;
```
Operators:
* Comparison: `<=`, `<`, `==`, `!=`, `>=`, `>` (evaluate to `bool`)
* Bit operators: `&`, `|`, `^` (bitwise exclusive or), `~` (bitwise negation)
* Shift operators: `<<` (left shift), `>>` (right shift)
* Arithmetic operators: `+`, `-`, unary `-`, `*`, `/`, `%` (modulo), `**` (exponentiation)
#### struct
Structs are custom defined types that can group several variables.
##### struct constructor
```TVMSolidity
struct Stakes {
uint total;
mapping(uint => uint) stakes;
}
// create struct with default values of its members
Stakes stakes;
// create struct using parameters
mapping(uint => uint) values;
values[100] = 200;
Stakes stakes = Stakes(200, values);
// create struct using named parameters
Stakes stakes = Stakes({stakes: values, total: 200});
```
##### \<struct\>.unpack()
```TVMSolidity
<struct>.unpack() returns (TypeA /*a*/, TypeB /*b*/, ...);
```
Unpacks all members stored in the `struct`.
Example:
```TVMSolidity
struct MyStruct {
uint a;
int b;
address c;
}
function f() pure public {
MyStruct s = MyStruct(1, -1, address(2));
(uint a, int b, address c) = s.unpack();
}
```
#### Arrays
##### Array literals
An array literal is a comma-separated list of one or more expressions, enclosed in square brackets.
For example: `[100, 200, 300]`.
Initializing constant state variable:
`uint[] constant fib = [uint(2), 3, 5, 8, 12, 20, 32];`
##### Creating new arrays
```TVMSolidity
uint[] arr; // create 0-length array
uint[] arr = new uint[](0); // create 0-length array
uint constant N = 5;
uint[] arr = new uint[](N); // create 5-length array
uint[] arr = new uint[](10); // create 10-length array
```
Note: If `N` is constant expression or integer literal, then the complexity of array creation -
`O(1)`. Otherwise, `O(N)`.
##### \<array\>.empty()
```TVMSolidity
<array>.empty() returns (bool);
```
Returns status flag whether the `array` is empty (its length is 0).
Example:
```TVMSolidity
uint[] arr;
bool b = arr.empty(); // b == true
arr.push(...);
bool b = arr.empty(); // b == false
```
#### bytesN
Variables of the `bytesN` types can be explicitly converted to `bytes`. Note: it costs ~500 gas.
```TVMSolidity
bytes3 b3 = 0x112233;
bytes b = bytes(b3);
```
#### bytes
`bytes` is an array of `byte`. It is similar to `byte[]`, but they are encoded in different ways.
Example of `bytes` initialization:
```TVMSolidity
// initialised with string
bytes a = "abzABZ0129";
// initialised with hex data
bytes b = hex"01239abf";
```
`bytes` can be converted to `TvmSlice`. Warning: if length of the array is greater than 127, then extra bytes are stored in the first reference of the slice. Use [\<TvmSlice\>.loadRef()](#tvmsliceloadref) to load that extra bytes.
##### \<bytes\>.empty()
```TVMSolidity
<bytes>.empty() returns (bool);
```
Returns status flag whether the `bytes` is empty (its length is 0).
##### \<bytes\>.operator[]
```TVMSolidity
<bytes>.operator[](uint index) returns (byte);
```
Returns the byte located at the **index** position.
Example:
```TVMSolidity
bytes byteArray = "abba";
int index = 0;
byte a0 = byteArray[index]; // a0 = 0x61
```
##### \<bytes\> slice
```TVMSolidity
<bytes>.operator[](uint from, uint to) returns (bytes);
```
Returns the slice of `bytes` [**from**, **to**), including **from** byte and
excluding **to**.
Example:
```TVMSolidity
bytes byteArray = "01234567890123456789";
bytes slice = byteArray[5:10]; // slice == "56789"
slice = byteArray[10:]; // slice == "0123456789"
slice = byteArray[:10]; // slice == "0123456789"
slice = byteArray[:]; // slice == "01234567890123456789"
```
##### \<bytes\>.length
```TVMSolidity
<bytes>.length returns (uint)
```
Returns length of the `bytes` array.
##### \<bytes\>.dataSize()
```TVMSolidity
<bytes>.dataSize(uint n) returns (uint /*cells*/, uint /*bits*/, uint /*refs*/);
```
Same as [\<TvmCell\>.dataSize()](#tvmcelldatasize).
##### \<bytes\>.dataSizeQ()
```TVMSolidity
<bytes>.dataSizeQ(uint n) returns (optional(uint /*cells*/, uint /*bits*/, uint /*refs*/));
```
Same as [\<TvmCell\>.dataSizeQ()](#tvmcelldatasizeq).
##### \<bytes\>.append()
```TVMSolidity
<bytes>.append(bytes tail);
```
Modifies the `bytes` by concatenating **tail** data to the end of the `bytes`.
##### bytes conversion
```TVMSolidity
bytes byteArray = "1234";
bytes4 bb = byteArray;
```
`bytes` can be converted to `bytesN`.
If `bytes` object has less than **N** bytes, extra bytes are padded with zero bits.
#### string
TVM Solidity compiler expands `string` type with the following functions:
**Note**: Due to VM restrictions string length can't exceed `1024 * 127 = 130048` bytes.
`string` can be converted to `TvmSlice`.
##### \<string\>.empty()
```TVMSolidity
<string>.empty() returns (bool);
```
Returns status flag whether the `string` is empty (its length is 0).
##### \<string\>.byteLength()
```TVMSolidity
<string>.byteLength() returns (uint32);
```
Returns byte length of the `string` data.
##### \<string\>.substr()
```TVMSolidity
(1)
<string>.substr(uint pos) returns (string);
(2)
<string>.substr(uint pos, uint count) returns (string);
```
(1) Returns a string that is a substring of this string. The substring begins with the character at the specified `pos` and extends to the end of this string.
(2) Returns a string that is a substring of this string. The substring begins with the character at the specified `pos` and copies at most `count` characters from the source string. If the string is shorter, as many characters as possible are used.
Throws an exception if `pos > string.byteLength()`.
```TVMSolidity
string text = "0123456789";
string a = text.substr(1, 2); // a = "12"
string b = text.substr(6); // b = "6789"
string c = text.substr(6, 100); // c = "6789"
string d = text.substr(777); // throws an exception
```
**Note:** if [Array Slices](https://docs.soliditylang.org/en/latest/types.html#array-slices) are used (they are written as `x[start:end]`), then `0 <= start <= end <= byteLength()`.
```TVMSolidity
string str = "01234567890";
string c = text.substr(6, 100); // c = "6789"
string d = text[6:106]; // throws an exception
```
##### \<string\>.append()
```TVMSolidity
<string>.append(string tail);
```
Appends the tail `string` to the `string`.
##### \<string\>.operator+
```TVMSolidity
<string>.operator+(string) returns (string);
<string>.operator+(bytesN) returns (string);
```
Creates new `string` as a concatenation of left and right arguments of the operator +. Example:
```TVMSolidity
string a = "abc";
bytes2 b = "12";
string c = a + b; // "abc12"
```
##### \<string\>.find() and \<string\>.findLast()
```TVMSolidity
<string>.find(bytes1 symbol) returns (optional(uint32));
<string>.find(string substr) returns (optional(uint32));
<string>.findLast(bytes1 symbol) returns (optional(uint32));
```
Looks for **symbol** (or substring) in the `string` and returns index of the first (`find`) or the
last (`findLast`) occurrence. If there is no such symbol in the `string`, an empty optional is returned.
Example:
```TVMSolidity
string str = "01234567890";
optional(uint32) a = str.find(byte('0'));
bool s = a.hasValue(); // s == true
uint32 n = a.get(); // n == 0
byte symbol = 'a';
optional(uint32) b = str.findLast(symbol);
bool s = b.hasValue(); // s == false
string sub = "111";
optional(uint32) c = str.find(sub);
bool s = c.hasValue(); // s == false
```
##### \<string\>.dataSize()
```TVMSolidity
<string>.dataSize(uint n) returns (uint /*cells*/, uint /*bits*/, uint /*refs*/);
```
Same as [\<TvmCell\>.dataSize()](#tvmcelldatasize).
##### \<string\>.dataSizeQ()
```TVMSolidity
<string>.dataSizeQ(uint n) returns (optional(uint /*cells*/, uint /*bits*/, uint /*refs*/));
```
Same as [\<TvmCell\>.dataSizeQ()](#tvmcelldatasizeq).
##### \<string\>.toUpperCase()` and \<string\>.toLowerCase()
```TVMSolidity
<string>.toUpperCase() returns (string)
<string>.toLowerCase() returns (string)
```
Converts `string` to upper/lower case. It treats `string` as ASCII string and converts only 'A'..'Z'
and 'a'..'z' symbols. Example:
```TVMSolidity
string s = "Hello";
string a = s.toUpperCase(); // a == "HELLO"
string b = s.toLowerCase(); // b == "hello"
```
##### format()
```TvmSolidity
format(string template, TypeA a, TypeB b, ...) returns (string);
```
Builds a `string` with arbitrary parameters. Empty placeholder `{}` can be filled with integer
(in decimal view), address, string or fixed point number. Fixed point number is printed
based on its type (`fixedMxN` is printed with `N` digits after dot).
Placeholder should be specified in such formats:
* `"{}"` - empty placeholder
* `"{:[0]<width>{"x","d","X","t"}}"` - placeholder for integers. Fills num with 0 if format starts with "0".
Formats integer to have specified `width`. Can format integers in decimal ("d" postfix), lower hex ("x")
or upper hex ("X") form. Format "t" prints number (in nanoevers) as a fixed point sum.
Warning: this function consumes too much gas, that's why it's better not to use it onchain.
Example:
```TVMSolidity
string str = format("Hello {} 0x{:X} {} {}.{} evers", 123, 255, address.makeAddrStd(-33,0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF123456789ABCDE), 100500, 32);
// str == "Hello 123 0xFF -21:7fffffffffffffffffffffffffffffffffffffffffffffffff123456789abcde 100500.32 evers"
str = format("Hello {}", 123); // str == "Hello 123"
str = format("Hello 0x{:X}", 123); // str == "Hello 0x7B"
str = format("{}", -123); // str == "-123"
str = format("{}", address.makeAddrStd(127,0)); // str == "7f:0000000000000000000000000000000000000000000000000000000000000000"
str = format("{}", address.makeAddrStd(-128,0)); // str == "-80:0000000000000000000000000000000000000000000000000000000000000000"
str = format("{:6}", 123); // str == " 123"
str = format("{:06}", 123); // str == "000123"
str = format("{:06d}", 123); // str == "000123"
str = format("{:06X}", 123); // str == "00007B"
str = format("{:6x}", 123); // str == " 7b"
uint128 a = 1 ever;
str = format("{:t}", a); // str == "1.000000000"
a = 123;
str = format("{:t}", a); // str == "0.000000123"
fixed32x3 v = 1.5;
str = format("{}", v); // str == "1.500"
fixed256x10 vv = -987123.4567890321;
str = format("{}", vv); // str == "-987123.4567890321"
bool flag = true;
str = format("Hello, {}!", flag); // str == "Hello, true!"
```
##### stoi()
```TvmSolidity
stoi(string inputStr) returns (optional(int) /*result*/);
```
Converts a `string` into an integer. If `string` starts with '0x' it will be converted from a hexadecimal format,
otherwise it is meant to be number in decimal format. Function returns the integer in case of success.
Otherwise, returns `null`.
Warning: this function consumes too much gas, that's why it's better not to use it onchain.
Example:
```TVMSolidity
optional(int) res;
res = stoi("123"); // res ==123
string hexstr = "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF123456789ABCDE";
res = stoi(hexstr); // res == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF123456789ABCDE
res = stoi("0xag"); // res == null
res = stoi(""); // res == null
```
##### string conversion
```TVMSolidity
string s = "1";
bytes2 b = bytes2(s); // b == 0x3100
string s = "11";
bytes2 b = bytes2(s); // b = 0x3131
```
`string` can be converted to `bytesN` which causes **N** * 8 bits being loaded from the cell and saved to variable.
If `string` object has less than **N** bytes, extra bytes are padded with zero bits.
#### address
`address` represents different types of TVM addresses: **addr_none**, **addr_extern**,
**addr_std** and **addr_var**. TVM Solidity compiler expands `address` type with the following
members and functions:
##### Object creating
##### constructor()
```TVMSolidity
uint address_value;
address addrStd = address(address_value);
```
Constructs `address` of type **addr_std** with zero workchain id and given address value.
##### address.makeAddrStd()
```TVMSolidity
int8 wid;
uint address;
address addrStd = address.makeAddrStd(wid, address);
```
Constructs `address` of type **addr_std** with given workchain id **wid** and value **address_value**.
##### address.addrNone
```TVMSolidity
address addrNone = address.addrNone;
```
Constructs `address` of type **addr_none**.
##### address.makeAddrExtern()
```TVMSolidity
uint addrNumber;
uint bitCnt;
address addrExtern = address.makeAddrExtern(addrNumber, bitCnt);
```
Constructs `address` of type **addr_extern** with given **value** with **bitCnt** bit-length.
##### Members
##### \<address\>.wid
```TVMSolidity
<address>.wid returns (int8);
```
Returns the workchain id of **addr_std** or **addr_var**. Throws "range check error" [exception](#tvm-exception-codes) for other `address` types.
##### \<address\>.value
```TVMSolidity
<address>.value returns (uint);
```
Returns the `address` value of **addr_std** or **addr_var** if **addr_var** has 256-bit `address` value. Throws "range check error" [exception](#tvm-exception-codes) for other `address` types.
##### \<address\>.balance
```TVMSolidity
address(this).balance returns (varuint16);
```
Returns balance of the current contract account in nanoevers.
##### \<address\>.currencies
```TVMSolidity
address(this).currencies returns (mapping(uint32 => varuint32));
```
Returns currencies on the balance of the current contract account.
##### Functions
##### \<address\>.getType()
```TVMSolidity
<address>.getType() returns (uint8);
```
Returns type of the `address`:
0 - **addr_none**
1 - **addr_extern**
2 - **addr_std**
##### \<address\>.isStdZero()
```TVMSolidity
<address>.isStdZero() returns (bool);
```
Returns the result of comparison between this `address` with zero `address` of type **addr_std**.
##### \<address\>.isStdAddrWithoutAnyCast()
```TVMSolidity
<address>.isStdAddrWithoutAnyCast() returns (bool);
```
Checks whether this `address` is of type **addr_std** without any cast.
##### \<address\>.isExternZero()
```TVMSolidity
<address>.isExternZero() returns (bool);
```
Returns the result of comparison between this `address` with zero `address` of type **addr_extern**.
##### \<address\>.isNone()
```TVMSolidity
<address>.isNone() returns (bool);
```
Checks whether this `address` is of type **addr_none**.
##### \<address\>.unpack()
```TVMSolidity
<address>.unpack() returns (int32 /*wid*/, uint256 /*value*/);
```
Parses `address` containing a valid `MsgAddressInt` (`addr_std`), applies rewriting
from the anycast (if exists) to the same-length prefix of the address, and returns both the
workchain `wid` and the 256-bit address `value`. If the address `value` is not 256-bit, or if
`address` is not a valid serialization of `MsgAddressInt`, throws a cell deserialization
[exception](#tvm-exception-codes).
It is wrapper for opcode `REWRITESTDADDR`.
Example:
```TVMSolidity
(int32 wid, uint addr) = address(this).unpack();
```
##### \<address\>.transfer()
```TVMSolidity
<address>.transfer(varuint16 value, bool bounce, uint16 flag, TvmCell body, mapping(uint32 => varuint32) currencies, TvmCell stateInit);
```
Sends an internal outbound message to the `address`. Function parameters:
* `value` (`varuint16`) - amount of nanoevers sent attached to the message. Note: the sent value is
withdrawn from the contract's balance even if the contract has been called by internal inbound message.
* `currencies` (`mapping(uint32 => varuint32)`) - additional currencies attached to the message. Defaults to
an empty set.
* `bounce` (`bool`) - if it's set and transaction (generated by the internal outbound message) falls
(only at the computing phase, not at the action phase!), then funds will be returned. Otherwise, (flag isn't
set or transaction terminated successfully) the address accepts the funds even if the account
doesn't exist or is frozen. Defaults to `true`.
* `flag` (`uint16`) - flag that used to send the internal outbound message. Defaults to `0`.
* `body` (`TvmCell`) - body (payload) attached to the internal message. Defaults to an empty
TvmCell.
* `stateInit` (`TvmCell`) - represents field `init` of `Message X`. If `stateInit` has a wrong
format, a cell underflow [exception](#tvm-exception-codes) at the computing phase is thrown.
See [here](https://github.com/ton-blockchain/ton/blob/master/crypto/block/block.tlb#L148).
Normally, `stateInit` is used in 2 cases: to deploy the contract or to unfreeze the contract.
All parameters can be omitted, except `value`.
Possible values of parameter `flag`:
* `0` - message carries funds equal to the `value` parameter. Forward fee is subtracted from
the `value`.
* `128` - message carries all the remaining balance of the current smart contract. Parameter `value` is
ignored. The contract's balance will be equal to zero after the message processing.
Parameter `flag` can also be modified:
* `flag + 1` - means that the sender wants to pay transfer fees separately from contract's balance.
* `flag + 32` - means that the current account must be destroyed if its resulting balance is zero.
For example, `flag: 128 + 32` is used to send all balance and destroy the contract.
In order to clarify flags usage see [this sample](https://github.com/everx-labs/samples/blob/master/solidity/20_bomber.sol).
```TVMSolidity
address dest = ...;
uint128 value = ...;
bool bounce = ...;
uint16 flag = ...;
TvmCell body = ...;
mapping(uint32 => varuint32) c = ...;
TvmCell stateInit = ...;
// sequential order of parameters
addr.transfer(value);
addr.transfer(value, bounce);
addr.transfer(value, bounce, flag);
addr.transfer(value, bounce, flag, body);
addr.transfer(value, bounce, flag, body, c);
// using named parameters
destination.transfer({value: 1 ever, bounce: false, flag: 128, body: cell, currencies: c});
destination.transfer({bounce: false, value: 1 ever, flag: 128, body: cell});
destination.transfer({value: 1 ever, bounce: false, stateInit: stateInit});
```
See example of `address.transfer()` usage:
* [giver](https://github.com/everx-labs/samples/blob/master/solidity/7_Giver.sol)
#### mapping
TVM Solidity compiler expands `mapping` type with the following functions. In examples
below `\<map\>` defines the object of `mapping(KeyType => ValueType)` type.
Address, bytes, string, bool, contract, enum, fixed bytes, integer and struct types can
be used as a `KeyType`. Struct type can be used as `KeyType` only if it contains only
integer, boolean, fixed bytes or enum types and fits ~1023 bit. Example of mapping that
has a struct as a `KeyType`:
```TVMSolidity
struct Point {
uint x;
uint y;
uint z;
}
mapping(Point => address) map;
function test(uint x, uint y, uint z, address addr) public {
Point p = Point(x, y, z);
map[p] = addr;
}
```
If you use `mapping(KeyType => TvmSlice) map;` be sure that sum of bit-length of `KeyType`
and bit-length of `TvmSlice` is less than 1023 bit.
Struct `KeyType` can be used to sort keys of the mapping in ascending order. In the example
above addresses in the mapping are sorted by their keys. `x` field of the Point struct
is used in comparison first, second is `y` and the last is `z`.
If `bytes`, `string` or `TvmCell` types are used as `KeyType`, then `mapping` stores
only hashes of mapping keys. That's why for these types the `delMin`/`min`/`next` and
another mapping methods return `uint256` as key (not `bytes`/`string`/`TvmCell`).
If you use mapping as an input or output param for public/external functions,
`KeyType` of the mapping can only be of type `address` or of `int<M>`/`uint<M>` types with M from 8 to 256.
See example of how to work with mappings:
* [database](https://github.com/everx-labs/samples/blob/master/solidity/13_BankCollector.sol)
* [client](https://github.com/everx-labs/samples/blob/master/solidity/13_BankCollectorClient.sol)
##### Keyword `emptyMap`
Keyword `emptyMap` is a constant that is used to indicate a mapping of arbitrary type without values.
Example:
```TVMSolidity
struct Stakes {
uint total;
mapping(uint => uint) stakes;
}
// create struct with empty mapping `stakes`
Stakes stakes = Stakes({stakes: emptyMap, total: 200});
```
##### \<mapping\>.operator[]
```TVMSolidity
<map>.operator[](KeyType index) returns (ValueType);
```
Returns the item of `ValueType` with **index** key or returns the default value
if key is not in the mapping.
##### \<mapping\>.at()
```TVMSolidity
<map>.operator[](KeyType index) returns (ValueType);
```
Returns the item of `ValueType` with **index** key. Throws an [exception](#tvm-exception-codes) if key
is not in the mapping.
##### \<mapping\>.min() and \<mapping\>.max()
```TVMSolidity
<map>.min() returns (optional(KeyType, ValueType));
<map>.max() returns (optional(KeyType, ValueType));
```
Computes the minimal (maximal) key in the `mapping` and returns an `optional`
value containing that key and the associated value. If `mapping` is empty,
this function returns an empty `optional`.
##### \<mapping\>.next() and \<mapping\>.prev()
```TVMSolidity
<map>.next(KeyType key) returns (optional(KeyType, ValueType));
<map>.prev(KeyType key) returns (optional(KeyType, ValueType));
```
Computes the minimal (maximal) key in the `mapping` that is lexicographically
greater (less) than **key** and returns an `optional` value containing that
key and the associated value. Returns an empty `optional` if there is no such key.
If KeyType is an integer type, argument for this functions can not possibly fit `KeyType`.
Example:
```TVMSolidity
KeyType key;
// init key
optional(KeyType, ValueType) nextPair = map.next(key);
optional(KeyType, ValueType) prevPair = map.prev(key);
if (nextPair.hasValue()) {
(KeyType nextKey, ValueType nextValue) = nextPair.get(); // unpack optional value
// using nextKey and nextValue
}
mapping(uint8 => uint) m;
optional(uint8, uint) = m.next(-1); // ok, param for next/prev can be negative
optional(uint8, uint) = m.prev(65537); // ok, param for next/prev can not possibly fit to KeyType (uint8 in this case)
```
##### \<mapping\>.nextOrEq() and \<mapping\>.prevOrEq()
```TVMSolidity
<map>.nextOrEq(KeyType key) returns (optional(KeyType, ValueType));
<map>.prevOrEq(KeyType key) returns (optional(KeyType, ValueType));
```
Computes the minimal (maximal) key in the `mapping` that is lexicographically greater than
or equal to (less than or equal to) **key** and returns an `optional` value containing that
key and the associated value. Returns an empty `optional` if there is no such key.
If KeyType is an integer type, argument for this functions can not possibly fit `KeyType`.
##### \<mapping\>.delMin() and \<mapping\>.delMax()
```TVMSolidity
<map>.delMin() returns (optional(KeyType, ValueType));
<map>.delMax() returns (optional(KeyType, ValueType));
```
If mapping is not empty, then this function computes the minimal (maximum) key of the `mapping`,
deletes that key and the associated value from the `mapping` and returns an `optional` value
containing that key and the associated value. Returns an empty `optional` if there is no such key.
##### \<mapping\>.fetch()
```TVMSolidity
<map>.fetch(KeyType key) returns (optional(ValueType));
```
Checks whether **key** exists in the `mapping` and returns an `optional` with the associated value.
Returns an empty `optional` if there is no such key.
##### \<mapping\>.exists()
```TVMSolidity
<map>.exists(KeyType key) returns (bool);
```
Returns whether **key** exists in the `mapping`.
##### \<mapping\>.empty()
```TVMSolidity
<map>.empty() returns (bool);
```
Returns whether the `mapping` is empty.
##### \<mapping\>.replace()
```TVMSolidity
<map>.replace(KeyType key, ValueType value) returns (bool);
```
Sets the value associated with **key** only if **key** exists in the `mapping` and
returns the success flag.
##### \<mapping\>.add()
```TVMSolidity
<map>.add(KeyType key, ValueType value) returns (bool);
```
Sets the value associated with **key** only if **key** does not exist in the `mapping`.
##### \<mapping\>.getSet()
```TVMSolidity
<map>.getSet(KeyType key, ValueType value) returns (optional(ValueType));
```
Sets the value associated with **key**, but also returns an `optional` with the
previous value associated with the **key**, if any. Otherwise, returns an empty `optional`.
##### \<mapping\>.getAdd()
```TVMSolidity
<map>.getAdd(KeyType key, ValueType value) returns (optional(ValueType));
```
Sets the value associated with **key**, but only if **key** does not exist in the `mapping`.
Returns an `optional` with the old value without changing the dictionary if that value exists
in the `mapping`, otherwise returns an empty `optional`.
##### \<mapping\>.getDel()
```TVMSolidity
<map>.getDel(KeyType key) returns (optional(ValueType));
```
Deletes the **key** from the `mapping` **map** and returns an `optional`
with the corresponding value. Returns an empty optional if the key does not exist.
##### \<mapping\>.getReplace()
```TVMSolidity
<map>.getReplace(KeyType key, ValueType value) returns (optional(ValueType));
```
Sets the value associated with **key**, but only if **key** exists in the `mapping`.
On success, returns an `optional` with the old value associated with the **key**.
Otherwise, returns an empty `optional`.
#### Fixed point number
`fixed` / `ufixed`: Signed and unsigned fixed point number of various sizes. Keywords `ufixedMxN`
and `fixedMxN`, where `M` represents the number of bits taken by the type and `N` represents how
many decimal points are available. `M` must be divisible by 8 and goes from 8 to 256 bits. `N` must
be between 0 and 80, inclusive. `ufixed` and `fixed` are aliases for `ufixed128x18` and
`fixed128x18`, respectively.
Operators:
* Comparison: `<=`, `<`, `==`, `!=`, `>=`, `>` (evaluate to `bool`)
* Arithmetic operators: `+`, `-`, unary `-`, `*`, `/`, `%` (modulo)
* Math operations: [math.min(), math.max()](#mathmin-mathmax), [math.minmax()](#mathminmax),
[math.abs()](#mathabs), [math.divr(), math.divc()](#mathdivr-mathdivc)
#### \<mapping\>.keys() \<mapping\>.values()
```TVMSolidity
(1)
<map>.keys() returns (KeyType[]);
(2)
<map>.values() returns (ValueType[]);
```
(1) Returns all mapping's keys/values.
(2) Returns all values of the mapping as an array.
**Note:** these functions iterate over the whole mapping, thus the cost is proportional to the
mapping's size.
```TVMSolidity
mapping(uint16 => uint8) map;
map[11] = 10;
map[22] = 20;
map[33] = 30;
uint16[] keys = map.keys(); // keys == [11, 22, 33]
uint8[] values = map.values(); // values == [10, 20, 30]
```
#### Function type
Function types are the types of functions. Variables of function type can be assigned from functions
and function parameters of function type can be used to pass functions to and return functions from
function calls.
If unassigned variable of function type is called, then exception with code 65 is thrown.
```TVMSolidity
function getSum(int a, int b) internal pure returns (int) {
return a + b;
}
function getSub(int a, int b) internal pure returns (int) {
return a - b;
}
function process(int a, int b, uint8 mode) public returns (int) {
function (int, int) returns (int) fun;
if (mode == 0) {
fun = getSum;
} else if (mode == 1) {
fun = getSub;
}
return fun(a, b); // if `fun` isn't initialized, then exception is thrown
}
```
#### User-defined type
A user-defined value type allows creating a zero cost abstraction over an elementary value type. This is similar to an alias, but with stricter type requirements.
More information can be found [here](https://docs.soliditylang.org/en/latest/types.html#user-defined-value-types).
You can define an operator for user-defined type. See [here](https://docs.soliditylang.org/en/latest/contracts.html#using-for).
#### require, revert
In case of exception state variables of the contract are reverted to the state before
[tvm.commit()](#tvmcommit) or to the state of the contract before it was called.
Use error codes that are greater than 100 because other error codes can be
[reserved](#solidity-runtime-errors).
**Note**: if a nonconstant error code is passed as the function argument and the error code
is less than 2, then the error code will be set to 100.
##### require
```TVMSolidity
(1)
require(bool condition, [uint errorCode = 100, [Type exceptionArgument]]);
(2)
require(bool condition, string text);
```
`require` function can be used to check the condition and throw an exception if the condition
is not met.
(1) Takes condition and optional parameters: error code and the object of any type.
(2) Takes condition and error text. Error code will be equal to 100.
Example:
```TVMSolidity
uint a = 5;
require(a == 5); // ok
require(a == 6); // throws an exception with code 100
require(a == 6, 101); // throws an exception with code 101
require(a == 6, 101, "a is not equal to six"); // throws an exception with code 101 and string
require(a == 6, 101, a); // throws an exception with code 101 and number a
require(a == 6, "a is not equal to six"); // throws an exception with code 100 and string
```
##### revert
```TVMSolidity
revert(uint errorCode = 100, [Type exceptionArgument]);
```
**revert** function can be used to throw exceptions. The function takes an optional error code
(unsigned integer) and the object of any type.
Example:
```TVMSolidity
uint a = 5;
revert(); // throw exception 100
revert(101); // throw exception 101
revert(102, "We have a some problem"); // throw exception 102 and string
revert(101, a); // throw exception 101 and number a
```
#### Libraries
Libraries are similar to contracts, but they can't have state variables
and can't inherit nor be inherited. Libraries can be seen as implicit
base contracts of the contracts that use them. They will not be
explicitly visible in the inheritance hierarchy, but calls of library
functions look just like calls of functions of explicit base contracts
(using qualified access like `LibName.func(a, b, c)`). There is also
another way to call library function: `obj.func(b, c)`.
For now libraries are stored as a part of the code of the contact that
uses libraries. In the future, it can be changed.
##### Function call via library name
Example of using library in the manner `LibName.func(a, b, c)`:
```TVMSolidity
// file MathHelper.sol
pragma solidity >= 0.72.0;
// Library declaration
library MathHelper {
// State variables are forbidden in library but constants are not
uint constant MAX_VALUE = 300;
// Library function
function sum(uint a, uint b) internal pure returns (uint) {
uint c = a + b;
require(c < MAX_VALUE);
return c;
}
}
// file MyContract.sol
pragma solidity >= 0.72.0;
import "MathHelper.sol";
contract MyContract {
uint s;
function addValue(uint value) public returns (uint) {
s = MathHelper.sum(s, value);
return s;
}
}
```
##### Function call via object
In TVM Solidity **arguments of a function call passed by value not by
reference**. It's effective for numbers and even for huge arrays.
See ([TVM][1] - A.2.3.2).
**But if a library function is called like `obj.func(b, c)`, then the
first argument `obj` is passed by reference.** It's similar to
the `self` variable in Python.
The directive [using A for B;](https://docs.soliditylang.org/en/latest/contracts.html#using-for) can be used to attach library functions
(from the library `A`) to any type (`B`) in the context of the contract.
These functions will receive the object they were called for as their
first parameter.
The effect of `using A for *;` is that the functions from the library
`A` are attached to all types.
Example of using library in the manner `obj.func(b, c)`:
```TVMSolidity
// file ArrayHelper.sol
pragma solidity >= 0.72.0;
library ArrayHelper {
// Delete value from the `array` at `index` position
function del(uint[] array, uint index) internal pure {
for (uint i = index; i + 1 < array.length; ++i){
array[i] = array[i + 1];
}
array.pop();
}
}
// file MyContract.sol
pragma solidity >= 0.72.0;
import "ArrayHelper.sol";
contract MyContract {
// Attach library function `del` to the type `uint[]`
using ArrayHelper for uint[];
uint[] array;
constructor() {
array = [uint(100), 200, 300];
}
function deleteElement(uint index) public {
// Library function call via object.
// Note: library function `del` has 2 arguments:
// array is passed by reference and index is passed by value
array.del(index);
}
}
```
#### Free function call via object
Free functions can be called via object as well as library functions. Use directive [using A for B;](https://docs.soliditylang.org/en/latest/contracts.html#using-for). Example:
```TVMSolidity
pragma tvm-solidity >= 0.72.0;
// Delete value from the `array` at `index` position
function del(uint[] array, uint index) {
for (uint i = index; i + 1 < array.length; ++i){
array[i] = array[i + 1];
}
array.pop();
}
contract MyContract {
// Attach function `del` to the type `uint[]`
using {del} for uint[];
uint[] public array;
constructor() {
array = [uint(100), 200, 300];
}
function deleteElement(uint index) public {
// Free function call via object.
// Note: free function `del` has 2 arguments:
// array is passed by reference and index is passed by value
array.del(index);
}
}
```
### Pragmas
`pragma` keyword is used to enable certain compiler features or checks.
A pragma directive is always local to a source file, so you have to add
the pragma to all your files if you want to enable it in your whole project.
If you import another file, the pragma from that file is not
automatically applied to the importing file.
#### pragma tvm-solidity
```TVMSolidity
pragma tvm-solidity >= 0.35.5; // Check if the compiler version is greater or equal than 0.35.5
pragma tvm-solidity ^ 0.35.5; // Check if the compiler version is greater or equal than 0.35.5 and less than 0.36.0
pragma tvm-solidity < 0.35.5; // Check if the compiler version is less than 0.35.5
pragma tvm-solidity >= 0.35.5 < 0.35.7; // Check if the compiler version is equal to either 0.35.5 or 0.35.6
```
Used to restrict source file compilation to the particular compiler versions.
#### pragma-copyleft
```TVMSolidity
pragma copyleft <type>, <wallet_address>;
```
[Capabilities](#tvm-capabilities) required: `CapCopyleft`.
Parameters:
* `<type>` (`uint8`) - copyleft type.
* `<wallet_address>` (`uint256`) - author's wallet address in masterchain.
If contract has the `copyleft` pragma, it means that after each transaction some part of validator's fee
is transferred to `<wallet_address>` according to the `<type>` rule.
For example:
```TVMSolidity
pragma copyleft 0, 0x2cfbdc31c9c4478b61472c72615182e9567595b857b1bba9e0c31cd9942f6ca41;
```
#### pragma ignoreIntOverflow
```TVMSolidity
pragma ignoreIntOverflow;
```
All arithmetic operations revert on over- and underflow by default. This pragma turns off such behavior. It is up to programmer to control range of integer types if they use the pragma.
For example:
```TVMSolidity
pragma ignoreIntOverflow;
uint8 a = 2;
uint8 b = 3;
uint8 c = a - b; // c == -1, no exception thrown
```
See also: [unchecked block](#unchecked-block).
#### pragma AbiHeader
```TVMSolidity
pragma AbiHeader notime;
pragma AbiHeader pubkey;
pragma AbiHeader expire;
```
Defines headers that are used in external messages:
* `notime` - disables `time` abi header, which is enabled by default. Abi header `time` β `uint64` local time when message was created, used for replay protection
* `pubkey` (`uint256`) - optional public key that the message can be signed with.
* `expire` (`uint32`) - time when the message should be meant as expired.
**Note:**
Defined headers are listed in `*.abi.json` file in `header` section.
See also: [Contract execution](#contract-execution), [afterSignatureCheck](#aftersignaturecheck),
[msg.pubkey()](#msgpubkey) and [tvm.pubkey()](#tvmpubkey).
To read more about these fields and ABI follow this [link](https://docs.ton.dev/86757ecb2/p/40ba94-abi-specification-v2).
Here is example of [message expiration time](https://docs.ton.dev/86757ecb2/p/88321a-message-expiration-time) usage.
#### pragma msgValue
```TVMSolidity
pragma msgValue <value>;
```
Allows specifying default value in nanoevers attached to the
internal outbound messages used to call another contract. If it's not
specified, this value is set to 10 000 000 nanoevers.
Example:
```TVMSolidity
pragma msgValue 123456789;
pragma msgValue 1e8;
pragma msgValue 10 ever;
pragma msgValue 10_000_000_123;
```
#### pragma upgrade func/oldsol
```TVMSolidity
pragma upgrade func;
pragma upgrade oldsol;
```
Defines that code is compiled with special selector that is needed to upgrade FunC/Solidity contracts.
### State variables
#### Decoding state variables
You can decode state variables using ever-cli. See `ever-cli decode account --help`.
See also: [abi.decodeData()](#abidecodedata).
#### Keyword `constant`
For `constant` variables, the value has to be a compile time constant and this value is
substituted where the variable is used. The value has to be assigned where the variable is declared.
Example:
```TVMSolidity
contract MyContract {
uint constant cost = 100;
uint constant cost2 = cost + 200;
address constant dest = address.makeAddrStd(-1, 0x89abcde);
}
```
#### Keyword `static`
Static state variables are used in the contract initial state generation.
Such variables can be set while deploying contract from contract
(onchain) or by tvm-linker (offchain). Example:
```TVMSolidity
contract C {
uint static a; // ok
uint static b = 123; // error
}
```
See also:
* [`code` option usage](#code-option-usage)
* [New contract address problem](#new-contract-address-problem)
#### Keyword `nostorage`
`nostorage` state variables are not saved in the contract's storage. They have default values at the
beginning of each transaction.
```TVMSolidity
contract C {
uint nostorage m_value;
function f() public {
// here m_value == 0
++m_value;
// here m_value == 1
}
}
```
#### Keyword `public`
For each public state variable, a getter function is generated. Generated
function has the same name and return type as the public variable. This
function can be called only locally. Public state variables are useful,
because you don't need to write functions that return a particular state variable manually.
Example:
```TVMSolidity
contract C {
uint public a;
uint public static b; // it's ok. Variable is both public and static.
}
```
### Special contract functions
#### receive
`receive` function is called in two cases:
1. [msg.body](#msgbody) (or message body) is empty.
2. [msg.body](#msgbody) starts with 32-bit zero. Then message body may contain data,
for example [string](#string) with comment.
If in the contract there is no `receive` function, then the contract has an implicit empty `receive`
function.
```TVMSolidity
// file sink.sol
contract Sink {
uint public counter = 0;
uint public msgWithPayload = 0;
receive() external {
++counter;
// if the inbound internal message has payload, then we can get it using `msg.body`
TvmSlice s = msg.body;
if (!s.empty()) {
++msgWithPayload;
}
}
}
// file bomber.sol
contract Bomber {
// This function send evers 3 times to the Sink contract. Sink's function receive will handle
// that messages.
function f(address addr) pure public {
tvm.accept();
addr.transfer({value: 1 ever}); // message's body is empty
TvmBuilder b;
addr.transfer({value: 1 ever, body: b.toCell()}); // message's body is empty, too
b.store(uint32(0), "Thank you for the coffee!");
// body of the message contains 32-bit zero number and the string
addr.transfer({value: 20 ever, body: b.toCell()});
}
}
```
##### fallback
`fallback` function is called on receiving an inbound internal/external message in such cases:
1. The message contains a function id that the contract doesn't contain.
2. Bit-length of the message is from 1 to 31 (including).
3. Bit-length of the message is equal to zero, but the message contains reference(s).
**Note**: if the message has correct function id but invalid encoded function parameters, then
the transaction fail with an exception (e.g. [cell underflow exception](#tvm-exception-codes)).
If in the contract there is no fallback function, then contract has implicit fallback function
that throws [exception](#solidity-runtime-errors).
Example:
```TVMSolidity
// file ContractA.sol
contract ContractA {
uint public counter = 0;
function f(uint a, uint b) public pure { /*...*/ }
fallback() external {
++counter;
}
}
// file ContractB.sol
import "ContractA.sol";
import "ContractAnother.sol";
contract ContractB {
// Let's condider that `addr` is ContractA's address. 4 messages are send. ContractA's fallback
// function will handle that messages except the last one.
function g(address addr) public pure {
tvm.accept();
// The message contains a function id that the contract doesn't contain.
// There is wrong casting to ContractAnother. `addr` is ContractA's address.
ContractAnother(addr).sum{value: 1 ever}(2, 2);
{
TvmBuilder b;
b.storeUnsigned(1, 1);
// Bit-length of the message is equal to 20 bits.
addr.transfer({value: 2 ever, body: b.toCell()});
}
{
TvmBuilder b;
b.storeRef(b);
// Bit-length of the message is equal to zero but the message contains one reference.
addr.transfer({value: 1 ever, body: b.toCell()});
}
TvmBuilder b;
uint32 id = abi.functionId(ContractA.f);
b.store(id, uint(2));
// ContractA's fallback function won't be called because the message body doesn't contain
// the second ContractA.f's parameter. It will cause cell underflow exception in ContractA.
addr.transfer({value: 1 ever, body: b.toCell()});
}
}
```
#### onBounce
```TVMSolidity
onBounce(TvmSlice body) external {
/*...*/
}
```
`onBounce` function is executed when contract receives a bounced inbound internal message.
The message is generated by the network if the contract sends an internal message with `bounce: true` and either
* called contract doesn't exist;
* called contract fails at the storage/credit/computing phase (not at the action phase!)
The message is generated only if the remaining message value is enough for sending one back.
`body` depends on TVM realisation and settings:
1. `body` can be empty.
2. If `CapBounceMsgBody` [capability](#tvm-capabilities) is set, then `body` contains at most 256 data bits of the original message (without references). Note: the function id takes `32` bits and the function's parameters can take at most `224` bits.
3. If `CapBounceMsgBody` and `CapFullBodyInBounced` [capabilities](#tvm-capabilities) are set, then `body` is the same as in the second option, but `body` contains the full original message in the first reference.
If `onBounce` function is not defined, then the contract does
nothing on receiving a bounced message.
If the `onBounce` function throws an exception, then another bounced messages are not generated.
Example of how to use `onBounce` function for option 2:
* [onBounceHandler](https://github.com/everx-labs/samples/blob/master/solidity/16_onBounceHandler.sol)
Example of getting function ID if `CapBounceMsgBody` and `CapFullBodyInBounced` [capabilities](#tvm-capabilities) are set:
```TVMSolidity
onBounce(TvmSlice body) external {
TvmSlice fullBody = body.loadRef().toSlice();
uint32 functionId = fullBody.load(uint32);
}
```
#### onTickTock
`onTickTock` function is executed on tick/tock transaction.
These transactions are automatically generated for certain special accounts.
See ([TBLKCH][2] - 4.2.4.)
For tick transactions **isTock** is false, for tock transactions - true.
Prototype:
```TVMSolidity
onTickTock(bool isTock) external {
/*...*/
}
```
#### onCodeUpgrade
`onCodeUpgrade` function can have an arbitrary set of arguments and should be
executed after [tvm.setcode()](#tvmsetcode) function call. In this function
[tvm.resetStorage()](#tvmresetstorage) should be called if the set of state
variables is changed in the new version of the contract. This function implicitly
calls [tvm.commit()](#tvmcommit). `onCodeUpgrade` function does not return value. `onCodeUpgrade` function
finishes TVM execution with exit code 0.
Prototype:
```TVMSolidity
function onCodeUpgrade(...) private {
/*...*/
}
```
Function `onCodeUpgrade` had function id = 2 (for compiler <= 0.65.0). Now, it has another id, but you can set
`functionID(2)` in new contracts for the `onCodeUpgrade` to upgrade old ones.
See example of how to upgrade code of the contract:
* [old contract](https://github.com/everx-labs/samples/blob/master/solidity/12_BadContract.sol)
* [new contract](https://github.com/everx-labs/samples/blob/master/solidity/12_NewVersion.sol)
It's good to pass `TvmCell cell` to the public function that calls `onCodeUpgrade(TvmCell cell, ...)`
function. `TvmCell cell` may contain some data that may be useful for the new contract.
```TVMSolidity
// old contract
// Public function that changes the code and takes some cell
function updateCode(TvmCell newcode, TvmCell cell) public pure checkPubkeyAndAccept {
tvm.setcode(newcode);
tvm.setCurrentCode(newcode);
// pass cell to new contract
onCodeUpgrade(cell);
}
function onCodeUpgrade(TvmCell cell) private pure {
}
// new contract
function onCodeUpgrade(TvmCell cell) private pure {
// new code can use cell that was passed from the old version of the contract
}
```
#### afterSignatureCheck
```TVMSolidity
function afterSignatureCheck(TvmSlice body, TvmCell message) private inline returns (TvmSlice) {
/*...*/
}
```
`afterSignatureCheck` function is used to define custom replay protection function instead of the
default one.
NB: Do not use [tvm.commit()](#tvmcommit) or [tvm.accept()](#tvmaccept) in this function as it called before the constructor call.
**time** and **expire** header fields can be used for replay protection and, if set, they should be read in `afterSignatureCheck`.
See also: [Contract execution](#contract-execution).
See an example of how to define this function:
* [Custom replay protection](https://github.com/everx-labs/samples/blob/master/solidity/14_CustomReplayProtection.sol)
### Function specifiers
#### Function mutability: pure, view and default
Function mutability shows how this function treats state variables.
Possible values of the function mutability:
* `pure` - function neither reads nor assigns state variables;
* `view` - function may read but doesn't assign state variables;
* default - function may read and/or assign state variables.
Example:
```TVMSolidity
contract Test {
uint a;
event MyEvent(uint val);
// pure mutability
function f() public pure {
emit MyEvent(123);
}
// view mutability
function g() public view {
emit MyEvent(a);
}
// default mutability (not set)
function e(uint newA) public {
a = newA;
}
}
```
#### Keyword inline
`inline` specifier instructs the compiler to insert a copy of the private function
body into each place where the function is called.
Keyword can be used only for private and internal functions.
Example:
```TVMSolidity
// This function is called as a usual function.
function getSum(uint a, uint b) public returns (uint) {
return sum(a, b);
}
// Code of this function is inserted in place of the call site.
function sum(uint a, uint b) private inline returns (uint) {
return a + b;
}
```
#### Assembly
To make inline assembler you should mark [free function](https://docs.soliditylang.org/en/latest/contracts.html#functions) as `assembly`. Function body must contain lines of assembler code separated by commas.
It is up to user to set correct mutability (`pure`, `view` or default), return parameters of the function and so on.
```TVMSolidity
function checkOverflow(uint a, uint b) assembly pure returns (bool) {
"QADD",
"ISNAN",
"NOT",
}
contract Contract {
function f(uint a, uint b) private {
bool ok = checkOverflow(a, b);
if (ok) {
uint c = a + b;
}
}
}
```
You can use inline assembler to support new opcodes in experimental or another implementations of TVM.
```TVMSolidity
function incomingValue() assembly pure returns (uint) {
".blob xF82B", // it's opcode INCOMINGVALUE
}
```
#### functionID()
`functionID` keyword allows assigning function identifier explicitly.
Each public function has a unique 32-bit identifier (id). id 0 is reserved for [receive](#receive) function. id 1 is reserved for Constructor function.
In case `functionID` is not defined explicitly, it is calculated as a hash of the function signature.
In general, there is no purpose to set the function id manually.
Example:
```TVMSolidity
function f() public pure functionID(123) {
/*...*/
}
```
#### externalMsg and internalMsg
Keywords `externalMsg` and `internalMsg` specify which messages the function can handle.
If the function marked by keyword `externalMsg` is called by internal message, the function throws an
exception with code 71.
If the function marked by keyword `internalMsg` is called by external message, the function throws
an exception with code 72.
Example:
```TVMSolidity
function f() public externalMsg { // this function receives only external messages
/*...*/
}
// Note: keyword `external` specifies function visibility
function ff() external externalMsg { // this function also receives only external messages
/*...*/
}
function g() public internalMsg { // this function receives only internal messages
/*...*/
}
// These function receives both internal and external messages.
function fun() public { /*...*/ }
```
### Events and return
#### emit
`emit` statement sends an external outbound message. Use `{dest: ...}`to set destination address.
The address must be of **addr_extern** type.
If option `dest` is not used, the destination address is set to **addr_none**.
**Note:** fee for creating the external outbound message is withdrawn from the contract's
balance even if the contract has been called by internal inbound message.
Example:
```TVMSolidity
event SomethingIsReceived(uint a, uint b, uint sum);
...
address addr = address.makeAddrExtern(...);
emit SomethingIsReceived{dest: addr}(2, 8, 10); // dest address is set
emit SomethingIsReceived(10, 15, 25); // dest address == addr_none
```
#### return
`return` statement has different effects depending on:
* visibility of the function;
* type of the inbound message;
* presence of the `responsible` modifier.
In `private` or `internal` visible functions, `return` assigns the return variables to the specified parameters;
In `public` or `external` visible functions, `return` statement:
* on an external inbound message, generates an external outbound message with a destination address set to
the source address of the inbound (external) message. All arguments of the return statement are ignored.
* on an external inbound message:
* if the function is marked as `responsible`, generates an internal outbound message;
* otherwise has no effect.
`value`, `bounce`, `flag` and `currencies` options are used to create the message. Some options can
be omitted. See [\<address\>.transfer()](#addresstransfer) where these options and their default
values are described.
**Note**: if the function `f` below is called with `n = 5` and internal/external message must be
generated, then only one message is sent with result `120` (120 = 5 * 4 * 3 * 2 * 1).
**Hint:** Use `{value: 0, bounce: false, flag: 64}` for `responsible` function.
Because `flag: 0` is used by default.
See also: [External function calls](#external-function-calls).
```TVMSolidity
function f(uint n) public pure {
return n <= 1 ? 1 : n * f(n - 1);
}
function f(uint n) public responsible pure {
return{value: 0, bounce: false, flag: 64} n <= 1 ? 1 : n * f(n - 1);
}
```
### External function calls
TVM Solidity compiler allows specifying different parameters of the outbound internal message that
is sent via external function call. Note, all external function calls are asynchronous, so
callee function will be called after termination of the current transaction.
`value`, `currencies`, `bounce`, `flag` and `stateInit` options can be set. See [\<address\>.transfer()](#addresstransfer)
where these options are described.
**Note:** if `value` isn't set, then the default value is equal to 0.01 ever, or 10^7 nanoever. It's equal
to 10_000 units of gas in workchain.
If the callee function returns some value and marked as `responsible`, then `callback` option must be set.
This callback function will be called by another contract. Remote function will pass its return
values as function arguments for the callback function. That's why types of return values of the
callee function must be equal to function arguments of the callback function.
If the function marked as `responsible`, then field `answerId` appears in the list of input parameters of the
function in `*abi.json` file. `answerId` is function id that will be called.
Example of the external call of the function that returns nothing:
```TVMSolidity
interface IContract {
function f(uint a) external;
}
contract Caller {
function callExt(address addr) public {
IContract(addr).f(123); // attached default value: 0.01 ever
IContract(addr).f{value: 10 ever}(123);
IContract(addr).f{value: 10 ever, flag: 3}(123);
IContract(addr).f{value: 10 ever, bounce: true}(123);
IContract(addr).f{value: 1 micro, bounce: false, flag: 128}(123);
mapping(uint32 => varuint32) cc;
cc[12] = 1000;
IContract(addr).f{value: 10 ever, currencies:cc}(123);
}
}
```
Example of the external call of the function that returns some values:
```TVMSolidity
contract RemoteContract {
// Note this function is marked as responsible to call callback function
function getCost(uint x) public pure responsible returns (uint) {
uint cost = x == 0 ? 111 : 222;
// return cost and set option for outbound internal message.
return{value: 0, bounce: false, flag: 64} cost;
}
}
contract Caller {
function test(address addr, uint x) public pure {
// `getCost` returns result to `onGetCost`
RemoteContract(addr).getCost{value: 1 ever, callback: Caller.onGetCost}(x);
}
function onGetCost(uint cost) public {
// Check if msg.sender is expected address
// we get cost value, we can handle this value
}
}
```
`*.abi.json` for `responsible` function `getCost`:
```json
{
"name": "getCost",
"inputs": [
{"name":"answerId","type":"uint32"},
{"name":"x","type":"uint256"}
],
"outputs": [
{"name":"value0","type":"uint256"}
]
}
```
See also:
* Example of callback usage: [24_SquareProvider](https://github.com/everx-labs/samples/blob/master/solidity/24_SquareProvider.sol)
* Example of callback usage: [4.1_CentralBank](https://github.com/everx-labs/samples/blob/master/solidity/4.1_CentralBank.sol)
and [4.1_CurrencyExchange.sol](https://github.com/everx-labs/samples/blob/master/solidity/4.1_CurrencyExchange.sol)
* [return](#return)
### Delete variables
As in classic Solidity `delete` operation assigns the initial value for the type to a variable.
Delete operation can be applied not only to variables itself, but to its fields or index values.
Example:
```TVMSolidity
int a = 5;
delete a; // a == 0
uint[] arr;
arr.push(11);
arr.push(22);
arr.push(33);
delete arr[1];
// arr[0] == 11
// arr[1] == 0
// arr[2] == 33
delete arr; // arr.length == 0
mapping(uint => uint) l_map;
l_map[1] = 2;
delete l_map[1];
bool e = l_map.exists(1); // e == false
l_map[1] = 2;
delete l_map;
bool e = l_map.exists(1); // e == false
struct DataStruct {
uint m_uint;
bool m_bool;
}
DataStruct l_struct;
l_struct.m_uint = 1;
delete l_struct; // l_struct.m_uint == 0
TvmBuilder b;
uint i = 0x54321;
b.store(i);
TvmCell c = b.toCell();
delete c;
TvmCell empty; // tvm.hash(empty) == tvm.hash(c)
b.store(c);
TvmSlice slice = b.toSlice();
uint16 bits = slice.bits(); // bits == 256
uint8 refs = slice.refs(); // refs == 1
delete slice;
uint16 bits = slice.bits(); // bits == 256
uint8 refs = slice.refs(); // refs == 1
uint16 bits = b.bits(); // bits == 256
uint8 refs = b.refs(); // refs == 1
delete b;
uint16 bits = b.bits(); // bits == 256
uint8 refs = b.refs(); // refs == 1
```
### API functions and members
#### Type information
The expression `type(T)` can be used to retrieve information about the type T.
The following properties are available for an integer, [variable integer](#varint-and-varuint) and enum type `T`:
* `type(T).min` - the smallest value representable by type `T`.
* `type(T).max` - the largest value representable by type `T`.
#### **msg** namespace
##### msg.sender
```TVMSolidity
msg.sender (address)
```
Returns:
* sender of the message for internal message.
* address(0) for external message.
* address(0) for tick/tock transaction.
##### msg.value
```TVMSolidity
msg.value (varuint16)
```
Returns:
* Balance of the inbound message in nanoevers for internal message.
* 0 for external message.
* Undefined value for tick/tock transaction.
##### msg.currencies
```TVMSolidity
msg.currencies (mapping(uint32 => varuint32))
```
Collections of arbitrary currencies contained in the balance of
the inbound message.
##### msg.pubkey()
```TVMSolidity
msg.pubkey() returns (uint256);
```
Returns public key that is used to check the message signature. If the message isn't signed, then it's equal to `0`.
See also: [Contract execution](#contract-execution), [pragma AbiHeader](#pragma-abiheader).
##### msg.isInternal, msg.isExternal and msg.isTickTock
Returns flag whether the contract is called by internal message, external message or by tick/tock transactions.
##### msg.createdAt
```TVMSolidity
msg.createdAt (uint32)
```
Returns the field **created_at** of the external inbound message.
##### msg.data
```TVMSolidity
msg.data (TvmCell)
```
Returns [the whole message](https://github.com/ton-blockchain/ton/blob/master/crypto/block/block.tlb#L155).
##### msg.forwardFee
```TVMSolidity
msg.forwardFee (varuint16)
```
Returns:
* the [forward fee](https://github.com/ton-blockchain/ton/blob/master/crypto/block/block.tlb#L126) for the internal inbound message.
* `0` for the external inbound message.
##### msg.importFee
```TVMSolidity
msg.importFee (varuint16)
```
Returns:
* the field [import_fee](https://github.com/ton-blockchain/ton/blob/master/crypto/block/block.tlb#L130) for external inbound message. **Note:** field `import_fee` is set offchain by user as they want and does not reflect the real import fee of the message.
* `0` for the internal inbound message.
##### msg.body
```TVMSolidity
msg.body (TvmSlice)
```
Returns the payload (message body) of an inbound message.
##### msg.hasStateInit
```TVMSolidity
msg.hasStateInit (bool)
```
Whether the internal/external inbound message contains field `stateInit`.
Returns undefined value for tick/tock transaction. See [TL-B scheme][3] of `Message X`.
#### **tvm** namespace
##### TVM instructions
##### tvm.accept()
```TVMSolidity
tvm.accept();
```
Executes TVM instruction "ACCEPT" ([TVM][1] - A.11.2).
This instruction sets current gas limit to its maximal allowed value.
This action is required to process external messages that bring no value.
See example of how to use this function:
* [accumulator](https://github.com/everx-labs/samples/blob/master/solidity/1_Accumulator.sol)
##### tvm.setGasLimit()
```TVMSolidity
tvm.setGasLimit(uint g);
```
Executes TVM instruction "SETGASLIMIT" ([TVM][1] - A.11.2).
Sets current gas limit **g<sub>l</sub>** to the minimum of **g** and **g<sub>m</sub>**, and resets the gas
credit **g<sub>c</sub>** to zero. If the gas consumed so far (including the present instruction) exceeds
the resulting value of **g<sub>l</sub>**, an (unhandled) out of gas exception is thrown before setting
new gas limits. Notice that `tvm.setGasLimit(...)` with an argument **g** β₯ 2<sup>63</sup> - 1 is
equivalent to `tvm.accept()`.
`tvm.setGasLimit()` is similar to `tvm.accept()`. `tvm.accept()` sets gas limit **g<sub>l</sub>** to
the maximum possible value (depends on the network configuration parameters, usually is equal to
1_000_000 units of gas). `tvm.setGasLimit()` is generally used for accepting external messages and restricting
max possible gas consumption. It may be used to protect from flood by "bad" owner
in a contract that is used by multiple users. Let's consider some scenario:
1. Check whether msg.pubkey() != 0 and msg.pubkey() belongs to the list of trusted public keys;
2. Check whether `m_floodCounter[msg.pubkey()] < 5` where **m_floodCounter** is count of pending
operations of `msg.pubkey()` user.
3. `tvm.setGasLimit(75_000);` accept external message and set gas limit to 75_000.
4. `++m_floodCounter[msg.pubkey()];` increase count of pending operations for current users.
5. `tvm.commit();` save current state if it needs
6. Do other things.
So if some user's public key will be stolen, then a hacker can spam with external messages and
burn at most `5 * 75_000` units of gas instead of `5 * 1_000_000`, because we use `tvm.setGasLimit()` instead
of `tvm.accept()`.
##### tvm.buyGas()
```TVMSolidity
tvm.buyGas(uint value);
```
Computes the amount of gas that can be bought for `value` nanoevers, and sets **g<sub>l</sub>**
accordingly in the same way as [tvm.setGasLimit()](#tvmsetgaslimit).
##### tvm.commit()
```TVMSolidity
tvm.commit();
```
Creates a "check point" of the state variables (by copying them from c7 to c4) and register c5.
If the contract throws an exception at the computing phase, then the state variables and register c5
will roll back to the "check point", and the computing phase will be considered "successful".
If contract doesn't throw an exception, it has no effect.
##### tvm.rawCommit()
```TVMSolidity
tvm.rawCommit();
```
Same as [tvm.commit()](#tvmcommit) but doesn't copy the state variables from c7 to c4. It's a wrapper
for opcode `COMMIT`. See [TVM][1].
**Note**: Don't use `tvm.rawCommit()` after `tvm.accept()` in processing external messages because
you don't save from c7 to c4 the hidden state variable `timestamp` that is used for replay protection.
##### tvm.getData()
```TVMSolidity
tvm.getData() returns (TvmCell);
```
**Note:** Function is experimental.
A dual of the `tvm.setData()`function. It returns value of the `c4` register. Obtaining a raw storage
cell can be useful when upgrading a new version of the contract that introduces an altered data layout.
Manipulation with a raw storage cell requires understanding of the way the compiler stores the data.
Refer to the description of `tvm.setData()` below to get more details.
**Note:** state variables and replay protection timestamp stored in the data cell have the same values
that were before the transaction. See [tvm.commit()](#tvmcommit) to learn about register `c4` update.
##### tvm.setData()
```TVMSolidity
tvm.setData(TvmCell data);
```
**Note:** Function is experimental.
Stores cell `data` in the register `c4`. Mind that after returning from a public function all state variables
from `c7` are copied to `c4` and `tvm.setData` will have no effect. Example hint, how to set `c4`:
```TVMSolidity
TvmCell data = ...;
tvm.setData(data); // set register c4
tvm.rawCommit(); // save register c4 and c5
revert(200); // throw the exception to terminate the transaction
```
Be careful with the hidden state variable `timestamp` and think about possibility of external
messages replaying.
##### tvm.log()
```TVMSolidity
tvm.log(string log);
logtvm(string log);
```
Dumps `log` string. This function is a wrapper for TVM instructions
`PRINTSTR` (for constant literal strings shorter than 16 symbols) and
`STRDUMP` (for other strings). `logtvm` is an alias for `tvm.log(string)`. Example:
```TVMSolidity
tvm.log("Hello, world!");
logtvm("99_Bottles");
string s = "Some_text";
tvm.log(s);
```
**Note:** For long strings dumps only the first 127 symbols.
##### tvm.hexdump() and tvm.bindump()
```TVMSolidity
tvm.hexdump(T a);
tvm.bindump(T a);
```
Dumps cell data or integer. Note that for cells this function dumps data only
from the first cell. `T` must be an integer type or TvmCell.
Example:
```TVMSolidity
TvmBuilder b;
b.storeUnsigned(0x9876543210, 40);
TvmCell c = b.toCell();
tvm.hexdump(c);
tvm.bindump(c);
uint a = 123;
tvm.hexdump(a);
tvm.bindump(a);
int b = -333;
tvm.hexdump(b);
tvm.bindump(b);
```
Expected output for the example:
```TVMLog
CS<9876543210>(0..40)
CS<10011000011101100101010000110010000100001>(0..40)
7B
1111011
-14D
-101001101
```
##### tvm.setcode()
```TVMSolidity
tvm.setcode(TvmCell newCode);
```
This command creates an output action that would change this smart contract
code to that given by the `TvmCell` **newCode** (this change will take effect only
after the successful termination of the current run of the smart contract).
See example of how to use this function:
* [old contract](https://github.com/everx-labs/samples/blob/master/solidity/12_BadContract.sol)
* [new contract](https://github.com/everx-labs/samples/blob/master/solidity/12_NewVersion.sol)
##### tvm.configParam()
```TVMSolidity
tvm.configParam(uint8 paramNumber) returns (TypeA a, TypeB b, ...);
```
Executes TVM instruction "CONFIGPARAM" ([TVM][1] - A.11.4. - F832).
This command returns the value of the global configuration parameter with
integer index **paramNumber**. Argument should be an integer literal.
Supported **paramNumbers**: 1, 15, 17, 34.
##### tvm.rawConfigParam()
```TVMSolidity
tvm.rawConfigParam(uint8 paramNumber) returns (TvmCell cell, bool status);
```
Executes TVM instruction "CONFIGPARAM" ([TVM][1] - A.11.4. - F832).
Returns the value of the global configuration parameter with
integer index **paramNumber** as a `TvmCell` and a boolean status.
##### tvm.rawReserve()
```TVMSolidity
tvm.rawReserve(uint value, uint8 flag);
tvm.rawReserve(uint value, mapping(uint32 => varuint32) currency, uint8 flag);
```
Creates an output action that reserves **reserve** nanoevers. It is roughly equivalent to
create an outbound message carrying **reserve** nanoevers to oneself, so that the subsequent output
actions would not be able to spend more money than the remainder. It's a wrapper for opcodes
"RAWRESERVE" and "RAWRESERVEX". See [TVM][1].
Let's denote:
* `original_balance` is balance of the contract before the computing phase that is equal to balance
of the contract before the transaction minus storage fee. Note: `original_balance` doesn't include
`msg.value` and `original_balance` is not equal to `address(this).balance`.
* `remaining_balance` is contract's current remaining balance at the action phase after some handled
actions and before handing the "rawReserve" action.
Let's consider how much nanoevers (**reserve**) are reserved in all cases of **flag**:
* 0 -> `reserve = value` nanoevers.
* 1 -> `reserve = remaining_balance - value` nanoevers.
All other values of `flag` are invalid.
To make it clear, let's consider the order of `reserve` calculation:
1. if `flag` has bit `+8`, then `value = -value`.
2. if `flag` has bit `+4`, then `value += original_balance`.
3. Check `value >= 0`.
4. if `flag` has bit `+2`, then `value = min(value, remaining_balance)`.
5. if `flag` has bit `+1`, then `value = remaining_balance - value`.
6. `reserve = value`.
7. Check `0 <= reserve <= remaining_balance`.
Example:
```TVMSolidity
tvm.rawReserve(1 ever, 4 + 8);
```
See also: [23_rawReserve.sol](https://github.com/everx-labs/samples/blob/master/solidity/23_rawReserve.sol)
##### tvm.initCodeHash()
```TVMSolidity
tvm.initCodeHash() returns (uint256 hash)
```
Returns the initial code hash that contract had when it was deployed.
[Capabilities](#tvm-capabilities) required: `CapInitCodeHash`.
##### Hashing and cryptography
##### tvm.hash()
```TVMSolidity
tvm.hash(TvmCell cellTree) returns (uint256);
tvm.hash(string data) returns (uint256);
tvm.hash(bytes data) returns (uint256);
tvm.hash(TvmSlice data) returns (uint256);
```
Executes TVM instruction "HASHCU" or "HASHSU" ([TVM][1] - A.11.6. - F900).
It computes the representation hash of a given argument and returns
it as a 256-bit unsigned integer. For `string` and `bytes` it computes
hash of the tree of cells that contains data but not data itself.
See [sha256](#sha256) to count hash of data.
Example:
```TVMSolidity
uint256 hash = tvm.hash(TvmCell cellTree);
uint256 hash = tvm.hash(string);
uint256 hash = tvm.hash(bytes);
```
##### tvm.checkSign()
```TVMSolidity
(1)
tvm.checkSign(uint256 dataHash, uint256 SignHighPart, uint256 SignLowPart, uint256 pubkey) returns (bool);
(2)
tvm.checkSign(uint256 dataHash, TvmSlice signature, uint256 pubkey) returns (bool);
(3)
tvm.checkSign(TvmSlice data, TvmSlice signature, uint256 pubkey) returns (bool);
```
Executes TVM instruction "CHKSIGNU" ([TVM][1] - A.11.6. - F910) for options 1 and 2.
This command checks the Ed25519-signature of the **dataHash** using public key **pubkey**.
Signature is represented by two uint256 **SignHighPart** and **SignLowPart** in the
first option and by the slice **signature** in the second option.
In the third option executes TVM instruction "CHKSIGNS" ([TVM][1] - A.11.6. - F911).
This command checks Ed25519-signature of the **data** using public key **pubkey**.
Signature is represented by the slice **signature**.
If `CapSignatureWithId` [capability](#tvm-capabilities) is set, then TVM use some predefined ID during signature check. Usually ID is `global_id` that can be found in the last block for example.
Example:
```TVMSolidity
// option 1
uint256 dataHash;
uint256 SignHighPart;
uint256 SignLowPart;
uint256 pubkey;
bool signatureIsValid = tvm.checkSign(dataHash, SignHighPart, SignLowPart, pubkey);
// option 2
uint256 dataHash;
TvmSlice signature;
uint256 pubkey;
bool signatureIsValid = tvm.checkSign(dataHash, signature, pubkey);
// option 3
TvmSlice data;
TvmSlice signature;
uint256 pubkey;
bool signatureIsValid = tvm.checkSign(data, signature, pubkey);
```
##### Deploy contract from contract
##### Deploy via new
Either `code` or `stateInit` option must be set when you deploy a contract
from contract via keyword `new`. `stateInit` is a tree of cells that contains
original state of the contract. `stateInit` contains `data`, `code` and another members.
See also ([TBLKCH][2] - A.2.3.2) to read about `stateInit`.
Use `stateInit` option if you have the created account state (maybe offchain or
onchain) and use `code` if you want to create account state in the `new` expression.
**Note**: Address of the new account is calculated as a hash of the `stateInit`.
Constructor function parameters don't influence the address. See
[New contract address problem](#new-contract-address-problem).
[Step-by-step description how to deploy contracts from the contract here](https://github.com/everx-labs/samples/blob/master/solidity/17_ContractProducer.md).
Examples:
* [WalletProducer](https://github.com/everx-labs/samples/blob/master/solidity/17_ContractProducer.sol).
* [SelfDeployer](https://github.com/everx-labs/samples/blob/master/solidity/21_self_deploy.sol).
##### `stateInit` option usage
`stateInit` defines the origin state of the new account.
```TVMSolidity
TvmCell stateInit = ...;
address newWallet = new SimpleWallet{value: 1 ever, stateInit: stateInit}(arg0, arg1, ...);
```
##### `code` option usage
`code` option defines the code of the new contract.
```TVMSolidity
TvmCell code = ...;
address newWallet = new SimpleWallet{value: 1 ever, code: code}(arg0, arg1, ...);
```
The following options can only be used with the `code` option:
* `pubkey` (`uint256`) - defines the public key of the new contract.
* `varInit` (`initializer list`) - used to set [static](#keyword-static) variables of the new contract.
* `splitDepth` (`uint8`) - splitting depth. `0 <= splitDepth <= 31`. By default, it has no value.
Example of usage of these options:
```TVMSolidity
// file SimpleWallet.sol
...
contract SimpleWallet {
address static m_owner;
uint static m_value;
...
}
// file containing a contract that deploys a SimpleWallet
TvmCell code = ...;
address newWallet = new SimpleWallet{
value: 1 ever,
code: code,
pubkey: 0xe8b1d839abe27b2abb9d4a2943a9143a9c7e2ae06799bd24dec1d7a8891ae5dd,
splitDepth: 15,
varInit: {m_owner: address(this), m_value: 15}
}(arg0, arg1, ...);
```
##### Other deploy options
The following options can be used with both `stateInit` and `code`:
* `value` (`varuint16`) - funds attached to the outbound internal message, that creates new account.
This value must be set.
* `currencies` (`mapping(uint32 => varuint32)`) - currencies attached to the outbound internal message.
Defaults to an empty set.
* `bounce` (`bool`) - if it's set and deploy falls (only at the computing phase, not at the action
phase!), then funds will be returned. Otherwise, (flag isn't set or deploying terminated successfully)
the address accepts the funds. Defaults to `true`.
* `wid` (`uint8`) - workchain id of the new account address. Defaults to `0`.
* `flag` (`uint16`) - flag used to send the outbound internal message. Defaults to `0`.
Possible values of the `flag` are described here: [\<address\>.transfer()](#addresstransfer).
```TVMSolidity
TvmCell stateInit = ...;
address newWallet = new SimpleWallet{
stateInit: stateInit,
value: 1 ever,
wid: -1,
flag: 0
}(arg0, arg1, ...);
```
##### Deploy via \<address\>.transfer()
You can also deploy the contract via [\<address\>.transfer()](#addresstransfer).
Just set the option `stateInit`.
* [Example of usage](https://github.com/everx-labs/samples/blob/master/solidity/11_ContractDeployer.sol)
* [Step-by-step description how to deploy contracts from the contract here](https://github.com/everx-labs/samples/blob/master/solidity/17_ContractProducer.md).
##### Deploy the contract with no constructor
If the contract does not have constructor explicitly and does not have state variables with initialisation, then in `*.abi.json` file there is no `constructor` function and no `_constructorFlag` field.
For example: [1_Accumulator_no_ctor.sol](https://github.com/everx-labs/samples/blob/master/solidity/1_Accumulator_no_ctor.sol) and [1_Accumulator_no_ctor.abi.json](https://github.com/everx-labs/samples/blob/master/solidity/1_Accumulator_no_ctor.abi.json). To deploy this contractor by external message with help `ever-cli`, use parameter `method` for `deploy` and `deployx` commands:
```bash
ever-cli deploy --method add '{"delta": 123}' ...
```
To deploy a contractor by internal message, use option `stateInit` for [External function calls](#external-function-calls). See `deployNoConstructor` and `deployNoConstructor2` functions [11_ContractDeployer.sol](https://github.com/everx-labs/samples/blob/master/solidity/11_ContractDeployer.sol) as samples of deploying [11_Waller_no_constructor.sol](https://github.com/everx-labs/samples/blob/master/solidity/11_Waller_no_constructor.sol).
##### New contract address problem
Address of the new account is calculated as a hash of the `stateInit`.
Parameters of the constructor don't influence the address. The problem
is that hacker can deploy the contract with your `stateInit` before you
with malicious constructor parameters.
Let's consider how to protect against this problem:
1. Constructor is called by external message.
We must Check if we didn't forget to set the public key in the contract and the
inbound message is signed by that key. If hacker doesn't have your private
key, then he can't sign message to call the constructor.
See [constructor of WalletProducer](https://github.com/everx-labs/samples/blob/master/solidity/17_ContractProducer.sol).
2. Constructor is called by internal message.
We should define static variable in the new contract that will contain
address of the creator. Address of the creator will be a part of the `stateInit`.
And in the constructor we must check address of the message sender.
See [function `deployWallet` how to deploy contract](https://github.com/everx-labs/samples/blob/master/solidity/17_ContractProducer.sol).
See [constructor of SimpleWallet](https://github.com/everx-labs/samples/blob/master/solidity/17_SimpleWallet.sol).
If some contract should deploy plenty of contracts (with some contract's
public key), then it's a good idea to declare static variable in the deployed
contract. This variable can contain some sequence number. It will allow
each new contact to have unique `stateInit`.
See [SimpleWallet](https://github.com/everx-labs/samples/blob/master/solidity/17_SimpleWallet.sol).
**Note**: contract's public key (`tvm.pubkey()`) is a part of `stateInit`.
##### Misc functions from `tvm`
##### tvm.code()
```TVMSolidity
tvm.code() returns (TvmCell);
```
Returns contract's code. [Capabilities](#tvm-capabilities) required: `CapMycode`.
See [SelfDeployer](https://github.com/everx-labs/samples/blob/master/solidity/21_self_deploy.sol).
##### tvm.pubkey()
```TVMSolidity
tvm.pubkey() returns (uint256);
```
Returns contract's public key, stored in contract data. If key is not set, function returns 0.
##### tvm.setPubkey()
```TVMSolidity
tvm.setPubkey(uint256 newPubkey);
```
Set new contract's public key. Contract's public key can be obtained from `tvm.pubkey`.
##### tvm.setCurrentCode()
```TVMSolidity
tvm.setCurrentCode(TvmCell newCode);
```
Changes this smart contract current code to that given by Cell **newCode**. Unlike [tvm.setcode()](#tvmsetcode)
this function changes code of the smart contract only for current TVM execution, but has no effect
after termination of the current run of the smart contract.
See example of how to use this function:
* [old contract](https://github.com/everx-labs/samples/blob/master/solidity/12_BadContract.sol)
* [new contract](https://github.com/everx-labs/samples/blob/master/solidity/12_NewVersion.sol)
##### tvm.resetStorage()
```TVMSolidity
tvm.resetStorage();
```
Resets all state variables to their default values.
##### tvm.exit() and tvm.exit1()
```TVMSolidity
tvm.exit();
tvm.exit1();
```
Functions are used to save state variables and quickly terminate execution of the smart contract.
Exit code of the compute phase is equal to zero/one for `tvm.exit`/`tvm.exit1`.
Example:
```TVMSolidity
function g0(uint a) private {
if (a == 0) {
tvm.exit();
}
//...
}
function g1(uint a) private {
if (a == 0) {
tvm.exit1();
}
//...
}
```
#### tvm.sendrawmsg()
```TVMSolidity
tvm.sendrawmsg(TvmCell msg, uint8 flag);
```
Send the internal/external message `msg` with `flag`. It's a wrapper for opcode
`SENDRAWMSG` ([TVM][1] - A.11.10).
Internal message `msg` can be generated by [abi.encodeIntMsg()](#abiencodeintmsg).
Possible values of `flag` are described here: [\<address\>.transfer()](#addresstransfer).
**Note:** make sure that `msg` has a correct format and follows the [TL-B scheme][3] of `Message X`.
For example:
```TVMSolidity
TvmCell msg = ...
tvm.sendrawmsg(msg, 2);
```
If the function is called by external message and `msg` has a wrong format (for example, the field
`init` of `Message X` is not valid), then the transaction will be replayed despite the usage of flag 2.
It will happen because the transaction will fail at the action phase.
#### **bls** namespace
Operations on a pairing friendly BLS12-381 curve. BLS values are represented in TVM in the following way:
* G1-points and public keys: 48-byte slice.
* G2-points and signatures: 96-byte slice.
* Elements of field FP: 48-byte slice.
* Elements of field FP2: 96-byte slice.
* Messages: slice. Number of bits should be divisible by 8.
When input value is a point or a field element, the slice may have more than 48/96 bytes. In this case only the first 48/96 bytes are taken. If the slice has less bytes (or if message size is not divisible by 8), cell underflow exception is thrown.
[Capabilities](#tvm-capabilities) required: `CapTvmV20`.
#### bls.verify
```TVMSolidity
bls.verify(TvmSlice pubkey, TvmSlice message, TvmSlice sign) returns (bool)
```
Checks BLS signature. Returns `true` on success, `false` otherwise. Example:
```TVMSolidity
TvmSlice pubkey = "b65cfaf56cebd6083320bf9a1c2010d4775310c5e7b348546dce0f62aa1ad0c29e15a58e251582faa7879d74e9d4034b";
TvmSlice message = TvmSlice(bytes("Hello, BLS verify!"));
TvmSlice sign = "aa652737cad33a9b332300ecd53f1995e5d6c6ff5eb233c04b2e32ca1169524fee64d58575cb42a1a34e1bf3a61c550814d0147b2b82a668ef7c917c756e489e8ff57d64efbbf533d7995db28377d6442ec952268a2bf30d5770d4e8a9d56f9c";
bool ok = bls.verify(pubkey, message, sign);
```
#### bls.aggregate
```TVMSolidity
(1)
bls.aggregate(vector(TvmSlice) signs) returns (TvmSlice sign)
(2)
bls.aggregate(TvmSlice sign0, TvmSlice sign1, ...) returns (TvmSlice sign)
```
(1) Aggregates signatures if `signs.length() > 0`. Throw exception if `signs.empty()` or if some `signs[i]` is not a valid signature.
(2) Same as (1) but takes `TvmSlice`'s.
Example:
```TVMSolidity
vector(TvmSlice) signs;
signs.push("8b1eac18b6e7a38f2b2763c9a03c3b6cff4110f18c4d363eec455463bd5c8671fb81204c4732406d72468a1474df6133147a2240f4073a472ef419f23011ee4d6cf02fceb844398e33e2e331635dace3b26464a6851e10f6895923c568582fbd");
signs.push("94ec60eb8d2b657dead5e1232b8f9cc0162467b08f02e252e97622297787a74b6496607036089837fe5b52244bbbb6d00d3d7cc43812688451229d9e96f704401db053956c588203ba7638e8882746c16e701557f34b0c08bbe097483aec161e");
signs.push("8cdbeadb3ee574a4f796f10d656885f143f454cc6a2d42cf8cabcd592d577c5108e4258a7b14f0aafe6c86927b3e70030432a2e5aafa97ee1587bbdd8b69af044734defcf3c391515ab26616e15f5825b4b022a7df7b44f65a8792c54762e579");
TvmSlice sign = bls.aggregate(signs);
```
```TVMSolidity
TvmSlice sign0 = "8b1eac18b6e7a38f2b2763c9a03c3b6cff4110f18c4d363eec455463bd5c8671fb81204c4732406d72468a1474df6133147a2240f4073a472ef419f23011ee4d6cf02fceb844398e33e2e331635dace3b26464a6851e10f6895923c568582fbd";
TvmSlice sign1 = "94ec60eb8d2b657dead5e1232b8f9cc0162467b08f02e252e97622297787a74b6496607036089837fe5b52244bbbb6d00d3d7cc43812688451229d9e96f704401db053956c588203ba7638e8882746c16e701557f34b0c08bbe097483aec161e";
TvmSlice sign2 = "8cdbeadb3ee574a4f796f10d656885f143f454cc6a2d42cf8cabcd592d577c5108e4258a7b14f0aafe6c86927b3e70030432a2e5aafa97ee1587bbdd8b69af044734defcf3c391515ab26616e15f5825b4b022a7df7b44f65a8792c54762e579";
TvmSlice sign = bls.aggregate(sign0, sign1, sign2);
```
#### bls.fastAggregateVerify
```TVMSolidity
(1)
bls.fastAggregateVerify(vector(TvmSlice) pubkeys, TvmSlice message, TvmSlice singature) returns (bool ok)
(2)
bls.fastAggregateVerify(TvmSlice pubkey0, TvmSlice pubkey1, ..., TvmSlice message, TvmSlice singature) returns (bool ok)
```
(1) Checks aggregated BLS signature for `pubkeys` and `message`. Returns `true` on success, `false` otherwise. Return `false` if `pubkeys.empty()`.
(2) Same as (1) but takes `TvmSlice`'s.
Example:
```TVMSolidity
vector(TvmSlice) pubkeys;
pubkeys.push("a44184a47ad3fc0069cf7a95650a28af2ed715beab28651a7ff433e26c0fff714d21cc5657367bc563c6df28fb446d8f");
pubkeys.push("832c0eca9f8cae87a1c6362838b34723cf63a1f69e366d64f3c61fc237217c4bea601cfbf4d6c18849ed4f9487b4a20c");
pubkeys.push("9595aa3c5cb3d7c763fa6b52294ebde264bdf49748efbbe7737c35532db8fabc666bb0d186f329c8bdafddfbdcbc3ca6");
TvmSlice message = TvmSlice(bytes("Hello, BLS fast aggregate and verify!"));
TvmSlice singature = "8420b1944c64f74dd67dc9f5ab210bab928e2edd4ce7e40c6ec3f5422c99322a5a8f3a8527eb31366c9a74752d1dce340d5a98fbc7a04738c956e74e7ba77b278cbc52afc63460c127998aae5aa1c3c49e8c48c30cc92451a0a275a47f219602";
bool ok = bls.fastAggregateVerify(pubkeys, message, singature);
```
```TVMSolidity
TvmSlice pk0 = "a44184a47ad3fc0069cf7a95650a28af2ed715beab28651a7ff433e26c0fff714d21cc5657367bc563c6df28fb446d8f";
TvmSlice pk1 = "832c0eca9f8cae87a1c6362838b34723cf63a1f69e366d64f3c61fc237217c4bea601cfbf4d6c18849ed4f9487b4a20c";
TvmSlice pk2 = "9595aa3c5cb3d7c763fa6b52294ebde264bdf49748efbbe7737c35532db8fabc666bb0d186f329c8bdafddfbdcbc3ca6";
TvmSlice message = TvmSlice(bytes("Hello, BLS fast aggregate and verify!"));
TvmSlice singature = "8420b1944c64f74dd67dc9f5ab210bab928e2edd4ce7e40c6ec3f5422c99322a5a8f3a8527eb31366c9a74752d1dce340d5a98fbc7a04738c956e74e7ba77b278cbc52afc63460c127998aae5aa1c3c49e8c48c30cc92451a0a275a47f219602";
bool ok = bls.fastAggregateVerify(pk0, pk1, pk2, message, singature);
```
#### bls.aggregateVerify()
```TVMSolidity
(1)
bls.aggregateVerify(vector(TvmSlice, TvmSlice) pubkeysMessages, TvmSlice singature) returns (bool ok)
(2)
bls.aggregateVerify(TvmSlice pubkey0, TvmSlice pubkey1, ..., TvmSlice message0, TvmSlice message1, ..., TvmSlice singature) returns (bool ok)
```
(1) Checks aggregated BLS signature for key-message pairs `pubkeysMessages`. Returns `true` on success, `false` otherwise. Returns `false` if `pubkeysMessages.empty()`.
(2) Same as (1) but takes `TvmSlice`'s.
```TVMSolidity
vector(TvmSlice, TvmSlice) pubkeysMessages;
TvmSlice pubkey0 = "b75f0360095de73c4790f803153ded0f3e6aefa6f0aac8bfd344a44a3de361e3f6f111c0cf0ad0c4a0861492f9f1aeb1";
TvmSlice message0 = TvmSlice(bytes("Hello, BLS fast aggregate and verify 0!"));
pubkeysMessages.push(pubkey0, message0);
TvmSlice pubkey1 = "a31e12bb4ffa75aabbae8ec2367015ba3fc749ac3826539e7d0665c285397d02b48414a23f8b33ecccc750b3afffacf6";
TvmSlice message1 = TvmSlice(bytes("Hello, BLS fast aggregate and verify 1!"));
pubkeysMessages.push(pubkey1, message1);
TvmSlice pubkey2 = "8de5f18ca5938efa896fbc4894c6044cdf89e778bf88584be48d6a6235c504cd45a44a68620f763aea043b6381add1f7";
TvmSlice message2 = TvmSlice(bytes("Hello, BLS fast aggregate and verify 2!"));
pubkeysMessages.push(pubkey2, message2);
TvmSlice singature = "8b8238896dfe3b02dc463c6e645e36fb78add51dc8ce32f40ecf60a418e92762856c3427b672be67278b5c4946b8c5a30fee60e5c38fdb644036a4f29ac9a039ed4e3b64cb7fef303052f33ac4391f95d482a27c8341246516a13cb72e58097b";
bool ok = bls.aggregateVerify(pubkeysMessages, singature);
```
```TVMSolidity
TvmSlice pubkey0 = "b75f0360095de73c4790f803153ded0f3e6aefa6f0aac8bfd344a44a3de361e3f6f111c0cf0ad0c4a0861492f9f1aeb1";
TvmSlice message0 = TvmSlice(bytes("Hello, BLS fast aggregate and verify 0!"));
TvmSlice pubkey1 = "a31e12bb4ffa75aabbae8ec2367015ba3fc749ac3826539e7d0665c285397d02b48414a23f8b33ecccc750b3afffacf6";
TvmSlice message1 = TvmSlice(bytes("Hello, BLS fast aggregate and verify 1!"));
TvmSlice pubkey2 = "8de5f18ca5938efa896fbc4894c6044cdf89e778bf88584be48d6a6235c504cd45a44a68620f763aea043b6381add1f7";
TvmSlice message2 = TvmSlice(bytes("Hello, BLS fast aggregate and verify 2!"));
TvmSlice singature = "8b8238896dfe3b02dc463c6e645e36fb78add51dc8ce32f40ecf60a418e92762856c3427b672be67278b5c4946b8c5a30fee60e5c38fdb644036a4f29ac9a039ed4e3b64cb7fef303052f33ac4391f95d482a27c8341246516a13cb72e58097b";
bool ok = bls.aggregateVerify(pubkey0, message0, pubkey1, message1, pubkey2, message2, singature);
```
#### bls.g1Zero() and bls.g2Zero()
```TVMSolidity
bls.g1Zero() returns (TvmSlice)
bls.g2Zero() returns (TvmSlice)
```
Returns zero point in G1/G2.
#### bls.g1IsZero() and bls.g2IsZero()
```TVMSolidity
bls.g1Zero(TvmSlice x) returns (bool isZero)
bls.g2Zero(TvmSlice x) returns (bool isZero)
```
Checks that G1/G2 point `x` is equal to zero.
#### bls.g1Add() and bls.g2Add()
```TVMSolidity
bls.g1Add(TvmSlice a, TvmSlice b) returns (TvmSlice res)
bls.g2Add(TvmSlice a, TvmSlice b) returns (TvmSlice res)
```
Addition on G1/G2.
#### bls.g1Sub() and bls.g2Sub()
```TVMSolidity
bls.g1Sub(TvmSlice a, TvmSlice b) returns (TvmSlice res)
bls.g2Sub(TvmSlice a, TvmSlice b) returns (TvmSlice res)
```
Subtraction on G1/G2.
#### bls.g1Neg() and bls.g2Neg()
```TVMSolidity
bls.g1Neg(TvmSlice x) returns (TvmSlice res)
bls.g2Neg(TvmSlice x) returns (TvmSlice res)
```
Negation on G1/G2.
#### bls.g1Mul() and bls.g2Mul()
```TVMSolidity
bls.g1Mul(TvmSlice x, int s) returns (TvmSlice res)
bls.g2Mul(TvmSlice x, int s) returns (TvmSlice res)
```
Multiplies G1/G2 point `x` by scalar `s`. Any `s` is valid, including negative.
#### bls.g1InGroup() and bls.g2InGroup()
```TVMSolidity
bls.g1Mul(TvmSlice x) returns (bool ok)
bls.g2Mul(TvmSlice x) returns (bool ok)
```
Checks that slice `x` represents a valid element of G1/G2.
#### bls.r()
```TVMSolidity
bls.r() returns (uint255)
```
Pushes the order of G1 and G2 (approx. 2^255). It's 52435875175126190479447740508185965837690552500527637822603658699938581184513.
#### bls.g1MultiExp() and bls.g2MultiExp()
```TVMSolidity
(1)
bls.g1MultiExp(vector(TvmSlice, int) x_s) returns (TvmSlice)
bls.g2MultiExp(vector(TvmSlice, int) x_s) returns (TvmSlice)
(2)
bls.g1MultiExp(TvmSlice x0, int s0, TvmSlice x1, int s1, ...) returns (TvmSlice)
bls.g2MultiExp(TvmSlice x0, int s0, TvmSlice x1, int s1, ...) returns (TvmSlice)
```
(1) Calculates `x_1*s_1+...+x_n*s_n` for G1/G2 points `x_i` and scalars `s_i`. Returns zero point if `n==0`. Any `s_i` is valid, including negative.
(2) Same as (1) but takes `TvmSlice`'s and `int`'s.
```TVMSolidity
TvmSlice a = bls.mapToG1("7abd13983c76661a98659da83066c71bd6581baf20c82c825b007bf8057a258dc53f7a6d44fb6fdecb63d9586e845d92");
TvmSlice b = bls.mapToG1("7abd13983c76661118659da83066c71bd6581baf20c82c825b007bf8057a258dc53f7a6d44fb6fdecb63d9586e845d92");
TvmSlice c = bls.mapToG1("7abd13983c76661118659da83066c71bd658100020c82c825b007bf8057a258dc53f7a6d44fb6fdecb63d9586e845d92");
vector(TvmSlice, int) values;
values.push(a, 2);
values.push(b, 5);
values.push(c, 13537812947843);
TvmSlice res = bls.g1MultiExp(values);
TvmSlice aa = bls.g1Mul(a, 2);
TvmSlice bb = bls.g1Mul(b, 5);
TvmSlice cc = bls.g1Mul(c, 13537812947843);
TvmSlice res2 = bls.g1Add(bls.g1Add(aa, bb), cc);
require(res == res2);
```
```TVMSolidity
TvmSlice a = bls.mapToG1("7abd13983c76661a98659da83066c71bd6581baf20c82c825b007bf8057a258dc53f7a6d44fb6fdecb63d9586e845d92");
TvmSlice b = bls.mapToG1("7abd13983c76661118659da83066c71bd6581baf20c82c825b007bf8057a258dc53f7a6d44fb6fdecb63d9586e845d92");
TvmSlice c = bls.mapToG1("7abd13983c76661118659da83066c71bd658100020c82c825b007bf8057a258dc53f7a6d44fb6fdecb63d9586e845d92");
TvmSlice res = bls.g1MultiExp(a, 2, b, 5, c, 13537812947843);
TvmSlice aa = bls.g1Mul(a, 2);
TvmSlice bb = bls.g1Mul(b, 5);
TvmSlice cc = bls.g1Mul(c, 13537812947843);
TvmSlice res2 = bls.g1Add(bls.g1Add(aa, bb), cc);
require(res == res2);
```
#### **math** namespace
`T` is an integer, [variable integer](#varint-and-varuint), [qintN and quintN](#qintn-and-quintn) or fixed point type in the `math.*` functions where applicable.
Fixed point type is not applicable for `math.modpow2()`, `math.muldiv[r|c]()`, `math.muldivmod()` and `math.divmod()`.
##### math.min() math.max()
```TVMSolidity
math.min(T a, T b, ...) returns (T);
math.max(T a, T b, ...) returns (T);
```
Returns the minimal (maximal) value of the passed arguments.
##### math.minmax()
```TVMSolidity
math.minmax(T a, T b) returns (T /*min*/, T /*max*/);
```
Returns minimal and maximal values of the passed arguments.
Example:
```TVMSolidity
(uint a, uint b) = math.minmax(20, 10); // (a, b) == (10, 20)
```
##### math.abs()
```TVMSolidity
math.abs(T1 val) returns (T2);
```
Computes the absolute value of the given integer. Throws an exception if absolute value of `val` does not fit into T2.
Example:
```TVMSolidity
int256 a = -100;
uint255 b = math.abs(a); // b == 100
int256 a = type(int256).min;
uint255 b = math.abs(a); // throws an exception
```
##### math.modpow2()
```TVMSolidity
math.modpow2(T value, uint power) returns (T);
```
Computes the `value mod 2^power`. Note: `power` should be a constant integer.
Example:
```TVMSolidity
uint a = math.modpow2(21, 4); // a == 5 because 21 % (2**4) == 21 % 16 == 5
uint constant pow = 10;
uint val = 1026;
uint b = math.modpow2(val, pow); // b == 2 because 1026 % (2**10) == 1026 % 1024 == 2
```
##### math.divr() math.divc()
```TVMSolidity
math.divc(T a, T b) returns (T);
math.divr(T a, T b) returns (T);
```
Returns result of the division of two integers. The return value is rounded. **ceiling** and **nearest** modes are used for `divc` and `divr`
respectively. See also: [Division and rounding](#division-and-rounding).
Example:
```TVMSolidity
int c = math.divc(10, 3); // c == 4
int c = math.divr(10, 3); // c == 3
fixed32x2 a = 0.25;
fixed32x2 res = math.divc(a, 2); // res == 0.13
```
##### math.divmod()
```TVMSolidity
math.divmod(T a, T b) returns (T /*result*/, T /*remainder*/);
```
This function divides the first number by the second and returns the result and the
remainder. Result is rounded to the floor.
Example:
```TVMSolidity
uint a = 11;
uint b = 3;
(uint d, uint r) = math.divmod(a, b); // (d, r) == (3, 2)
int e = -11;
int f = 3;
(int h, int p) = math.divmod(e, f); // (h, p) == (-3, 2)
```
##### math.muldiv() math.muldivr() math.muldivc()
```TVMSolidity
math.muldiv(T a, T b, T c) returns (T);
math.muldivr(T a, T b, T c) returns (T);
math.muldivc(T a, T b, T c) returns (T);
```
Multiplies two values and then divides the result by a third value. The return value is rounded. **floor**, **ceiling** and **nearest** modes are used for `muldiv`,
`muldivc` and `muldivr` respectively. See also: [Division and rounding](#division-and-rounding).
Example:
```TVMSolidity
uint res = math.muldiv(3, 7, 2); // res == 10
uint res = math.muldivr(3, 7, 2); // res == 11
uint res = math.muldivc(3, 7, 2); // res == 11
```
##### math.muldivmod()
```TVMSolidity
math.muldivmod(T a, T b, T c) returns (T /*quotient*/, T /*remainder*/);
```
This instruction multiplies first two arguments, divides the result by third argument and returns
the result and the remainder. Intermediate result is stored in the 514 bit buffer, and the final
result is rounded to the floor.
Example:
```TVMSolidity
uint a = 3;
uint b = 2;
uint c = 5;
(uint d, uint r) = math.muldivmod(a, b, c); // (d, r) == (1, 1)
int e = -1;
int f = 3;
int g = 2;
(int h, int p) = math.muldivmod(e, f, g); // (h, p) == (-2, 1)
```
##### math.mulmod()
```TVMSolidity
math.mulmod(T a, T b, T c) returns (T /*remainder*/);
```
Same as [math.muldivmod()](#mathmuldivmod) but returns only remainder. Example:
```TVMSolidity
uint constant P = 2**255 - 19;
function f() public pure {
uint a = rnd.next(P);
uint b = rnd.next(P);
uint c = math.mulmod(a, b, P);
//...
}
```
##### math.sign()
```TVMSolidity
math.sign(T val) returns (int2);
```
Returns:
* -1 if `val` is negative;
* 0 if `val` is zero;
* 1 if `val` is positive.
Example:
```TVMSolidity
int8 sign = math.sign(-100); // sign == -1
int8 sign = math.sign(100); // sign == 1
int8 sign = math.sign(0); // sign == 0
```
##### **tx** namespace
##### tx.logicaltime
```TVMSolidity
tx.logicaltime (uint64);
```
Returns the logical time of the current transaction.
##### tx.storageFee
```TVMSolidity
tx.storageFee (varuint16);
```
Returns the storage fee paid in the current transaction. [Capabilities](#tvm-capabilities) required: `CapStorageFeeToTvm`.
##### **block** namespace
##### block.timestamp
```TVMSolidity
block.timestamp (uint32);
```
Returns the current Unix time. Unix time is the same for the all transactions from one block.
##### block.seqno
```TVMSolidity
block.seqno returns (uint32);
```
Returns the seq no of the current block.
##### block.logicaltime
```TVMSolidity
block.logicaltime (uint64);
```
Returns the starting logical time of the current block.
##### **rnd** namespace
The pseudorandom number generator uses the random seed. The
initial value of the random seed before a smart contract execution in
TVM compatible blockchains is a hash of the smart contract address and the global
block random seed. If there are several runs of the same smart contract
inside a block, then all of these runs will have the same random seed.
This can be fixed, for example, by running `rnd.shuffle()` (without
parameters) each time before using the pseudorandom number generator.
##### rnd.next
```TVMSolidity
(1)
rnd.next() returns (uint);
(2)
rnd.next(T limit) returns (T);
```
Generates a new pseudo-random number.
(1) Returns `uint256` number.
(2) If the first argument `limit > 0`, then function returns the value in the
range `0..limit-1`. Else if `limit < 0`, then the returned value lies in range
`limit..-1`. Else if `limit == 0`, then it returns `0`.
Example:
```TVMSolidity
// (1)
uint256 r0 = rnd.next(); // 0..2^256-1
// (2)
uint8 r1 = rnd.next(100); // 0..99
int8 r2 = rnd.next(int8(100)); // 0..99
int8 r3 = rnd.next(int8(-100)); // -100..-1
```
##### rnd.getSeed
```TVMSolidity
rnd.getSeed() returns (uint256);
```
Returns the current random seed.
##### rnd.setSeed
```TVMSolidity
rnd.setSeed(uint256 x);
```
Sets the random seed to `x`.
##### rnd.shuffle
```TVMSolidity
(1)
rnd.shuffle(uint someNumber);
(2)
rnd.shuffle();
```
Randomizes the random seed.
(1) Mixes the random seed and `someNumber`. The result is set as the random seed.
(2) Mixes the random seed and the logical time of the current transaction.
The result is set as the random seed.
Example:
```TVMSolidity
(1)
uint256 someNumber = ...;
rnd.shuffle(someNumber);
(2)
rnd.shuffle();
```
#### abi namespace
##### abi.encode(), abi.decode()
```TVMSolidity
(1)
abi.encode(TypeA a, TypeB b, ...) returns (TvmCell /*cell*/);
(2)
abi.decode(TvmCell cell, (TypeA, TypeB, ...)) returns (TypeA /*a*/, TypeB /*b*/, ...);
```
`abi.encode` creates `cell` from the values.
`abi.decode` decodes the `cell` and returns the values. Note: all types must be set in `abi.decode`.
Otherwise, `abi.decode` throws an exception.
Example:
```TVMSolidity
// pack the values to the cell
TvmCell cell = abi.encode(uint(1), uint(2), uint(3), uint(4));
// unpack the cell
(uint a, uint b, uint c, uint d) = abi.decode(cell, (uint, uint, uint, uint));
// a == 1
// b == 2
// c == 3
// d == 4
```
##### abi.encodeData()
```TVMSolidity
abi.encodeData({uint256 pubkey, contract Contract, varInit: {VarName0: varValue0, ...}});
```
Generates `data` field of the `StateInit` ([TBLKCH][2] - 3.1.7.). Parameters are the same as in
[abi.encodeStateInit()](#abiencodestateinit).
```TVMSolidity
// SimpleWallet.sol
contract SimpleWallet {
uint static m_id;
address static m_creator;
// ...
}
// Usage
TvmCell data = abi.encodeData({
contr: SimpleWallet,
varInit: {m_id: 1, m_creator: address(this)},
pubkey: 0x3f82435f2bd40915c28f56d3c2f07af4108931ae8bf1ca9403dcf77d96250827
});
TvmCell code = ...;
TvmCell stateInit = abi.encodeStateInit({code: code, data: data});
// Note, the code above can be simplified to:
TvmCell stateInit = abi.encodeStateInit({
code: code,
contr: SimpleWallet,
varInit: {m_id: 1, m_creator: address(this)},
pubkey: 0x3f82435f2bd40915c28f56d3c2f07af4108931ae8bf1ca9403dcf77d96250827
});
```
##### abi.encodeOldDataInit()
```TVMSolidity
abi.encodeOldDataInit() returns (TvmCell);
```
Same as [abi.encodeData()](#abiencodedata) but generate data in the format that was used in the compiler < 0.72.0. This function can be used to deploy old contracts (that was compiled < 0.72.0) from new ones. Example:
File with old contracts that was compiled by compiler < 0.72.0:
```TVMSolidity
pragma tvm-solidity >= 0.72.0; // set new version
contract SimpleContractA {
uint static m_x0;
uint static m_x1;
}
// Remove all code from old contracts but state variables and contructor declaration
contract SimpleContractB is SimpleContractA {
uint static m_x2;
constructor(string name) {
}
}
```
File with new contracts:
```TVMSolidity
pragma tvm-solidity >= 0.72.0;
contract ContractCreator {
function deploy(uint pubkey, TvmCell code) public pure returns (address) {
TvmCell data = abi.encodeOldDataInit({
pubkey: pubkey,
varInit: {
m_x0: 100,
m_x1: 200,
m_x2: 300
},
contr: SimpleContractB
});
TvmCell stateInit = abi.encodeStateInit({
code: code,
data: data
});
SimpleContractB addr = new SimpleContractB{
wid: 0,
value: 1 ever,
stateInit: stateInit,
flag: 1
}("Hello, world!");
return addr;
}
}
```
##### abi.decodeData()
```TVMSolidity
abi.decodeData(ContractName, TvmSlice) returns (uint256 /*pubkey*/, uint64 /*timestamp*/, bool /*constructorFlag*/, Type1 /*var1*/, Type2 /*var2*/, ...);
```
Loads state variables from `TvmSlice` that is obtained from the field `data` of `stateInit`.
Example:
```
contract A {
uint a = 111;
uint b = 22;
uint c = 3;
uint d = 44;
address e = address(12);
address f;
}
contract B {
function f(TvmCell data) public pure {
TvmSlice s = data.toSlice();
(uint256 pubkey, uint64 timestamp, bool flag,
uint a, uint b, uint c, uint d, address e, address f) = abi.decodeData(A, s);
// pubkey - pubkey of the contract A
// timestamp - timestamp that used for replay protection
// flag - always is equal to true
// a == 111
// b == 22
// c == 3
// d == 44
// e == address(12)
// f == address.addrNone
// s.empty()
}
}
```
##### abi.encodeStateInit()
```TVMSolidity
// 1)
abi.encodeStateInit(TvmCell code, TvmCell data) returns (TvmCell stateInit);
// 2)
abi.encodeStateInit(TvmCell code, TvmCell data, uint8 splitDepth) returns (TvmCell stateInit);
// 3)
abi.encodeStateInit({TvmCell code, TvmCell data, uint8 splitDepth,
uint256 pubkey, Contract contr, varInit: {VarName0: varValue0, ...}});
```
Generates a `StateInit` ([TBLKCH][2] - 3.1.7.) from `code` and `data` `TvmCell`s.
Member `splitDepth` of the tree of cell `StateInit`:
1) is not set. Has no value.
2) is set. `0 <= splitDepth <= 31`
3) Arguments can also be set with names.
List of possible names:
* `code` (`TvmCell`) - defines the code field of the `StateInit`. Must be specified.
* `data` (`TvmCell`) - defines the data field of the `StateInit`. Conflicts with `pubkey` and
`varInit`. Can be omitted, in this case data field would be built from `pubkey` and `varInit`.
* `splitDepth` (`uint8`) - splitting depth. `0 <= splitDepth <= 31`. Can be omitted. By default,
it has no value.
* `pubkey` (`uint256`) - defines the public key of the new contract. Conflicts with `data`.
Can be omitted, default value is 0.
* `varInit` (`initializer list`) - used to set [static](#keyword-static) variables of the contract.
Conflicts with `data` and requires `contr` to be set. Can be omitted.
* `contr` (`contract`) - defines the contract whose `StateInit` is being built. Mandatory to be set if the
option `varInit` is specified.
Examples of this function usage:
```TvmSolidity
contract A {
uint static var0;
address static var1;
}
contract C {
function f() public pure {
TvmCell code;
TvmCell data;
uint8 depth;
TvmCell stateInit = abi.encodeStateInit(code, data);
stateInit = abi.encodeStateInit(code, data, depth);
}
function f1() public pure {
TvmCell code;
TvmCell data;
uint8 depth;
uint pubkey;
uint var0;
address var1;
TvmCell stateInit1 = abi.encodeStateInit({code: code, data: data, splitDepth: depth});
stateInit1 = abi.encodeStateInit({code: code, splitDepth: depth, varInit: {var0: var0, var1: var1}, pubkey: pubkey, contr: A});
stateInit1 = abi.encodeStateInit({varInit: {var0: var0, var1: var1}, pubkey: pubkey, contr: A, code: code, splitDepth: depth});
stateInit1 = abi.encodeStateInit({contr: A, varInit: {var0: var0, var1: var1}, pubkey: pubkey, code: code, splitDepth: depth});
stateInit1 = abi.encodeStateInit({contr: A, varInit: {var0: var0, var1: var1}, pubkey: pubkey, code: code});
stateInit1 = abi.encodeStateInit({contr: A, varInit: {var0: var0, var1: var1}, code: code, splitDepth: depth});
}
}
```
##### abi.stateInitHash()
```TVMSolidity
abi.stateInitHash(uint256 codeHash, uint256 dataHash, uint16 codeDepth, uint16 dataDepth) returns (uint256);
```
Calculates hash of the stateInit for given code and data specifications.
Example:
```TVMSolidity
TvmCell code = ...;
TvmCell data = ...;
uint codeHash = tvm.hash(code);
uint dataHash = tvm.hash(data);
uint16 codeDepth = code.depth();
uint16 dataDepth = data.depth();
uint256 hash = abi.stateInitHash(codeHash, dataHash, codeDepth, dataDepth);
```
See also [internal doc](https://github.com/everx-labs/TVM-Solidity-Compiler/blob/master/docs/internal/stateInit_hash.md) to read more about this
function mechanics.
##### abi.encodeBody()
```TVMSolidity
abi.encodeBody(function, arg0, arg1, arg2, ...) returns (TvmCell);
abi.encodeBody(function, callbackFunction, arg0, arg1, arg2, ...) returns (TvmCell);
abi.encodeBody(contract, arg0, arg1, arg2, ...) returns (TvmCell);
```
Constructs a message body for the function call. Body can be used as a payload for [\<address\>.transfer()](#addresstransfer).
If the **function** is `responsible`, **callbackFunction** must be set.
Example:
```TVMSolidity
contract Remote {
constructor(uint x, uint y, uint z) public { /* */ }
function func(uint256 num, int64 num2) public pure { /* */ }
function getCost(uint256 num) public responsible pure returns (uint128) { /* */ }
}
// deploy the contract
TvmCell body = abi.encodeBody(Remote, 100, 200, 300);
addr.transfer({value: 10 ever, body: body, stateInit: stateInit });
// call the function
TvmCell body = abi.encodeBody(Remote.func, 123, -654);
addr.transfer({value: 10 ever, body: body});
// call the responsible function
TvmCell body = abi.encodeBody(Remote.getCost, onGetCost, 105);
addr.transfer({value: 10 ever, body: body});
```
See also:
* [External function calls](#external-function-calls)
* [abi.loadFunctionParams()](#abidecodefunctionparams)
* [abi.encodeIntMsg()](#abiencodeintmsg)
##### abi.decodeFunctionParams()
```TVMSolidity
// Loads parameters of the public/external function without "responsible" attribute
abi.decodeFunctionParams(functionName) returns (TypeA /*a*/, TypeB /*b*/, ...);
// Loads parameters of the public/external function with "responsible" attribute
abi.decodeFunctionParams(functionName) returns (uint32 callbackFunctionId, TypeA /*a*/, TypeB /*b*/, ...);
// Loads constructor parameters
abi.decodeFunctionParams(ContractName) returns (TypeA /*a*/, TypeB /*b*/, ...);
```
Loads parameters of the function or constructor (if contract type is provided). This function is usually used in
**[onBounce](#onbounce)** function.
See example of how to use **onBounce** function:
* [onBounceHandler](https://github.com/everx-labs/samples/blob/master/solidity/16_onBounceHandler.sol)
##### abi.codeSalt()
```TVMSolidity
abi.codeSalt(TvmCell code) returns (optional(TvmCell) optSalt);
```
If **code** contains salt, then **optSalt** contains one. Otherwise, **optSalt** doesn't contain any value.
##### abi.setCodeSalt()
```TVMSolidity
abi.setCodeSalt(TvmCell code, TvmCell salt) returns (TvmCell newCode);
```
Inserts **salt** into **code** and returns new code **newCode**.
##### abi.functionId()
```TVMSolidity
// id of public function
abi.functionId(functionName) returns (uint32);
// id of public constructor
abi.functionId(ContractName) returns (uint32);
```
Returns the function id (uint32) of a public/external function or constructor.
Example:
```TVMSolidity
contract MyContract {
constructor(uint a) public {
}
/*...*/
}
function f() public pure returns (uint) {
/*...*/
}
function getConstructorID() public pure returns (uint32) {
uint32 functionId = abi.functionId(MyContract);
return functionId;
}
function getFuncID() public pure returns (uint32) {
uint32 functionId = abi.functionId(f);
return functionId;
}
}
```
See example of how to use this function:
* [onBounceHandler](https://github.com/everx-labs/samples/blob/master/solidity/16_onBounceHandler.sol)
##### abi.encodeIntMsg()
```TVMSolidity
abi.encodeIntMsg({
dest: address,
value: uint128,
call: {function, [callbackFunction,] arg0, arg1, arg2, ...},
bounce: bool,
currencies: mapping(uint32 => varuint32),
stateInit: TvmCell
})
returns (TvmCell);
```
Generates an internal outbound message that contains a function call. The result `TvmCell` can be used to send a
message using [tvm.sendrawmsg()](#tvmsendrawmsg). If the `function` is `responsible`, then
`callbackFunction` parameter must be set.
`dest`, `value` and `call` parameters are mandatory. Another parameters can be omitted. See
[\<address\>.transfer()](#addresstransfer) where these options and their default values are
described.
See also:
* sample [22_sender.sol](https://github.com/everx-labs/samples/blob/master/solidity/22_sender.sol)
* [abi.encodeBody()](#abiencodebody)
### **gosh** namespace
All `gosh.*` functions are experimental features and are available only in certain blockchain
networks in which `CapDiff` [capability](#tvm-capabilities) is set.
#### gosh.diff and gosh.zipDiff
```TVMSolidity
(1)
gosh.diff(string oldText, string newText) returns (string patch)
(2)
gosh.zipDiff(bytes oldText, bytes newText) returns (bytes patch)
```
(1) Calculates [patch](https://en.wikipedia.org/wiki/Diff) between `oldText` and `newText`.
(2) It's the same as `gosh.diff` but it calculates `patch` between compressed strings.
```TVMSolidity
string oldText = ...;
string newText = ...;
string patch = gosh.diff(oldText, newText);
```
#### gosh.applyPatch, gosh.applyPatchQ, gosh.applyZipPatch, gosh.applyZipPatchQ, gosh.applyZipBinPatch and gosh.applyZipBinPatchQ
```TVMSolidity
(1)
gosh.applyPatch(string oldText, string patch) returns (string newText)
gosh.applyPatchQ(string oldText, string patch) returns (optional(string) newText)
(2)
gosh.applyBinPatch(bytes oldText, bytes patch) returns (bytes newText)
gosh.applyBinPatchQ(bytes oldText, bytes patch) returns (optional(bytes) newText)
(3)
gosh.applyZipPatch(bytes oldText, bytes patch) returns (bytes newText)
gosh.applyZipPatchQ(bytes oldText, bytes patch) returns (optional(bytes) newText)
(4)
gosh.applyZipBinPatch(bytes oldText, bytes patch) returns (bytes newText)
gosh.applyZipBinPatchQ(bytes oldText, bytes patch) returns (optional(bytes) newText)
```
(1) Applies `patch` to the `oldText`. If it's impossible (bad patch), `gosh.applyPatch` throws an exception with type check
error code (-8) but`gosh.applyPatchQ` returns `null`.
(2) These are the same as `gosh.applyPatch`/`gosh.applyPatchQ` but these functions are applied to binary arrays.
(3) These are the same as `gosh.applyPatch`/`gosh.applyPatchQ` but these functions are applied to compressed strings.
(4) These are the same as `gosh.applyPatch`/`gosh.applyPatchQ` but these functions are applied to compressed binary arrays.
```TVMSolidity
string oldText = ...;
string patch = ...;
string newText = gosh.applyPatch(oldText, patch);
```
#### gosh.zip and gosh.unzip
```TVMSolidity
gosh.zip(string text) returns (bytes zip)
gosh.unzip(bytes zip) returns (optional(string) text)
```
`gosh.zip` converts the `text` to compressed `bytes`. `gosh.unzip` reverts such compression.
#### gosh.mintshell and gosh.mintshellq
```TVMSolidity
gosh.mintshell(uint64)
gosh.mintshellq(uint64)
```
`gosh.mintshell` ints vmshell tokens from the dApp configuration.
Succeeds if dApp config balance β₯ amount and mint rest of config balance in quiet version
```TVMSolidity
gosh.mintshell(1_000_000); // Mint 1M nanovmshell
```
#### gosh.sendtodappconfig
```TVMSolidity
gosh.sendtodappconfig(uint64)
```
`gosh.sendtodappconfig` deposits vmshell to dApp configuration.
```TVMSolidity
gosh.sendtodappconfig(1_000_000); // Deposit 1M nanovmshell
```
#### gosh.getavailablebalance
```TVMSolidity
gosh.getavailablebalance() returns (uint128)
```
`gosh.getavailablebalance` gets available vmshell balance in dApp config.
```TVMSolidity
gosh.getavailablebalance();
```
#### gosh.burnecc
```TVMSolidity
gosh.burnecc(uint64 amount, uint32 currency_id)
```
`gosh.burnecc` burns ECC tokens. If amount == 0, burn all ecc balance with this currency_id.
```TVMSolidity
gosh.burnecc(100, 2); // Burn 100 ecc-shell tokens
```
#### gosh.cnvrtshellq
```TVMSolidity
gosh.cnvrtshellq(uint64 amount)
```
`gosh.cnvrtshellq` converts ECC (currency_id=2) to vmshell. If balance of ECC <= amount, instruction convert all balance.
```TVMSolidity
gosh.cnvrtshellq(300); // Convert 300 ecc-shell tokens
```
#### gosh.mintecc
```TVMSolidity
gosh.mintecc(uint64 amount, uint32 currency_id)
```
`gosh.mintecc` mints ECC tokens (special contracts only).
```TVMSolidity
gosh.mintecc(300, 2); // Mint 300 ecc-shell tokens
```
#### Exponentiation
Exponentiation `**` is only available for unsigned types in the exponent. The resulting type of an
exponentiation is always equal to the type of the base. Please take care that it is large enough to
hold the result and prepare for potential assertion failures or wrapping behaviour.
Note that `0**0` throws an exception.
Example:
```TVMSolidity
uint b = 3;
uint32 p = 4;
uint res = b ** p; // res == 81
```
#### selfdestruct
```TVMSolidity
selfdestruct(address dest_addr);
```
Creates and sends the message that carries all the remaining balance
of the current smart contract and destroys the current account.
See example of how to use the `selfdestruct` function:
* [Kamikaze](https://github.com/everx-labs/samples/blob/master/solidity/8_Kamikaze.sol)
#### sha256
```TVMSolidity
(1)
sha256(TvmSlice slice) returns (uint256)
(2)
sha256(bytes b) returns (uint256)
(3)
sha256(string str) returns (uint256)
```
1. Computes the SHA-256 hash. If the bit-length of `slice` is not divisible by eight, throws a cell
underflow [exception](#tvm-exception-codes). References of `slice` are not used to compute the hash. Only data bits located
in the root cell of `slice` are used.
2. Computes the SHA-256 hash only for the first 127 bytes. If `bytes.length > 127`, then `b[128],
b[129], b[130] ...` elements are ignored.
3. Same as for `bytes`: only the first 127 bytes are taken into account.
See also [tvm.hash()](#tvmhash) to compute representation hash of the whole tree of cells.
#### gasToValue
```TVMSolidity
gasToValue(uint128 gas) returns (uint128 value)
gasToValue(uint128 gas, int8 wid) returns (uint128 value)
```
Returns worth of **gas** in workchain **wid**.
Throws an exception if **wid** is not equal to `0` or `-1`.
If `wid` is omitted than used the contract's `wid`.
#### valueToGas
```TVMSolidity
valueToGas(uint128 value) returns (uint128 gas)
valueToGas(uint128 value, int8 wid) returns (uint128 gas)
```
Counts how much **gas** could be bought on **value** nanoevers in workchain **wid**.
Throws an exception if **wid** is not equal to `0` or `-1`.
If `wid` is omitted than used the contract's `wid`.
#### gasleft
```TVMSolidity
gasleft() returns (uint64)
```
[Capabilities](#tvm-capabilities) required: `CapsTvmBugfixes2022`.
Returns the remaining gas.
### TVM capabilities
Rust implementation of TVM has capabilities. Capabilities are flags that can be set to turn on
some features or behavior of TVM. Full list of capabilities can be found in `enum GlobalCapabilities` in [ever-block](https://github.com/everx-labs/ever-block/blob/master/src/config_params.rs) repo.
Set capabilities store in 8th parameter of the global config of the blockchain. To get it you can use command:
```bash
ever-cli --json getconfig 8
```
### TVM exception codes
| Name | Code | Definition |
|-------------------|:----:|-----------------------------------------------------------------------------------------------------------------------------------------------------|
| Stack underflow | 2 | Not enough arguments in the stack for a primitive |
| Stack overflow | 3 | More values have been stored on a stack than allowed by this version of TVM |
| Integer overflow | 4 | Integer does not fit into expected range (by default β2<sup>256</sup> β€ x < 2<sup>256</sup>), or a division by zero has occurred |
| Range check error | 5 | Integer out of expected range |
| Invalid opcode | 6 | Instruction or its immediate arguments cannot be decoded |
| Type check error | 7 | An argument to a primitive is of incorrect value type |
| Cell overflow | 8 | Error in one of the serialization primitives |
| Cell underflow | 9 | Deserialization error |
| Dictionary error | 10 | Error while deserializing a dictionary object |
| Unknown error | 11 | Unknown error, may be thrown by user programs |
| Fatal error | 12 | Thrown by TVM in situations deemed impossible |
| Out of gas | -14 | Thrown by TVM when the remaining gas (g<sub>r</sub>) becomes negative. This exception cannot be caught and leads to an immediate termination of TVM |
See also: [TVM][1] - 4.5.7
### Solidity runtime errors
Smart-contract written in TVM Solidity can throw runtime errors while execution.
Solidity runtime error codes:
* **40** - External inbound message has an invalid signature. See [tvm.pubkey()](#tvmpubkey) and [msg.pubkey()](#msgpubkey).
* **50** - Array index or index of [\<mapping\>.at()](#mappingat) is out of range.
* **51** - Contract's constructor has already been called.
* **52** - Replay protection exception. See `timestamp` in [pragma AbiHeader](#pragma-abiheader).
* **54** - `<array>.pop` call for an empty array.
* **57** - External inbound message is expired. See `expire` in [pragma AbiHeader](#pragma-abiheader).
* **58** - External inbound message has no signature but has public key. See `pubkey` in [pragma AbiHeader](#pragma-abiheader).
* **60** - Inbound message has wrong function id. In the contract there are no functions with such function id and there is no fallback function that could handle the message. See [fallback](#fallback).
* **61** - Deploying `StateInit` has no public key in `data` field.
* **62** - Reserved for internal usage.
* **63** - See [\<optional(T)\>.get()](#optionaltget).
* **67** - See [gasToValue](#gastovalue) and [valueToGas](#valuetogas).
* **68** - There is no config parameter 20 or 21.
* **69** - Zero to the power of zero calculation (`0**0` in TVM Solidity style or `0^0`).
* **70** - `string` method `substr` was called with substr longer than the whole string.
* **71** - Function marked by `externalMsg` was called by internal message.
* **72** - Function marked by `internalMsg` was called by external message.
* **76** - Public function was called before constructor.
* **77** - It's impossible to convert `variant` type to target type. See [variant.toUint()](#varianttouint).
* **78** - There's no private function with the function id.
* **79** - You are deploying contract that uses [pragma upgrade func/oldsol](#pragma-upgrade-funcoldsol). Use the
* **80** - See [\<T\>.get()](#tget).
### Division and rounding
Let consider we have `x` and `y` and we want to divide `x` by `y`. Compute the quotient `q` and the
remainder `r` of the division of `x` by `y`: `x = y*q + r` where `|r| < |y|`.
In TVM there are 3 options of rounding:
* **floor** - quotient `q` is rounded to ββ. `q = βx/yβ`, `r` has the same sign as `y`. This
rounding option is used for operator `/`.
Example:
```TVMSolidity
int res = int(-2) / 3; // res == -1
int res = int(-5) / 10; // res == -1
int res = int(5) / 10; // res == 0
int res = int(15) / 10; // res == 1
```
* **ceiling** - quotient `q` is rounded to +β. `q = βx/yβ`, `r` and `y` have opposite signs.
Example:
```TVMSolidity
int res = math.divc(-2, 3); // res == 0
int res = math.divc(-5, 10); // res == 0
int res = math.divc(5, 10); // res == 1
int res = math.divc(15, 10); // res == 2
```
* **nearest** - quotient `q` is rounded to the nearest number. `q = βx/y + 1/2β` and `|r| β€ |y|/2`.
Example:
```TVMSolidity
int res = math.divr(-2, 3); // res == -1
int res = math.divr(-5, 10); // res == 0
int res = math.divr(5, 10); // res == 1
int res = math.divr(15, 10); // res == 2
```
### Contract execution
Before executing any contract function special code is executed. In `*.code` file there are two special
functions: `main_internal` and `main_external` that run on internal and external messages
respectively. These functions initialize some internal global variables and call contract
function of special function like `receive`, `fallback`, `onBounce`, `onTickTock`, etc.
Before calling contract's function `main_external` does:
1. Checks the message signature. Let's consider how the signature is checked:
- If signature exists and `pubkey` header isn't defined, then `tvm.pubkey()` is used
for checking.
- If signature isn't exists and `pubkey` header isn't defined, then signature isn't checked.
- If signature exists, `pubkey` header is defined and `pubkey` isn't exists in the
message, then `tvm.pubkey()` is used for checking.
- If signature exists, `pubkey` header is defined and `pubkey` exists in the
message, then `msg.pubkey()` is used for checking.
- If signature isn't exists, `pubkey` header is defined and `pubkey` exists in the
message, then an [exception with code 58](#solidity-runtime-errors) is thrown.
2. Replay protection:
- [*time* header](#pragma-abiheader) exists (`pragma AbiHeader notime` is not used), then the contract checks whether
`oldTime` < `time` < `now * 1000 + 30 minutes`. If it's true, then `oldTime` is updated by new `time`.
Otherwise, an exception is thrown.
- there is `afterSignatureCheck` (despite usage of `time`), then make your own replay protection.
3. Message expiration:
- `expire` exists and there is no `afterSignatureCheck`, then the contract checks whether
`expire` > `now`.
- there is `afterSignatureCheck` (despite usage of `expire`), then make your own check.
See also: [pragma AbiHeader](#pragma-abiheader), [afterSignatureCheck](#aftersignaturecheck).
### Gas optimization hints
Try to reduce count of `[]` operations for mappings and arrays. For example:
```TVMSolidity
Struct Point {
uint x;
uint y;
uint z;
}
Point[] points;
```
Here we have 3 `[]` operations:
```TVMSolidity
points[0].x = 5;
points[0].y = 10;
points[0].z = -5;
```
We can use a temp variable:
```TVMSolidity
Point p = points[0];
p.x = 5;
p.y = 10;
p.z = -5;
points[0] = p;
```
[1]: https://test.ton.org/tvm.pdf "TVM"
[2]: https://test.ton.org/tblkch.pdf "TBLKCH"
[3]: https://github.com/ton-blockchain/ton/blob/master/crypto/block/block.tlb "TL-B scheme"
|