Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Atoi/Itoa #389

Merged
merged 13 commits into from
Nov 20, 2020
Merged

Add Atoi/Itoa #389

merged 13 commits into from
Nov 20, 2020

Conversation

shargon
Copy link
Member

@shargon shargon commented Nov 8, 2020

@shargon shargon requested a review from erikzhang November 8, 2020 09:32
Tommo-L
Tommo-L previously approved these changes Nov 9, 2020
@Tommo-L
Copy link
Contributor

Tommo-L commented Nov 9, 2020

waiting #387

@superboyiii
Copy link
Member

Test PASS

using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
using System;
using System.ComponentModel;
using System.Numerics;

namespace Sample
{
    [ManifestExtra("Author", "Neo")]
    [ManifestExtra("Name", "Sample")]
    [ManifestExtra("Email", "dev@neo.org")]
    [ManifestExtra("Description", "This is a contract example")]
    [Features(ContractFeatures.HasStorage)]
    public class Sample : SmartContract
    {
        #region Notifications
        [DisplayName("Put")]
        public static event Action<string> Onput;
        #endregion
        //TODO: Replace it with your own address.
        static readonly byte[] Owner = "NLtDqwnj9s7wQVyaiD5ohjV3e9fUVkZxDp".ToScriptHash();

        private static bool IsOwner() => Runtime.CheckWitness(Owner);

        public static bool Verify() => IsOwner();

        // TODO: Replace it with your methods.
        public static byte[] MyMethod()
        {
            return Storage.Get("Hello");
        }

        public static void Put(string word)
        {
            // It will be executed during deploy
            Storage.Put("Hello", word);
            Runtime.Log("Put " + word + " into storage completely.");
            Onput(word);
        }

        public static void Update(byte[] script, string manifest)
        {
            if (!IsOwner()) throw new Exception("No authorization.");
            Contract.Update(script, manifest);
        }

        public static void Destroy()
        {
            if (!IsOwner()) throw new Exception("No authorization.");
            Contract.Destroy();
        }
    }
}

invoke

{
  "jsonrpc": "2.0",
  "method": "invokefunction",
  "params": [
    "0x0a23fa703f033e4ba663c98ad7a61b1802ca63cd",
    "put",
    [
      {
        "type": "String",
        "value": "world"
      }
    ],
    [
      {
        "account": "NLtDqwnj9s7wQVyaiD5ohjV3e9fUVkZxDp",
        "scopes": "CalledByEntry"
      }
    ]
  ],
  "id": 3
}

Response:

{
    "jsonrpc": "2.0",
    "id": 3,
    "result": {
        "script": "DAV3b3JsZBHADANwdXQMFM1jygIYG6bXisljpks+Az9w+iMKQWJ9W1I=",
        "state": "HALT",
        "gasconsumed": "4274200",
        "exception": null,
        "stack": [
            {
                "type": "Any"
            }
        ],
        "tx": "ALXvdVUYOEEAAAAAAAZREgAAAAAAUxcAAAEKo4e1Ppa3mJpjFDGgVt0fQKBC9gEAKQwFd29ybGQRwAwDcHV0DBTNY8oCGBum14rJY6ZLPgM/cPojCkFifVtSAUIMQL/is98euFokwpfYWqJ4M9U9HX7QhnFe5ocmIL9OvlMRFrfyoKvgjkgqSgwpZmkJBneA1FNqpIcnftG7Aia71fopDCEDzqPi+B8a+TUi0p7eTySh8L7erXKTOR0ziA9Uddl4eMkLQZVEDXg="
    }
}

Console in cli
Runtime.Log("Put " + word + " into storage completely.");
image

GetApplicationLog

{
    "jsonrpc": "2.0",
    "id": 1,
    "result": {
        "txid": "0xe481e6a496783135616dc57d0839068ab0d2a0282196f4560246215f6dd62a08",
        "trigger": "Application",
        "vmstate": "HALT",
        "gasconsumed": "4274200",
        "stack": [
            {
                "type": "Any"
            }
        ],
        "notifications": [
            {
                "contract": "0x0a23fa703f033e4ba663c98ad7a61b1802ca63cd",
                "eventname": "Put",
                "state": {
                    "type": "Array",
                    "value": [
                        {
                            "type": "ByteString",
                            "value": "d29ybGQ="
                        }
                    ]
                }
            }
        ]
    }
}

