github.com/mit-dci/lit@v0.0.0-20221102210550-8c3d3b49f2ce/litrpc/dlccmds.go (about)

     1  package litrpc
     2  
     3  import (
     4  	"encoding/hex"
     5  
     6  	"github.com/mit-dci/lit/dlc"
     7  	"github.com/mit-dci/lit/lnutil"
     8  )
     9  
    10  type ListOraclesArgs struct {
    11  	// none
    12  }
    13  
    14  type ListOraclesReply struct {
    15  	Oracles []*dlc.DlcOracle
    16  }
    17  
    18  // ListOracles returns all oracles know to LIT
    19  func (r *LitRPC) ListOracles(args ListOraclesArgs,
    20  	reply *ListOraclesReply) error {
    21  	var err error
    22  
    23  	reply.Oracles, err = r.Node.DlcManager.ListOracles()
    24  	if err != nil {
    25  		return err
    26  	}
    27  
    28  	return nil
    29  }
    30  
    31  type ImportOracleArgs struct {
    32  	Url  string
    33  	Name string
    34  }
    35  
    36  type ImportOracleReply struct {
    37  	Oracle *dlc.DlcOracle
    38  }
    39  
    40  // ImportOracle imports an oracle from a REST API
    41  func (r *LitRPC) ImportOracle(args ImportOracleArgs,
    42  	reply *ImportOracleReply) error {
    43  	var err error
    44  	reply.Oracle, err = r.Node.DlcManager.ImportOracle(args.Url, args.Name)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	return nil
    50  }
    51  
    52  type AddOracleArgs struct {
    53  	Key  string
    54  	Name string
    55  }
    56  
    57  type AddOracleReply struct {
    58  	Oracle *dlc.DlcOracle
    59  }
    60  
    61  // AddOracle manually adds an oracle from its PubKey A
    62  func (r *LitRPC) AddOracle(args AddOracleArgs, reply *AddOracleReply) error {
    63  	var err error
    64  	parsedKey, err := hex.DecodeString(args.Key)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	var key [33]byte
    70  	copy(key[:], parsedKey)
    71  
    72  	reply.Oracle, err = r.Node.DlcManager.AddOracle(key, args.Name)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	return nil
    78  }
    79  
    80  type NewContractArgs struct {
    81  	// empty
    82  }
    83  
    84  type NewContractReply struct {
    85  	Contract *lnutil.DlcContract
    86  }
    87  
    88  // NewContract creates a new draft contract
    89  func (r *LitRPC) NewContract(args NewContractArgs,
    90  	reply *NewContractReply) error {
    91  	var err error
    92  
    93  	reply.Contract, err = r.Node.AddContract()
    94  	if err != nil {
    95  		return err
    96  	}
    97  
    98  	return nil
    99  }
   100  
   101  type ListContractsArgs struct {
   102  	// none
   103  }
   104  
   105  type ListContractsReply struct {
   106  	Contracts []*lnutil.DlcContract
   107  }
   108  
   109  // ListContracts returns all contracts know to LIT
   110  func (r *LitRPC) ListContracts(args ListContractsArgs,
   111  	reply *ListContractsReply) error {
   112  	var err error
   113  
   114  	reply.Contracts, err = r.Node.DlcManager.ListContracts()
   115  	if err != nil {
   116  		return err
   117  	}
   118  
   119  	return nil
   120  }
   121  
   122  type GetContractArgs struct {
   123  	Idx uint64
   124  }
   125  
   126  type GetContractReply struct {
   127  	Contract *lnutil.DlcContract
   128  }
   129  
   130  // GetContract returns a single contract based on its index
   131  func (r *LitRPC) GetContract(args GetContractArgs,
   132  	reply *GetContractReply) error {
   133  	var err error
   134  
   135  	reply.Contract, err = r.Node.DlcManager.LoadContract(args.Idx)
   136  	if err != nil {
   137  		return err
   138  	}
   139  
   140  	return nil
   141  }
   142  
   143  type SetContractOracleArgs struct {
   144  	CIdx uint64
   145  	OIdx uint64
   146  }
   147  
   148  type SetContractOracleReply struct {
   149  	Success bool
   150  }
   151  
   152  // SetContractOracle assigns a known oracle to a (new) contract
   153  func (r *LitRPC) SetContractOracle(args SetContractOracleArgs,
   154  	reply *SetContractOracleReply) error {
   155  	var err error
   156  
   157  	err = r.Node.DlcManager.SetContractOracle(args.CIdx, args.OIdx)
   158  	if err != nil {
   159  		return err
   160  	}
   161  
   162  	reply.Success = true
   163  	return nil
   164  }
   165  
   166  type SetContractDatafeedArgs struct {
   167  	CIdx uint64
   168  	Feed uint64
   169  }
   170  
   171  type SetContractDatafeedReply struct {
   172  	Success bool
   173  }
   174  
   175  // SetContractDatafeed sets a data feed by index to a contract, which is then
   176  // used to fetch the R-point from the oracle's REST API
   177  func (r *LitRPC) SetContractDatafeed(args SetContractDatafeedArgs,
   178  	reply *SetContractDatafeedReply) error {
   179  	var err error
   180  
   181  	err = r.Node.DlcManager.SetContractDatafeed(args.CIdx, args.Feed)
   182  	if err != nil {
   183  		return err
   184  	}
   185  
   186  	reply.Success = true
   187  	return nil
   188  }
   189  
   190  type SetContractRPointArgs struct {
   191  	CIdx   uint64
   192  	RPoint [33]byte
   193  }
   194  
   195  type SetContractRPointReply struct {
   196  	Success bool
   197  }
   198  
   199  // SetContractRPoint manually sets the R-point for the contract using a pubkey
   200  func (r *LitRPC) SetContractRPoint(args SetContractRPointArgs,
   201  	reply *SetContractRPointReply) error {
   202  	var err error
   203  
   204  	err = r.Node.DlcManager.SetContractRPoint(args.CIdx, args.RPoint)
   205  	if err != nil {
   206  		return err
   207  	}
   208  
   209  	reply.Success = true
   210  	return nil
   211  }
   212  
   213  type SetContractSettlementTimeArgs struct {
   214  	CIdx uint64
   215  	Time uint64
   216  }
   217  
   218  type SetContractSettlementTimeReply struct {
   219  	Success bool
   220  }
   221  
   222  // SetContractSettlementTime sets the time this contract will settle (the
   223  // unix epoch)
   224  func (r *LitRPC) SetContractSettlementTime(args SetContractSettlementTimeArgs,
   225  	reply *SetContractSettlementTimeReply) error {
   226  	var err error
   227  
   228  	err = r.Node.DlcManager.SetContractSettlementTime(args.CIdx, args.Time)
   229  	if err != nil {
   230  		return err
   231  	}
   232  
   233  	reply.Success = true
   234  	return nil
   235  }
   236  
   237  type SetContractFundingArgs struct {
   238  	CIdx        uint64
   239  	OurAmount   int64
   240  	TheirAmount int64
   241  }
   242  
   243  type SetContractFundingReply struct {
   244  	Success bool
   245  }
   246  
   247  // SetContractFunding sets the division in funding the channel. The arguments
   248  // decide how much we're funding and how much we expect the peer we offer the
   249  // contract to to fund
   250  func (r *LitRPC) SetContractFunding(args SetContractFundingArgs,
   251  	reply *SetContractFundingReply) error {
   252  	var err error
   253  
   254  	err = r.Node.DlcManager.SetContractFunding(args.CIdx,
   255  		args.OurAmount, args.TheirAmount)
   256  	if err != nil {
   257  		return err
   258  	}
   259  
   260  	reply.Success = true
   261  	return nil
   262  }
   263  
   264  type SetContractDivisionArgs struct {
   265  	CIdx             uint64
   266  	ValueFullyOurs   int64
   267  	ValueFullyTheirs int64
   268  }
   269  
   270  type SetContractDivisionReply struct {
   271  	Success bool
   272  }
   273  
   274  // SetContractDivision sets how the contract is settled. The parameters indicate
   275  // at what value the full contract funds are ours, and at what value they are
   276  // full funds are for our peer. Between those values, the contract will divide
   277  // the contract funds linearly
   278  func (r *LitRPC) SetContractDivision(args SetContractDivisionArgs,
   279  	reply *SetContractDivisionReply) error {
   280  	var err error
   281  
   282  	err = r.Node.DlcManager.SetContractDivision(args.CIdx,
   283  		args.ValueFullyOurs, args.ValueFullyTheirs)
   284  	if err != nil {
   285  		return err
   286  	}
   287  
   288  	reply.Success = true
   289  	return nil
   290  }
   291  
   292  type SetContractCoinTypeArgs struct {
   293  	CIdx     uint64
   294  	CoinType uint32
   295  }
   296  
   297  type SetContractCoinTypeReply struct {
   298  	Success bool
   299  }
   300  
   301  // SetContractCoinType sets the coin type the contract will be in. Note that a
   302  // peer that doesn't have a wallet of that type will automatically decline the
   303  // contract.
   304  func (r *LitRPC) SetContractCoinType(args SetContractCoinTypeArgs,
   305  	reply *SetContractCoinTypeReply) error {
   306  	var err error
   307  
   308  	err = r.Node.DlcManager.SetContractCoinType(args.CIdx, args.CoinType)
   309  	if err != nil {
   310  		return err
   311  	}
   312  
   313  	reply.Success = true
   314  	return nil
   315  }
   316  
   317  type OfferContractArgs struct {
   318  	CIdx    uint64
   319  	PeerIdx uint32
   320  }
   321  
   322  type OfferContractReply struct {
   323  	Success bool
   324  }
   325  
   326  // OfferContract offers a contract to a (connected) peer
   327  func (r *LitRPC) OfferContract(args OfferContractArgs,
   328  	reply *OfferContractReply) error {
   329  	var err error
   330  
   331  	err = r.Node.OfferDlc(args.PeerIdx, args.CIdx)
   332  	if err != nil {
   333  		return err
   334  	}
   335  
   336  	reply.Success = true
   337  	return nil
   338  }
   339  
   340  type ContractRespondArgs struct {
   341  	// True for accept, false for decline.
   342  	AcceptOrDecline bool
   343  	CIdx            uint64
   344  }
   345  
   346  type ContractRespondReply struct {
   347  	Success bool
   348  }
   349  
   350  // DeclineContract declines an offered contract
   351  func (r *LitRPC) ContractRespond(args ContractRespondArgs, reply *ContractRespondReply) error {
   352  	var err error
   353  
   354  	if args.AcceptOrDecline {
   355  		err = r.Node.AcceptDlc(args.CIdx)
   356  	} else {
   357  		err = r.Node.DeclineDlc(args.CIdx, 0x01)
   358  	}
   359  
   360  	if err != nil {
   361  		return err
   362  	}
   363  
   364  	reply.Success = true
   365  	return nil
   366  }
   367  
   368  type SettleContractArgs struct {
   369  	CIdx        uint64
   370  	OracleValue int64
   371  	OracleSig   [32]byte
   372  }
   373  
   374  type SettleContractReply struct {
   375  	Success      bool
   376  	SettleTxHash [32]byte
   377  	ClaimTxHash  [32]byte
   378  }
   379  
   380  // SettleContract uses the value and signature from the oracle to settle the
   381  // contract and send the equivalent settlement transaction to the blockchain.
   382  // It will subsequently claim the contract output back to our wallet
   383  func (r *LitRPC) SettleContract(args SettleContractArgs,
   384  	reply *SettleContractReply) error {
   385  	var err error
   386  
   387  	reply.SettleTxHash, reply.ClaimTxHash, err = r.Node.SettleContract(
   388  		args.CIdx, args.OracleValue, args.OracleSig)
   389  	if err != nil {
   390  		return err
   391  	}
   392  
   393  	reply.Success = true
   394  	return nil
   395  }