This file contains hidden or 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
// Closure-based function | |
func status(completion: @escaping RPCCompletion<RateContractStatus>) | |
// Combine counterpart | |
func statusPublisher() -> ContractPublisher<RateContractStatus> |
This file contains hidden or 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
import smartpy as sp | |
class Ballot(sp.Contract): | |
def __init__(self, name, manager, candidates, voters, numberOfVotes): | |
initialBallot = {} | |
for candidate in candidates: | |
# Initializing initialBallot where we save `candidateName` and set `numberOfVotes` to zero | |
initialBallot[candidate[0]] = sp.record(candidateName = candidate[1], numberOfVotes = 0) | |
initialVoters = { } | |
for voter in voters: |
This file contains hidden or 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
@sp.entry_point | |
def vote(self, params): | |
#1 | |
sp.verify(self.data.hasEnded == False) | |
#2 | |
sp.verify(self.data.voters.contains(sp.sender)) | |
#3 | |
sp.for vote in params.votes.items(): | |
sp.verify(self.data.voters[sp.sender] >= vote.value) | |
self.data.ballot[vote.key].numberOfVotes += vote.value |
This file contains hidden or 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
@sp.entry_point | |
def end(self, params): | |
#1 | |
sp.verify(sp.sender == self.data.manager) | |
sp.verify(self.data.hasEnded == False) | |
sp.for candidate in self.data.ballot.keys(): | |
#2 | |
sp.if self.data.ballot[candidate].numberOfVotes != 0: | |
sp.send(candidate, sp.split_tokens(sp.balance, sp.as_nat(self.data.ballot[candidate].numberOfVotes), sp.as_nat(self.data.totalNumberOfVotes))) | |
self.data.hasEnded = True |
This file contains hidden or 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
private func vote(votes: [Candidate.ID: Int], contractAddress: String) { | |
#1 | |
guard let wallet = userRepository.state.value.wallet else { return } | |
let nonZeroVotes = votes.filter { $0.value != 0 } | |
#2 | |
tezosClient | |
.rateContract(at: contractAddress) | |
.vote(nonZeroVotes) | |
.callPublisher(from: wallet, amount: Tez(0)) | |
.handleEvents(receiveOutput: { [weak self] output in |
This file contains hidden or 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
private func endVote(_ contractAddress: String) { | |
guard let wallet = userRepository.state.value.wallet else { return } | |
tezosClient | |
.rateContract(at: contractAddress) | |
#1 | |
.end() | |
.callPublisher(from: wallet, amount: Tez(0)) | |
.handleEvents(receiveOutput: { [weak self] output in | |
print(output) | |
#2 |
This file contains hidden or 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
private func updateStore(of contract: String) { | |
tezosClient.rateContract(at: contract) | |
.statusPublisher() | |
.map(\.storage) | |
// Do something with the synced storage | |
} |
This file contains hidden or 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
import ArgumentParser | |
import Foundation | |
struct Scaffold: ParsableCommand { | |
@Argument() | |
var template: String | |
func run() throws { | |
} |
This file contains hidden or 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
struct Scaffold: ParsableCommand { | |
@Argument() | |
var template: String | |
} |
This file contains hidden or 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
extension ArgumentSet { | |
init(_ type: ParsableArguments.Type) { | |
let a: [ArgumentSet] = Mirror(reflecting: type.init()) | |
.children | |
.compactMap { child in | |
guard | |
var codingKey = child.label, | |
let parsed = child.value as? ArgumentSetProvider | |
else { return nil } |