2021-04-14 14:07:49 +00:00
|
|
|
import std/math
|
|
|
|
import pkg/nitro
|
|
|
|
import pkg/questionable/results
|
|
|
|
import ../peercontext
|
|
|
|
|
|
|
|
export nitro
|
|
|
|
export results
|
|
|
|
export peercontext
|
|
|
|
|
|
|
|
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-04-19 14:37:38 +00:00
|
|
|
func getOrOpenChannel(wallet: WalletRef, peer: BitswapPeerCtx): ?!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-04-14 14:07:49 +00:00
|
|
|
peer: BitswapPeerCtx,
|
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"
|