Skip to content

Commit c1b0a5a

Browse files
committed
Various imporvements
1 parent ca5bb0f commit c1b0a5a

File tree

12 files changed

+16
-369
lines changed

12 files changed

+16
-369
lines changed

internal/times/time.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ package times
22

33
import "time"
44

5-
var timeNow func() int64 = GetCurrentTime
5+
var Now func() int64 = NowImpl
66

7-
func GetCurrentTime() int64 {
7+
func NowImpl() int64 {
88
return time.Now().Unix()
99
}
1010

11-
var CalcAuctionStartTimeFunc func(uint32, uint32) uint32 = CalcAuctionStartTime
11+
var CalculateAuctionStartTime func(uint32, uint32) uint32 = CalculateAuctionStartTimeImpl
1212

13-
func CalcAuctionStartTime(startAuctionIn uint32, additionalWaitPeriod uint32) uint32 {
13+
func CalculateAuctionStartTimeImpl(startAuctionIn uint32, additionalWaitPeriod uint32) uint32 {
1414
currentTime := time.Now().Unix()
1515
return uint32(currentTime) + additionalWaitPeriod + startAuctionIn
1616
}

sdk-clients/fusion/examples/place_order_custom_preset/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ const (
2121
usdc = "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359"
2222
wmatic = "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270"
2323
weth = "0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619"
24-
amountEth = "200000000000000" // ~50 cents of ETH
25-
amountUsdc = "500000" // 50 cents of USDC
24+
amountEth = "200000000000000"
25+
amountUsdc = "500000" // 50 cents of USDC
2626
chainId = 137
2727
)
2828

sdk-clients/fusion/extension.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func NewExtension(params ExtensionParams) (*Extension, error) {
8787

8888
var resolvingStartTime *big.Int
8989
if params.ResolvingStartTime == nil {
90-
resolvingStartTime = big.NewInt(times.GetCurrentTime())
90+
resolvingStartTime = big.NewInt(times.Now())
9191
} else {
9292
resolvingStartTime = params.ResolvingStartTime
9393
}

sdk-clients/fusion/extension_test.go

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package fusion
22

33
import (
4-
"bytes"
5-
"encoding/json"
6-
"fmt"
74
"math/big"
85
"testing"
96

@@ -111,7 +108,6 @@ func TestNewExtension(t *testing.T) {
111108
ProtocolFee: FromPercent(1, GetDefaultBase()),
112109
},
113110

114-
// todo these are needed now
115111
ResolvingStartTime: big.NewInt(0),
116112
},
117113
expectedExtension: &Extension{
@@ -159,7 +155,6 @@ func TestNewExtension(t *testing.T) {
159155
ProtocolFee: FromPercent(1, GetDefaultBase()),
160156
},
161157

162-
// todo these are needed now
163158
ResolvingStartTime: big.NewInt(0),
164159
},
165160
expectedExtension: &Extension{
@@ -259,25 +254,6 @@ func TestNewExtension(t *testing.T) {
259254
}
260255
}
261256

262-
func printSelectedFields(ext *Extension) string {
263-
selectedFields := map[string]string{
264-
"MakerAssetSuffix": ext.MakerAssetSuffix,
265-
"TakerAssetSuffix": ext.TakerAssetSuffix,
266-
"MakingAmountData": ext.MakingAmountData,
267-
"TakingAmountData": ext.TakingAmountData,
268-
"Predicate": ext.Predicate,
269-
"MakerPermit": ext.MakerPermit,
270-
"PreInteraction": ext.PreInteraction,
271-
"PostInteraction": ext.PostInteraction,
272-
}
273-
274-
jsonData, err := json.MarshalIndent(selectedFields, "", " ")
275-
if err != nil {
276-
return fmt.Sprint("Error marshalling to JSON:", err)
277-
}
278-
return string(jsonData)
279-
}
280-
281257
func TestConvertToOrderbookExtension(t *testing.T) {
282258
tests := []struct {
283259
name string
@@ -328,16 +304,6 @@ func TestConvertToOrderbookExtension(t *testing.T) {
328304
}
329305
}
330306

331-
var asset = "0xBAb2C3d4e5f67890123456789AbcDEf123456789"
332-
var permit = "9999999999999999999999"
333-
var fullAuctionDetails = &AuctionDetails{
334-
StartTime: 1,
335-
Duration: 2,
336-
InitialRateBump: 3,
337-
Points: []AuctionPointClassFixed{{Coefficient: 4, Delay: 5}},
338-
GasCost: GasCostConfigClassFixed{GasBumpEstimate: 6, GasPriceEstimate: 7},
339-
}
340-
341307
func TestBuildAmountGetterData(t *testing.T) {
342308
tests := []struct {
343309
name string
@@ -457,20 +423,3 @@ func TestBuildAmountGetterData(t *testing.T) {
457423
})
458424
}
459425
}
460-
461-
func extensionsEqual(a, b *Extension) bool {
462-
return a.MakerAssetSuffix == b.MakerAssetSuffix &&
463-
a.TakerAssetSuffix == b.TakerAssetSuffix &&
464-
a.MakingAmountData == b.MakingAmountData &&
465-
a.TakingAmountData == b.TakingAmountData &&
466-
a.Predicate == b.Predicate &&
467-
a.MakerPermit == b.MakerPermit &&
468-
a.PreInteraction == b.PreInteraction &&
469-
a.PostInteraction == b.PostInteraction
470-
// a.CustomData == b.CustomData
471-
}
472-
473-
// contains checks if the substring is present in the string.
474-
func contains(s, substr string) bool {
475-
return bytes.Contains([]byte(s), []byte(substr))
476-
}

sdk-clients/fusion/order.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func CreateAuctionDetails(preset *PresetClassFixed, additionalWaitPeriod float32
231231
func CreateSettlementPostInteractionData(details Details, whitelist []WhitelistItem, orderInfo FusionOrderV4) (*SettlementPostInteractionData, error) {
232232
resolverStartTime := details.ResolvingStartTime
233233
if details.ResolvingStartTime == nil || details.ResolvingStartTime.Cmp(big.NewInt(0)) == 0 {
234-
resolverStartTime = big.NewInt(times.GetCurrentTime())
234+
resolverStartTime = big.NewInt(times.Now())
235235
}
236236

237237
return &SettlementPostInteractionData{

sdk-clients/fusionplus/escrowextension.go

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ import (
44
"bytes"
55
"encoding/binary"
66
"fmt"
7-
"log"
87
"math/big"
9-
"strings"
108

11-
"github.com/1inch/1inch-sdk-go/internal/bytesiterator"
129
"github.com/1inch/1inch-sdk-go/internal/hexadecimal"
1310
"github.com/1inch/1inch-sdk-go/sdk-clients/fusion"
1411
"github.com/1inch/1inch-sdk-go/sdk-clients/orderbook"
@@ -86,86 +83,6 @@ func (e *EscrowExtension) ConvertToOrderbookExtension() (*orderbook.Extension, e
8683
}, nil
8784
}
8885

89-
func decodeExtraData(data []byte) (*EscrowExtraData, error) {
90-
iter := bytesiterator.New(data)
91-
hashlockData, err := iter.NextUint256()
92-
if err != nil {
93-
log.Fatalf("Failed to read first uint256: %v", err)
94-
}
95-
96-
dstChainIdData, err := iter.NextUint256()
97-
if err != nil {
98-
log.Fatalf("Failed to read second uint256: %v", err)
99-
}
100-
101-
addressBig, err := iter.NextUint256()
102-
if err != nil {
103-
log.Fatalf("Failed to read address: %v", err)
104-
}
105-
106-
addressHex := strings.ToLower(common.BigToAddress(addressBig).Hex())
107-
108-
safetyDepositData, err := iter.NextUint256()
109-
if err != nil {
110-
log.Fatalf("Failed to read third uint256: %v", err)
111-
}
112-
113-
// Define a 128-bit mask (2^128 - 1)
114-
mask := new(big.Int)
115-
mask.Exp(big.NewInt(2), big.NewInt(128), nil).Sub(mask, big.NewInt(1))
116-
117-
srcSafetyDeposit := new(big.Int).And(safetyDepositData, mask)
118-
dstSafetyDeposit := new(big.Int).Rsh(safetyDepositData, 128)
119-
120-
timelocksData, err := iter.NextUint256()
121-
if err != nil {
122-
log.Fatalf("Failed to read fourth uint256: %v", err)
123-
}
124-
125-
timelocks, err := decodeTimeLocks(timelocksData)
126-
if err != nil {
127-
log.Fatalf("Failed to decode timelocks: %v", err)
128-
}
129-
130-
return &EscrowExtraData{
131-
HashLock: &HashLock{
132-
hashlockData.String(),
133-
},
134-
DstChainId: float32(dstChainIdData.Uint64()),
135-
DstToken: common.HexToAddress(addressHex),
136-
SrcSafetyDeposit: srcSafetyDeposit,
137-
DstSafetyDeposit: dstSafetyDeposit,
138-
TimeLocks: timelocks,
139-
}, nil
140-
}
141-
142-
// decodeTimeLocks takes a *big.Int containing the raw hex data and returns a TimeLocks struct.
143-
func decodeTimeLocks(value *big.Int) (*TimeLocks, error) {
144-
tl := &TimeLocks{}
145-
146-
// Convert big.Int to byte slice
147-
data := value.Bytes()
148-
149-
if len(data) < 32 {
150-
padded := make([]byte, 32)
151-
copy(padded[32-len(data):], data)
152-
data = padded
153-
}
154-
155-
//TODO big.Int cannot preserve leading zeroes, so decoding the deploy time is impossible atm
156-
157-
// tl.DeployTime = float32(binary.BigEndian.Uint32((data[0:4])))
158-
tl.DstCancellation = float32(binary.BigEndian.Uint32((data[4:8])))
159-
tl.DstPublicWithdrawal = float32(binary.BigEndian.Uint32((data[8:12])))
160-
tl.DstWithdrawal = float32(binary.BigEndian.Uint32((data[12:16])))
161-
tl.SrcPublicCancellation = float32(binary.BigEndian.Uint32((data[16:20])))
162-
tl.SrcCancellation = float32(binary.BigEndian.Uint32((data[20:24])))
163-
tl.SrcPublicWithdrawal = float32(binary.BigEndian.Uint32((data[24:28])))
164-
tl.SrcWithdrawal = float32(binary.BigEndian.Uint32((data[28:32])))
165-
166-
return tl, nil
167-
}
168-
16986
type EscrowExtraData struct {
17087
HashLock *HashLock
17188
DstChainId float32

sdk-clients/fusionplus/order.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func CreateAuctionDetails(preset *Preset, additionalWaitPeriod float32) (*Auctio
270270
}
271271

272272
return &AuctionDetails{
273-
StartTime: times.CalcAuctionStartTimeFunc(uint32(preset.StartAuctionIn), uint32(additionalWaitPeriod)),
273+
StartTime: times.CalculateAuctionStartTime(uint32(preset.StartAuctionIn), uint32(additionalWaitPeriod)),
274274
Duration: uint32(preset.AuctionDuration),
275275
InitialRateBump: uint32(preset.InitialRateBump),
276276
Points: pointsFixed,
@@ -281,7 +281,7 @@ func CreateAuctionDetails(preset *Preset, additionalWaitPeriod float32) (*Auctio
281281
func CreateSettlementPostInteractionData(details Details, orderInfo CrossChainOrderDto) (*SettlementPostInteractionData, error) {
282282
resolverStartTime := details.ResolvingStartTime
283283
if details.ResolvingStartTime == nil || details.ResolvingStartTime.Cmp(big.NewInt(0)) == 0 {
284-
resolverStartTime = big.NewInt(times.GetCurrentTime())
284+
resolverStartTime = big.NewInt(times.Now())
285285
}
286286
return NewSettlementPostInteractionData(SettlementSuffixData{
287287
Whitelist: details.Whitelist,

sdk-clients/fusionplus/order_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func TestCreateAuctionDetails(t *testing.T) {
165165
},
166166
additionalWaitPeriod: 2,
167167
expected: &AuctionDetails{
168-
StartTime: times.CalcAuctionStartTimeFunc(5, 2),
168+
StartTime: times.CalculateAuctionStartTime(5, 2),
169169
Duration: 300,
170170
InitialRateBump: 1,
171171
Points: []AuctionPointClassFixed{
@@ -215,7 +215,7 @@ func TestCreateAuctionDetails(t *testing.T) {
215215
},
216216
additionalWaitPeriod: 2,
217217
expected: &AuctionDetails{
218-
StartTime: times.CalcAuctionStartTimeFunc(5, 2),
218+
StartTime: times.CalculateAuctionStartTime(5, 2),
219219
Duration: 300,
220220
InitialRateBump: 1,
221221
Points: []AuctionPointClassFixed{},

sdk-clients/fusionplus/settlementpostinteractiondata.go

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package fusionplus
22

33
import (
4-
"encoding/hex"
54
"errors"
65
"fmt"
76
"math/big"
87
"sort"
98
"strings"
109

1110
"github.com/1inch/1inch-sdk-go/internal/bytesbuilder"
12-
"github.com/1inch/1inch-sdk-go/internal/bytesiterator"
1311
"github.com/ethereum/go-ethereum/common"
14-
"github.com/ethereum/go-ethereum/common/hexutil"
1512
)
1613

1714
type SettlementPostInteractionData struct {
@@ -67,104 +64,10 @@ func NewSettlementPostInteractionData(data SettlementSuffixData) (*SettlementPos
6764
}, nil
6865
}
6966

70-
func Decode(data string) (SettlementPostInteractionData, error) {
71-
bytes, err := hexutil.Decode(data)
72-
if err != nil {
73-
return SettlementPostInteractionData{}, errors.New("invalid hex string")
74-
}
75-
76-
flags := big.NewInt(int64(bytes[len(bytes)-1]))
77-
bytesWithoutFlags := bytes[:len(bytes)-1]
78-
79-
iter := bytesiterator.New(bytesWithoutFlags)
80-
//var bankFee *big.Int
81-
var customReceiver common.Address
82-
83-
//if flags.Bit(0) == 1 {
84-
// bankFee, err = iter.NextUint32()
85-
// if err != nil {
86-
// return SettlementPostInteractionData{}, err
87-
// }
88-
//}
89-
90-
if flags.Bit(1) == 1 {
91-
92-
//ratio, err := iter.NextUint16()
93-
//if err != nil {
94-
// return SettlementPostInteractionData{}, err
95-
//}
96-
//
97-
//receiver, err := iter.NextUint160()
98-
//if err != nil {
99-
// return SettlementPostInteractionData{}, err
100-
//}
101-
//
102-
//integratorFee = &IntegratorFee{
103-
// Ratio: ratio,
104-
// Receiver: common.HexToAddress(receiver.Text(16)),
105-
//}
106-
107-
if flags.Bit(2) == 1 {
108-
109-
customReceiverRaw, err := iter.NextUint160()
110-
if err != nil {
111-
return SettlementPostInteractionData{}, err
112-
}
113-
114-
customReceiver = common.HexToAddress(customReceiverRaw.Text(16))
115-
}
116-
}
117-
118-
resolvingStartTime, err := iter.NextUint32()
119-
if err != nil {
120-
return SettlementPostInteractionData{}, err
121-
}
122-
var whitelist []WhitelistItem
123-
124-
for !iter.IsEmpty() {
125-
addressHalfRaw, err := iter.NextBytes(10)
126-
if err != nil {
127-
return SettlementPostInteractionData{}, err
128-
}
129-
addressHalf := hex.EncodeToString(addressHalfRaw)
130-
delay, err := iter.NextUint16()
131-
if err != nil {
132-
return SettlementPostInteractionData{}, err
133-
}
134-
whitelist = append(whitelist, WhitelistItem{
135-
AddressHalf: addressHalf,
136-
Delay: delay,
137-
})
138-
}
139-
140-
return SettlementPostInteractionData{
141-
ResolvingStartTime: resolvingStartTime,
142-
Whitelist: whitelist,
143-
CustomReceiver: customReceiver,
144-
}, nil
145-
}
146-
14767
func (spid SettlementPostInteractionData) Encode() (string, error) {
14868
bitMask := big.NewInt(0)
14969
bytes := bytesbuilder.New()
15070

151-
//if spid.BankFee != nil && spid.BankFee.Cmp(big.NewInt(0)) != 0 {
152-
// bitMask.SetBit(bitMask, 0, 1)
153-
// bytes.AddUint32(spid.BankFee)
154-
//}
155-
//
156-
//if spid.IntegratorFee != nil && spid.IntegratorFee.Ratio.Cmp(big.NewInt(0)) != 0 {
157-
// bitMask.SetBit(bitMask, 1, 1)
158-
// bytes.AddUint16(spid.IntegratorFee.Ratio)
159-
// bytes.AddAddress(spid.IntegratorFee.Receiver)
160-
//
161-
// // TODO this check is probably not good enough
162-
// if spid.CustomReceiver.Hex() != "0x0000000000000000000000000000000000000000" {
163-
// bitMask.SetBit(bitMask, 2, 1)
164-
// bytes.AddAddress(spid.CustomReceiver)
165-
// }
166-
//}
167-
16871
bytes.AddUint32(spid.ResolvingStartTime)
16972

17073
for _, wl := range spid.Whitelist {

0 commit comments

Comments
 (0)