コード例 #1
0
        public async void Test()
        {
           
                var contractByteCode =
                    "0x60606040526040516020806106f5833981016040528080519060200190919050505b80600160005060003373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005081905550806000600050819055505b506106868061006f6000396000f360606040523615610074576000357c010000000000000000000000000000000000000000000000000000000090048063095ea7b31461008157806318160ddd146100b657806323b872dd146100d957806370a0823114610117578063a9059cbb14610143578063dd62ed3e1461017857610074565b61007f5b610002565b565b005b6100a060048080359060200190919080359060200190919050506101ad565b6040518082815260200191505060405180910390f35b6100c36004805050610674565b6040518082815260200191505060405180910390f35b6101016004808035906020019091908035906020019091908035906020019091905050610281565b6040518082815260200191505060405180910390f35b61012d600480803590602001909190505061048d565b6040518082815260200191505060405180910390f35b61016260048080359060200190919080359060200190919050506104cb565b6040518082815260200191505060405180910390f35b610197600480803590602001909190803590602001909190505061060b565b6040518082815260200191505060405180910390f35b600081600260005060003373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005060008573ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600050819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040518082815260200191505060405180910390a36001905061027b565b92915050565b600081600160005060008673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600050541015801561031b575081600260005060008673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005060003373ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000505410155b80156103275750600082115b1561047c5781600160005060008573ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828282505401925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a381600160005060008673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282825054039250508190555081600260005060008673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005060003373ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828282505403925050819055506001905061048656610485565b60009050610486565b5b9392505050565b6000600160005060008373ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000505490506104c6565b919050565b600081600160005060003373ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600050541015801561050c5750600082115b156105fb5781600160005060003373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282825054039250508190555081600160005060008573ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828282505401925050819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a36001905061060556610604565b60009050610605565b5b92915050565b6000600260005060008473ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005060008373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005054905061066e565b92915050565b60006000600050549050610683565b9056";
                var abi =
                    @"[{""constant"":false,""inputs"":[{""name"":""_spender"",""type"":""address""},{""name"":""_value"",""type"":""uint256""}],""name"":""approve"",""outputs"":[{""name"":""success"",""type"":""bool""}],""type"":""function""},{""constant"":true,""inputs"":[],""name"":""totalSupply"",""outputs"":[{""name"":""supply"",""type"":""uint256""}],""type"":""function""},{""constant"":false,""inputs"":[{""name"":""_from"",""type"":""address""},{""name"":""_to"",""type"":""address""},{""name"":""_value"",""type"":""uint256""}],""name"":""transferFrom"",""outputs"":[{""name"":""success"",""type"":""bool""}],""type"":""function""},{""constant"":true,""inputs"":[{""name"":""_owner"",""type"":""address""}],""name"":""balanceOf"",""outputs"":[{""name"":""balance"",""type"":""uint256""}],""type"":""function""},{""constant"":false,""inputs"":[{""name"":""_to"",""type"":""address""},{""name"":""_value"",""type"":""uint256""}],""name"":""transfer"",""outputs"":[{""name"":""success"",""type"":""bool""}],""type"":""function""},{""constant"":true,""inputs"":[{""name"":""_owner"",""type"":""address""},{""name"":""_spender"",""type"":""address""}],""name"":""allowance"",""outputs"":[{""name"":""remaining"",""type"":""uint256""}],""type"":""function""},{""inputs"":[{""name"":""_initialAmount"",""type"":""uint256""}],""type"":""constructor""},{""anonymous"":false,""inputs"":[{""indexed"":true,""name"":""_from"",""type"":""address""},{""indexed"":true,""name"":""_to"",""type"":""address""},{""indexed"":false,""name"":""_value"",""type"":""uint256""}],""name"":""Transfer"",""type"":""event""},{""anonymous"":false,""inputs"":[{""indexed"":true,""name"":""_owner"",""type"":""address""},{""indexed"":true,""name"":""_spender"",""type"":""address""},{""indexed"":false,""name"":""_value"",""type"":""uint256""}],""name"":""Approval"",""type"":""event""}]";
                var addressOwner = "0x12890d2cce102216644c59dae5baed380d84830c";

                var web3 = new Nethereum.Web3.Web3(ClientFactory.GetClient());
            try
            {
                var eth = web3.Eth;
                var transactions = eth.Transactions;
                ulong totalSupply = 1000000;

                var pass = "******";
                var result =
                    await web3.Personal.UnlockAccount.SendRequestAsync(addressOwner, pass, new HexBigInteger(600));
                Assert.True(result, "Account should be unlocked");
                var newAddress = await web3.Personal.NewAccount.SendRequestAsync(pass);

                Assert.NotNull(newAddress);
                Console.WriteLine(newAddress);

                var transactionHash =
                    await
                        eth.DeployContract.SendRequestAsync(abi, contractByteCode, addressOwner,
                            new HexBigInteger(900000), totalSupply);

                result = await web3.Miner.Start.SendRequestAsync();
                Assert.True(result, "Mining should have started");

                //get the contract address 
                var receipt = await GetTransactionReceiptAsync(transactions, transactionHash);

                var code = await web3.Eth.GetCode.SendRequestAsync(receipt.ContractAddress);

                if (String.IsNullOrEmpty(code))
                {
                    throw new Exception(
                        "Code was not deployed correctly, verify bytecode or enough gas was uto deploy the contract");
                }

                var tokenService = new StandardTokenService(web3, receipt.ContractAddress);

                var transfersEvent = tokenService.GetTransferEvent();
              

                var totalSupplyDeployed = await tokenService.GetTotalSupplyAsync<ulong>();
                Assert.Equal(totalSupply, totalSupplyDeployed);

                var ownerBalance = await tokenService.GetBalanceOfAsync<ulong>(addressOwner);
                Assert.Equal(totalSupply, ownerBalance);

               transactionHash = await tokenService.TransferAsync(addressOwner, newAddress, 1000);
              
               var transferReceipt = await GetTransactionReceiptAsync(transactions, transactionHash);

                ownerBalance = await tokenService.GetBalanceOfAsync<ulong>(addressOwner);
                Assert.Equal(totalSupply - 1000, ownerBalance);

                var newAddressBalance = await tokenService.GetBalanceOfAsync<ulong>(newAddress);
                Assert.Equal((ulong) 1000, newAddressBalance);

                var allTransfersFilter = await transfersEvent.CreateFilterAsync(new BlockParameter(transferReceipt.BlockNumber));
                var eventLogsAll = await transfersEvent.GetAllChanges<Transfer>(allTransfersFilter);
                Assert.Equal(1, eventLogsAll.Count);
                var transferLog = eventLogsAll.First();
                Assert.Equal(transferLog.Log.TransactionIndex.HexValue, transferReceipt.TransactionIndex.HexValue);
                Assert.Equal(transferLog.Log.BlockNumber.HexValue, transferReceipt.BlockNumber.HexValue);
                Assert.Equal(transferLog.Event.AddressTo, newAddress);
                Assert.Equal(transferLog.Event.Value, (ulong)1000);

            }
            finally
            {
                var result = await web3.Miner.Stop.SendRequestAsync();
                Assert.True(result, "Mining should have stop");
                result = await web3.Personal.LockAccount.SendRequestAsync(addressOwner);
                Assert.True(result, "Account should be locked");
            }
           
        }