superboyiii
superboyiii previously approved these changes Nov 12, 2020
@superboyiii
Copy link
Member

@erikzhang Merge?

@Tommo-L
Copy link
Contributor

Tommo-L commented Nov 12, 2020

Conflicts

@shargon shargon dismissed stale reviews from superboyiii and Tommo-L via a4629f8 November 12, 2020 08:26
Tommo-L
Tommo-L previously approved these changes Nov 12, 2020
@shargon
Copy link
Member Author

shargon commented Nov 12, 2020

Merge?

superboyiii
superboyiii previously approved these changes Nov 13, 2020
@superboyiii
Copy link
Member

@erikzhang

@shargon shargon dismissed stale reviews from superboyiii and Tommo-L via 6cdde5c November 13, 2020 11:21
@vncoelho
Copy link
Member

vncoelho commented Nov 13, 2020

@superboyiii,
In your example I got error on these conversions here:

Contract1.cs(20,40): error CS0266: Cannot implicitly convert type 'Neo.UInt160' to 'byte[]'. An explicit conversion exists (are you missing a cast?) [/neo-devpack-dotnet/templates/Template.CSharpNeoCompiler/Template.CSharpNeoCompiler.csproj]
Contract1.cs(22,63): error CS1503: Argument 1: cannot convert from 'byte[]' to 'Neo.UInt160' [/neo-devpack-dotnet/templates/Template.CSharpNeoCompiler/Template.CSharpNeoCompiler.csproj]
    0 Warning(s)
    2 Error(s)

@shargon
Copy link
Member Author

shargon commented Nov 14, 2020

@superboyiii your test must be with an integer

public static void Put(string word)
        {
            // It will be executed during deploy
            Storage.Put("Hello", word);
            Runtime.Log("Put " + word + " into storage completely.");
            Onput(word);
        }

@superboyiii
Copy link
Member

@superboyiii,
In your example I got error on these conversions here:

Contract1.cs(20,40): error CS0266: Cannot implicitly convert type 'Neo.UInt160' to 'byte[]'. An explicit conversion exists (are you missing a cast?) [/neo-devpack-dotnet/templates/Template.CSharpNeoCompiler/Template.CSharpNeoCompiler.csproj]
Contract1.cs(22,63): error CS1503: Argument 1: cannot convert from 'byte[]' to 'Neo.UInt160' [/neo-devpack-dotnet/templates/Template.CSharpNeoCompiler/Template.CSharpNeoCompiler.csproj]
    0 Warning(s)
    2 Error(s)

I saw it was changed recently in 6051662
image

@superboyiii
Copy link
Member

@superboyiii your test must be with an integer

public static void Put(string word)
        {
            // It will be executed during deploy
            Storage.Put("Hello", word);
            Runtime.Log("Put " + word + " into storage completely.");
            Onput(word);
        }

Retest: PASS

using Neo;
using Neo.SmartContract.Framework;
using Neo.SmartContract.Framework.Services.Neo;
using System;
using System.ComponentModel;

namespace Sample
{
    [ManifestExtra("Author", "Neo")]
    [ManifestExtra("Name", "Sample")]
    [ManifestExtra("Email", "dev@neo.org")]
    [ManifestExtra("Description", "This is a contract example")]
    public class Sample : SmartContract
    {
        #region Notifications
        [DisplayName("Word")]
        public static event Action<string> Word;

        [DisplayName("InvokePutCount")]
        public static event Action<byte[]> InvokePutCount;
        #endregion
        //TODO: Replace it with your own address.
        static readonly UInt160 Owner = "NLtDqwnj9s7wQVyaiD5ohjV3e9fUVkZxDp".ToScriptHash();

        private static bool IsOwner() => Runtime.CheckWitness(Owner);

        public static bool Verify() => IsOwner();

        // TODO: Replace it with your methods.
        public static byte[] MyMethod()
        {
            return Storage.Get("Hello");
        }

        public static void Put(string word)
        {
            int times = 2;
            // It will be executed during deploy
            Runtime.Log("Invoke Stroage.Put for " + times + " times.");
            Storage.Put("Hello", word);
            if (Storage.Get("No") != null)
            {
                Storage.Put("No", Storage.Get("No").ToBigInteger() + 1);
                
            } else
            {
                Storage.Put("No", 1);
            }
            var num = Storage.Get("No");
            Runtime.Log("Put " + word + " into storage completely");
            Word(word);
            InvokePutCount(num);


        }

