GetClient() публичный статический Метод

public static GetClient ( ) : IClient
Результат IClient
Пример #1
0
        public async void ShouldDeployUsingMultipleParameters()
        {
            var contractByteCode =
                "0x606060408181528060bd833960a090525160805160009182556001556095908190602890396000f3606060405260e060020a60003504631df4f1448114601c575b6002565b34600257608360043560015460005460408051918402909202808252915173ffffffffffffffffffffffffffffffffffffffff33169184917f841774c8b4d8511a3974d7040b5bc3c603d304c926ad25d168dacd04e25c4bed9181900360200190a3919050565b60408051918252519081900360200190f3";

            var abi =
                @"[{'constant':false,'inputs':[{'name':'a','type':'int256'}],'name':'multiply','outputs':[{'name':'r','type':'int256'}],'payable':false,'type':'function'},{'inputs':[{'name':'multiplier','type':'int256'},{'name':'another','type':'int256'}],'type':'constructor'},{'anonymous':false,'inputs':[{'indexed':true,'name':'a','type':'int256'},{'indexed':true,'name':'sender','type':'address'},{'indexed':false,'name':'result','type':'int256'}],'name':'Multiplied','type':'event'}]";

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

            var gethTester = GethTesterFactory.GetLocal(web3);

            await web3.Miner.Start.SendRequestAsync(6);

            var transaction =
                await
                web3.Eth.DeployContract.SignAndSendRequestAsync(gethTester.Password, abi, contractByteCode,
                                                                gethTester.Account, new HexBigInteger(900000), 7, 8);

            var receipt = await gethTester.GetTransactionReceipt(transaction);

            await web3.Miner.Stop.SendRequestAsync();

            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(3864, callResult);
        }
Пример #2
0
        public async void ShouldDeployAContractAndPerformACall()
        {
            //The compiled solidity contract to be deployed
            //contract test { function multiply(uint a) returns(uint d) { return a * 7; } }
            var contractByteCode =
                "0x606060405260728060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa1146037576035565b005b604b60048080359060200190919050506061565b6040518082815260200191505060405180910390f35b6000600782029050606d565b91905056";

            var abi =
                @"[{""constant"":false,""inputs"":[{""name"":""a"",""type"":""uint256""}],""name"":""multiply"",""outputs"":[{""name"":""d"",""type"":""uint256""}],""type"":""function""}]";

            var web3       = new Web3(ClientFactory.GetClient());
            var gethTester = GethTesterFactory.GetLocal(web3);

            var receipt = await gethTester.DeployTestContractLocal(contractByteCode);

            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);
        }