コード例 #2
0
        public async void Test()
        {
            var contractByteCode =
                "0x6060604052341561000f57600080fd5b604051602080610711833981016040528080519150505b60018054600160a060020a03191633600160a060020a0390811691909117918290556000838155911681526002602052604090208190555b505b6106a28061006f6000396000f300606060405236156100a15763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166306fdde0381146100a6578063095ea7b31461013157806318160ddd1461016757806323b872dd1461018c578063313ce567146101c857806370a08231146101f15780638da5cb5b1461022257806395d89b4114610251578063a9059cbb146102dc578063dd62ed3e14610312575b600080fd5b34156100b157600080fd5b6100b9610349565b60405160208082528190810183818151815260200191508051906020019080838360005b838110156100f65780820151818401525b6020016100dd565b50505050905090810190601f1680156101235780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561013c57600080fd5b610153600160a060020a0360043516602435610380565b604051901515815260200160405180910390f35b341561017257600080fd5b61017a6103ed565b60405190815260200160405180910390f35b341561019757600080fd5b610153600160a060020a03600435811690602435166044356103f4565b604051901515815260200160405180910390f35b34156101d357600080fd5b6101db610510565b60405160ff909116815260200160405180910390f35b34156101fc57600080fd5b61017a600160a060020a0360043516610515565b60405190815260200160405180910390f35b341561022d57600080fd5b610235610534565b604051600160a060020a03909116815260200160405180910390f35b341561025c57600080fd5b6100b9610543565b60405160208082528190810183818151815260200191508051906020019080838360005b838110156100f65780820151818401525b6020016100dd565b50505050905090810190601f1680156101235780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156102e757600080fd5b610153600160a060020a036004351660243561057a565b604051901515815260200160405180910390f35b341561031d57600080fd5b61017a600160a060020a0360043581169060243516610649565b60405190815260200160405180910390f35b60408051908101604052601a81527f4578616d706c6520466978656420537570706c7920546f6b656e000000000000602082015281565b600160a060020a03338116600081815260036020908152604080832094871680845294909152808220859055909291907f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259085905190815260200160405180910390a35060015b92915050565b6000545b90565b600160a060020a0383166000908152600260205260408120548290108015906104445750600160a060020a0380851660009081526003602090815260408083203390941683529290522054829010155b80156104505750600082115b80156104755750600160a060020a038316600090815260026020526040902054828101115b1561050457600160a060020a0380851660008181526002602081815260408084208054899003905560038252808420338716855282528084208054899003905594881680845291905290839020805486019055917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9085905190815260200160405180910390a3506001610508565b5060005b5b9392505050565b601281565b600160a060020a0381166000908152600260205260409020545b919050565b600154600160a060020a031681565b60408051908101604052600581527f4649584544000000000000000000000000000000000000000000000000000000602082015281565b600160a060020a0333166000908152600260205260408120548290108015906105a35750600082115b80156105c85750600160a060020a038316600090815260026020526040902054828101115b1561063a57600160a060020a033381166000818152600260205260408082208054879003905592861680825290839020805486019055917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9085905190815260200160405180910390a35060016103e7565b5060006103e7565b5b92915050565b600160a060020a038083166000908152600360209081526040808320938516835292905220545b929150505600a165627a7a72305820ec01add6c7f9d88976180c397e2a5b2e9f8fc1f95f5abb00e2a4c9dbf7bcfaf20029";
            var abi =
                "[{\"constant\":true,\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_amount\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"totalSupply\",\"outputs\":[{\"name\":\"totalSupply\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_from\",\"type\":\"address\"},{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_amount\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_owner\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"name\":\"balance\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"owner\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_amount\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_owner\",\"type\":\"address\"},{\"name\":\"_spender\",\"type\":\"address\"}],\"name\":\"allowance\",\"outputs\":[{\"name\":\"remaining\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"totalSupply\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"_from\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"_to\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"Transfer\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"_owner\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"_spender\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"Approval\",\"type\":\"event\"}]";
            var addressOwner = "0x12890d2cce102216644c59dae5baed380d84830c";
            var pass         = "******";
            var web3         = new Web3Geth(new ManagedAccount(addressOwner, pass), ClientFactory.GetClient());

            try
            {
                var   eth          = web3.Eth;
                var   transactions = eth.Transactions;
                ulong totalSupply  = 1000000;


                var newAddress = await web3.Personal.NewAccount.SendRequestAsync(pass);

                Assert.NotNull(newAddress);
                Console.WriteLine(newAddress);
                var gas = await eth.DeployContract.EstimateGasAsync(abi, contractByteCode, addressOwner, totalSupply);

                var receipt =
                    await
                    eth.DeployContract.SendRequestAndWaitForReceiptAsync(abi, contractByteCode, addressOwner,
                                                                         new HexBigInteger(3000000), null, totalSupply);

                var tokenService = new StandardTokenService(web3, receipt.ContractAddress);

                var transfersEvent = tokenService.GetTransferEvent();


                var totalSupplyDeployed = await tokenService.GetTotalSupplyAsync <ulong>();

                Assert.Equal(totalSupply, totalSupplyDeployed);

                var ownerBalance = await tokenService.GetBalanceOfAsync <ulong>(addressOwner);

                Assert.Equal(totalSupply, ownerBalance);

                var transactionHash = await tokenService.TransferAsync(addressOwner, newAddress, 1000, new HexBigInteger(900000));

                var transferReceipt = await GetTransactionReceiptAsync(transactions, transactionHash);

                ownerBalance = await tokenService.GetBalanceOfAsync <ulong>(addressOwner);

                Assert.Equal(totalSupply - 1000, ownerBalance);

                var newAddressBalance = await tokenService.GetBalanceOfAsync <ulong>(newAddress);

                Assert.Equal((ulong)1000, newAddressBalance);

                var allTransfersFilter = await transfersEvent.CreateFilterAsync(new BlockParameter(transferReceipt.BlockNumber));

                var eventLogsAll = await transfersEvent.GetAllChanges <Transfer>(allTransfersFilter);

                Assert.Equal(1, eventLogsAll.Count);
                var transferLog = eventLogsAll.First();
                Assert.Equal(transferLog.Log.TransactionIndex.HexValue, transferReceipt.TransactionIndex.HexValue);
                Assert.Equal(transferLog.Log.BlockNumber.HexValue, transferReceipt.BlockNumber.HexValue);
                Assert.Equal(transferLog.Event.AddressTo, newAddress);
                Assert.Equal(transferLog.Event.Value, (ulong)1000);
            }
            finally
            {
                // var result = await web3.Personal.LockAccount.SendRequestAsync(addressOwner);
                // Assert.True(result, "Account should be locked");
            }
        }