        public static void Update(byte[] script, string manifest)
        {
            if (!IsOwner()) throw new Exception("No authorization.");
            Contract.Update(script, manifest);
        }

        public static void Destroy()
        {
            if (!IsOwner()) throw new Exception("No authorization.");
            Contract.Destroy();
        }
    }
}

Invoke Put second time.

Request

{
  "jsonrpc": "2.0",
  "method": "invokefunction",
  "params": [
    "0xe04c57554ffafc05cce6bed65b7f013efd0f3660",
    "put",
    [
      {
        "type": "String",
        "value": "world"
      }
    ],
    [
      {
        "account": "NLtDqwnj9s7wQVyaiD5ohjV3e9fUVkZxDp",
        "scopes": "CalledByEntry"
      }
    ]
  ],
  "id": 3
}
{
    "jsonrpc": "2.0",
    "id": 3,
    "result": {
        "script": "DAV3b3JsZBHADANwdXQMFGA2D/0+AX9b1r7mzAX8+k9VV0zgQWJ9W1I=",
        "state": "HALT",
        "gasconsumed": "9013350",
        "exception": null,
        "stack": [
            {
                "type": "Any"
            }
        ],
        "tx": "APwDDidmiIkAAAAAAAZREgAAAAAAMRkAAAEKo4e1Ppa3mJpjFDGgVt0fQKBC9gEAKQwFd29ybGQRwAwDcHV0DBRgNg/9PgF/W9a+5swF/PpPVVdM4EFifVtSAUIMQHjWKfrIJf1HHjccEb3hEQuiGIZz4cdV6LyUBAoI9eMCWi3O2v1AnY+iUekPhGLQBb6FBeEUlRpl0XiJXF9toZcpDCEDzqPi+B8a+TUi0p7eTySh8L7erXKTOR0ziA9Uddl4eMkLQZVEDXg="
    }
}

image

GetApplicationLog

{
    "jsonrpc": "2.0",
    "id": 1,
    "result": {
        "txid": "0xab9101da4367f47d897ee2766564ddc5819b1ca67c5cc797b3a76fb9d406373a",
        "executions": [
            {
                "trigger": "Application",
                "vmstate": "HALT",
                "gasconsumed": "9013350",
                "stack": [
                    {
                        "type": "Any"
                    }
                ],
                "notifications": [
                    {
                        "contract": "0xe04c57554ffafc05cce6bed65b7f013efd0f3660",
                        "eventname": "Word",
                        "state": {
                            "type": "Array",
                            "value": [
                                {
                                    "type": "ByteString",
                                    "value": "d29ybGQ="
                                }
                            ]
                        }
                    },
                    {
                        "contract": "0xe04c57554ffafc05cce6bed65b7f013efd0f3660",
                        "eventname": "InvokePutCount",
                        "state": {
                            "type": "Array",
                            "value": [
                                {
                                    "type": "ByteString",
                                    "value": "Ag=="
                                }
                            ]
                        }
                    }
                ]
            }
        ]
    }
}

It works well on mine.

@superboyiii
Copy link
Member

@shargon Shall we make BigInteger can be added as well?
image

@shargon
Copy link
Member Author

shargon commented Nov 16, 2020

@superboyiii Biginteger use object.ToString(), so if we do that, it won't be accurate. image

@superboyiii
Copy link
Member

superboyiii commented Nov 17, 2020

@superboyiii Biginteger use object.ToString(), so if we do that, it won't be accurate.

That's fine. It's good for me now.

@cloud8little
Copy link
Contributor

@superboyiii Biginteger use object.ToString(), so if we do that, it won't be accurate. image

It can be called like this instead of ToString():

        public static string test2()
        {
            BigInteger a = 200000000000000000;
            string b = "test: " + Binary.Itoa(a, 10);
            return b;
        }

@shargon
Copy link
Member Author

shargon commented Nov 19, 2020

Merge?

@shargon
Copy link
Member Author

shargon commented Nov 20, 2020

I will merge it, if there are any change we can do it in other Pull Request.

@shargon shargon merged commit ac7e39c into neo-project:master Nov 20, 2020
@shargon shargon deleted the sync-neo branch November 20, 2020 09:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support add operation between String and Int
5 participants