Esempio n. 1
0
        public override void on_deposit(long amount)
        {
            checkState();
            var prev_contract_id = get_prev_call_frame_contract_address();

            if (Type(prev_contract_id) != "nil" && tostring(prev_contract_id) != "")
            {
                var preid = tostring(prev_contract_id);
                Error("this api can't called by contract:" + preid);
            }

            var remain         = fast_map_get("users", this.storage.admin);
            var headerblocknum = get_header_block_num();

            if (headerblocknum >= this.storage.icoEndBlocknum)
            {
                Error("ico has ended");
            }
            if (amount <= 0)
            {
                Error("amount should greater than 0");
            }
            long remainamount = Type(remain) == "number" ? tointeger(remain) : 0;

            if (remainamount <= 0)
            {
                Error("no tokens left");
            }
            long wantamount = amount * this.storage.rate;

            if ((wantamount / this.storage.rate) != amount)
            {
                Error("invalid amount");
            }
            if (remainamount < wantamount)
            {
                Error("not enlough tokens");
            }

            var  calleraddress = caller_address();
            var  balance       = fast_map_get("users", calleraddress);
            long toBalance     = (Type(balance) == "number") ? tointeger(balance) : 0;

            if (wantamount <= 0 || toBalance + wantamount < toBalance || toBalance < 0)
            {
                Error("invalid amount");
            }

            fast_map_set("users", this.storage.admin, remainamount - wantamount);
            fast_map_set("users", caller_address(), toBalance + wantamount);

            var eventarg = caller_address() + ",";

            eventarg = eventarg + tostring(amount);
            eventarg = eventarg + tostring(wantamount);
            MyEventEmitteer.EmitReceived(eventarg);
        }
Esempio n. 2
0
        //arg format json : {"toaddress":"xxxxxxxxxxxxxxxxx","amount":300}
        public void transfer(string args)
        {
            checkState();

            var prev_contract_id = get_prev_call_frame_contract_address();

            if (Type(prev_contract_id) != "nil" && tostring(prev_contract_id) != "")
            {
                var preid = tostring(prev_contract_id);
                Error("this api can't called by contract:" + preid);
            }

            var headerblocknum = get_header_block_num();
            //if (headerblocknum < this.storage.icoEndBlocknum)
            //{
            //    Error("ico not end yet , not allowed transfer ");
            //}
            var jsonModule = importModule <UvmJsonModule>("json");
            var jsonargs   = (UvmMap <string>)jsonModule.Loads(args);

            string to_address = tostring(jsonargs.Get("toaddress"));
            long   amount     = tointeger(jsonargs.Get("amount"));

            if (!is_valid_address(to_address))
            {
                Error("invalid toaddress");
            }

            string concaller = caller_address();
            var    balance   = fast_map_get("users", concaller);

            long fromBalance = (Type(balance) == "number") ? tointeger(balance) : 0;

            balance = fast_map_get("users", to_address);
            long toBalance = (Type(balance) == "number") ? tointeger(balance) : 0;

            if (amount <= 0 || fromBalance < amount || toBalance + amount < toBalance || toBalance < 0)
            {
                Error("invalid amount");
            }
            fromBalance = fromBalance - amount;
            toBalance   = toBalance + amount;
            fast_map_set("users", concaller, fromBalance);
            fast_map_set("users", to_address, toBalance);

            var eventarg = to_address + ",";

            eventarg = eventarg + tostring(amount);
            MyEventEmitteer.EmitTransfer(to_address);
        }