-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from tonkeeper/feature/swap-message-builder
Feature: Swap message builder
- Loading branch information
Showing
7 changed files
with
136 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} |