Exemple #1
0
        public async void ShouldGetTransferEventLogs()
        {
            var destinationAddress = "0x6C547791C3573c2093d81b919350DB1094707011";
            //Using ropsten infura if wanted for only a tests
            //var web3 = _ethereumClientIntegrationFixture.GetInfuraWeb3(InfuraNetwork.Ropsten);
            var web3 = _ethereumClientIntegrationFixture.GetWeb3();

            var erc20TokenDeployment = new ERC20TokenDeployment()
            {
                DecimalUnits = 18, TokenName = "TST", TokenSymbol = "TST", InitialAmount = Web3.Convert.ToWei(10000)
            };

            //Deploy our custom token
            var tokenDeploymentReceipt = await ERC20TokenService.DeployContractAndWaitForReceiptAsync(web3, erc20TokenDeployment);

            //Creating a new service
            var tokenService = new ERC20TokenService(web3, tokenDeploymentReceipt.ContractAddress);

            //using Web3.Convert.ToWei as it has 18 decimal places (default)
            var transferReceipt1 = await tokenService.TransferRequestAndWaitForReceiptAsync(destinationAddress, Web3.Convert.ToWei(10, 18));

            var transferReceipt2 = await tokenService.TransferRequestAndWaitForReceiptAsync(destinationAddress, Web3.Convert.ToWei(10, 18));

            var transferEvent  = web3.Eth.GetEvent <TransferEventDTO>();
            var transferFilter = transferEvent.GetFilterBuilder().AddTopic(x => x.To, destinationAddress).Build(tokenService.ContractHandler.ContractAddress,
                                                                                                                new BlockRange(transferReceipt1.BlockNumber, transferReceipt2.BlockNumber));

            var transferEvents = await transferEvent.GetAllChanges(transferFilter);

            Assert.Equal(2, transferEvents.Count);
        }
Exemple #2
0
        public async void ShouldTransferToken()
        {
            var destinationAddress = "0x6C547791C3573c2093d81b919350DB1094707011";
            //Using ropsten infura if wanted for only a tests
            //var web3 = _ethereumClientIntegrationFixture.GetInfuraWeb3(InfuraNetwork.Ropsten);
            var web3 = _ethereumClientIntegrationFixture.GetWeb3();

            var erc20TokenDeployment = new ERC20TokenDeployment()
            {
                DecimalUnits = 18, TokenName = "TST", TokenSymbol = "TST", InitialAmount = Web3.Convert.ToWei(10000)
            };

            //Deploy our custom token
            var tokenDeploymentReceipt = await ERC20TokenService.DeployContractAndWaitForReceiptAsync(web3, erc20TokenDeployment);

            //Creating a new service
            var tokenService = new ERC20TokenService(web3, tokenDeploymentReceipt.ContractAddress);

            //using Web3.Convert.ToWei as it has 18 decimal places (default)
            var transferReceipt = await tokenService.TransferRequestAndWaitForReceiptAsync(destinationAddress, Web3.Convert.ToWei(10, 18));

            //validate the current balance
            var balance = await tokenService.BalanceOfQueryAsync(destinationAddress);

            Assert.Equal(10, Web3.Convert.FromWei(balance));

            //retrieving the event from the receipt
            var eventTransfer = transferReceipt.DecodeAllEvents <TransferEventDTO>()[0];

            Assert.Equal(10, Web3.Convert.FromWei(eventTransfer.Event.Value));
            Assert.True(destinationAddress.IsTheSameAddress(eventTransfer.Event.To));
        }
