-
Notifications
You must be signed in to change notification settings - Fork 31
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 #281 from soramitsu/hotfix/crowdloans
Private crowdloans & Bifrost flow support
- Loading branch information
Showing
35 changed files
with
601 additions
and
513 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,7 @@ | ||
import Foundation | ||
import FearlessUtils | ||
|
||
struct CrowdloanAddMemo: Codable { | ||
@StringCodable var index: ParaId | ||
@BytesCodable var memo: Data | ||
} |
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,24 @@ | ||
import Foundation | ||
|
||
@propertyWrapper | ||
public struct BytesCodable: Codable, Equatable { | ||
public var wrappedValue: Data | ||
|
||
public init(wrappedValue: Data) { | ||
self.wrappedValue = wrappedValue | ||
} | ||
|
||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.singleValueContainer() | ||
let byteArray = try container.decode([StringScaleMapper<UInt8>].self) | ||
|
||
wrappedValue = Data(byteArray.map(\.value)) | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.singleValueContainer() | ||
let bytes = wrappedValue.map { StringScaleMapper(value: $0) } | ||
|
||
try container.encode(bytes) | ||
} | ||
} |
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
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
54 changes: 54 additions & 0 deletions
54
fearless/Modules/Crowdloan/CustomCrowdloan/Bifrost/BifrostBonusService.swift
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,54 @@ | ||
import Foundation | ||
import RobinHood | ||
import FearlessUtils | ||
import BigInt | ||
|
||
final class BifrostBonusService { | ||
static let defaultReferralCode = "FRLS69" | ||
|
||
var bonusRate: Decimal { 0.05 } | ||
|
||
var termsURL: URL { | ||
URL(string: "https://docs.google.com/document/d/1PDpgHnIcAmaa7dEFusmLYgjlvAbk2VKtMd755bdEsf4/edit?usp=sharing")! | ||
} | ||
|
||
private(set) var referralCode: String? | ||
|
||
let paraId: ParaId | ||
|
||
init(paraId: ParaId) { | ||
self.paraId = paraId | ||
} | ||
} | ||
|
||
extension BifrostBonusService: CrowdloanBonusServiceProtocol { | ||
func save(referralCode: String, completion closure: @escaping (Result<Void, Error>) -> Void) { | ||
guard let codeData = referralCode.data(using: .utf8), codeData.count <= 32 else { | ||
closure(.failure(CrowdloanBonusServiceError.invalidReferral)) | ||
return | ||
} | ||
|
||
self.referralCode = referralCode | ||
closure(.success(())) | ||
} | ||
|
||
func applyOffchainBonusForContribution( | ||
amount _: BigUInt, | ||
with closure: @escaping (Result<Void, Error>) -> Void | ||
) { | ||
closure(.success(())) | ||
} | ||
|
||
func applyOnchainBonusForContribution( | ||
amount _: BigUInt, | ||
using builder: ExtrinsicBuilderProtocol | ||
) throws -> ExtrinsicBuilderProtocol { | ||
guard let memo = referralCode?.data(using: .utf8) else { | ||
return builder | ||
} | ||
|
||
let addMemo = SubstrateCallFactory().addMemo(to: paraId, memo: memo) | ||
|
||
return try builder.adding(call: addMemo) | ||
} | ||
} |
11 changes: 10 additions & 1 deletion
11
fearless/Modules/Crowdloan/CustomCrowdloan/CrowdloanBonusService.swift
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 |
---|---|---|
@@ -1,11 +1,20 @@ | ||
import Foundation | ||
import BigInt | ||
import FearlessUtils | ||
|
||
protocol CrowdloanBonusServiceProtocol: AnyObject { | ||
var bonusRate: Decimal { get } | ||
var termsURL: URL { get } | ||
var referralCode: String? { get } | ||
|
||
func save(referralCode: String, completion closure: @escaping (Result<Void, Error>) -> Void) | ||
func applyBonusForContribution(amount: BigUInt, with closure: @escaping (Result<Void, Error>) -> Void) | ||
func applyOffchainBonusForContribution( | ||
amount: BigUInt, | ||
with closure: @escaping (Result<Void, Error>) -> Void | ||
) | ||
|
||
func applyOnchainBonusForContribution( | ||
amount: BigUInt, | ||
using builder: ExtrinsicBuilderProtocol | ||
) throws -> ExtrinsicBuilderProtocol | ||
} |
27 changes: 27 additions & 0 deletions
27
fearless/Modules/Crowdloan/CustomCrowdloan/CrowdloanBonusServiceError.swift
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,27 @@ | ||
import Foundation | ||
|
||
enum CrowdloanBonusServiceError: Error, ErrorContentConvertible { | ||
case invalidReferral | ||
case internalError | ||
case veficationFailed | ||
|
||
func toErrorContent(for locale: Locale?) -> ErrorContent { | ||
switch self { | ||
case .invalidReferral: | ||
return ErrorContent( | ||
title: R.string.localizable.commonErrorGeneralTitle(preferredLanguages: locale?.rLanguages), | ||
message: R.string.localizable.crowdloanReferralCodeInvalid(preferredLanguages: locale?.rLanguages) | ||
) | ||
case .internalError: | ||
return ErrorContent( | ||
title: R.string.localizable.commonErrorGeneralTitle(preferredLanguages: locale?.rLanguages), | ||
message: R.string.localizable.crowdloanReferralCodeInternal(preferredLanguages: locale?.rLanguages) | ||
) | ||
case .veficationFailed: | ||
return ErrorContent( | ||
title: R.string.localizable.commonErrorGeneralTitle(preferredLanguages: locale?.rLanguages), | ||
message: R.string.localizable.crowdloanBonusVerificationError(preferredLanguages: locale?.rLanguages) | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.