Skip to content

Commit

Permalink
Merge pull request #11 from tonkeeper/feature/swap-message-builder
Browse files Browse the repository at this point in the history
Feature: Swap message builder
  • Loading branch information
grishamsc authored May 7, 2024
2 parents 452670b + 616917f commit 5c14ab1
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 18 deletions.
23 changes: 7 additions & 16 deletions Source/TonSwift/Jettons/JettonTransferData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,17 @@ public struct JettonTransferData: CellCodable {
public let toAddress: Address
public let responseAddress: Address
public let forwardAmount: BigUInt
public let comment: String?
public let forwardPayload: Cell?

public func storeTo(builder: Builder) throws {
try builder.store(uint: 0xf8a7ea5, bits: 32)
try builder.store(uint: OpCodes.JETTON_TRANSFER, bits: 32)
try builder.store(uint: queryId, bits: 64)
try builder.store(coins: Coins(amount.magnitude))
try builder.store(AnyAddress(toAddress))
try builder.store(AnyAddress(responseAddress))
try builder.store(bit: false)
try builder.store(coins: Coins(forwardAmount.magnitude))
var commentCell: Cell?
if let comment = comment {
commentCell = try Builder().store(int: 0, bits: 32).writeSnakeData(Data(comment.utf8)).endCell()
}
try builder.storeMaybe(ref: commentCell)
try builder.storeMaybe(ref: forwardPayload)
}

public static func loadFrom(slice: Slice) throws -> JettonTransferData {
Expand All @@ -41,19 +37,14 @@ public struct JettonTransferData: CellCodable {
let forwardAmount = try slice.loadCoins().amount

let hasComment = try slice.loadBoolean()
var comment: String?
if hasComment {
let commentCell = try slice.loadRef()
let slice = try commentCell.toSlice()
try slice.skip(32)
comment = try slice.loadSnakeString()
}
let forwardPayload = try slice.loadRef()


return JettonTransferData(queryId: queryId,
amount: amount,
toAddress: toAddress,
responseAddress: responseAddress,
forwardAmount: forwardAmount,
comment: comment)
forwardPayload: forwardPayload)
}
}
7 changes: 6 additions & 1 deletion Source/TonSwift/Jettons/JettonTransferMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ public struct JettonTransferMessage {
let forwardAmount = BigUInt(stringLiteral: "1")
let jettonTransferAmount = BigUInt(stringLiteral: "640000000")
let queryId = UInt64(Date().timeIntervalSince1970)

var commentCell: Cell?
if let comment = comment {
commentCell = try Builder().store(int: 0, bits: 32).writeSnakeData(Data(comment.utf8)).endCell()
}

let jettonTransferData = JettonTransferData(queryId: queryId,
amount: amount.magnitude,
toAddress: to,
responseAddress: from,
forwardAmount: forwardAmount,
comment: comment)
forwardPayload: commentCell)

