2023-12-20 09:55:29 +00:00
|
|
|
|
using CodexContractsPlugin.Marketplace;
|
|
|
|
|
using GethPlugin;
|
2023-09-19 11:39:24 +00:00
|
|
|
|
using Logging;
|
2023-12-20 10:34:23 +00:00
|
|
|
|
using Nethereum.ABI;
|
2023-12-20 12:21:53 +00:00
|
|
|
|
using Nethereum.Hex.HexTypes;
|
2023-12-20 10:34:23 +00:00
|
|
|
|
using Nethereum.Util;
|
|
|
|
|
using NethereumWorkflow;
|
2023-12-20 08:48:22 +00:00
|
|
|
|
using Utils;
|
2023-09-19 11:39:24 +00:00
|
|
|
|
|
|
|
|
|
namespace CodexContractsPlugin
|
2023-09-19 08:24:43 +00:00
|
|
|
|
{
|
|
|
|
|
public interface ICodexContracts
|
|
|
|
|
{
|
2023-09-20 08:13:29 +00:00
|
|
|
|
CodexContractsDeployment Deployment { get; }
|
2023-09-19 11:39:24 +00:00
|
|
|
|
|
2023-12-18 12:32:58 +00:00
|
|
|
|
bool IsDeployed();
|
2023-12-15 10:02:06 +00:00
|
|
|
|
string MintTestTokens(IHasEthAddress owner, TestToken testTokens);
|
|
|
|
|
string MintTestTokens(EthAddress ethAddress, TestToken testTokens);
|
2023-10-30 12:30:14 +00:00
|
|
|
|
TestToken GetTestTokenBalance(IHasEthAddress owner);
|
|
|
|
|
TestToken GetTestTokenBalance(EthAddress ethAddress);
|
2023-12-20 08:48:22 +00:00
|
|
|
|
|
2024-01-22 15:05:04 +00:00
|
|
|
|
Request[] GetStorageRequests(TimeRange timeRange);
|
2024-01-26 22:29:57 +00:00
|
|
|
|
EthAddress? GetSlotHost(Request storageRequest, decimal slotIndex);
|
2024-01-22 15:27:32 +00:00
|
|
|
|
RequestState GetRequestState(Request request);
|
2024-01-22 15:05:04 +00:00
|
|
|
|
RequestFulfilledEventDTO[] GetRequestFulfilledEvents(TimeRange timeRange);
|
2024-01-22 15:27:32 +00:00
|
|
|
|
RequestCancelledEventDTO[] GetRequestCancelledEvents(TimeRange timeRange);
|
2023-12-20 12:21:53 +00:00
|
|
|
|
SlotFilledEventDTO[] GetSlotFilledEvents(TimeRange timeRange);
|
|
|
|
|
SlotFreedEventDTO[] GetSlotFreedEvents(TimeRange timeRange);
|
2023-09-19 08:24:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 15:27:32 +00:00
|
|
|
|
public enum RequestState
|
|
|
|
|
{
|
|
|
|
|
New,
|
|
|
|
|
Started,
|
|
|
|
|
Cancelled,
|
|
|
|
|
Finished,
|
|
|
|
|
Failed
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-19 08:24:43 +00:00
|
|
|
|
public class CodexContractsAccess : ICodexContracts
|
|
|
|
|
{
|
2023-09-19 11:39:24 +00:00
|
|
|
|
private readonly ILog log;
|
2023-10-30 12:30:14 +00:00
|
|
|
|
private readonly IGethNode gethNode;
|
2023-09-19 11:39:24 +00:00
|
|
|
|
|
2023-10-30 12:30:14 +00:00
|
|
|
|
public CodexContractsAccess(ILog log, IGethNode gethNode, CodexContractsDeployment deployment)
|
2023-09-19 08:24:43 +00:00
|
|
|
|
{
|
2023-09-19 11:39:24 +00:00
|
|
|
|
this.log = log;
|
2023-10-30 12:30:14 +00:00
|
|
|
|
this.gethNode = gethNode;
|
2023-09-20 07:16:57 +00:00
|
|
|
|
Deployment = deployment;
|
2023-09-19 08:24:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-20 08:13:29 +00:00
|
|
|
|
public CodexContractsDeployment Deployment { get; }
|
2023-09-19 11:39:24 +00:00
|
|
|
|
|
2023-12-18 12:32:58 +00:00
|
|
|
|
public bool IsDeployed()
|
|
|
|
|
{
|
2023-12-20 08:48:22 +00:00
|
|
|
|
return !string.IsNullOrEmpty(StartInteraction().GetTokenName(Deployment.TokenAddress));
|
2023-12-18 12:32:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-15 10:02:06 +00:00
|
|
|
|
public string MintTestTokens(IHasEthAddress owner, TestToken testTokens)
|
2023-09-19 14:22:07 +00:00
|
|
|
|
{
|
2023-12-15 10:02:06 +00:00
|
|
|
|
return MintTestTokens(owner.EthAddress, testTokens);
|
2023-09-19 14:22:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-15 10:02:06 +00:00
|
|
|
|
public string MintTestTokens(EthAddress ethAddress, TestToken testTokens)
|
2023-09-19 11:39:24 +00:00
|
|
|
|
{
|
2023-12-20 08:48:22 +00:00
|
|
|
|
return StartInteraction().MintTestTokens(ethAddress, testTokens.Amount, Deployment.TokenAddress);
|
2023-09-19 11:39:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-30 12:30:14 +00:00
|
|
|
|
public TestToken GetTestTokenBalance(IHasEthAddress owner)
|
2023-09-19 14:22:07 +00:00
|
|
|
|
{
|
2023-10-30 12:30:14 +00:00
|
|
|
|
return GetTestTokenBalance(owner.EthAddress);
|
2023-09-19 14:22:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-30 12:30:14 +00:00
|
|
|
|
public TestToken GetTestTokenBalance(EthAddress ethAddress)
|
2023-09-19 11:39:24 +00:00
|
|
|
|
{
|
2023-12-20 08:48:22 +00:00
|
|
|
|
var balance = StartInteraction().GetBalance(Deployment.TokenAddress, ethAddress.Address);
|
2023-09-19 11:39:24 +00:00
|
|
|
|
return balance.TestTokens();
|
|
|
|
|
}
|
2023-12-20 08:48:22 +00:00
|
|
|
|
|
2023-12-20 09:55:29 +00:00
|
|
|
|
public Request[] GetStorageRequests(TimeRange timeRange)
|
2023-12-20 08:48:22 +00:00
|
|
|
|
{
|
2023-12-20 09:55:29 +00:00
|
|
|
|
var events = gethNode.GetEvents<StorageRequestedEventDTO>(Deployment.MarketplaceAddress, timeRange);
|
|
|
|
|
var i = StartInteraction();
|
|
|
|
|
return events
|
2023-12-20 10:34:23 +00:00
|
|
|
|
.Select(e =>
|
|
|
|
|
{
|
|
|
|
|
var requestEvent = i.GetRequest(Deployment.MarketplaceAddress, e.Event.RequestId);
|
2023-12-20 12:21:53 +00:00
|
|
|
|
var result = requestEvent.ReturnValue1;
|
|
|
|
|
result.BlockNumber = e.Log.BlockNumber.ToUlong();
|
|
|
|
|
result.RequestId = e.Event.RequestId;
|
|
|
|
|
return result;
|
2023-12-20 10:34:23 +00:00
|
|
|
|
})
|
2023-12-20 09:55:29 +00:00
|
|
|
|
.ToArray();
|
2023-12-20 08:48:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 15:05:04 +00:00
|
|
|
|
public RequestFulfilledEventDTO[] GetRequestFulfilledEvents(TimeRange timeRange)
|
|
|
|
|
{
|
|
|
|
|
var events = gethNode.GetEvents<RequestFulfilledEventDTO>(Deployment.MarketplaceAddress, timeRange);
|
|
|
|
|
return events.Select(e =>
|
|
|
|
|
{
|
|
|
|
|
var result = e.Event;
|
|
|
|
|
result.BlockNumber = e.Log.BlockNumber.ToUlong();
|
|
|
|
|
return result;
|
|
|
|
|
}).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 15:27:32 +00:00
|
|
|
|
public RequestCancelledEventDTO[] GetRequestCancelledEvents(TimeRange timeRange)
|
|
|
|
|
{
|
|
|
|
|
var events = gethNode.GetEvents<RequestCancelledEventDTO>(Deployment.MarketplaceAddress, timeRange);
|
|
|
|
|
return events.Select(e =>
|
|
|
|
|
{
|
|
|
|
|
var result = e.Event;
|
|
|
|
|
result.BlockNumber = e.Log.BlockNumber.ToUlong();
|
|
|
|
|
return result;
|
|
|
|
|
}).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-20 12:21:53 +00:00
|
|
|
|
public SlotFilledEventDTO[] GetSlotFilledEvents(TimeRange timeRange)
|
|
|
|
|
{
|
|
|
|
|
var events = gethNode.GetEvents<SlotFilledEventDTO>(Deployment.MarketplaceAddress, timeRange);
|
|
|
|
|
return events.Select(e =>
|
|
|
|
|
{
|
|
|
|
|
var result = e.Event;
|
|
|
|
|
result.BlockNumber = e.Log.BlockNumber.ToUlong();
|
|
|
|
|
result.Host = GetEthAddressFromTransaction(e.Log.TransactionHash);
|
|
|
|
|
return result;
|
|
|
|
|
}).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SlotFreedEventDTO[] GetSlotFreedEvents(TimeRange timeRange)
|
|
|
|
|
{
|
|
|
|
|
var events = gethNode.GetEvents<SlotFreedEventDTO>(Deployment.MarketplaceAddress, timeRange);
|
|
|
|
|
return events.Select(e =>
|
|
|
|
|
{
|
|
|
|
|
var result = e.Event;
|
|
|
|
|
result.BlockNumber = e.Log.BlockNumber.ToUlong();
|
|
|
|
|
return result;
|
|
|
|
|
}).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-26 22:29:57 +00:00
|
|
|
|
public EthAddress? GetSlotHost(Request storageRequest, decimal slotIndex)
|
2023-12-20 10:34:23 +00:00
|
|
|
|
{
|
|
|
|
|
var encoder = new ABIEncode();
|
|
|
|
|
var encoded = encoder.GetABIEncoded(
|
|
|
|
|
new ABIValue("bytes32", storageRequest.RequestId),
|
|
|
|
|
new ABIValue("uint256", slotIndex.ToBig())
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
var hashed = Sha3Keccack.Current.CalculateHash(encoded);
|
|
|
|
|
|
|
|
|
|
var func = new GetHostFunction
|
|
|
|
|
{
|
|
|
|
|
SlotId = hashed
|
|
|
|
|
};
|
2024-01-26 22:29:57 +00:00
|
|
|
|
var address = gethNode.Call<GetHostFunction, string>(Deployment.MarketplaceAddress, func);
|
|
|
|
|
if (string.IsNullOrEmpty(address)) return null;
|
|
|
|
|
return new EthAddress(address);
|
2023-12-20 10:34:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-22 15:27:32 +00:00
|
|
|
|
public RequestState GetRequestState(Request request)
|
|
|
|
|
{
|
|
|
|
|
var func = new RequestStateFunction
|
|
|
|
|
{
|
|
|
|
|
RequestId = request.RequestId
|
|
|
|
|
};
|
|
|
|
|
return gethNode.Call<RequestStateFunction, RequestState>(Deployment.MarketplaceAddress, func);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-20 12:21:53 +00:00
|
|
|
|
private EthAddress GetEthAddressFromTransaction(string transactionHash)
|
|
|
|
|
{
|
|
|
|
|
var transaction = gethNode.GetTransaction(transactionHash);
|
|
|
|
|
return new EthAddress(transaction.From);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-20 08:48:22 +00:00
|
|
|
|
private ContractInteractions StartInteraction()
|
|
|
|
|
{
|
|
|
|
|
return new ContractInteractions(log, gethNode);
|
|
|
|
|
}
|
2023-09-19 08:24:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|