Пример #3
0
        public async void ShouldBeAbleToHandleNoncesOfMultipleTxnSingleWeb3SingleThreaded()
        {
            var senderAddress = "0x12890d2cce102216644c59daE5baed380d84830c";
            var privateKey    = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";
            var abi           = @"[{""constant"":false,""inputs"":[{""name"":""val"",""type"":""int256""}],""name"":""multiply"",""outputs"":[{""name"":""d"",""type"":""int256""}],""type"":""function""},{""inputs"":[{""name"":""multiplier"",""type"":""int256""}],""type"":""constructor""}]";
            var byteCode      =
                "0x60606040526040516020806052833950608060405251600081905550602b8060276000396000f3606060405260e060020a60003504631df4f1448114601a575b005b600054600435026060908152602090f3";

            var multiplier = 7;

            var web3 = new Web3(new Account(privateKey), ClientFactory.GetClient());

            var txn1 = await
                       web3.Eth.DeployContract.SendRequestAsync(abi, byteCode, senderAddress, new HexBigInteger(900000), null, multiplier);

            var txn2 = await
                       web3.Eth.DeployContract.SendRequestAsync(abi, byteCode, senderAddress, new HexBigInteger(900000), null, multiplier);

            var txn3 = await
                       web3.Eth.DeployContract.SendRequestAsync(abi, byteCode, senderAddress, new HexBigInteger(900000), null, multiplier);

            var pollService = new TransactionReceiptPollingService(web3.TransactionManager);

            var receipt1 = pollService.PollForReceiptAsync(txn1);
            var receipt2 = pollService.PollForReceiptAsync(txn2);
            var receipt3 = pollService.PollForReceiptAsync(txn3);

            Assert.NotNull(receipt1);
            Assert.NotNull(receipt2);
            Assert.NotNull(receipt3);
        }
        public void ShouldDecodeMultipleParamsIncludingArray()
        {
            var abi =
                @"[{""constant"":false,""inputs"":[{""name"":""a"",""type"":""uint256""},{""name"":""b"",""type"":""string""},{""name"":""c"",""type"":""uint[3]""} ],""name"":""test"",""outputs"":[{""name"":""d"",""type"":""uint256""}],""type"":""function""}]";

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

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

            //get the function by name
            var testFunction = contract.GetFunction("test");
            var array        = new int[] { 1, 2, 3 };

            var str    = "hello";
            var data   = testFunction.GetData(69, str, array);
            var decode = testFunction.DecodeInput(data);

            Assert.Equal(69, (BigInteger)decode[0].Result);
            Assert.Equal(str, (string)decode[1].Result);

            var listObjects = decode[2].Result as List <object>;

            if (listObjects != null)
            {
                var newArray = System.Array.ConvertAll(listObjects.ToArray(), x => (int)((BigInteger)x));
                Assert.Equal(array, newArray);
            }
        }
Пример #5
0
        public async Task ShouldBeAbleToDeployAContractUsingPrivateKey()
        {
            var senderAddress = "0x12890d2cce102216644c59daE5baed380d84830c";
            var privateKey    = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";
            var abi           = @"[{""constant"":false,""inputs"":[{""name"":""val"",""type"":""int256""}],""name"":""multiply"",""outputs"":[{""name"":""d"",""type"":""int256""}],""type"":""function""},{""inputs"":[{""name"":""multiplier"",""type"":""int256""}],""type"":""constructor""}]";
            var byteCode      =
                "0x60606040526040516020806052833950608060405251600081905550602b8060276000396000f3606060405260e060020a60003504631df4f1448114601a575b005b600054600435026060908152602090f3";

            var multiplier = 7;

            var web3 = new Web3(new Account(privateKey), ClientFactory.GetClient());

            var web3Geth = new Web3Geth(ClientFactory.GetClient());
            await web3Geth.Miner.Start.SendRequestAsync();

            var transactionPolling = new TransactionReceiptPollingService(web3);

            //assumed client is mining already
            var contractAddress = await
                                  transactionPolling.DeployContractAndGetAddressAsync(
                () => web3.Eth.DeployContract.SendRequestAsync(abi, byteCode, senderAddress, new HexBigInteger(900000), multiplier)
                );

            await web3Geth.Miner.Stop.SendRequestAsync();

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

            var multiplyFunction = contract.GetFunction("multiply");

            var result = await multiplyFunction.CallAsync <int>(7);

            Assert.Equal(49, result);
        }
Пример #6
0
        public async Task ShouldBeAbleToDeployAContractUsingPersonalUnlock()
        {
            var senderAddress = "0x12890d2cce102216644c59daE5baed380d84830c";
            var password      = "******";
            var abi           = @"[{""constant"":false,""inputs"":[{""name"":""val"",""type"":""int256""}],""name"":""multiply"",""outputs"":[{""name"":""d"",""type"":""int256""}],""type"":""function""},{""inputs"":[{""name"":""multiplier"",""type"":""int256""}],""type"":""constructor""}]";
            var byteCode      =
                "0x60606040526040516020806052833950608060405251600081905550602b8060276000396000f3606060405260e060020a60003504631df4f1448114601a575b005b600054600435026060908152602090f3";

            var multiplier = 7;

            var web3 = new Web3(new ManagedAccount(senderAddress, password), ClientFactory.GetClient());

            var transactionPolling = new TransactionReceiptPollingService(web3);

            var web3Geth = new Web3Geth(ClientFactory.GetClient());
            await web3Geth.Miner.Start.SendRequestAsync();

            var contractAddress = await
                                  transactionPolling.DeployContractAndGetAddressAsync(
                () => web3.Eth.DeployContract.SendRequestAsync(abi, byteCode, senderAddress, new HexBigInteger(900000), multiplier)
                );

            await web3Geth.Miner.Stop.SendRequestAsync();

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

            var multiplyFunction = contract.GetFunction("multiply");

            var result = await multiplyFunction.CallAsync <int>(7);

            Assert.Equal(49, result);
        }
