Skip to content

Instantly share code, notes, and snippets.

@fortmarek
fortmarek / end_function.swift
Created February 28, 2020 16:20
End function
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
@fortmarek
fortmarek / vote_function.swift
Last active February 28, 2020 16:17
Vote function
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
@fortmarek
fortmarek / end_entrypoint.py
Created February 28, 2020 16:06
End entrypoint
@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
@fortmarek
fortmarek / vote_entrypoint.py
Created February 28, 2020 16:05
Vote entrypoint
@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
@fortmarek
fortmarek / contract_init.py
Created February 28, 2020 16:02
Contract init
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:
@fortmarek
fortmarek / status-tezos.swift
Created February 28, 2020 09:52
Status of contract
// Closure-based function
func status(completion: @escaping RPCCompletion<RateContractStatus>)
// Combine counterpart
func statusPublisher() -> ContractPublisher<RateContractStatus>
private var urlSession: URLSession?
private func upload() {
if urlSession == nil {
let config = URLSessionConfiguration.background(withIdentifier: (Bundle.main.bundleIdentifier ?? "") + UUID().uuidString)
urlSession = URLSession(configuration: config, delegate: self, delegateQueue: nil)
}
}
extension ViewController: URLSessionDelegate, URLSessionTaskDelegate, URLSessionDataDelegate {
let testContractAddress = try! Address(describing: "0xb8f016F3529b198b4a06574f3E9BDc04948ad852")
query.helloContract(at: testContractAddress).testString(greetString: "Greetings!").send(using: key, amount: Wei(10)).startWithResult { result in
switch result {
case .success(let hash):
print(hash)
print("Test greetings succeeded!")
case .failure(let error):
print(error)
print("Test greetings failed.")
}
let walletStorage = KeychainStorageStrategy(identifier: "cz.ackee.etherkit.example")
let key = HDKey.Private(walletStorage, network: .rinkeby, path: [
KeyPathNode(at: 44, hardened: true),
KeyPathNode(at: 60, hardened: true),
KeyPathNode(at: 0, hardened: true),
KeyPathNode(at: 1),
])
HDKey.Private.create(
with: MnemonicStorageStrategy(walletStorage),
mnemonic: sentence,
network: .main,
path: [
KeyPathNode(at: 44, hardened: true),
KeyPathNode(at: 60, hardened: true),
KeyPathNode(at: 0, hardened: true),
KeyPathNode(at: 0),
]