Exemple #3
0
        public async void ShoulAllowOnlyIssuerToChangeIssuer()
        {
            var newIssuer = "0x6C547791C3573c2093d81b919350DB1094707011";
            //Using ropsten infura if wanted for only a tests
            //var web3 = _ethereumClientIntegrationFixture.GetInfuraWeb3(InfuraNetwork.Ropsten);
            var web3 = _ethereumClientIntegrationFixture.GetWeb3();

            var erc20TokenDeployment = new ERC20TokenDeployment()
            {
                DecimalUnits = 18, TokenName = "Homeless", TokenSymbol = "Homeless"
            };

            //Deploy our custom token
            var tokenDeploymentReceipt = await ERC20TokenService.DeployContractAndWaitForReceiptAsync(web3, erc20TokenDeployment);

            //Creating a new service
            var tokenService = new ERC20TokenService(web3, tokenDeploymentReceipt.ContractAddress);

            var receipt = await tokenService.ChangeIssuerRequestAndWaitForReceiptAsync(newIssuer);

            var newIssuerInChain = await tokenService.IssuerQueryAsync();

            Assert.True(newIssuerInChain.IsTheSameAddress(newIssuer));

            //change again using our account
            try
            {
                var receipt2 = await tokenService.ChangeIssuerRequestAndWaitForReceiptAsync(newIssuer);
            }
            catch (Exception ex)
            {
                //different clients have different messages so just catching the exception
                Assert.True(ex.GetType() == typeof(RpcResponseException));
            }
        }
        static void Main(string[] args)
        {
            var ownerAddress       = "0x12890d2cce102216644c59daE5baed380d84830c";
            var password           = "******";
            var destinationAddress = "0x6C547791C3573c2093d81b919350DB1094707011";

            var web3 = GetWeb3(ownerAddress, password, CONST_ONLINE_TEST_CHAIN_URL);

            var erc20TokenDeployment =
                new ERC20TokenDeployment()
            {
                DecimalUnits = 18, TokenName = "TST", TokenSymbol = "TST", InitialAmount = Web3.Convert.ToWei(10000)
            };

            //Deploy our custom token
            var tokenDeploymentReceipt = ERC20TokenService.DeployContractAndWaitForReceiptAsync(web3, erc20TokenDeployment).Result;

            //Creating a new service
            var tokenService = new ERC20TokenService(web3, tokenDeploymentReceipt.ContractAddress);

            //Creating the rules engine, using the default set of rules
            var wonkaERC20Service =
                new WonkaERC20Service(ownerAddress,
                                      password,
                                      tokenDeploymentReceipt.ContractAddress,
                                      new StringBuilder(CONST_WONKA_DEFI_RULES),
                                      new WonkaDeFiDefaultMetadata(),
                                      null,
                                      false,
                                      CONST_ONLINE_TEST_CHAIN_URL);

            var RefEnv = WonkaRefEnvironment.GetInstance();

            WonkaRefAttr OwnerAddressAttr   = RefEnv.GetAttributeByAttrName("OwnerAddress");
            WonkaRefAttr VaultAddressAttr   = RefEnv.GetAttributeByAttrName("VaultAddress");
            WonkaRefAttr TokenTrxAmtAttr    = RefEnv.GetAttributeByAttrName("TokenTransferAmt");
            WonkaRefAttr VaultYieldRateAttr = RefEnv.GetAttributeByAttrName("VaultYieldRate");

            var trxData = new WonkaProduct();

            trxData.SetAttribute(OwnerAddressAttr, ownerAddress);
            trxData.SetAttribute(VaultAddressAttr, destinationAddress);
            trxData.SetAttribute(TokenTrxAmtAttr, "12");  // Must specify the amount in hex form

            var currentBalance = tokenService.BalanceOfQueryAsync(ownerAddress).Result;

            for (int i = 0; (i < 1000) && (currentBalance.CompareTo(0) > 0); ++i)
            {
                trxData.SetAttribute(VaultYieldRateAttr, GetCurrentInterestRate());

                wonkaERC20Service.Execute(trxData);

                currentBalance = tokenService.BalanceOfQueryAsync(ownerAddress).Result;

                var vaultBalance = tokenService.BalanceOfQueryAsync(destinationAddress).Result;

                Thread.Sleep(5000);
            }
        }
