Skip to content

Commit

Permalink
- added new extractors
Browse files Browse the repository at this point in the history
- updated tests
  • Loading branch information
simonmcl committed Dec 6, 2024
1 parent 13017bb commit e703138
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
56 changes: 55 additions & 1 deletion Sources/KukaiCoreSwift/Factories/OperationFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ public class OperationFactory {
}

/**
Filter and verify only 1 transaction exists thats setting a baker. If so return this operation, otherwise return false
Filter and verify only 1 transaction exists thats setting a baker. If so return this operation, otherwise return nil
*/
public static func isDelegate(operations: [Operation]) -> OperationDelegation? {
let filteredOperations = filterReveal(operations: operations)
Expand All @@ -445,6 +445,60 @@ public class OperationFactory {
return nil
}

/**
Filter and verify only 1 transaction exists thats performing a stake operation. If so return this operation, otherwise return nil
*/
public static func isStake(operations: [Operation]) -> OperationTransaction? {
let filteredOperations = filterReveal(operations: operations)
if filteredOperations.count == 1,
let op = filteredOperations.first as? OperationTransaction,
op.parameters?["entrypoint"] as? String == "stake",
let valueDict = op.parameters?["value"] as? [String: String],
Array(valueDict.keys) == ["prim"],
Array(valueDict.values) == ["Unit"]
{
return op
}

return nil
}

/**
Filter and verify only 1 transaction exists thats performing an unstake operation. If so return this operation, otherwise return nil
*/
public static func isUnstake(operations: [Operation]) -> OperationTransaction? {
let filteredOperations = filterReveal(operations: operations)
if filteredOperations.count == 1,
let op = filteredOperations.first as? OperationTransaction,
op.parameters?["entrypoint"] as? String == "unstake",
let valueDict = op.parameters?["value"] as? [String: String],
Array(valueDict.keys) == ["prim"],
Array(valueDict.values) == ["Unit"]
{
return op
}

return nil
}

/**
Filter and verify only 1 transaction exists thats performing a finalise unstake operation If so return this operation, otherwise return nil
*/
public static func isFinaliseUnstake(operations: [Operation]) -> OperationTransaction? {
let filteredOperations = filterReveal(operations: operations)
if filteredOperations.count == 1,
let op = filteredOperations.first as? OperationTransaction,
op.parameters?["entrypoint"] as? String == "finalize_unstake",
let valueDict = op.parameters?["value"] as? [String: String],
Array(valueDict.keys) == ["prim"],
Array(valueDict.values) == ["Unit"]
{
return op
}

return nil
}

/**
Filter and verify only 1 transaction exists thats sending a token. If so return this operation, otherwise return false
*/
Expand Down
21 changes: 21 additions & 0 deletions Tests/KukaiCoreSwiftTests/Factories/OperationFactoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,9 @@ class OperationFactoryTests: XCTestCase {
let dex = DipDupExchange(name: .quipuswap, address: "KT1abc", tezPool: "100000000000", tokenPool: "1000000000", sharesTotal: "100000", midPrice: "14", token: dexToken)
let swap = OperationFactory.swapXtzToToken(withDex: dex, xtzAmount: .init(fromNormalisedAmount: 14), minTokenAmount: .init(fromNormalisedAmount: 2, decimalPlaces: 3), walletAddress: "tz1abc", timeout: 60)
let delegate = OperationFactory.delegateOperation(to: "KT1abc", from: MockConstants.defaultHdWallet.address)
let stake = OperationFactory.stakeOperation(from: MockConstants.defaultHdWallet.address, amount: TokenAmount(fromNormalisedAmount: 14, decimalPlaces: 6))
let unstake = OperationFactory.unstakeOperation(from: MockConstants.defaultHdWallet.address, amount: TokenAmount(fromNormalisedAmount: 14, decimalPlaces: 6))
let finaliseUnstake = OperationFactory.finaliseUnstakeOperation(from: MockConstants.defaultHdWallet.address)


// is single transctions
Expand All @@ -606,6 +609,24 @@ class OperationFactoryTests: XCTestCase {
XCTAssert( OperationFactory.Extractor.isDelegate(operations: opFA1) == nil )


// Is Stake
let stakeOp = OperationFactory.Extractor.isStake(operations: stake)
XCTAssert( stakeOp != nil )
XCTAssert( stakeOp?.destination == MockConstants.defaultHdWallet.address)


// Is Unstake
let unstakeOp = OperationFactory.Extractor.isUnstake(operations: unstake)
XCTAssert( unstakeOp != nil )
XCTAssert( unstakeOp?.destination == MockConstants.defaultHdWallet.address)


// Is FinaliseUnstake
let finaliseUnstakeOp = OperationFactory.Extractor.isFinaliseUnstake(operations: finaliseUnstake)
XCTAssert( finaliseUnstakeOp != nil )
XCTAssert( finaliseUnstakeOp?.destination == MockConstants.defaultHdWallet.address)


// is token transfer
XCTAssert( OperationFactory.Extractor.isFaTokenTransfer(operations: xtzOp) == nil )

Expand Down
2 changes: 1 addition & 1 deletion Tests/KukaiCoreSwiftTests/MockConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ public struct MockConstants {
public static let blockchainHeadMinus3 = BlockchainHead(protocol: "PsFLorenaUUuikDWvMDr6fGBRG8kt3e3D3fHoXK1j1BFRxeSH4i", chainID: "NetXxkAx4woPLyu", hash: "BLEDGNuADAwZfKK7iZ6PHnu7gZFSXuRPVFXe2PhSnb6aMyKn3mKMinus3")
public static let operationMetadata = OperationMetadata(managerKey: "edpktsYtWnwnuiLkHySdNSihWEChdeFzyz9v8Vdb8aAWRibsPH7g7E", counter: 143230, blockchainHead: MockConstants.blockchainHead)
public static let operationMetadataNoManager = OperationMetadata(managerKey: nil, counter: 143230, blockchainHead: MockConstants.blockchainHead)
public static let networkConstants = NetworkConstants(minimal_block_delay: "30", hard_gas_limit_per_operation: "10400000", hard_gas_limit_per_block: "10400000", origination_size: 257, cost_per_byte: "250", hard_storage_limit_per_operation: "60000")
public static let networkConstants = NetworkConstants(minimal_block_delay: "10", hard_gas_limit_per_operation: "10400000", hard_gas_limit_per_block: "10400000", origination_size: 257, cost_per_byte: "250", hard_storage_limit_per_operation: "60000", blocks_per_cycle: 24576)
public static var sendOperations = OperationFactory.sendOperation(MockConstants.xtz_1, of: MockConstants.tokenXTZ, from: MockConstants.defaultHdWallet.address, to: MockConstants.defaultLinearWallet.address)
public static var sendOperationWithReveal = [OperationReveal(wallet: MockConstants.defaultHdWallet), OperationTransaction(amount: MockConstants.xtz_1, source: MockConstants.defaultHdWallet.address, destination: MockConstants.defaultLinearWallet.address)]
public static let sendOperationPayload = OperationFactory.operationPayload(fromMetadata: MockConstants.operationMetadata, andOperations: MockConstants.sendOperations, walletAddress: MockConstants.defaultHdWallet.address, base58EncodedPublicKey: MockConstants.defaultHdWallet.publicKeyBase58encoded())
Expand Down

0 comments on commit e703138

Please sign in to comment.