public async Task ReturnRemainingContractBalanceUponDelete() { // Setup the Simple Event Emitting Contract and An account for "send to". await using var fxAccount = await TestAccount.CreateAsync(_network); await using var fxContract = await EventEmittingContract.CreateAsync(_network); // Get the Info for the Account to receive funds before any changes happen. var infoBefore = await fxAccount.Client.GetAccountInfoAsync(fxAccount.Record.Address); Assert.Equal(fxAccount.CreateParams.InitialBalance, infoBefore.Balance); // Double check the balance on the contract, confirm it has hbars var contractBalanceBefore = await fxContract.Client.CallContractWithRecordAsync(new CallContractParams { Contract = fxContract.ContractRecord.Contract, Gas = await _network.TinybarsFromGas(400), FunctionName = "get_balance" }); Assert.NotNull(contractBalanceBefore); Assert.InRange(fxContract.ContractParams.InitialBalance, 1, int.MaxValue); Assert.Equal(fxContract.ContractParams.InitialBalance, contractBalanceBefore.CallResult.Result.As <long>()); // Delete the Contract, returning contract balance to Account var deleteContractRecord = await fxContract.Client.DeleteContractAsync(fxContract.ContractRecord.Contract, fxAccount.Record.Address, fxContract.PrivateKey); Assert.Equal(ResponseCode.Success, deleteContractRecord.Status); // Check the balance of account to see if it went up by contract's balance. var infoAfter = await fxAccount.Client.GetAccountInfoAsync(fxAccount.Record.Address); Assert.Equal(infoBefore.Balance + (ulong)fxContract.ContractParams.InitialBalance, infoAfter.Balance); }
public async Task CanRetrieveRecordsFromContractMethodCalls() { await using var fxContract = await StatefulContract.CreateAsync(_network); var transactionCount = Generator.Integer(2, 3); for (int i = 0; i < transactionCount; i++) { await fxContract.Client.CallContractWithRecordAsync(new CallContractParams { Contract = fxContract.ContractRecord.Contract, Gas = await _network.TinybarsFromGas(400), FunctionName = "get_message" }); } var records = await fxContract.Client.GetContractRecordsAsync(fxContract.ContractRecord.Contract); Assert.NotNull(records); Assert.Equal(transactionCount + 1, records.Length); foreach (var record in records.Skip(1)) { Assert.Equal(ResponseCode.Success, record.Status); Assert.InRange(record.Fee, 0UL, ulong.MaxValue); Assert.Empty(record.Memo); Assert.NotNull(record.Concensus); Assert.False(record.Hash.IsEmpty); } }
public async Task CanGetRecordForFailedTransaction() { await using var fx = await GreetingContract.CreateAsync(_network); var tex = await Assert.ThrowsAsync <TransactionException>(async() => { await fx.Client.CallContractWithRecordAsync(new CallContractParams { Contract = fx.ContractRecord.Contract, Gas = await _network.TinybarsFromGas(400), FunctionName = "not_a_real_method", }); }); var record = await fx.Client.GetTransactionRecordAsync(tex.TxId); Assert.NotNull(record); Assert.Equal(ResponseCode.ContractRevertExecuted, record.Status); Assert.False(record.Hash.IsEmpty); Assert.NotNull(record.Concensus); Assert.Empty(record.Memo); Assert.InRange(record.Fee, 0UL, ulong.MaxValue); }
public async Task CanCreateAContractAsync() { await using var fx = await GreetingContract.CreateAsync(_network); var record = await fx.Client.CallContractWithRecordAsync(new CallContractParams { Contract = fx.ContractRecord.Contract, Gas = await _network.TinybarsFromGas(400), FunctionName = "greet" }, ctx => ctx.Memo = "Call Contract"); Assert.NotNull(record); Assert.Equal(ResponseCode.Success, record.Status); Assert.False(record.Hash.IsEmpty); Assert.NotNull(record.Concensus); Assert.Equal("Call Contract", record.Memo); Assert.InRange(record.Fee, 0UL, ulong.MaxValue); Assert.Empty(record.CallResult.Error); Assert.True(record.CallResult.Bloom.IsEmpty); Assert.InRange(record.CallResult.Gas, 0UL, 30_000UL); Assert.Empty(record.CallResult.Events); Assert.Empty(record.CallResult.CreatedContracts); Assert.Equal("Hello, world!", record.CallResult.Result.As <string>());; }
public async Task CanRetrieveRecordsFromContractMethodCalls() { await using var fxContract = await StatefulContract.CreateAsync(_network); var transactionCount = Generator.Integer(2, 3); for (int i = 0; i < transactionCount; i++) { await fxContract.Client.CallContractWithRecordAsync(new CallContractParams { Contract = fxContract.ContractRecord.Contract, Gas = await _network.TinybarsFromGas(400), FunctionName = "get_message" }); } var records = await fxContract.Client.GetContractRecordsAsync(fxContract.ContractRecord.Contract); Assert.NotNull(records); Assert.Empty(records); }
public async Task CanUpdateContractBytecode() { await using var fx = await GreetingContract.CreateAsync(_network); await using var fx2 = await StatefulContract.SetupAsync(_network); var record = await fx.Client.UpdateContractWithRecordAsync(new UpdateContractParams { Contract = fx.ContractRecord.Contract, File = fx2.FileRecord.File }); var info = await fx.Client.GetContractInfoAsync(fx.ContractRecord.Contract); Assert.NotNull(info); Assert.Equal(fx.ContractRecord.Contract, info.Contract); Assert.Equal(fx.ContractRecord.Contract, info.Address); Assert.Equal(fx.ContractParams.Administrator, info.Administrator); Assert.Equal(fx.ContractParams.RenewPeriod, info.RenewPeriod); Assert.Equal(fx.Memo, info.Memo); Assert.Equal((ulong)fx.ContractParams.InitialBalance, info.Balance); // Call a method that was not part of the original contract to check update. // Note, this is a bug with the network, the call returned success but we are still // able to call the old contract method which should no longer exist. var callRecord = await fx.Client.CallContractWithRecordAsync(new CallContractParams { Contract = fx.ContractRecord.Contract, Gas = await _network.TinybarsFromGas(400), FunctionName = "greet", }); Assert.NotNull(callRecord); Assert.Equal(ResponseCode.Success, record.Status); Assert.Empty(callRecord.CallResult.Error); Assert.Equal("Hello, world!", callRecord.CallResult.Result.As <string>()); }
public async Task CanCreateAContractAsync() { await using var fx = await GreetingContract.CreateAsync(_network); var result = await fx.Client.QueryContractAsync(new QueryContractParams { Contract = fx.ContractRecord.Contract, Gas = await _network.TinybarsFromGas(400), ReturnValueCharge = await _network.TinybarsFromGas(400), FunctionName = "greet" }); Assert.NotNull(result); Assert.Empty(result.Error); Assert.True(result.Bloom.IsEmpty); Assert.InRange(result.Gas, 0UL, ulong.MaxValue); Assert.Empty(result.Events); Assert.Equal("Hello, world!", result.Result.As <string>()); }
public async Task CanGetContractBalanceFromCall() { await using var fx = await EventEmittingContract.CreateAsync(_network); var record = await fx.Client.CallContractWithRecordAsync(new CallContractParams { Contract = fx.ContractRecord.Contract, Gas = await _network.TinybarsFromGas(400), FunctionName = "get_balance" }); Assert.NotNull(record); Assert.Equal(ResponseCode.Success, record.Status); Assert.False(record.Hash.IsEmpty); Assert.NotNull(record.Concensus); Assert.Empty(record.Memo); Assert.InRange(record.Fee, 0UL, ulong.MaxValue); Assert.Empty(record.CallResult.Error); Assert.True(record.CallResult.Bloom.IsEmpty); Assert.InRange(record.CallResult.Gas, 0UL, 30_000UL); Assert.Empty(record.CallResult.Events); Assert.Equal(fx.ContractParams.InitialBalance, record.CallResult.Result.As <long>()); }