Exemple #5
0
        public async void ShouldGetTransferEventLogsUsingProcessorAndStoreThem()
        {
            var destinationAddress = "0x6C547791C3573c2093d81b919350DB1094707011";
            //Using ropsten infura if wanted for only a tests
            //var web3 = _ethereumClientIntegrationFixture.GetInfuraWeb3(InfuraNetwork.Ropsten);
            var web3 = _ethereumClientIntegrationFixture.GetWeb3();

            var erc20TokenDeployment = new ERC20TokenDeployment()
            {
                DecimalUnits = 18, TokenName = "TST", TokenSymbol = "TST", InitialAmount = Web3.Convert.ToWei(10000)
            };

            //Deploy our custom token
            var tokenDeploymentReceipt = await ERC20TokenService.DeployContractAndWaitForReceiptAsync(web3, erc20TokenDeployment);

            //Creating a new service
            var tokenService = new ERC20TokenService(web3, tokenDeploymentReceipt.ContractAddress);

            //using Web3.Convert.ToWei as it has 18 decimal places (default)
            var transferReceipt1 = await tokenService.TransferRequestAndWaitForReceiptAsync(destinationAddress, Web3.Convert.ToWei(10, 18));

            var transferReceipt2 = await tokenService.TransferRequestAndWaitForReceiptAsync(destinationAddress, Web3.Convert.ToWei(10, 18));


            //We are storing in a database the logs
            var storedMockedEvents = new List <EventLog <TransferEventDTO> >();

            //storage action mock
            Task StoreLogAsync(EventLog <TransferEventDTO> eventLog)
            {
                storedMockedEvents.Add(eventLog);
                return(Task.CompletedTask);
            }

            //progress repository to restart processing (simple in memory one, use the other adapters for other storage possibilities)
            var blockProgressRepository = new InMemoryBlockchainProgressRepository(transferReceipt1.BlockNumber.Value - 1);

            //create our processor to retrieve transfers
            //restrict the processor to Transfers for a specific contract address
            var processor = web3.Processing.Logs.CreateProcessorForContract <TransferEventDTO>(
                tokenService.ContractHandler.ContractAddress,    //the contract to monitor
                StoreLogAsync,                                   //action to perform when a log is found
                minimumBlockConfirmations: 0,                    // number of block confirmations to wait
                blockProgressRepository: blockProgressRepository //repository to track the progress
                );

            //if we need to stop the processor mid execution - call cancel on the token
            var cancellationToken = new CancellationToken();

            //crawl the required block range
            await processor.ExecuteAsync(
                cancellationToken : cancellationToken,
                toBlockNumber : transferReceipt2.BlockNumber.Value,
                startAtBlockNumberIfNotProcessed : transferReceipt1.BlockNumber.Value);

            Assert.Equal(2, storedMockedEvents.Count);
        }
        public async void ShouldInvokeERC20WonkaService()
        {
            var ownerAddress       = "0x12890d2cce102216644c59daE5baed380d84830c";
            var destinationAddress = "0x6C547791C3573c2093d81b919350DB1094707011";

            //Using ropsten infura if wanted for only a tests
            //var web3 = _ethereumClientIntegrationFixture.GetInfuraWeb3(InfuraNetwork.Ropsten);
            var web3 = _ethereumClientIntegrationFixture.GetWeb3();

            var erc20TokenDeployment = new ERC20TokenDeployment()
            {
                DecimalUnits = 18, TokenName = "TST", TokenSymbol = "TST", InitialAmount = Web3.Convert.ToWei(10000)
            };

            //Deploy our custom token
            var tokenDeploymentReceipt = await ERC20TokenService.DeployContractAndWaitForReceiptAsync(web3, erc20TokenDeployment);

            //Creating a new service
            var tokenService = new ERC20TokenService(web3, tokenDeploymentReceipt.ContractAddress);

            //Creating the rules engine, using the default set of rules
            var wonkaERC20Service =
                new WonkaERC20Service("0x12890d2cce102216644c59daE5baed380d84830c",
                                      "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7",
                                      tokenDeploymentReceipt.ContractAddress);

            var RefEnv = WonkaRefEnvironment.GetInstance();

            WonkaRefAttr OwnerAddressAttr = RefEnv.GetAttributeByAttrName("OwnerAddress");
            WonkaRefAttr VaultAddressAttr = RefEnv.GetAttributeByAttrName("VaultAddress");
            WonkaRefAttr TokenTrxAmtAttr  = RefEnv.GetAttributeByAttrName("TokenTransferAmt");

            var trxData = new WonkaProduct();

            trxData.SetAttribute(OwnerAddressAttr, ownerAddress);
            trxData.SetAttribute(VaultAddressAttr, destinationAddress);
            trxData.SetAttribute(TokenTrxAmtAttr, "12");  // Must specify the amount in hex form

            var ownerBalance = await tokenService.BalanceOfQueryAsync(ownerAddress);

            var beforeBalance = await tokenService.BalanceOfQueryAsync(destinationAddress);

            wonkaERC20Service.Execute(trxData);

            //validate the current balance (in decimal form)
            var afterBalance = await tokenService.BalanceOfQueryAsync(destinationAddress);

            Assert.Equal(18, afterBalance);
        }
