https://github.com/stratisproject/stratis.smartcontracts.testchain
A C# library to help with local testing of smart contracts.
https://github.com/stratisproject/stratis.smartcontracts.testchain
Last synced: 11 months ago
JSON representation
A C# library to help with local testing of smart contracts.
- Host: GitHub
- URL: https://github.com/stratisproject/stratis.smartcontracts.testchain
- Owner: stratisproject
- License: mit
- Created: 2019-01-09T03:13:55.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-02-11T05:35:58.000Z (over 5 years ago)
- Last Synced: 2025-04-01T11:55:12.606Z (over 1 year ago)
- Language: C#
- Size: 10.7 KB
- Stars: 2
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Stratis.SmartContracts.TestChain
A C# library to help with local testing of smart contracts.
See below for an example of how to use this tool in code.
```cs
using Stratis.Bitcoin.Features.SmartContracts.Models;
using Stratis.SmartContracts.CLR.Compilation;
using Stratis.SmartContracts.CLR.Local;
using Xunit;
namespace Stratis.SmartContracts.TestChain.Tests
{
public class TestChainExample
{
[Fact]
public void TestChain_Auction()
{
// Compile the contract we want to deploy
ContractCompilationResult compilationResult = ContractCompiler.CompileFile("SmartContracts/Auction.cs");
Assert.True(compilationResult.Success);
using (TestChain chain = new TestChain().Initialize())
{
// Get an address we can use for deploying
Base58Address deployerAddress = chain.PreloadedAddresses[0];
// Create and send transaction to mempool with parameters
SendCreateContractResult createResult = chain.SendCreateContractTransaction(deployerAddress, compilationResult.Compilation, 0, new object[] { 20uL });
// Mine a block which will contain our sent transaction
chain.MineBlocks(1);
// Check the receipt to see that contract deployment was successful
ReceiptResponse receipt = chain.GetReceipt(createResult.TransactionId);
Assert.Equal(deployerAddress, receipt.From);
// Check that the code is indeed saved on-chain
byte[] savedCode = chain.GetCode(createResult.NewContractAddress);
Assert.NotNull(savedCode);
// Use another identity to bid
Base58Address bidderAddress = chain.PreloadedAddresses[1];
// Send a call to the bid method
SendCallContractResult callResult = chain.SendCallContractTransaction(bidderAddress, "Bid", createResult.NewContractAddress, 1);
chain.MineBlocks(1);
// Call a method locally to check the state is as expected
ILocalExecutionResult localCallResult = chain.CallContractMethodLocally(bidderAddress, "HighestBidder", createResult.NewContractAddress, 0);
Address storedHighestBidder = (Address)localCallResult.Return;
Assert.NotEqual(default(Address), storedHighestBidder); // TODO: A nice way of comparing hex and base58 representations
}
}
}
}
```