Пример #1
0
        public static bool CreateOracleRequest(string request, byte[] address)
        {
            //TODO: check request format

            //TODO: transfer ong

            Transaction tx = (Transaction)ExecutionEngine.ScriptContainer;

            byte[] txHash = tx.Hash;
            //Runtime.Notify("txHash is :", txHash);

            Map <byte[], string> undoRequest = new Map <byte[], string>();

            byte[] v = Storage.Get(Storage.CurrentContext, "UndoRequest");
            if (v.Length != 0)
            {
                undoRequest = (Map <byte[], string>)Helper.Deserialize(v);
            }
            undoRequest[txHash] = request;

            byte[] b = Helper.Serialize(undoRequest);
            Storage.Put(Storage.CurrentContext, "UndoRequest", b);
            Runtime.Notify("CreateOracleRequest Done");
            return(true);
        }
Пример #2
0
    public static void Main()
    {
        // create struct instance
        Person p = new Person();

        p.Name = "bob";
        p.Age  = 20;
        Runtime.Notify(p.Name);
        Runtime.Notify(p.Age);

        // serialize struct instance to byte[]
        byte[] b = Helper.Serialize(p);

        // deserialize byte[] to struct
        Person p2 = (Person)Helper.Deserialize(b);

        Runtime.Notify(p2.Name);
        Runtime.Notify(p2.Age);

        // create struct array
        Person[] array = new Person[] { new Person {
                                            Name = "tom", Age = 18
                                        }, new Person {
                                            Name = "jack", Age = 20
                                        } };
    }
Пример #3
0
        public static bool SetOracleOutcome(byte[] txHash, byte[] result)
        {
            //TODO: check witness
            if (!Runtime.CheckWitness(admin))
            {
                Runtime.Notify("Checkwitness failed.");
                return(false);
            }

            Storage.Put(Storage.CurrentContext, txHash, result);

            //remove txHash from undoRequest map
            Map <byte[], string> undoRequest = new Map <byte[], string>();

            byte[] v = Storage.Get(Storage.CurrentContext, "UndoRequest");
            if (v.Length != 0)
            {
                undoRequest = (Map <byte[], string>)Helper.Deserialize(v);
            }
            undoRequest.Remove(txHash);

            byte[] b = Helper.Serialize(undoRequest);
            Storage.Put(Storage.CurrentContext, "UndoRequest", b);
            Runtime.Notify("SetOracleOutcome Done");
            return(true);
        }
Пример #4
0
    public static bool IssueBond(string bondName, uint parValue, uint purchaseEndTime, uint interval, uint round, uint couponRate, ulong totalCap, byte[] Account)
    {
        if (!Runtime.CheckWitness(admin))
        {
            return(false);
        }
        if (purchaseEndTime <= Runtime.Time)
        {
            return(false);
        }
        if (totalCap < minIssueCap || round < minRound || couponRate <= 0 || interval < minInterval)
        {
            return(false);
        }
        if (!validateAddress(Account))
        {
            return(false);
        }

        BondItem bond = new BondItem();

        bond.purchaseEndTime = purchaseEndTime;
        bond.CouponRate      = couponRate;
        bond.Interval        = interval;
        bond.TotalCap        = totalCap;
        bond.remainCap       = totalCap;
        bond.Round           = round;
        bond.Maturity        = purchaseEndTime + round * interval;

        byte[] b = Helper.Serialize(bond);
        Storage.Put(Storage.CurrentContext, bondPrefix.Concat(bondName.AsByteArray()), b);

        return(true);
    }
Пример #5
0
        public static bool Revoke(byte[] claimId, byte[] ontId)
        {
            byte[] v = Storage.Get(Storage.CurrentContext, claimId);

            if (v == null)
            {
                ErrorEvent(claimId, " not existed!");
                return(false);
            }

            ClaimTx c = (ClaimTx)Helper.Deserialize(v);

            if (c.status != 1)
            {
                ErrorEvent(claimId, " invalid status.");
                return(false);
            }
            if (!EqualBytes(c.commiterId, ontId))
            {
                ErrorEvent(ontId, " invalid.");
                return(false);
            }

            c.status = 0;

            byte[] tx = Helper.Serialize(c);
            Storage.Put(Storage.CurrentContext, claimId, tx);

            PushEvent(ontId, " revoke claim: ", claimId);
            return(true);
        }