Пример #7
0
        public async void ShouldDeployAContractWithValueAndSendAValue()
        {
            var contractByteCode =
                "0x6060604052600180546c0100000000000000000000000033810204600160a060020a03199091161790556002340460008190556002023414603e576002565b6103258061004c6000396000f3606060405236156100615760e060020a600035046308551a53811461006657806335a063b41461007d5780633fa4f245146100a05780637150d8ae146100ae57806373fac6f0146100c5578063c19d93fb146100e8578063d696069714610101575b610002565b346100025761011f600154600160a060020a031681565b346100025761013b60015433600160a060020a0390811691161461014f57610002565b346100025761013d60005481565b346100025761011f600254600160a060020a031681565b346100025761013b60025433600160a060020a039081169116146101e457610002565b346100025761013d60025460ff60a060020a9091041681565b61013b60025460009060ff60a060020a90910416156102a457610002565b60408051600160a060020a039092168252519081900360200190f35b005b60408051918252519081900360200190f35b60025460009060a060020a900460ff161561016957610002565b6040517f80b62b7017bb13cf105e22749ee2a06a417ffba8c7f57b665057e0f3c2e925d990600090a16040516002805460a160020a60a060020a60ff0219909116179055600154600160a060020a0390811691309091163180156108fc02916000818181858888f1935050505015156101e157610002565b50565b60025460019060a060020a900460ff1681146101ff57610002565b6040517f64ea507aa320f07ae13c28b5e9bf6b4833ab544315f5f2aa67308e21c252d47d90600090a16040516002805460a060020a60ff02191660a160020a179081905560008054600160a060020a03909216926108fc8315029291818181858888f19350505050158061029a5750600154604051600160a060020a039182169130163180156108fc02916000818181858888f19350505050155b156101e157610002565b6000546002023414806102b657610002565b6040517f764326667cab2f2f13cad5f7b7665c704653bd1acc250dcb7b422bce726896b490600090a150506002805460a060020a73ffffffffffffffffffffffffffffffffffffffff199091166c01000000000000000000000000338102041760a060020a60ff02191617905556";

            var abi =
                "[{'constant':true,'inputs':[],'name':'seller','outputs':[{'name':'','type':'address'}],'payable':false,'type':'function'},{'constant':false,'inputs':[],'name':'abort','outputs':[],'payable':false,'type':'function'},{'constant':true,'inputs':[],'name':'value','outputs':[{'name':'','type':'uint256'}],'payable':false,'type':'function'},{'constant':true,'inputs':[],'name':'buyer','outputs':[{'name':'','type':'address'}],'payable':false,'type':'function'},{'constant':false,'inputs':[],'name':'confirmReceived','outputs':[],'payable':false,'type':'function'},{'constant':true,'inputs':[],'name':'state','outputs':[{'name':'','type':'uint8'}],'payable':false,'type':'function'},{'constant':false,'inputs':[],'name':'confirmPurchase','outputs':[],'payable':true,'type':'function'},{'inputs':[],'type':'constructor'},{'anonymous':false,'inputs':[],'name':'aborted','type':'event'},{'anonymous':false,'inputs':[],'name':'purchaseConfirmed','type':'event'},{'anonymous':false,'inputs':[],'name':'itemReceived','type':'event'}]";

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

            var gethTester = GethTesterFactory.GetLocal(web3);

            await gethTester.StartMining();

            await gethTester.UnlockAccount();

            var transaction =
                await
                web3.Eth.DeployContract.SendRequestAsync(abi, contractByteCode,
                                                         gethTester.Account, new HexBigInteger(900000), new HexBigInteger(10000));

            var receipt = await gethTester.GetTransactionReceipt(transaction);

            await gethTester.StopMining();

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

            //get the function by name
            var valueFuntion = contract.GetFunction("value");

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

            Assert.Equal(5000, callResult);

            var confirmPurchaseFunction = contract.GetFunction("confirmPurchase");
            var tx = await confirmPurchaseFunction.SendTransactionAsync(gethTester.Account,
                                                                        new HexBigInteger(900000), new HexBigInteger(10000));

            await gethTester.StartMining();

            receipt = await gethTester.GetTransactionReceipt(tx);

            await gethTester.StopMining();

            var stateFunction = contract.GetFunction("state");

            callResult = await stateFunction.CallAsync <int>();

            Assert.Equal(1, callResult);
        }
