status-lib/status/stickers.nim

155 lines
6.4 KiB
Nim
Raw Normal View History

2021-09-08 18:05:39 +00:00
import # global deps
atomics, sequtils, strutils, tables
import # project deps
chronicles, web3/[ethtypes, conversions], stint
import # local deps
utils as status_utils,
2021-09-22 17:49:45 +00:00
statusgo_backend/settings as status_settings,
eth/contracts as eth_contracts,
eth/stickers as eth_stickers,
2021-09-22 17:49:45 +00:00
transactions,
statusgo_backend/wallet, ../eventemitter
2021-09-22 17:49:45 +00:00
import ./types/[sticker, transaction, rpc_response, network_type, network]
2021-09-08 18:05:39 +00:00
logScope:
topics = "stickers-model"
type
StickersModel* = ref object
events*: EventEmitter
recentStickers*: seq[Sticker]
availableStickerPacks*: Table[int, StickerPack]
installedStickerPacks*: Table[int, StickerPack]
purchasedStickerPacks*: seq[int]
StickerArgs* = ref object of Args
sticker*: Sticker
save*: bool
proc newStickersModel*(events: EventEmitter): StickersModel =
result = StickersModel()
result.events = events
result.recentStickers = @[]
result.availableStickerPacks = initTable[int, StickerPack]()
result.installedStickerPacks = initTable[int, StickerPack]()
result.purchasedStickerPacks = @[]
proc addStickerToRecent*(self: StickersModel, sticker: Sticker, save: bool = false) =
self.recentStickers.insert(sticker, 0)
self.recentStickers = self.recentStickers.deduplicate()
if self.recentStickers.len > 24:
self.recentStickers = self.recentStickers[0..23] # take top 24 most recent
if save:
eth_stickers.saveRecentStickers(self.recentStickers)
2021-09-08 18:05:39 +00:00
proc init*(self: StickersModel) =
self.events.on("stickerSent") do(e: Args):
var evArgs = StickerArgs(e)
self.addStickerToRecent(evArgs.sticker, evArgs.save)
2021-09-10 17:27:49 +00:00
proc buildTransaction(packId: Uint256, address: Address, price: Uint256, approveAndCall: var ApproveAndCall[100], sntContract: var Erc20Contract, gas = "", gasPrice = "", isEIP1559Enabled = false, maxPriorityFeePerGas = "", maxFeePerGas = ""): TransactionData =
2021-09-22 17:49:45 +00:00
let network = status_settings.getCurrentNetwork().toNetwork()
sntContract = eth_contracts.findErc20Contract(network.chainId, network.sntSymbol())
2021-09-08 18:05:39 +00:00
let
stickerMktContract = eth_contracts.findContract(network.chainId, "sticker-market")
2021-09-08 18:05:39 +00:00
buyToken = BuyToken(packId: packId, address: address, price: price)
buyTxAbiEncoded = stickerMktContract.methods["buyToken"].encodeAbi(buyToken)
approveAndCall = ApproveAndCall[100](to: stickerMktContract.address, value: price, data: DynamicBytes[100].fromHex(buyTxAbiEncoded))
2021-09-10 17:27:49 +00:00
transactions.buildTokenTransaction(address, sntContract.address, gas, gasPrice, isEIP1559Enabled, maxPriorityFeePerGas, maxFeePerGas)
2021-09-08 18:05:39 +00:00
proc estimateGas*(packId: int, address: string, price: string, success: var bool): int =
var
approveAndCall: ApproveAndCall[100]
2021-09-22 17:49:45 +00:00
network = status_settings.getCurrentNetwork().toNetwork()
sntContract = eth_contracts.findErc20Contract(network.chainId, network.sntSymbol())
2021-09-08 18:05:39 +00:00
tx = buildTransaction(
packId.u256,
status_utils.parseAddress(address),
status_utils.eth2Wei(parseFloat(price), sntContract.decimals),
2021-09-08 18:05:39 +00:00
approveAndCall,
sntContract
)
let response = sntContract.methods["approveAndCall"].estimateGas(tx, approveAndCall, success)
if success:
result = fromHex[int](response)
2021-09-10 17:27:49 +00:00
proc buyPack*(self: StickersModel, packId: int, address, price, gas, gasPrice: string, isEIP1559Enabled: bool, maxPriorityFeePerGas: string, maxFeePerGas: string, password: string, success: var bool): string =
2021-09-08 18:05:39 +00:00
var
sntContract: Erc20Contract
approveAndCall: ApproveAndCall[100]
tx = buildTransaction(
packId.u256,
status_utils.parseAddress(address),
status_utils.eth2Wei(parseFloat(price), 18), # SNT
2021-09-08 18:05:39 +00:00
approveAndCall,
sntContract,
gas,
2021-09-10 17:27:49 +00:00
gasPrice,
isEIP1559Enabled,
maxPriorityFeePerGas,
maxFeePerGas
2021-09-08 18:05:39 +00:00
)
result = sntContract.methods["approveAndCall"].send(tx, approveAndCall, password, success)
if success:
trackPendingTransaction(result, address, $sntContract.address, PendingTransactionType.BuyStickerPack, $packId)
proc getStickerMarketAddress*(self: StickersModel): Address =
2021-09-22 17:49:45 +00:00
let network = status_settings.getCurrentNetwork().toNetwork()
result = eth_contracts.findContract(network.chainId, "sticker-market").address
2021-09-08 18:05:39 +00:00
proc getPurchasedStickerPacks*(self: StickersModel, address: Address): seq[int] =
try:
let
2021-09-22 17:49:45 +00:00
network = status_settings.getCurrentNetwork().toNetwork()
balance = eth_stickers.getBalance(network.chainId, address)
tokenIds = toSeq[0..<balance].mapIt(eth_stickers.tokenOfOwnerByIndex(network.chainId, address, it.u256))
purchasedPackIds = tokenIds.mapIt(eth_stickers.getPackIdFromTokenId(network.chainId, it.u256))
2021-09-08 18:05:39 +00:00
self.purchasedStickerPacks = self.purchasedStickerPacks.concat(purchasedPackIds)
result = self.purchasedStickerPacks
except RpcException:
error "Error getting purchased sticker packs", message = getCurrentExceptionMsg()
result = @[]
proc getInstalledStickerPacks*(self: StickersModel): Table[int, StickerPack] =
if self.installedStickerPacks != initTable[int, StickerPack]():
return self.installedStickerPacks
self.installedStickerPacks = eth_stickers.getInstalledStickerPacks()
2021-09-08 18:05:39 +00:00
result = self.installedStickerPacks
2021-09-22 17:49:45 +00:00
proc getAvailableStickerPacks*(running: var Atomic[bool]): Table[int, StickerPack] =
let network = status_settings.getCurrentNetwork().toNetwork()
return eth_stickers.getAvailableStickerPacks(network.chainId, running)
2021-09-08 18:05:39 +00:00
proc getRecentStickers*(self: StickersModel): seq[Sticker] =
result = eth_stickers.getRecentStickers()
2021-09-08 18:05:39 +00:00
proc installStickerPack*(self: StickersModel, packId: int) =
if not self.availableStickerPacks.hasKey(packId):
return
let pack = self.availableStickerPacks[packId]
self.installedStickerPacks[packId] = pack
eth_stickers.saveInstalledStickerPacks(self.installedStickerPacks)
2021-09-08 18:05:39 +00:00
proc removeRecentStickers*(self: StickersModel, packId: int) =
self.recentStickers.keepItIf(it.packId != packId)
eth_stickers.saveRecentStickers(self.recentStickers)
2021-09-08 18:05:39 +00:00
proc uninstallStickerPack*(self: StickersModel, packId: int) =
if not self.installedStickerPacks.hasKey(packId):
return
let pack = self.availableStickerPacks[packId]
self.installedStickerPacks.del(packId)
eth_stickers.saveInstalledStickerPacks(self.installedStickerPacks)
2021-09-08 18:05:39 +00:00
proc decodeContentHash*(value: string): string =
result = status_utils.decodeContentHash(value)
2021-09-08 18:05:39 +00:00
proc getPackIdFromTokenId*(tokenId: Stuint[256]): int =
2021-09-22 17:49:45 +00:00
let network = status_settings.getCurrentNetwork().toNetwork()
result = eth_stickers.getPackIdFromTokenId(network.chainId, tokenId)