public async void ShouldDeployAContractWithConstructor()
        {
            //The compiled solidity contract to be deployed

            /*
             * contract test {
             *
             * uint _multiplier;
             *
             * function test(uint multiplier){
             *     _multiplier = multiplier;
             * }
             *
             * function getMultiplier() constant returns(uint d){
             *      return _multiplier;
             * }
             *
             * function multiply(uint a) returns(uint d) { return a * _multiplier; }
             *
             * string public contractName = "Multiplier";
             * }
             */

            var contractByteCode =
                "0x6060604052604060405190810160405280600a81526020017f4d756c7469706c6965720000000000000000000000000000000000000000000081526020015060016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061008c57805160ff19168380011785556100bd565b828001600101855582156100bd579182015b828111156100bc57825182600050559160200191906001019061009e565b5b5090506100e891906100ca565b808211156100e457600081815060009055506001016100ca565b5090565b5050604051602080610303833981016040528080519060200190919050505b806000600050819055505b506101e2806101216000396000f360606040526000357c01000000000000000000000000000000000000000000000000000000009004806340490a901461004f57806375d0c0dc14610072578063c6888fa1146100ed5761004d565b005b61005c6004805050610119565b6040518082815260200191505060405180910390f35b61007f6004805050610141565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600f02600301f150905090810190601f1680156100df5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610103600480803590602001909190505061012b565b6040518082815260200191505060405180910390f35b60006000600050549050610128565b90565b60006000600050548202905061013c565b919050565b60016000508054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101da5780601f106101af576101008083540402835291602001916101da565b820191906000526020600020905b8154815290600101906020018083116101bd57829003601f168201915b50505050508156";

            var abi =
                @"[{""constant"":true,""inputs"":[],""name"":""getMultiplier"",""outputs"":[{""name"":""d"",""type"":""uint256""}],""type"":""function""},{""constant"":true,""inputs"":[],""name"":""contractName"",""outputs"":[{""name"":"""",""type"":""string""}],""type"":""function""},{""constant"":false,""inputs"":[{""name"":""a"",""type"":""uint256""}],""name"":""multiply"",""outputs"":[{""name"":""d"",""type"":""uint256""}],""type"":""function""},{""inputs"":[{""name"":""multiplier"",""type"":""uint256""}],""type"":""constructor""}]";

            var addressFrom = "0x12890d2cce102216644c59dae5baed380d84830c";
            var pass        = "******";

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

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

            Assert.True(result, "Account should be unlocked");

            //deploy the contract, including abi and a paramter of 7.
            var transactionHash = await web3.Eth.DeployContract.SendRequestAsync(abi, contractByteCode, addressFrom, new HexBigInteger(900000), 7);

            Assert.NotNull(transactionHash);

            result = await web3.Personal.LockAccount.SendRequestAsync(addressFrom);

            Assert.True(result, "Account should be locked");

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

            Assert.True(result, "Mining should have started");
            //the contract should be mining now

            //get the contract address
            TransactionReceipt receipt = null;

            //wait for the contract to be mined to the address
            while (receipt == null)
            {
                Thread.Sleep(1000);
                receipt = await web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(transactionHash);
            }

            Assert.NotNull(receipt.ContractAddress);

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

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

            var contract = web3.Eth.GetContract(abi, receipt.ContractAddress);

            //get the function by name
            var multiplyFunction = contract.GetFunction("multiply");

            //do a function call (not transaction) and get the result
            var callResult = await multiplyFunction.CallAsync <int>(69);

            Assert.Equal(483, callResult);

            var multiplierFunction = contract.GetFunction("getMultiplier");

            var multiplier = await multiplierFunction.CallAsync <int>();

            Assert.Equal(7, multiplier);

            var contractNameFunction = contract.GetFunction("contractName");

            var name = await contractNameFunction.CallAsync <string>();

            Assert.Equal("Multiplier", name);
        }
Ejemplo n.º 2
0
        public async Task Test()
        {
            //The compiled solidity contract to be deployed

            /*
             * contract test {
             *
             *  uint _multiplier;
             *
             *  event Multiplied(uint indexed a, uint indexed result);
             *
             *  event MultipliedLog(uint indexed a, uint indexed result, string indexed hello, address sender );
             *
             *  function test(uint multiplier){
             *      _multiplier = multiplier;
             *  }
             *
             *  function multiply(uint a) returns(uint d) {
             *      d = a * _multiplier;
             *      Multiplied(a, d);
             *      MultipliedLog(a, d, "Hello world", msg.sender);
             *      return d;
             *  }
             *
             *  function multiply1(uint a) returns(uint d) {
             *      return a * _multiplier;
             *  }
             *
             *  function multiply2(uint a, uint b) returns(uint d){
             *      return a * b;
             *  }
             *
             * }
             *
             */

            var contractByteCode =
                "0x6060604052604051602080610213833981016040528080519060200190919050505b806000600050819055505b506101d88061003b6000396000f360606040526000357c01000000000000000000000000000000000000000000000000000000009004806361325dbc1461004f578063c23f4e3e1461007b578063c6888fa1146100b05761004d565b005b61006560048080359060200190919050506100dc565b6040518082815260200191505060405180910390f35b61009a60048080359060200190919080359060200190919050506100f2565b6040518082815260200191505060405180910390f35b6100c66004808035906020019091905050610104565b6040518082815260200191505060405180910390f35b6000600060005054820290506100ed565b919050565b600081830290506100fe565b92915050565b600060006000505482029050805080827f51ae5c4fa89d1aa731ff280d425357e6e5c838c6fc8ed6ca0139ea31716bbd5760405180905060405180910390a360405180807f48656c6c6f20776f726c64000000000000000000000000000000000000000000815260200150600b019050604051809103902081837f74053123e4f45ba0f8cbf86301034a4ab00cdc75cd155a0df7c5d815bd97dcb533604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390a48090506101d3565b91905056";

            var abi =
                @"[{""constant"":false,""inputs"":[{""name"":""a"",""type"":""uint256""}],""name"":""multiply1"",""outputs"":[{""name"":""d"",""type"":""uint256""}],""type"":""function""},{""constant"":false,""inputs"":[{""name"":""a"",""type"":""uint256""},{""name"":""b"",""type"":""uint256""}],""name"":""multiply2"",""outputs"":[{""name"":""d"",""type"":""uint256""}],""type"":""function""},{""constant"":false,""inputs"":[{""name"":""a"",""type"":""uint256""}],""name"":""multiply"",""outputs"":[{""name"":""d"",""type"":""uint256""}],""type"":""function""},{""inputs"":[{""name"":""multiplier"",""type"":""uint256""}],""type"":""constructor""},{""anonymous"":false,""inputs"":[{""indexed"":true,""name"":""a"",""type"":""uint256""},{""indexed"":true,""name"":""result"",""type"":""uint256""}],""name"":""Multiplied"",""type"":""event""},{""anonymous"":false,""inputs"":[{""indexed"":true,""name"":""a"",""type"":""uint256""},{""indexed"":true,""name"":""result"",""type"":""uint256""},{""indexed"":true,""name"":""sender"",""type"":""string""},{""indexed"":false,""name"":""hello"",""type"":""address""}],""name"":""MultipliedLog"",""type"":""event""}]";

            var addressFrom = "0x12890d2cce102216644c59dae5baed380d84830c";
            var pass        = "******";

            var web3 = new Web3Geth(ClientFactory.GetClient());

            var result = await web3.Personal.UnlockAccount.SendRequestAsync(addressFrom, pass, 600000);

            Assert.True(result, "Account should be unlocked");



            var eth          = web3.Eth;
            var transactions = eth.Transactions;

            //deploy the contract, including abi and a paramter of 7.
            var transactionHash = await eth.DeployContract.SendRequestAsync(abi, contractByteCode, addressFrom, new HexBigInteger(900000), 7);

            Assert.NotNull(transactionHash);

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

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

            //the contract should be mining now

            //get the contract address
            TransactionReceipt receipt = null;

            //wait for the contract to be mined to the address
            while (receipt == null)
            {
                await Task.Delay(5000);

                receipt = await transactions.GetTransactionReceipt.SendRequestAsync(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 contract = eth.GetContract(abi, receipt.ContractAddress);

            var multipliedEvent   = contract.GetEvent("Multiplied");
            var filterAllContract = await contract.CreateFilterAsync();

            var filterAll = await multipliedEvent.CreateFilterAsync();

            //filter on the first indexed parameter
            var filter69 = await multipliedEvent.CreateFilterAsync(69);

            //filter on the second indexed parameter
            var filter49 = await multipliedEvent.CreateFilterAsync <object, int>(null, 49);

            //filter OR on the first indexed parameter
            var filter69And18 = await multipliedEvent.CreateFilterAsync(new[] { 69, 18 });


            var multipliedEventLog = contract.GetEvent("MultipliedLog");
            var filterAllLog       = await multipliedEventLog.CreateFilterAsync();

            //get the function by name
            var multiplyFunction = contract.GetFunction("multiply");

            var gas = await multiplyFunction.EstimateGasAsync(69);

            var transaction69 = await multiplyFunction.SendTransactionAsync(addressFrom, 69);

            var transaction18 = await multiplyFunction.SendTransactionAsync(addressFrom, 18);

            var transaction7 = await multiplyFunction.SendTransactionAsync(addressFrom, 7);

            var multiplyFunction2 = contract.GetFunction("multiply2");
            var callResult        = await multiplyFunction2.CallAsync <int>(7, 7);

            TransactionReceipt receiptTransaction = null;

            while (receiptTransaction == null)
            {
                await Task.Delay(5000);

                receiptTransaction = await transactions.GetTransactionReceipt.SendRequestAsync(transaction7);
            }

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

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

            var logs = await eth.Filters.GetFilterChangesForEthNewFilter.SendRequestAsync(filterAllContract);

            var eventLogsAll = await multipliedEvent.GetFilterChanges <EventMultiplied>(filterAll);

            var eventLogs69 = await multipliedEvent.GetFilterChanges <EventMultiplied>(filter69);

            var eventLogsResult49 = await multipliedEvent.GetFilterChanges <EventMultiplied>(filter49);

            var eventLogsFor69and18 = await multipliedEvent.GetFilterChanges <EventMultiplied>(filter69And18);


            var multipliedLogEvents = await multipliedEventLog.GetFilterChanges <EventMultipliedSenderLog>(filterAllLog);

            Assert.Equal(eventLogs69.First().Event.Result, 483);
            Assert.Equal(multipliedLogEvents.First().Event.Hello, "0xed6c11b0b5b808960df26f5bfc471d04c1995b0ffd2055925ad1be28d6baadfd"); //The sha3 keccak of "Hello world" as it is an indexed string
            Assert.Equal(multipliedLogEvents.First().Event.Sender, addressFrom);
        }
Ejemplo n.º 3
0
        public async void ShouldCreateEnsRegistarResolverAndRegiterandResolveANewAddress()
        {
            //The address we want to resolve when using "test.eth"
            var addressToResolve = "0x12890d2cce102216644c59dae5baed380d84830c";

            var defaultGas = new HexBigInteger(900000);

            var addressFrom = "0x12890d2cce102216644c59dae5baed380d84830c";
            var pass        = "******";

            var web3     = new Web3(new ManagedAccount(addressFrom, pass), ClientFactory.GetClient());
            var web3Geth = new Web3Geth(ClientFactory.GetClient());

            var txService = new TransactionReceiptPollingService(web3);

            // var addressFrom = (await web3.Eth.Accounts.SendRequestAsync()).First();
            //uncomment to use geth instead of test-rpc

            await web3Geth.Miner.Start.SendRequestAsync();

            //deploy ENS contract
            var ensAddress = await txService.DeployContractAndGetAddressAsync(() => EnsService.DeployContractAsync(web3, addressFrom, defaultGas));

            var ensUtil = new EnsUtil();
            var ethNode = ensUtil.GetEnsNameHash("eth");

            //create a new First in First service registrar for "eth"
            var fifsAddress = await txService.DeployContractAndGetAddressAsync(() => FIFSRegistrarService.DeployContractAsync(web3, addressFrom, ensAddress, ethNode.HexToByteArray(),
                                                                                                                              defaultGas));


            //create a public registry, which will allow us to find the registered address
            var publicResolverAddress =
                await
                txService.DeployContractAndGetAddressAsync(
                    () =>
                    PublicResolverService.DeployContractAsync(web3, addressFrom, ensAddress,
                                                              defaultGas));


            var ensService = new EnsService(web3, ensAddress);

            //set ownership of "eth" to the fifs service
            //we are owners of "", so a subnode label "eth" will now be owned by the FIFS registar, which will allow to also to set ownership in Ens of further subnodes of Eth.
            var ethLabel = ensUtil.GetEnsLabelHash("eth");

            await txService.SendRequestAsync(() => ensService.SetSubnodeOwnerAsync(addressFrom, ensUtil.GetEnsNameHash("").HexToByteArray(),
                                                                                   ethLabel.HexToByteArray(), fifsAddress, defaultGas));

            //Now the owner of Eth is the FIFS
            var ownerOfEth = await ensService.OwnerAsyncCall(ethNode.HexToByteArray());

            Assert.Equal(fifsAddress, ownerOfEth);
            /**** setup done **/

            //registration of "myname"

            //create a service for the registrar
            var fifsService = new FIFSRegistrarService(web3, fifsAddress);

            //create a label
            var testLabel = ensUtil.GetEnsLabelHash("myname");
            //submit the registration using the label bytes, and set ourselves as the owner
            await txService.SendRequestAsync(() => fifsService.RegisterAsync(addressFrom, testLabel.HexToByteArray(), addressFrom, defaultGas));

            //now using the the full name
            var fullNameNode = ensUtil.GetEnsNameHash("myname.eth");
            //set the resolver (the public one)
            await txService.SendRequestAsync(() => ensService.SetResolverAsync(addressFrom, fullNameNode.HexToByteArray(), publicResolverAddress, defaultGas));

            var publicResolverService = new PublicResolverService(web3, publicResolverAddress);
            // set the address in the resolver which we want to resolve, ownership is validated using ENS in the background
            await txService.SendRequestAsync(() => publicResolverService.SetAddrAsync(addressFrom, fullNameNode.HexToByteArray(), addressToResolve, defaultGas));

            //Now as "end user" we can start resolving...

            //get the resolver address from ENS
            var resolverAddress = await ensService.ResolverAsyncCall(fullNameNode.HexToByteArray());

            //using the resolver address we can create our service (should be an abstract / interface based on abi as we can have many)
            var resolverService = new PublicResolverService(web3, resolverAddress);

            //and get the address from the resolver
            var theAddress = await resolverService.AddrAsyncCall(fullNameNode.HexToByteArray());

            Assert.Equal(addressToResolve, theAddress);

            await web3Geth.Miner.Stop.SendRequestAsync();
        }