@@ -2,9 +2,12 @@ package api
22
33import (
44 "fmt"
5+ "math"
56
7+ "github.com/stellar/go/build"
68 hProtocol "github.com/stellar/go/protocols/horizon"
79 "github.com/stellar/go/txnbuild"
10+ "github.com/stellar/go/xdr"
811 "github.com/stellar/kelp/model"
912)
1013
@@ -222,11 +225,92 @@ type Balance struct {
222225
223226// ExchangeShim is the interface we use as a generic API for all crypto exchanges
224227type ExchangeShim interface {
225- SubmitOps (ops []txnbuild. Operation , asyncCallback func (hash string , e error )) error
226- SubmitOpsSynch (ops []txnbuild. Operation , asyncCallback func (hash string , e error )) error // forced synchronous version of SubmitOps
228+ SubmitOps (ops []build. TransactionMutator , asyncCallback func (hash string , e error )) error
229+ SubmitOpsSynch (ops []build. TransactionMutator , asyncCallback func (hash string , e error )) error // forced synchronous version of SubmitOps
227230 GetBalanceHack (asset hProtocol.Asset ) (* Balance , error )
228231 LoadOffersHack () ([]hProtocol.Offer , error )
229232 Constrainable
230233 OrderbookFetcher
231234 FillTrackable
232235}
236+
237+ // ConvertOperation2TM is a temporary adapter to support transitioning from the old Go SDK to the new SDK without having to bump the major version
238+ func ConvertOperation2TM (ops []txnbuild.Operation ) []build.TransactionMutator {
239+ muts := []build.TransactionMutator {}
240+ for _ , o := range ops {
241+ var mob build.ManageOfferBuilder
242+ if mso , ok := o .(* txnbuild.ManageSellOffer ); ok {
243+ mob = build .ManageOffer (
244+ false ,
245+ build .Amount (mso .Amount ),
246+ build.Rate {
247+ Selling : build.Asset {Code : mso .Selling .GetCode (), Issuer : mso .Selling .GetIssuer (), Native : mso .Selling .IsNative ()},
248+ Buying : build.Asset {Code : mso .Buying .GetCode (), Issuer : mso .Buying .GetIssuer (), Native : mso .Buying .IsNative ()},
249+ Price : build .Price (mso .Price ),
250+ },
251+ build .OfferID (mso .OfferID ),
252+ )
253+ if mso .SourceAccount != nil {
254+ mob .Mutate (build.SourceAccount {AddressOrSeed : mso .SourceAccount .GetAccountID ()})
255+ }
256+ } else {
257+ panic (fmt .Sprintf ("could not convert txnbuild.Operation to build.TransactionMutator: %v\n " , o ))
258+ }
259+ muts = append (muts , mob )
260+ }
261+ return muts
262+ }
263+
264+ // ConvertTM2Operation is a temporary adapter to support transitioning from the old Go SDK to the new SDK without having to bump the major version
265+ func ConvertTM2Operation (muts []build.TransactionMutator ) []txnbuild.Operation {
266+ ops := []txnbuild.Operation {}
267+ for _ , m := range muts {
268+ var mso * txnbuild.ManageSellOffer
269+ if mob , ok := m .(build.ManageOfferBuilder ); ok {
270+ mso = convertMOB2MSO (mob )
271+ } else if mob , ok := m .(* build.ManageOfferBuilder ); ok {
272+ mso = convertMOB2MSO (* mob )
273+ } else {
274+ panic (fmt .Sprintf ("could not convert build.TransactionMutator to txnbuild.Operation: %v (type=%T)\n " , m , m ))
275+ }
276+ ops = append (ops , mso )
277+ }
278+ return ops
279+ }
280+
281+ func convertMOB2MSO (mob build.ManageOfferBuilder ) * txnbuild.ManageSellOffer {
282+ mso := & txnbuild.ManageSellOffer {
283+ Amount : fmt .Sprintf ("%.7f" , float64 (mob .MO .Amount )/ math .Pow (10 , 7 )),
284+ OfferID : int64 (mob .MO .OfferId ),
285+ Price : fmt .Sprintf ("%.7f" , float64 (mob .MO .Price .N )/ float64 (mob .MO .Price .D )),
286+ }
287+ if mob .O .SourceAccount != nil {
288+ mso .SourceAccount = & txnbuild.SimpleAccount {
289+ AccountID : mob .O .SourceAccount .Address (),
290+ }
291+ }
292+
293+ if mob .MO .Buying .Type == xdr .AssetTypeAssetTypeNative {
294+ mso .Buying = txnbuild.NativeAsset {}
295+ } else {
296+ var tipe , code , issuer string
297+ mob .MO .Buying .MustExtract (& tipe , & code , & issuer )
298+ mso .Buying = txnbuild.CreditAsset {
299+ Code : code ,
300+ Issuer : issuer ,
301+ }
302+ }
303+
304+ if mob .MO .Selling .Type == xdr .AssetTypeAssetTypeNative {
305+ mso .Selling = txnbuild.NativeAsset {}
306+ } else {
307+ var tipe , code , issuer string
308+ mob .MO .Selling .MustExtract (& tipe , & code , & issuer )
309+ mso .Selling = txnbuild.CreditAsset {
310+ Code : code ,
311+ Issuer : issuer ,
312+ }
313+ }
314+
315+ return mso
316+ }
0 commit comments