return MessageRelaxed.internal(
to: jettonAddress,
Expand Down
2 changes: 1 addition & 1 deletion Source/TonSwift/NFT/NFTTransferData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public struct NFTTransferData: CellCodable {
public let forwardPayload: Cell?

public func storeTo(builder: Builder) throws {
try builder.store(uint: 0x5fcc3d14, bits: 32) // transfer op
try builder.store(uint: OpCodes.NFT_TRANSFER, bits: 32) // transfer op
try builder.store(uint: queryId, bits: 64)
try builder.store(AnyAddress(newOwnerAddress))
try builder.store(AnyAddress(responseAddress))
Expand Down
40 changes: 40 additions & 0 deletions Source/TonSwift/StonfiSwap/StonfiSwapData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import Foundation
import BigInt

public struct StonfiSwapData: CellCodable {
public let assetToSwap: Address
public let minAskAmount: BigUInt
public let userWalletAddress: Address
public let referralAddress: Address?

public func storeTo(builder: Builder) throws {
try builder.store(uint: OpCodes.STONFI_SWAP, bits: 32)
try builder.store(AnyAddress(assetToSwap))
try builder.store(coins: Coins(minAskAmount.magnitude))
try builder.store(AnyAddress(userWalletAddress))

if referralAddress != nil {
try builder.store(bit: true)
try builder.store(AnyAddress(referralAddress))
} else {
try builder.store(bit: false)
}
}

public static func loadFrom(slice: Slice) throws -> StonfiSwapData {
_ = try slice.loadUint(bits: 32)
let assetToSwap: Address = try slice.loadType()
let minAskAmount = try slice.loadCoins().amount

let userWalletAddress: Address = try slice.loadType()
let withReferral = try slice.loadBoolean()

var referralAddress: Address? = nil
if withReferral {
referralAddress = try slice.loadType()
}


return StonfiSwapData(assetToSwap: assetToSwap, minAskAmount: minAskAmount, userWalletAddress: userWalletAddress, referralAddress: referralAddress)
}
}
38 changes: 38 additions & 0 deletions Source/TonSwift/StonfiSwap/StonfiSwapMessage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Foundation
import BigInt

public struct StonfiSwapMessage {
/// Jetton --> Jetton swap message
public static func internalMessage(userWalletAddress: Address,
minAskAmount: BigUInt,
offerAmount: BigUInt,
jettonFromWalletAddress: Address,
jettonToWalletAddress: Address,
referralAddress: Address? = nil,
forwardAmount: BigUInt,
attachedAmount: BigUInt
) throws -> MessageRelaxed {
let queryId = UInt64(Date().timeIntervalSince1970)

let stonfiSwapData = StonfiSwapData(assetToSwap: jettonToWalletAddress, minAskAmount: minAskAmount, userWalletAddress: userWalletAddress, referralAddress: referralAddress)

let stonfiSwapCell = try Builder().store(stonfiSwapData).endCell()

let jettonTransferData = JettonTransferData(
queryId: queryId,
amount: offerAmount,
toAddress: try! Address.parse(STONFI_CONSTANTS.RouterAddress),
responseAddress: userWalletAddress,
forwardAmount: forwardAmount,
forwardPayload: stonfiSwapCell
)

return MessageRelaxed.internal(
to: jettonFromWalletAddress,
value: attachedAmount,
bounce: true,
body: try Builder().store(jettonTransferData).endCell()
)
}
}

5 changes: 5 additions & 0 deletions Source/TonSwift/Util/OpCodes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public enum OpCodes {
public static var JETTON_TRANSFER: Int32 = 0xf8a7ea5
public static var NFT_TRANSFER: Int32 = 0x5fcc3d14
public static var STONFI_SWAP: Int32 = 0x25938561
}
39 changes: 39 additions & 0 deletions Source/TonSwift/Util/StonfiConstants.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import BigInt

enum DEX_VERSION: String {
case v1 = "v1"
}

public enum STONFI_CONSTANTS {
public static var RouterAddress: String {
"0:779dcc815138d9500e449c5291e7f12738c23d575b5310000f6a253bd607384e"
}

public static var TONProxyAddress: String {
"0:8cdc1d7640ad5ee326527fc1ad0514f468b30dc84b0173f0e155f451b4e11f7c"
}

public enum SWAP_JETTON_TO_JETTON {
public static var GasAmount: BigUInt {
BigUInt("265000000")
}
public static var ForwardGasAmount: BigUInt {
BigUInt("205000000")
}
}

public enum SWAP_JETTON_TO_TON {
public static var GasAmount: BigUInt {
BigUInt("185000000")
}
public static var ForwardGasAmount: BigUInt {
BigUInt("125000000")
}
}

public enum SWAP_TON_TO_JETTON {
public static var ForwardGasAmount: BigUInt {
BigUInt("215000000")
}
}
}

0 comments on commit 5c14ab1

Please sign in to comment.