public async Task SaveDLC(DLCState dlc) { var dir = Path.Combine(RepositoryDirectory, "DLCs"); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } var file = GetDLCFilePath(dlc.Id); if (!File.Exists(file)) { throw new InvalidOperationException("This DLC does not exists"); } await File.WriteAllTextAsync(file, JsonConvert.SerializeObject(dlc, JsonSettings)); }
public async Task <DLCState> NewDLC(OracleInfo oracleInfo, DLCTransactionBuilder builder) { var dir = Path.Combine(RepositoryDirectory, "DLCs"); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } var s = new DLCState() { OracleInfo = oracleInfo, BuilderState = builder.ExportStateJObject(), Id = RandomUtils.GetUInt256() }; var file = GetDLCFilePath(s.Id); await File.WriteAllTextAsync(file, JsonConvert.SerializeObject(s, JsonSettings)); return(s); }
public static void AssertState(this InvocationContext ctx, string optionName, DLCState currentState, bool expectedOfferer, DLCNextStep expectedState, Network network) { if (currentState.BuilderState is null) { throw new CommandException(optionName, "The DLC is in an invalid state for this action"); } var isOfferer = currentState.GetBuilder(network).State.IsInitiator; if (isOfferer && !expectedOfferer) { throw new CommandException(optionName, "This action must be run by the acceptor, but you are the offerer of the DLC"); } if (!isOfferer && expectedOfferer) { throw new CommandException(optionName, "This action must be run by the offerer, but you are the acceptor of the DLC"); } var actualStep = currentState.GetNextStep(network); if (actualStep != expectedState) { throw new CommandException(optionName, $"The DLC is in an invalid state for this action. The expected state is '{expectedState}' but your state is '{actualStep}'."); } }