Пример #1
0
        public async Task <byte[]> DeploySmartContract(int category, byte[] contract)
        {
            SmartContractRegistration registration = new SmartContractRegistration
            {
                Category      = category,
                ContractBytes = ByteString.CopyFrom(contract),
                ContractHash  = Hash.FromRawBytes(contract)
            };

            var tx = Api.GetTransaction();

            ulong serialNumber = _serialNumber.Increment().Value;

            var creator = Api.GetTransaction().From;

            var info = new ContractInfo()
            {
                Owner        = creator,
                SerialNumber = serialNumber
            };

            var address = info.Address;
            // calculate new account address
            var account = DataPath.CalculateAccountAddress(tx.From, tx.IncrementId);

            await Api.DeployContractAsync(account, registration);

            Console.WriteLine("TestContractZero: Deployment success, {0}", account.DumpHex());
            return(account.DumpByteArray());
        }
Пример #2
0
        public static void Reduce(Address owner, ulong amount)
        {
            var pair = new AddressPair()
            {
                First = owner, Second = Api.GetTransaction().From
            };

            _allowances[pair] = _allowances[pair].Sub(amount);
        }
Пример #3
0
        public static void Approve(Address spender, ulong amount)
        {
            var pair = new AddressPair()
            {
                First = Api.GetTransaction().From, Second = spender
            };

            _allowances[pair] = _allowances[pair].Add(amount);
        }
Пример #4
0
        public void TransferFrom(Address from, Address to, ulong amount)
        {
            var allowance = Allowances.GetAllowance(from, Api.GetTransaction().From);

            Api.Assert(allowance > amount, "Insufficient allowance.");

            DoTransfer(from, to, amount);
            Allowances.Reduce(from, amount);
        }
Пример #5
0
 public void UnApprove(Address spender, ulong amount)
 {
     Allowances.Reduce(spender, amount);
     new UnApproved()
     {
         Owner   = Api.GetTransaction().From,
         Spender = spender,
         Amount  = amount
     }.Fire();
 }
Пример #6
0
 public void Initialize(string symbol, string tokenName, ulong totalSupply, uint decimals)
 {
     Api.Assert(!_initialized.GetValue(), "Already initialized.");
     // Api.Assert(Api.GetContractOwner().Equals(Api.GetTransaction().From), "Only owner can initialize the contract state.");
     _symbol.SetValue(symbol);
     _tokenName.SetValue(tokenName);
     _totalSupply.SetValue(totalSupply);
     _decimals.SetValue(decimals);
     _balances[Api.GetTransaction().From] = totalSupply;
     _initialized.SetValue(true);
 }
Пример #7
0
        public override async Task InvokeAsync()
        {
            var tx = Api.GetTransaction();

            var methodname = tx.MethodName;
            var type       = GetType();
            var member     = type.GetMethod(methodname);
            // params array
            var parameters = Parameters.Parser.ParseFrom(tx.Params).Params.Select(p => p.Value()).ToArray();

            // invoke
            //await (Task<object>)member.Invoke(this, parameters);
            await(Task <object>) member.Invoke(this, parameters);
        }
Пример #8
0
        public void Transfer(Hash to, ulong amount)
        {
            var from      = Api.GetTransaction().From;
            var balSender = _balances[from];

            Api.Assert(balSender > amount, "Insufficient balance.");
            var balReceiver = _balances[to];

            balSender       = balSender.Sub(amount);
            balReceiver     = balReceiver.Add(amount);
            _balances[from] = balSender;
            _balances[to]   = balReceiver;
            new Transfered()
            {
                From   = from,
                To     = to,
                Amount = amount
            }.Fire();
        }
Пример #9
0
        public byte[] CreateSideChain(Hash chainId, Address lockedAddress, ulong lockedToken)
        {
            ulong serialNumber = _sideChainSerialNumber.Increment().Value;
            var   info         = new SideChainInfo
            {
                Owner          = Api.GetTransaction().From,
                ChainId        = chainId,
                SerialNumer    = serialNumber,
                Status         = SideChainStatus.Pending,
                LockedAddress  = lockedAddress,
                LockedToken    = lockedToken,
                CreationHeight = Api.GetCurerntHeight() + 1
            };

            _sideChainInfos[chainId] = info;
            new SideChainCreationRequested()
            {
                ChainId = chainId,
                Creator = Api.GetTransaction().From
            }.Fire();
            return(chainId.DumpByteArray());
        }
Пример #10
0
        public async Task <object> Transfer(Hash from, Hash to, ulong qty)
        {
            // This is for testing batched transaction sequence
            await TransactionStartTimes.SetValueAsync(Api.GetTransaction().GetHash(), Now());

            var fromBalBytes = await Balances.GetValue(from);

            var fromBal    = fromBalBytes.ToUInt64();
            var toBalBytes = await Balances.GetValue(to);

            var toBal      = toBalBytes.ToUInt64();
            var newFromBal = fromBal - qty;

            var newToBal = toBal + qty;
            await Balances.SetValueAsync(from, newFromBal.ToBytes());

            await Balances.SetValueAsync(to, newToBal.ToBytes());

            // This is for testing batched transaction sequence
            await TransactionEndTimes.SetValueAsync(Api.GetTransaction().GetHash(), Now());

            return(null);
        }
Пример #11
0
        public void Transfer(Address to, ulong amount)
        {
            var from = Api.GetTransaction().From;

            DoTransfer(from, to, amount);
        }