Пример #8
0
        public async void GetBlock()
        {
            var web3  = new Web3(ClientFactory.GetClient());
            var block = await web3.Eth.Blocks.GetBlockWithTransactionsHashesByNumber.SendRequestAsync(new HexBigInteger(1139657));

            var transaction =
                await
                web3.Eth.Transactions.GetTransactionByHash.SendRequestAsync(
                    "0x9122a4bba873e30c9c6e71481bd60ef61f559f60e26e50a38272f3324b7befca");

            var receipt = await
                          web3.Eth.Transactions.GetTransactionReceipt.SendRequestAsync(
                "0x9122a4bba873e30c9c6e71481bd60ef61f559f60e26e50a38272f3324b7befca");
        }
Пример #9
0
        public async void ShouldEstimateContactDeployment()
        {
            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 Web3Geth(new ManagedAccount(addressFrom, pass), ClientFactory.GetClient());

            var estimate = await web3.Eth.DeployContract.EstimateGasAsync(abi, contractByteCode, addressFrom, 7);
        }
Пример #10
0
        public async void ShouldBeAbleToHandleNoncesOfMultipleTxnMultipleWeb3sMultithreaded()
        {
            var senderAddress = "0x12890d2cce102216644c59daE5baed380d84830c";
            var privateKey    = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";
            var abi           = @"[{""constant"":false,""inputs"":[{""name"":""val"",""type"":""int256""}],""name"":""multiply"",""outputs"":[{""name"":""d"",""type"":""int256""}],""type"":""function""},{""inputs"":[{""name"":""multiplier"",""type"":""int256""}],""type"":""constructor""}]";
            var byteCode      =
                "0x60606040526040516020806052833950608060405251600081905550602b8060276000396000f3606060405260e060020a60003504631df4f1448114601a575b005b600054600435026060908152602090f3";

            var multiplier = 7;

            var client        = ClientFactory.GetClient();
            var nonceProvider = new InMemoryNonceService(senderAddress, client);
            //tested with 1000
            var listTasks = 10;
            var taskItems = new List <int>();

            for (int i = 0; i < listTasks; i++)
            {
                taskItems.Add(i);
            }

            var numProcs             = Environment.ProcessorCount;
            var concurrencyLevel     = numProcs * 2;
            var concurrentDictionary = new ConcurrentDictionary <int, string>(concurrencyLevel, listTasks * 2);


            Parallel.ForEach(taskItems, async(item, state) =>
            {
                var account          = new Account(privateKey);
                account.NonceService = nonceProvider;
                var web3             = new Web3(account, client);
                var txn = await
                          web3.Eth.DeployContract.SendRequestAsync(abi, byteCode, senderAddress, new HexBigInteger(900000),
                                                                   null, multiplier);
                concurrentDictionary.TryAdd(item, txn);
            });

            var web31       = new Web3(new Account(privateKey), client);
            var pollService = new TransactionReceiptPollingService(web31.TransactionManager);

            for (int i = 0; i < listTasks; i++)
            {
                string txn = null;
                concurrentDictionary.TryGetValue(i, out txn);
                var receipt = pollService.PollForReceiptAsync(txn);
                Assert.NotNull(receipt);
            }
        }
        public void ShouldDecodeInt()
        {
            var abi =
                @"[{""constant"":false,""inputs"":[{""name"":""a"",""type"":""uint256""}],""name"":""multiply"",""outputs"":[{""name"":""d"",""type"":""uint256""}],""type"":""function""}]";

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

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

            //get the function by name
            var multiplyFunction = contract.GetFunction("multiply");
            var data             = multiplyFunction.GetData(69);
            var decode           = multiplyFunction.DecodeInput(data);

            Assert.Equal((BigInteger)decode[0].Result, 69);
        }