コード例 #3
0
        public async void Test()
        {
            var contractByteCode =
                "0x60606040526040516020806106f5833981016040528080519060200190919050505b80600160005060003373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005081905550806000600050819055505b506106868061006f6000396000f360606040523615610074576000357c010000000000000000000000000000000000000000000000000000000090048063095ea7b31461008157806318160ddd146100b657806323b872dd146100d957806370a0823114610117578063a9059cbb14610143578063dd62ed3e1461017857610074565b61007f5b610002565b565b005b6100a060048080359060200190919080359060200190919050506101ad565b6040518082815260200191505060405180910390f35b6100c36004805050610674565b6040518082815260200191505060405180910390f35b6101016004808035906020019091908035906020019091908035906020019091905050610281565b6040518082815260200191505060405180910390f35b61012d600480803590602001909190505061048d565b6040518082815260200191505060405180910390f35b61016260048080359060200190919080359060200190919050506104cb565b6040518082815260200191505060405180910390f35b610197600480803590602001909190803590602001909190505061060b565b6040518082815260200191505060405180910390f35b600081600260005060003373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005060008573ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600050819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040518082815260200191505060405180910390a36001905061027b565b92915050565b600081600160005060008673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600050541015801561031b575081600260005060008673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005060003373ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000505410155b80156103275750600082115b1561047c5781600160005060008573ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828282505401925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a381600160005060008673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282825054039250508190555081600260005060008673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005060003373ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828282505403925050819055506001905061048656610485565b60009050610486565b5b9392505050565b6000600160005060008373ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000505490506104c6565b919050565b600081600160005060003373ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600050541015801561050c5750600082115b156105fb5781600160005060003373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282825054039250508190555081600160005060008573ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828282505401925050819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a36001905061060556610604565b60009050610605565b5b92915050565b6000600260005060008473ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005060008373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060005054905061066e565b92915050565b60006000600050549050610683565b9056";
            var abi =
                @"[{""constant"":false,""inputs"":[{""name"":""_spender"",""type"":""address""},{""name"":""_value"",""type"":""uint256""}],""name"":""approve"",""outputs"":[{""name"":""success"",""type"":""bool""}],""type"":""function""},{""constant"":true,""inputs"":[],""name"":""totalSupply"",""outputs"":[{""name"":""supply"",""type"":""uint256""}],""type"":""function""},{""constant"":false,""inputs"":[{""name"":""_from"",""type"":""address""},{""name"":""_to"",""type"":""address""},{""name"":""_value"",""type"":""uint256""}],""name"":""transferFrom"",""outputs"":[{""name"":""success"",""type"":""bool""}],""type"":""function""},{""constant"":true,""inputs"":[{""name"":""_owner"",""type"":""address""}],""name"":""balanceOf"",""outputs"":[{""name"":""balance"",""type"":""uint256""}],""type"":""function""},{""constant"":false,""inputs"":[{""name"":""_to"",""type"":""address""},{""name"":""_value"",""type"":""uint256""}],""name"":""transfer"",""outputs"":[{""name"":""success"",""type"":""bool""}],""type"":""function""},{""constant"":true,""inputs"":[{""name"":""_owner"",""type"":""address""},{""name"":""_spender"",""type"":""address""}],""name"":""allowance"",""outputs"":[{""name"":""remaining"",""type"":""uint256""}],""type"":""function""},{""inputs"":[{""name"":""_initialAmount"",""type"":""uint256""}],""type"":""constructor""},{""anonymous"":false,""inputs"":[{""indexed"":true,""name"":""_from"",""type"":""address""},{""indexed"":true,""name"":""_to"",""type"":""address""},{""indexed"":false,""name"":""_value"",""type"":""uint256""}],""name"":""Transfer"",""type"":""event""},{""anonymous"":false,""inputs"":[{""indexed"":true,""name"":""_owner"",""type"":""address""},{""indexed"":true,""name"":""_spender"",""type"":""address""},{""indexed"":false,""name"":""_value"",""type"":""uint256""}],""name"":""Approval"",""type"":""event""}]";
            var addressOwner = "0x12890d2cce102216644c59dae5baed380d84830c";

            var web3 = new Nethereum.Web3.Web3(ClientFactory.GetClient());

            try
            {
                var   eth          = web3.Eth;
                var   transactions = eth.Transactions;
                ulong totalSupply  = 1000000;

                var pass   = "******";
                var result =
                    await web3.Personal.UnlockAccount.SendRequestAsync(addressOwner, pass, new HexBigInteger(600));

                Assert.True(result, "Account should be unlocked");
                var newAddress = await web3.Personal.NewAccount.SendRequestAsync(pass);

                Assert.NotNull(newAddress);
                Console.WriteLine(newAddress);

                var transactionHash =
                    await
                    eth.DeployContract.SendRequestAsync(abi, contractByteCode, addressOwner,
                                                        new HexBigInteger(900000), totalSupply);

                result = await web3.Miner.Start.SendRequestAsync();

                Assert.True(result, "Mining should have started");

                //get the contract address
                var receipt = await GetTransactionReceiptAsync(transactions, transactionHash);

                var code = await web3.Eth.GetCode.SendRequestAsync(receipt.ContractAddress);

                if (String.IsNullOrEmpty(code))
                {
                    throw new Exception(
                              "Code was not deployed correctly, verify bytecode or enough gas was uto deploy the contract");
                }

                var tokenService = new StandardTokenService(web3, receipt.ContractAddress);

                var transfersEvent = tokenService.GetTransferEvent();


                var totalSupplyDeployed = await tokenService.GetTotalSupplyAsync <ulong>();

                Assert.Equal(totalSupply, totalSupplyDeployed);

                var ownerBalance = await tokenService.GetBalanceOfAsync <ulong>(addressOwner);

                Assert.Equal(totalSupply, ownerBalance);

                transactionHash = await tokenService.TransferAsync(addressOwner, newAddress, 1000);

                var transferReceipt = await GetTransactionReceiptAsync(transactions, transactionHash);

                ownerBalance = await tokenService.GetBalanceOfAsync <ulong>(addressOwner);

                Assert.Equal(totalSupply - 1000, ownerBalance);

                var newAddressBalance = await tokenService.GetBalanceOfAsync <ulong>(newAddress);

                Assert.Equal((ulong)1000, newAddressBalance);

                var allTransfersFilter = await transfersEvent.CreateFilterAsync(new BlockParameter(transferReceipt.BlockNumber));

                var eventLogsAll = await transfersEvent.GetAllChanges <Transfer>(allTransfersFilter);

                Assert.Equal(1, eventLogsAll.Count);
                var transferLog = eventLogsAll.First();
                Assert.Equal(transferLog.Log.TransactionIndex.HexValue, transferReceipt.TransactionIndex.HexValue);
                Assert.Equal(transferLog.Log.BlockNumber.HexValue, transferReceipt.BlockNumber.HexValue);
                Assert.Equal(transferLog.Event.AddressTo, newAddress);
                Assert.Equal(transferLog.Event.Value, (ulong)1000);
            }
            finally
            {
                var result = await web3.Miner.Stop.SendRequestAsync();

                Assert.True(result, "Mining should have stop");
                result = await web3.Personal.LockAccount.SendRequestAsync(addressOwner);

                Assert.True(result, "Account should be locked");
            }
        }
