nim-codex/tests/dagger/examples.nim

72 lines
1.9 KiB
Nim
Raw Normal View History

2021-04-07 10:19:00 +00:00
import std/random
import std/sequtils
import pkg/libp2p
2021-04-07 10:19:00 +00:00
import pkg/nitro
2022-03-23 12:57:48 +00:00
import pkg/stint
import pkg/dagger/rng
import pkg/dagger/stores
import pkg/dagger/blocktype
2022-03-23 12:57:48 +00:00
import pkg/dagger/purchasing
2021-04-07 10:19:00 +00:00
proc example*(_: type EthAddress): EthAddress =
EthPrivateKey.random().toPublicKey.toAddress
proc example*(_: type UInt256): UInt256 =
var bytes: array[32, byte]
for b in bytes.mitems:
b = rand(byte)
UInt256.fromBytes(bytes)
2021-04-07 14:53:00 +00:00
proc example*(_: type UInt48): UInt48 =
# workaround for https://github.com/nim-lang/Nim/issues/17670
uint64.rand mod (UInt48.high + 1)
2022-03-23 12:57:48 +00:00
proc example*[T: SomeInteger](_: type T): T =
rand(T)
proc example*[T,N](_: type array[N, T]): array[N, T] =
for item in result.mitems:
item = T.example
2021-04-08 13:05:04 +00:00
proc example*(_: type Wallet): Wallet =
Wallet.init(EthPrivateKey.random())
2021-04-19 14:37:38 +00:00
proc example*(_: type WalletRef): WalletRef =
WalletRef.new(EthPrivateKey.random())
proc example*(_: type SignedState): SignedState =
2021-04-08 13:05:04 +00:00
var wallet = Wallet.example
let hub, asset, receiver = EthAddress.example
let chainId, amount = UInt256.example
2021-04-07 14:53:00 +00:00
let nonce = UInt48.example
let channel = wallet.openLedgerChannel(hub, chainId, nonce, asset, amount).get
wallet.pay(channel, asset, receiver, amount).get
2021-04-08 07:45:41 +00:00
proc example*(_: type Pricing): Pricing =
Pricing(
address: EthAddress.example,
price: uint32.rand.u256
2021-04-08 07:45:41 +00:00
)
proc example*(_: type Block): Block =
let length = rand(4096)
let bytes = newSeqWith(length, rand(uint8))
Block.new(bytes).tryGet()
proc example*(_: type PeerId): PeerID =
let key = PrivateKey.random(Rng.instance[]).get
PeerId.init(key.getPublicKey().get).get
proc example*(_: type BlockExcPeerCtx): BlockExcPeerCtx =
BlockExcPeerCtx(id: PeerID.example)
2021-04-26 14:34:04 +00:00
proc example*(_: type Cid): Cid =
Block.example.cid
2022-03-23 12:57:48 +00:00
proc example*(_: type PurchaseRequest): PurchaseRequest =
PurchaseRequest(
duration: uint16.example.u256,
size: uint32.example.u256,
contentHash: array[32, byte].example
)