示例#1
0
        public static bool TryLoad(ProtocolSettings settings, DataCache snapshot, UInt160 scriptHash, out Nep17Contract contract)
        {
            if (scriptHash == NativeContract.NEO.Hash)
            {
                contract = Nep17Contract.Create(NativeContract.NEO);
                return(true);
            }

            if (scriptHash == NativeContract.GAS.Hash)
            {
                contract = Nep17Contract.Create(NativeContract.GAS);
                return(true);
            }

            var contractState = NativeContract.ContractManagement.GetContract(snapshot, scriptHash);

            if (contractState != null)
            {
                using var sb = new ScriptBuilder();
                sb.EmitDynamicCall(scriptHash, "symbol");
                sb.EmitDynamicCall(scriptHash, "decimals");

                using var engine = sb.Invoke(settings, snapshot);
                if (engine.State != VMState.FAULT && engine.ResultStack.Count >= 2)
                {
                    var decimals = (byte)engine.ResultStack.Pop <Neo.VM.Types.Integer>().GetInteger();
                    var symbol   = Encoding.UTF8.GetString(engine.ResultStack.Pop().GetSpan());
                    contract = new Nep17Contract(symbol, decimals, scriptHash);
                    return(true);
                }
            }

            contract = default;
            return(false);
        }
示例#2
0
        public static bool TryLoad(DataCache snapshot, UInt160 scriptHash, out Nep17Contract contract)
        {
            if (scriptHash == NativeContract.NEO.Hash)
            {
                contract = new Nep17Contract(
                    NativeContract.NEO.Name,
                    NativeContract.NEO.Symbol,
                    NativeContract.NEO.Decimals,
                    NativeContract.NEO.Hash);
                return(true);
            }

            if (scriptHash == NativeContract.GAS.Hash)
            {
                contract = new Nep17Contract(
                    NativeContract.GAS.Name,
                    NativeContract.GAS.Symbol,
                    NativeContract.GAS.Decimals,
                    NativeContract.GAS.Hash);
                return(true);
            }

            var contractState = NativeContract.ContractManagement.GetContract(snapshot, scriptHash);

            if (contractState != null)
            {
                using var sb = new ScriptBuilder();
                sb.EmitDynamicCall(scriptHash, "symbol");
                sb.EmitDynamicCall(scriptHash, "decimals");

                using var engine = Neo.SmartContract.ApplicationEngine.Run(sb.ToArray(), snapshot);
                if (engine.State != VMState.FAULT && engine.ResultStack.Count >= 2)
                {
                    var decimals = (byte)engine.ResultStack.Pop <Neo.VM.Types.Integer>().GetInteger();
                    var symbol   = Encoding.UTF8.GetString(engine.ResultStack.Pop().GetSpan());
                    contract = new Nep17Contract(contractState.Manifest.Name, symbol, decimals, scriptHash);
                    return(true);
                }
            }

            contract = default;
            return(false);
        }