{
"id": "ssc-00000000000000000002",
"json": {
"contractName": "contract",
"contractAction": "deploy",
"contractPayload": {
"name": "tokens",
"params": "",
"code": "Ly9jb25zdCBhY3Rpb25zID0ge30KLy9jb25zdCBhcGkgPSB7fQoKYWN0aW9ucy5jcmVhdGVTU0MgPSBhc3luYyAocGF5bG9hZCkgPT4gewogIGF3YWl0IGFwaS5kYi5jcmVhdGVUYWJsZSgndG9rZW5zJywgWydzeW1ib2wnXSk7CiAgYXdhaXQgYXBpLmRiLmNyZWF0ZVRhYmxlKCdiYWxhbmNlcycsIFsnYWNjb3VudCddKTsKICBhd2FpdCBhcGkuZGIuY3JlYXRlVGFibGUoJ2NvbnRyYWN0c0JhbGFuY2VzJywgWydhY2NvdW50J10pOwogIGF3YWl0IGFwaS5kYi5jcmVhdGVUYWJsZSgncGFyYW1zJyk7CiAgYXdhaXQgYXBpLmRiLmNyZWF0ZVRhYmxlKCdwZW5kaW5nVW5zdGFrZXMnLCBbJ2FjY291bnQnLCAndW5zdGFrZUNvbXBsZXRlVGltZXN0YW1wJ10pOwoKICBjb25zdCBwYXJhbXMgPSB7fTsKICBwYXJhbXMudG9rZW5DcmVhdGlvbkZlZSA9ICcwJzsKICBhd2FpdCBhcGkuZGIuaW5zZXJ0KCdwYXJhbXMnLCBwYXJhbXMpOwp9OwoKYWN0aW9ucy51cGRhdGVQYXJhbXMgPSBhc3luYyAocGF5bG9hZCkgPT4gewogIGlmIChhcGkuc2VuZGVyICE9PSBhcGkub3duZXIpIHJldHVybjsKCiAgY29uc3QgeyB0b2tlbkNyZWF0aW9uRmVlIH0gPSBwYXlsb2FkOwoKICBjb25zdCBwYXJhbXMgPSBhd2FpdCBhcGkuZGIuZmluZE9uZSgncGFyYW1zJywge30pOwoKICBwYXJhbXMudG9rZW5DcmVhdGlvbkZlZSA9IHR5cGVvZiB0b2tlbkNyZWF0aW9uRmVlID09PSAnbnVtYmVyJyA/IHRva2VuQ3JlYXRpb25GZWUudG9GaXhlZCg4KSA6IHRva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YXBpLmFzc2VydCh0b2tlbiA9PT0gbnVsbCwgJ3N5bWJvbCBhbHJlYWR5IGV4aXN0cycpKSB7CiAgICAgICAgY29uc3QgZmluYWxVcmwgPSB1cmwgPT09IHVuZGVmaW5lZCA/ICcnIDogdXJsOwoKICAgICAgICBsZXQgbWV0YWRhdGEgPSB7CiAgICAgICAgICB1cmw6IGZpbmFsVXJsLAogICAgICAgIH07CgogICAgICAgIG1ldGFkYXRhID0gSlNPTi5zdHJpbmdpZnkobWV0YWRhdGEpOwogICAgICAgIGNvbnN0IG5ld1Rva2VuID0gewogICAgICAgICAgaXNzdWVyOiBhcGkuc2VuZGVyLAogICAgICAgICAgc3ltYm9sLAogICAgICAgICAgbmFtZSwKICAgICAgICAgIG1ldGFkYXRhLAogICAgICAgICAgcHJlY2lzaW9uLAogICAgICAgICAgbWF4U3VwcGx5OiBhcGkuQmlnTnVtYmVyKG1heFN1cHBseSkudG9GaXhlZChwcmVjaXNpb24pLAogICAgICAgICAgc3VwcGx5OiAnMCcsCiAgICAgICAgICBjaXJjdWxhdGluZ1N1cHBseTogJzAnLAogICAgICAgICAgc3Rha2luZ0VuYWJsZWQ6IGZhbHNlLAogICAgICAgICAgdW5zdGFraW5nQ29vbGRvd246IDEsCiAgICAgICAgICBuYkRheXMxMDBQZXJjZW50Vm90ZVJlZ2VuZXJhdGlvbjogMSwKICAgICAgICB9OwoKICAgICAgICBhd2FpdCBhcGkuZGIuaW5zZXJ0KCd0b2tlbnMnLCBuZXdUb2tlbik7CgogICAgICAgIC8vIGJ1cm4gdGhlIHRva2VuIGNyZWF0aW9uIGZlZXMKICAgICAgICBpZiAoYXBpLkJpZ051bWJlcih0b2tlbkNyZWF0aW9uRmVlKS5ndCgwKSkgewogICAgICAgICAgYXdhaXQgYWN0aW9ucy50cmFuc2Zlcih7CiAgICAgICAgICAgIHRvOiAnbnVsbCcsIHN5bWJvbDogIlNTQyIsIHF1YW50aXR5OiB0b2tlbkNyZWF0aW9uRmVlLCBpc1N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T09IG51bGwsICdzeW1ib2wgZG9lcyBub3QgZXhpc3QnKQogICAgICAgICAgJiYgYXBpLmFzc2VydChjb3VudERlY2ltYWxzKHF1YW50aXR5KSA8PSB0b2tlbi5wcmVjaXNpb24sICdzeW1ib2wgcHJlY2lzaW9uIG1pc21hdGNoJykKICAgICAgICAgICYmIGFwaS5hc3NlcnQocXVhbnRpdHkgPiAwLCAnbXVzdCB0cmFuc2ZlciBwb3NpdGl2ZSBxdWFudGl0eScpKSB7CgogICAgICAgICAgaWYgKGF3YWl0IHN1YkJhbGFuY2VWT25lKGFwaS5zZW5kZXIsIHRva2VuLCBxdWFudGl0eSwgJ2JhbGFuY2VzJykpIHsKICAgICAgICAgICAgY29uc3QgcmVzID0gYXdhaXQgYWRkQmFsYW5jZVZPbmUoZmluYWxUbywgdG9rZW4sIHF1YW50aXR5LCAnYmFsYW5jZXMnKTsKCiAgICAgICAgICAgIGlmIChyZXMgPT09IGZhbHNlKSB7CiAgICAgICAgICAgICAgYXdhaXQgYWRkQmFsYW5jZVZPbmUoYXBpLnNlbmRlciwgdG9rZW4sIHF1YW50aXR5LCAnYmFsYW5jZXMnKTsKCiAgICAgICAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgICAgICAgICB9CgogICAgICAgICAgICBpZiAoZmluYWxUbyA9PT0gJ251bGwnKSB7CiAgICAgICAgICAgICAgdG9rZW4uY2lyY3VsYXRpbmdTdXBwbHkgPSBjYWxjdWxhdGVCYWxhbmNlVk9uZSgKICAgICAgICAgICAgICAgIHRva2VuLmNpcmN1bGF0aW5nU3VwcGx5LCBxdWFudGl0eSwgdG9rZW4ucHJlY2lzaW9uLCBmYWxzZSwKICAgICAgICAgICAgICApOwogICAgICAgICAgICAgIGF3YWl0IGFwaS5kYi51cGRhdGUoJ3Rva2VucycsIHRva2VuKTsKICAgICAgICAgICAgfQoKICAgICAgICAgICAgYXBpLmVtaXQoJ3RyYW5zZmVyJywgewogICAgICAgICAgICAgIGZyb206IGFwaS5zZW5k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KGF3YWl0IHN1YkJhbGFuY2VWVHdvKGFwaS5zZW5kZXIsIHRva2VuLCBxdWFudGl0eSwgJ2JhbGFuY2VzJykpIHsKICAgICAgICAgICAgY29uc3QgcmVzID0gYXdhaXQgYWRkQmFsYW5jZVZUd28oZmluYWxUbywgdG9rZW4sIHF1YW50aXR5LCAnY29udHJhY3RzQmFsYW5jZXMnKTsKCiAgICAgICAgICAgIGlmIChyZXMgPT09IGZhbHNlKSB7CiAgICAgICAgICAgICAgYXdhaXQgYWRkQmFsYW5jZVZUd28oYXBpLnNlbmRlciwgdG9rZW4sIHF1YW50aXR5LCAnYmFsYW5jZXMnKTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICBpZiAoZmluYWxUbyA9PT0gJ251bGwnKSB7CiAgICAgICAgICAgICAgICB0b2tlbi5jaXJjdWxhdGluZ1N1cHBseSA9IGNhbGN1bGF0ZUJhbGFuY2VWVHdvKAogICAgICAgICAgICAgICAgICB0b2tlbi5jaXJjdWxhdGluZ1N1cHBseSwgcXVhbnRpdHksIHRva2VuLnByZWNpc2lvbiwgZmFsc2UsCiAgICAgICAgICAgICAgICApOwogICAgICAgICAgICAgICAgYXdhaXQgYXBpLmRiLnVwZGF0ZSgndG9rZW5zJywgdG9rZW4pOwogICAgICAgICAgICAgIH0KCiAgICAgICAgICAgICAgYXBpLmVtaXQoJ3RyYW5zZmVyVG9Db250cmFjdCcsIHsKICAgICAgICAgICAgICAgIGZyb206IGFwaS5zZW5kZXIsIHRvOiBmaW5hbFRvLCBzeW1ib2wsIHF1YW50aXR5LAogICAgICAgICAgICAgIH0pOwogICAgICAgICAgICB9CiAgICAgICAgICB9CiAgICAgICAgfQogICAgICB9CiAgICB9CiAgfQp9OwoKYWN0aW9ucy50cmFuc2ZlckZyb21Db250cmFjdCA9IGFzeW5jIChwYXlsb2FkKSA9PiB7CiAgLy8gdGhpcyBhY3Rpb24gY2FuIG9ubHkgYmUgY2FsbGVkIGJ5IHRoZSAnbnV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YWRkQmFsYW5jZVZUd28oZmluYWxUbywgdG9rZW4sIHF1YW50aXR5LCB0YWJsZSk7CgogICAgICAgICAgICAgIGlmIChyZXMgPT09IGZhbHNlKSB7CiAgICAgICAgICAgICAgICBhd2FpdCBhZGRCYWxhbmNlVlR3byhmcm9tLCB0b2tlbiwgcXVhbnRpdHksICdjb250cmFjdHNCYWxhbmNlcycpOwogICAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBpZiAoZmluYWxUbyA9PT0gJ251bGwnKSB7CiAgICAgICAgICAgICAgICAgIHRva2VuLmNpcmN1bGF0aW5nU3VwcGx5ID0gY2FsY3VsYXRlQmFsYW5jZVZUd28oCiAgICAgICAgICAgICAgICAgICAgdG9rZW4uY2lyY3VsYXRpbmdTdXBwbHksIHF1YW50aXR5LCB0b2tlbi5wcmVjaXNpb24sIGZhbHNlLAogICAgICAgICAgICAgICAgICApOwogICAgICAgICAgICAgICAgICBhd2FpdCBhcGkuZGIudXBkYXRlKCd0b2tlbnMnLCB0b2tlbik7CiAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgYXBpLmVtaXQoJ3RyYW5zZmVyRnJvbUNvbnRyYWN0JywgewogICAgICAgICAgICAgICAgICBmcm9tLCB0bzogZmluYWxUbywgc3ltYm9sLCBxdWFudGl0eSwKICAgICAgICAgICAgICAgIH0pOwogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgfQogICAgICAgIH0KICAgICAgfQogICAgfQogIH0KfTsKCmNvbnN0IHByb2Nlc3NVbnN0YWtlID0gYXN5bmMgKHVuc3Rha2UpID0+IHsKICBjb25zdCB7CiAgICBhY2NvdW50LAogICAgc3ltYm9sLAogICAgcXVhbnRpdHksCiAgICBxdWFudGl0eUxlZnQsCiAgICBudW1iZXJUcmFuc2FjdGlvbnNMZWZ0LAogIH0gPSB1bnN0YWtlOwoKICBjb25zdCBuZXdVbnN0YWtlID0gdW5zdGFrZTsKCiAgY29uc3QgYmF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