コード例 #4
0
        /// <summary>
        /// This method provides the main entry point for the background thread that keeps ethereum wallet information synchronised.
        /// </summary>
        private void NetworkCheckThreadEntry()
        {
            Logger.ApplicationInstance.Info("Network status thread started");
            bool     connectedYet          = false;
            DateTime nextContractCheckTime = DateTime.UtcNow;

            while (_isAlive)
            {
                try
                {
                    // Get a list of all accounts currently know about by our wallet
                    string[] addresses = _web3.Eth.Accounts.SendRequestAsync().Result;

                    // First remove any existing accounts that do not exist in our new list
                    if (addresses != null)
                    {
                        EthereumAccount[] removedAccounts = Accounts.Where(ac => !addresses.Contains(ac.Address)).ToArray();
                        foreach (EthereumAccount removedAccount in removedAccounts)
                        {
                            Accounts.Remove(removedAccount);
                        }

                        // Process each of these accounts
                        foreach (string address in addresses)
                        {
                            // Get the current balance for this account
                            decimal balance = decimal.Parse(_web3.Eth.GetBalance.SendRequestAsync(address).Result.Value.ToString());

                            // And check the SIFT balance
                            ulong siftBalance = _tokenService.GetBalanceOfAsync <ulong>(address).Result;

                            // See if we have an existing account - if not we'll need to create a new one
                            EthereumAccount existingAccount = Accounts.FirstOrDefault(ac => ac.Address == address);
                            if (existingAccount == null)
                            {
                                Accounts.Add(new EthereumAccount(address, balance, siftBalance));
                                Logger.ApplicationInstance.Info("Found new account " + address + " with " + balance + " ETH / " + siftBalance + " SIFT");
                            }
                            else
                            {
                                if (existingAccount.BalanceWei != balance)
                                {
                                    existingAccount.BalanceWei = balance;
                                    Logger.ApplicationInstance.Info("Account " + address + " now has " + balance + " ETH");
                                }
                                if (existingAccount.SiftBalance != siftBalance)
                                {
                                    existingAccount.SiftBalance = siftBalance;
                                    Logger.ApplicationInstance.Info("Account " + address + " now has " + siftBalance + " SIFT");
                                }
                            }
                        }
                    }

                    // Let's get the block number and check if we're syncing to just set some basics bits and bobs up
                    BlockNumber = ulong.Parse(_web3.Eth.Blocks.GetBlockNumber.SendRequestAsync().Result.Value.ToString());
                    IsSyncing   = _web3.Eth.Syncing.SendRequestAsync().Result.IsSyncing;

                    // Ask the contract our status but first check we've got the right contract
                    if (DateTime.UtcNow >= nextContractCheckTime)
                    {
                        // First check the ICO contract
                        Logger.ApplicationInstance.Debug("Checking ICO contract version");
                        decimal icoContractVersion = decimal.Parse(_icoContract.GetFunction("contractVersion").CallAsync <ulong>().Result.ToString());
                        if (icoContractVersion == IcoContractVersion)
                        {
                            // Get the phase of the ICO
                            bool isIcoPhase = _icoContract.GetFunction("icoPhase").CallAsync <bool>().Result;
                            ContractPhase = isIcoPhase ? ContractPhase.Ico : ContractPhase.Trading;

                            // Check if the ICO is abandoned
                            IsIcoAbandoned = _icoContract.GetFunction("icoAbandoned").CallAsync <bool>().Result;

                            // Check the ICO dates
                            IcoStartDate = DateFromTimestamp(_icoContract.GetFunction("icoStartTime").CallAsync <ulong>().Result);
                            IcoEndDate   = DateFromTimestamp(_icoContract.GetFunction("icoEndTime").CallAsync <ulong>().Result);

                            // Whilst we're here we also want to check the gas cost to send
                            DefaultGasInfo = CalculateGasCostForEtherSend("0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", IcoContractAddress, 1000000000000000000m).Result;
                            Logger.ApplicationInstance.Error("Default gas calculating for sending as " + DefaultGasInfo.Gas + " at " + DefaultGasInfo.GasPrice + " = " + DefaultGasInfo.GasCost);
                        }
                        else
                        {
                            ContractPhase = ContractPhase.Unknown;
                            Logger.ApplicationInstance.Error("ICO contract mismatch - expected " + IcoContractVersion + " but got " + icoContractVersion + " at " + IcoContractAddress);
                        }

                        // Now check SIFT contract
                        Logger.ApplicationInstance.Debug("Checking SIFT contract version");
                        decimal siftContractVersion = decimal.Parse(_siftContract.GetFunction("contractVersion").CallAsync <ulong>().Result.ToString());
                        if (siftContractVersion != SiftContractVersion)
                        {
                            ContractPhase = ContractPhase.Unknown;
                            Logger.ApplicationInstance.Error("SIFT contract mismatch - expected " + SiftContractVersion + " but got " + siftContractVersion + " at " + SiftContractAddress);
                        }

                        // Now check the SIFT contract
                        nextContractCheckTime = DateTime.UtcNow.AddSeconds(10);
                    }

                    // If valid contract check total supply
                    if (ContractPhase != ContractPhase.Unknown)
                    {
                        Logger.ApplicationInstance.Debug("Checking total supply and shareholding");
                        TotalSupply = _tokenService.GetTotalSupplyAsync <ulong>().Result;
                        foreach (EthereumAccount account in Accounts)
                        {
                            account.ShareholdingPercentage = TotalSupply == 0 ? 0 : (decimal)account.SiftBalance / TotalSupply * 100;
                        }
                    }

                    // Signify a successful loop
                    LastChecksSuccessful = true;
                    if (!connectedYet)
                    {
                        connectedYet = true;
                        Logger.ApplicationInstance.Info("Completed first successful network check, we should be good to go");
                    }
                }
                catch (Exception ex)
                {
                    LastChecksSuccessful = false;
                    Logger.ApplicationInstance.Error("There was an unexpected error updating Ethereum network information", ex);
                }

                // Wait to try again
                DateTime nextTime = DateTime.UtcNow.AddSeconds(3);
                while (DateTime.UtcNow < nextTime && _isAlive)
                {
                    Thread.Sleep(100);
                }
            }
        }