Skip to content

Commit

Permalink
Make well known gateways identified (#124)
Browse files Browse the repository at this point in the history
* Make well known gateways identified

* Pr changes
  • Loading branch information
micbakos-rdx authored May 2, 2024
1 parent 10e7e3f commit 7c989cd
Show file tree
Hide file tree
Showing 29 changed files with 425 additions and 347 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Foundation
import SargonUniFFI

extension Gateways {

public init(_ elements: [Element]) {
self = newGateways(gateways: elements)
}

public init(element: Element) {
self = newGatewaysWithGateway(gateway: element)
}

public var elements: [Element] {
gatewaysGetElements(gateways: self)
}

public func appending(_ element: Element) -> Self {
newGatewaysByAppending(gateway: element, to: self)
}

public func updatingOrInserting(element gateway: Element, at index: Int) -> Self {
newGatewaysByUpdatingOrInsertingAtIndex(gateway: gateway, to: self, index: UInt64(index))
}

public func updatingOrAppending(_ element: Element) -> Self {
newGatewaysByUpdatingOrAppending(gateway: element, to: self)
}

public func removing(_ id: Element.ID) -> Self {
newGatewaysRemovedById(idOfGateway: id, from: self)
}

public func removing(element: Element) -> Self {
newGatewaysRemovedElement(gateway: element, from: self)
}

public func get(id: Element.ID) -> Element? {
gatewaysGetGatewayById(gateways: self, id: id)
}

public var count: Int {
Int(gatewaysElementCount(gateways: self))
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension Gateway {
gatewayIsWellknown(gateway: self)
}

public static let wellknown: [Self] = gatewayWellknownGateways()
public static let wellknown: Gateways = gatewayWellknownGateways()

public init(url: String, networkID: NetworkID) throws {
self = try newGatewayWithUrlOnNetwork(url: url, networkId: networkID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
import Foundation
import SargonUniFFI

extension Gateways {
extension SavedGateways {

public init(current: Gateway) {
self = newGateways(current: current)
self = newSavedGateways(current: current)
}

public var all: [Gateway] {
gatewaysGetAllElements(gateways: self)
savedGatewaysGetAllElements(gateways: self)
}

public static let `default`: Self = newGatewaysDefault()
public static let `default`: Self = newSavedGatewaysDefault()

/// Swaps current and other gateways:
///
Expand All @@ -28,7 +28,7 @@ extension Gateways {
public mutating func changeCurrent(
to newCurrent: Gateway
) throws {
self = try newGatewaysChangingCurrent(
self = try newSavedGatewaysChangingCurrent(
to: newCurrent,
gateways: self
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
//
// File.swift
//
//
// Created by Alexander Cyon on 2024-04-15.
//

import Foundation
import SargonUniFFI

#if DEBUG
extension Gateways {
public static let sample: Self = newGatewaysSample()
public static let sampleOther: Self = newGatewaysSampleOther()
}
#endif
#endif // DEBUG

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import SargonUniFFI

#if DEBUG
extension SavedGateways {
public static let sample: Self = newSavedGatewaysSample()
public static let sampleOther: Self = newSavedGatewaysSampleOther()
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
import Foundation
import SargonUniFFI

extension OtherGateways: CanBeEmptyIdentifiedCollection {
extension Gateways: CanBeEmptyIdentifiedCollection {
public typealias Element = Gateway
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import SargonUniFFI

extension Gateways: SargonModel {}
extension Gateways {
extension SavedGateways: SargonModel {}
extension SavedGateways {
public static let preset: Self = .default
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import Sargon
import SargonUniFFI
import XCTest

final class OtherGatewaysTests: CanBeEmptyCollectionTest<OtherGateways> {}
final class GatewaysTests: CanBeEmptyCollectionTest<Gateways> {}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Sargon
import SargonUniFFI
import XCTest

final class GatewaysTests: Test<Gateways> {
final class SavedGatewaysTests: Test<SavedGateways> {
func test_preset_is_default() {
XCTAssertEqual(SUT.preset, SUT.default)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.Gateway
import com.radixdlt.sargon.Gateways
import com.radixdlt.sargon.NetworkId
import com.radixdlt.sargon.gatewayIsWellknown
import com.radixdlt.sargon.gatewayMainnet
Expand All @@ -16,7 +17,7 @@ val Gateway.Companion.mainnet: Gateway
val Gateway.Companion.stokenet: Gateway
get() = gatewayStokenet()

val Gateway.Companion.wellKnown: List<Gateway>
val Gateway.Companion.wellKnown: Gateways
get() = gatewayWellknownGateways()

@Throws(SargonException::class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,52 @@
@file:JvmName("GatewaysKt")

package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.Gateway
import com.radixdlt.sargon.Gateways
import com.radixdlt.sargon.gatewaysGetAllElements
import com.radixdlt.sargon.Url
import com.radixdlt.sargon.gatewaysElementCount
import com.radixdlt.sargon.gatewaysGetElements
import com.radixdlt.sargon.gatewaysGetGatewayById
import com.radixdlt.sargon.newGateways
import com.radixdlt.sargon.newGatewaysChangingCurrent
import com.radixdlt.sargon.newGatewaysDefault

/**
* Constructs [Gateways] with [current] set as active Gateway.
*/
fun Gateways.Companion.init(current: Gateway) = newGateways(current = current)

/**
* The default configuration for each new user:
* Current: mainnet
* Other: [ stokenet ]
*/
val Gateways.Companion.default
get() = newGatewaysDefault()

val Gateways.all: List<Gateway>
get() = gatewaysGetAllElements(gateways = this)

@Throws(SargonException::class)
fun Gateways.changeCurrent(newCurrent: Gateway) = newGatewaysChangingCurrent(
to = newCurrent,
gateways = this
)
import com.radixdlt.sargon.newGatewaysByAppending
import com.radixdlt.sargon.newGatewaysByUpdatingOrAppending
import com.radixdlt.sargon.newGatewaysByUpdatingOrInsertingAtIndex
import com.radixdlt.sargon.newGatewaysRemovedById
import com.radixdlt.sargon.newGatewaysRemovedElement

fun Gateways.Companion.init(vararg gateway: Gateway): Gateways =
init(gateway.asList())

fun Gateways.Companion.init(gateways: List<Gateway>): Gateways =
newGateways(gateways = gateways)

operator fun Gateways.invoke() = gatewaysGetElements(gateways = this)

operator fun Gateways.get(index: Int) = invoke().get(index = index)

operator fun Gateways.contains(element: Gateway) = invoke().contains(element = element)

val Gateways.size: Int
get() = gatewaysElementCount(gateways = this).toInt()

fun Gateways.append(gateway: Gateway): Gateways =
newGatewaysByAppending(gateway = gateway, to = this)

fun Gateways.updateOrInsert(gateway: Gateway, index: Int): Gateways =
newGatewaysByUpdatingOrInsertingAtIndex(
gateway = gateway,
to = this,
index = index.toULong()
)

fun Gateways.updateOrAppend(gateway: Gateway): Gateways =
newGatewaysByUpdatingOrAppending(gateway = gateway, to = this)

fun Gateways.removeByUrl(url: Url): Gateways =
newGatewaysRemovedById(idOfGateway = url, from = this)

fun Gateways.remove(gateway: Gateway): Gateways =
newGatewaysRemovedElement(gateway = gateway, from = this)

fun Gateways.getBy(url: Url): Gateway? = gatewaysGetGatewayById(gateways = this, id = url)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.radixdlt.sargon.extensions

import com.radixdlt.sargon.Gateway
import com.radixdlt.sargon.Gateways
import com.radixdlt.sargon.SavedGateways
import com.radixdlt.sargon.newSavedGateways
import com.radixdlt.sargon.newSavedGatewaysChangingCurrent
import com.radixdlt.sargon.newSavedGatewaysDefault
import com.radixdlt.sargon.savedGatewaysGetAllElements

/**
* Constructs [Gateways] with [current] set as active Gateway.
*/
fun SavedGateways.Companion.init(current: Gateway) = newSavedGateways(current = current)

/**
* The default configuration for each new user:
* Current: mainnet
* Other: [ stokenet ]
*/
val SavedGateways.Companion.default
get() = newSavedGatewaysDefault()

val SavedGateways.all: List<Gateway>
get() = savedGatewaysGetAllElements(gateways = this)

@Throws(SargonException::class)
fun SavedGateways.changeCurrent(newCurrent: Gateway) = newSavedGatewaysChangingCurrent(
to = newCurrent,
gateways = this
)
Loading

0 comments on commit 7c989cd

Please sign in to comment.