Пример #6
0
    public static bool PayInterstOrPrincipal(string bondName, byte[] account)
    {
        if (!validateBond(bondName))
        {
            return(false);
        }

        byte[]     investorKey = bondInvestorPrefix.Concat(bondName.AsByteArray()).Concat(account);
        BigInteger balance     = Storage.Get(Storage.CurrentContext, investorKey).AsBigInteger();

        if (balance < minInvestCap)
        {
            return(false);
        }

        BondItem bond = (BondItem)Helper.Deserialize(GetBond(bondName));

        byte[]     paidKey      = bondPaidPrefix.Concat(bondName.AsByteArray()).Concat(account);
        BigInteger paidRound    = Storage.Get(Storage.CurrentContext, paidKey).AsBigInteger();
        BigInteger currentRound = (Runtime.Time - bond.purchaseEndTime) / bond.Interval;

        if (paidRound > bond.Round)
        {
            return(false);
        }
        if (currentRound > bond.Round)
        {
            currentRound = bond.Round;
        }

        BigInteger investValue = Storage.Get(Storage.CurrentContext, investorKey).AsBigInteger();
        BigInteger interst     = (currentRound - paidRound) * (investValue * bond.CouponRate / 100);

        byte[] ret;
        if (currentRound == bond.Round)
        {
            ret = Native.Invoke(0, ontAddr, "transfer", new object[1] {
                new Transfer {
                    From = bond.Account, To = account, Value = (ulong)(interst + investValue)
                }
            });
        }
        else
        {
            ret = Native.Invoke(0, ontAddr, "transfer", new object[1] {
                new Transfer {
                    From = bond.Account, To = account, Value = (ulong)interst
                }
            });
        }

        if (ret[0] != 1)
        {
            return(false);
        }
        Storage.Put(Storage.CurrentContext, paidKey, paidRound + 1);

        return(true);
    }
Пример #7
0
    public static bool InvestBond(string bondName, byte[] account, uint bondNumber)
    {
        if (!Runtime.CheckWitness(account))
        {
            return(false);
        }
        if (bondNumber <= 0 || !validateAddress(account) || !validateBond(bondName))
        {
            return(false);
        }
        BondItem bond = (BondItem)Helper.Deserialize(GetBond(bondName));

        if (Runtime.Time > bond.purchaseEndTime)
        {
            Runtime.Notify("bond subscription has been ended.");
            return(false);
        }

        uint investValue = bondNumber * bond.ParValue;

        if (bond.remainCap < investValue)
        {
            Runtime.Notify("bond remain invest capacity not enough.");
            return(false);
        }

        byte[] ret = Native.Invoke(0, ontAddr, "transfer", new object[1] {
            new Transfer {
                From = account, To = bond.Account, Value = investValue
            }
        });
        if (ret[0] != 1)
        {
            return(false);
        }
        bond.remainCap = bond.TotalCap - investValue;

        byte[]     investorKey = bondInvestorPrefix.Concat(bondName.AsByteArray()).Concat(account);
        BigInteger balance     = Storage.Get(Storage.CurrentContext, investorKey).AsBigInteger();

        Storage.Put(Storage.CurrentContext, investorKey, balance + investValue);

        return(true);
    }
Пример #8
0
    public static void Main()
    {
        Map <string, int> m = new Map <string, int>();

        m["k1"] = 100;
        m["k2"] = 200;
        Runtime.Notify(m["k1"]);
        Runtime.Notify(m["k2"]);

        // seriallize
        byte[] b = Helper.Serialize(m);
        Storage.Put(Storage.CurrentContext, "mapexample", b);
        byte[] v = Storage.Get(Storage.CurrentContext, "mapexample");

        // deserialize
        Map <string, int> m2 = (Map <string, int>)Helper.Deserialize(v);

        Runtime.Notify(m2["k1"]);
        Runtime.Notify(m2["k2"]);
    }
Пример #9
0
        // claimId 文件哈希
        // commiterId 具体描述
        // ownerId ontid
        public static bool Commit(byte[] claimId, byte[] commiterId, byte[] ownerId)
        {
            byte[] v = Storage.Get(Storage.CurrentContext, claimId);

            if (v != null)
            {
                ErrorEvent(claimId, " existed!");
                return(false);
            }

            ClaimTx c = new ClaimTx();

            c.claimId    = claimId;
            c.status     = 1;
            c.commiterId = commiterId;
            c.ownerId    = ownerId;

            byte[] tx = Helper.Serialize(c);

            Storage.Put(Storage.CurrentContext, claimId, tx);
            PushEvent(commiterId, " create new claim: ", claimId);
            return(true);
        }