public async Task MineTest() { EthNetwork.UseDefaultTestNet(); var prefundedAccount = new Account(EthNetwork.Instance.PrefundedPrivateKey); var tokenOwnerAccount = EthAccountFactory.Create(); var systemAccount = EthAccountFactory.Create(); var coreTeamAccount = EthAccountFactory.Create(); await EthNetwork.Instance.RefillAsync(tokenOwnerAccount); await EthNetwork.Instance.RefillAsync(systemAccount); await EthNetwork.Instance.RefillAsync(coreTeamAccount); // Create the ERC20 token... var token = new AlgoTokenV1(EthNetwork.Instance.GetWeb3(tokenOwnerAccount), EthNetwork.Instance.GasPriceProvider); await token.DeployAsync(); // Create a fees... var fees1 = new AlgoFees(EthNetwork.Instance.GetWeb3(coreTeamAccount), EthNetwork.Instance.GasPriceProvider); await fees1.DeployAsync(token.ContractAddress); // Register the system account... await fees1.AddSystemAsync(systemAccount.Address); // Transfer some tokens to the fees... await token.TransferAsync(fees1.ContractAddress, 151.Algo()); // Create one miner per category... var minerAccounts = new IAccount[6]; var referralAccounts = new IAccount[6]; var miners = new AlgoMiner[6]; for (byte i = 0; i <= 5; i++) { // Create an account for the miner and the referral... minerAccounts[i] = EthAccountFactory.Create(); referralAccounts[i] = EthAccountFactory.Create(); await EthNetwork.Instance.RefillAsync(minerAccounts[i]); // Create the miner as "NonPoolBased" so it can be activated... miners[i] = new AlgoMiner(EthNetwork.Instance.GetWeb3(coreTeamAccount), EthNetwork.Instance.GasPriceProvider); await miners[i].DeployAsync(1, i, minerAccounts[i].Address, referralAccounts[i].Address, token.ContractAddress); // Activate the miner... await miners[i].ActivateMinerAsync(); // Start mining... { var miner = new AlgoMiner(miners[i].ContractAddress, EthNetwork.Instance.GetWeb3(minerAccounts[i]), EthNetwork.Instance.GasPriceProvider); await miner.StartMiningAsync(); } // Register a miner... await fees1.RegisterMinerAsync(miners[i].ContractAddress); } // Mine the fees... { var fees = new AlgoFees(fees1.ContractAddress, EthNetwork.Instance.GetWeb3(systemAccount), EthNetwork.Instance.GasPriceProvider); await fees.MineAsync(); } // Ensure the miners received the payment from the fees... var balance0 = await token.BalanceOfAsync(minerAccounts[0].Address); var balance1 = await token.BalanceOfAsync(minerAccounts[1].Address); var balance2 = await token.BalanceOfAsync(minerAccounts[2].Address); var balance3 = await token.BalanceOfAsync(minerAccounts[3].Address); var balance4 = await token.BalanceOfAsync(minerAccounts[4].Address); var balance5 = await token.BalanceOfAsync(minerAccounts[5].Address); Assert.Equal(1.Algo(), balance0); Assert.Equal(10.Algo(), balance1); Assert.Equal(20.Algo(), balance2); Assert.Equal(30.Algo(), balance3); Assert.Equal(40.Algo(), balance4); Assert.Equal(50.Algo(), balance5); }
protected override async Task <TransactionReceipt> ExecuteAsync(RuntimeContext context, string contractAddress, Web3 web3) { var algoMiner = new AlgoMiner(contractAddress, web3, context.GasPriceProvider); return(await algoMiner.StartMiningAsync()); }
public async Task PoolBasedMinerBasicWorkfloWTest() { EthNetwork.UseDefaultTestNet(); var prefundedAccount = new Account(EthNetwork.Instance.PrefundedPrivateKey); var tokenOwnerAccount = EthAccountFactory.Create(); var systemAccount = EthAccountFactory.Create(); var coreTeamAccount = EthAccountFactory.Create(); var supervisorAccount = EthAccountFactory.Create(); var minerAccount = EthAccountFactory.Create(); var referralAccount = EthAccountFactory.Create(); await EthNetwork.Instance.RefillAsync(tokenOwnerAccount); await EthNetwork.Instance.RefillAsync(systemAccount); await EthNetwork.Instance.RefillAsync(coreTeamAccount); await EthNetwork.Instance.RefillAsync(supervisorAccount); await EthNetwork.Instance.RefillAsync(minerAccount); // Create the ERC20 token... var token = new AlgoTokenV1(EthNetwork.Instance.GetWeb3(tokenOwnerAccount), EthNetwork.Instance.GasPriceProvider); await token.DeployAsync(); // Create the pools to transfer the proper number of tokens to the miners... var pool1 = new AlgoPool(EthNetwork.Instance.GetWeb3(coreTeamAccount), EthNetwork.Instance.GasPriceProvider); await pool1.DeployAsync(0, token.ContractAddress); var pool2 = new AlgoPool(EthNetwork.Instance.GetWeb3(coreTeamAccount), EthNetwork.Instance.GasPriceProvider); await pool2.DeployAsync(1, token.ContractAddress); // Transfer some tokens to the pools... await token.TransferAsync(pool1.ContractAddress, 100.MAlgo()); await token.TransferAsync(pool2.ContractAddress, 100.MAlgo()); // Create a miner category 2... var miner1 = new AlgoMiner(EthNetwork.Instance.GetWeb3(coreTeamAccount), EthNetwork.Instance.GasPriceProvider); await miner1.DeployAsync(0, 2, minerAccount.Address, referralAccount.Address, token.ContractAddress); // Add roles to the miner... await miner1.AddSystemAsync(systemAccount.Address); await miner1.AddSupervisorAsync(supervisorAccount.Address); // Transfer tokens to the miner... await pool1.TransferToMinerAsync(miner1.ContractAddress); await pool2.TransferToMinerAsync(miner1.ContractAddress); // Ensure the miner received the tokens according to its category 2. Assert.Equal(2.MAlgo() + 2.MAlgo() * 10 / 100, await token.BalanceOfAsync(miner1.ContractAddress)); // Activate the miner... await miner1.ActivateMinerAsync(); // Start mining... miner1.Bind(EthNetwork.Instance.GetWeb3(minerAccount)); await miner1.StartMiningAsync(); // Mine 5 days... miner1.Bind(EthNetwork.Instance.GetWeb3(systemAccount)); var paymentPerDay = 2.MAlgo() / 2 / 365; BigInteger expectedMinerBalance = 0; BigInteger expectedReferralBalance = 0; for (int i = 0; i < 5; i++) { await miner1.MineAsync(); expectedMinerBalance += paymentPerDay; expectedReferralBalance += paymentPerDay * 10 / 100; Assert.Equal(expectedMinerBalance, await token.BalanceOfAsync(minerAccount.Address)); Assert.Equal(expectedReferralBalance, await token.BalanceOfAsync(referralAccount.Address)); } // Pause the miner... miner1.Bind(EthNetwork.Instance.GetWeb3(supervisorAccount)); await miner1.PauseMiningAsync(); // Try to mine one day... miner1.Bind(EthNetwork.Instance.GetWeb3(systemAccount)); await Assert.ThrowsAsync <TransactionRejectedException>( () => miner1.MineAsync()); // Ensure the balance is not changed... Assert.Equal(expectedMinerBalance, await token.BalanceOfAsync(minerAccount.Address)); // Resume the miner... miner1.Bind(EthNetwork.Instance.GetWeb3(supervisorAccount)); await miner1.ResumeMiningAsync(); // Mine one day... miner1.Bind(EthNetwork.Instance.GetWeb3(coreTeamAccount)); await miner1.MineAsync(); expectedMinerBalance += paymentPerDay; expectedReferralBalance += paymentPerDay * 10 / 100; Assert.Equal(expectedMinerBalance, await token.BalanceOfAsync(minerAccount.Address)); Assert.Equal(expectedReferralBalance, await token.BalanceOfAsync(referralAccount.Address)); }