Exemple #7
0
        public async void ShouldIssueTokens()
        {
            var donor = "0x6C547791C3573c2093d81b919350DB1094707011";
            //Using ropsten infura if wanted for only a tests
            //var web3 = _ethereumClientIntegrationFixture.GetInfuraWeb3(InfuraNetwork.Ropsten);
            var web3 = _ethereumClientIntegrationFixture.GetWeb3();

            var erc20TokenDeployment = new ERC20TokenDeployment()
            {
                DecimalUnits = 18, TokenName = "Homeless", TokenSymbol = "Homeless"
            };

            //Deploy our custom token
            var tokenDeploymentReceipt = await ERC20TokenService.DeployContractAndWaitForReceiptAsync(web3, erc20TokenDeployment);

            //Creating a new service
            var tokenService = new ERC20TokenService(web3, tokenDeploymentReceipt.ContractAddress);

            var donorIssuance = new DonorIssuance()
            {
                Donor           = donor,
                Value           = Web3.Convert.ToWei(10),
                TxnHashDonation = "0x30912b7fa14ccf619f8cfbff4ada42aabbccaa0d6373ffdbe0f0b509ac8883d9".HexToByteArray()
            };

            var issueTokensReceipt = await tokenService.IssueTokenToDonorRequestAndWaitForReceiptAsync(new List <DonorIssuance> {
                donorIssuance
            });

            //validate the current balance
            var balance = await tokenService.BalanceOfQueryAsync(donor);

            Assert.Equal(10, Web3.Convert.FromWei(balance));

            //retrieving the event from the receipt
            var eventTransfer = issueTokensReceipt.DecodeAllEvents <IssueToDonorEventDTO>()[0];

            Assert.Equal(10, Web3.Convert.FromWei(eventTransfer.Event.Value));
            Assert.True(donor.IsTheSameAddress(eventTransfer.Event.Donor));
            Assert.True(donorIssuance.TxnHashDonation.ToHex().IsTheSameHex(eventTransfer.Event.TxnHash.ToHex()));
        }
Exemple #8
0
        public async void ShouldTransferAsUsual()
        {
            var donor = EthereumClientIntegrationFixture.AccountAddress; // we are the donors
            //Using ropsten infura if wanted for only a tests
            //var web3 = _ethereumClientIntegrationFixture.GetInfuraWeb3(InfuraNetwork.Ropsten);
            var web3 = _ethereumClientIntegrationFixture.GetWeb3();

            var erc20TokenDeployment = new ERC20TokenDeployment()
            {
                DecimalUnits = 18, TokenName = "Homeless", TokenSymbol = "Homeless"
            };

            //Deploy our custom token
            var tokenDeploymentReceipt = await ERC20TokenService.DeployContractAndWaitForReceiptAsync(web3, erc20TokenDeployment);

            //Creating a new service
            var tokenService = new ERC20TokenService(web3, tokenDeploymentReceipt.ContractAddress);

            var donorIssuance = new DonorIssuance()
            {
                Donor           = donor,
                Value           = Web3.Convert.ToWei(10),
                TxnHashDonation = "0x30912b7fa14ccf619f8cfbff4ada42aabbccaa0d6373ffdbe0f0b509ac8883d9".HexToByteArray()
            };

            var issueTokensReceipt = await tokenService.IssueTokenToDonorRequestAndWaitForReceiptAsync(new List <DonorIssuance> {
                donorIssuance
            });

            //validate the current balance
            var balance = await tokenService.BalanceOfQueryAsync(donor);

            Assert.Equal(10, Web3.Convert.FromWei(balance));


            var transferToken = await tokenService.TransferRequestAndWaitForReceiptAsync("0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe", Web3.Convert.ToWei(1));

            balance = await tokenService.BalanceOfQueryAsync(donor);

            Assert.Equal(9, Web3.Convert.FromWei(balance));
        }
Exemple #9
0
        public static async Task <ERC20TokenService> DeployContractAndGetServiceAsync(Nethereum.Web3.Web3 web3, ERC20TokenDeployment eRC20TokenDeployment, CancellationTokenSource cancellationTokenSource = null)
        {
            var receipt = await DeployContractAndWaitForReceiptAsync(web3, eRC20TokenDeployment, cancellationTokenSource);

            return(new ERC20TokenService(web3, receipt.ContractAddress));
        }
Exemple #10
0
 public static Task <string> DeployContractAsync(Nethereum.Web3.Web3 web3, ERC20TokenDeployment eRC20TokenDeployment)
 {
     return(web3.Eth.GetContractDeploymentHandler <ERC20TokenDeployment>().SendRequestAsync(eRC20TokenDeployment));
 }
Exemple #11
0
 public static Task <TransactionReceipt> DeployContractAndWaitForReceiptAsync(Nethereum.Web3.Web3 web3, ERC20TokenDeployment eRC20TokenDeployment, CancellationTokenSource cancellationTokenSource = null)
 {
     return(web3.Eth.GetContractDeploymentHandler <ERC20TokenDeployment>().SendRequestAndWaitForReceiptAsync(eRC20TokenDeployment, cancellationTokenSource));
 }