2022-05-19 22:28:53 +00:00
|
|
|
## Nim-Codex
|
|
|
|
## Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2021-04-14 14:07:49 +00:00
|
|
|
import std/math
|
|
|
|
import pkg/nitro
|
|
|
|
import pkg/questionable/results
|
2022-05-19 02:29:15 +00:00
|
|
|
import ../peers
|
2021-04-14 14:07:49 +00:00
|
|
|
|
|
|
|
export nitro
|
|
|
|
export results
|
|
|
|
|
|
|
|
push: {.upraises: [].}
|
|
|
|
|
2021-04-22 08:11:24 +00:00
|
|
|
const ChainId* = 0.u256 # invalid chain id for now
|
|
|
|
const Asset* = EthAddress.zero # invalid ERC20 asset address for now
|
2021-04-14 14:27:17 +00:00
|
|
|
const AmountPerChannel = (10'u64^18).u256 # 1 asset, ERC20 default is 18 decimals
|
2021-04-14 14:07:49 +00:00
|
|
|
|
2021-04-19 14:37:38 +00:00
|
|
|
func openLedgerChannel*(wallet: WalletRef,
|
2021-04-14 14:07:49 +00:00
|
|
|
hub: EthAddress,
|
|
|
|
asset: EthAddress): ?!ChannelId =
|
|
|
|
wallet.openLedgerChannel(hub, ChainId, asset, AmountPerChannel)
|
|
|
|
|
2021-08-30 19:25:20 +00:00
|
|
|
func getOrOpenChannel(wallet: WalletRef, peer: BlockExcPeerCtx): ?!ChannelId =
|
2021-04-14 14:07:49 +00:00
|
|
|
if channel =? peer.paymentChannel:
|
2021-04-15 09:23:49 +00:00
|
|
|
success channel
|
2021-05-10 14:21:47 +00:00
|
|
|
elif account =? peer.account:
|
|
|
|
let channel = ?wallet.openLedgerChannel(account.address, Asset)
|
2021-04-14 14:07:49 +00:00
|
|
|
peer.paymentChannel = channel.some
|
2021-04-15 09:23:49 +00:00
|
|
|
success channel
|
2021-04-14 14:07:49 +00:00
|
|
|
else:
|
2021-05-10 14:21:47 +00:00
|
|
|
failure "no account set for peer"
|
2021-04-14 14:07:49 +00:00
|
|
|
|
2021-04-19 14:37:38 +00:00
|
|
|
func pay*(wallet: WalletRef,
|
2021-08-30 19:25:20 +00:00
|
|
|
peer: BlockExcPeerCtx,
|
2021-05-10 11:47:15 +00:00
|
|
|
amount: UInt256): ?!SignedState =
|
2021-05-10 14:21:47 +00:00
|
|
|
if account =? peer.account:
|
2021-04-22 08:11:24 +00:00
|
|
|
let asset = Asset
|
2021-05-10 14:21:47 +00:00
|
|
|
let receiver = account.address
|
2021-04-14 14:07:49 +00:00
|
|
|
let channel = ?wallet.getOrOpenChannel(peer)
|
|
|
|
wallet.pay(channel, asset, receiver, amount)
|
|
|
|
else:
|
2021-05-10 14:21:47 +00:00
|
|
|
failure "no account set for peer"
|