Пример #12
0
        public async Task ShouldBeAbleToDeployAContractLoadingEncryptedPrivateKey()
        {
            var senderAddress = "0x12890d2cce102216644c59daE5baed380d84830c";
            var password      = "******";
            //this is your wallet key file which can be found on

            //Linux: ~/.ethereum/keystore
            //Mac: /Library/Ethereum/keystore
            //Windows: %APPDATA%/Ethereum

            var keyStoreEncryptedJson = @"{""crypto"":{""cipher"":""aes-128-ctr"",""ciphertext"":""b4f42e48903879b16239cd5508bc5278e5d3e02307deccbec25b3f5638b85f91"",""cipherparams"":{""iv"":""dc3f37d304047997aa4ef85f044feb45""},""kdf"":""scrypt"",""mac"":""ada930e08702b89c852759bac80533bd71fc4c1ef502291e802232b74bd0081a"",""kdfparams"":{""n"":65536,""r"":1,""p"":8,""dklen"":32,""salt"":""2c39648840b3a59903352b20386f8c41d5146ab88627eaed7c0f2cc8d5d95bd4""}},""id"":""19883438-6d67-4ab8-84b9-76a846ce544b"",""address"":""12890d2cce102216644c59dae5baed380d84830c"",""version"":3}";

            var abi      = @"[{""constant"":false,""inputs"":[{""name"":""val"",""type"":""int256""}],""name"":""multiply"",""outputs"":[{""name"":""d"",""type"":""int256""}],""type"":""function""},{""inputs"":[{""name"":""multiplier"",""type"":""int256""}],""type"":""constructor""}]";
            var byteCode =
                "0x60606040526040516020806052833950608060405251600081905550602b8060276000396000f3606060405260e060020a60003504631df4f1448114601a575b005b600054600435026060908152602090f3";

            var multiplier = 7;

            //if not using portable or netstandard (^net45) you can use LoadFromKeyStoreFile to load the file from the file system.

            var acccount = Account.LoadFromKeyStore(keyStoreEncryptedJson, password);

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

            var transactionPolling = new TransactionReceiptPollingService(web3);

            var web3Geth = new Web3Geth(ClientFactory.GetClient());
            await web3Geth.Miner.Start.SendRequestAsync();

            //assumed client is mining already
            var contractAddress = await
                                  transactionPolling.DeployContractAndGetAddressAsync(
                () => web3.Eth.DeployContract.SendRequestAsync(abi, byteCode, senderAddress, new HexBigInteger(900000), multiplier)
                );

            await web3Geth.Miner.Stop.SendRequestAsync();

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

            var multiplyFunction = contract.GetFunction("multiply");

            var result = await multiplyFunction.CallAsync <int>(7);

            Assert.Equal(49, result);
        }
Пример #13
0
        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 Web3Geth(new ManagedAccount(addressFrom, pass), ClientFactory.GetClient());
            var result = true;

            //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.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);
        }
        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 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);

            //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(100);
                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, gas, null, 69);

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

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

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

            TransactionReceipt receiptTransaction = null;

            while (receiptTransaction == null)
            {
                Thread.Sleep(100);
                receiptTransaction = await transactions.GetTransactionReceipt.SendRequestAsync(transaction7);
            }

            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);
        }
Пример #15
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.TransactionManager);

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

            //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);
        }