-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from rbino/feat/get_account_balances_transfers
Add `get_account_balances` and `get_account_transfers`
- Loading branch information
Showing
10 changed files
with
491 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
defmodule TigerBeetlex.AccountFilterBatch do | ||
@moduledoc """ | ||
Account Filter Batch creation and manipulation. | ||
""" | ||
|
||
use TypedStruct | ||
|
||
typedstruct do | ||
field :ref, reference(), enforce: true | ||
end | ||
|
||
alias TigerBeetlex.AccountFilter | ||
alias TigerBeetlex.AccountFilterBatch | ||
alias TigerBeetlex.BatchFullError | ||
alias TigerBeetlex.InvalidBatchError | ||
alias TigerBeetlex.NifAdapter | ||
alias TigerBeetlex.OutOfMemoryError | ||
alias TigerBeetlex.Types | ||
|
||
@doc """ | ||
Creates a new account filter batch. | ||
""" | ||
@spec new(filter :: AccountFilter.t()) :: | ||
{:ok, t()} | {:error, Types.create_batch_error()} | {:error, Types.append_error()} | ||
def new(%AccountFilter{} = filter) do | ||
binary = AccountFilter.to_binary(filter) | ||
# 1 is the only valid value here - since that's what tigerbeetle expects | ||
# https://docs.tigerbeetle.com/reference/requests/#batching-events | ||
with {:ok, ref} <- NifAdapter.create_account_filter_batch(1), | ||
:ok <- NifAdapter.append_account_filter(ref, binary) do | ||
{:ok, %AccountFilterBatch{ref: ref}} | ||
end | ||
end | ||
|
||
@doc """ | ||
Creates a new account filter batch, rasing in case of an error. | ||
""" | ||
@spec new!(filter :: AccountFilter.t()) :: t() | ||
def new!(%AccountFilter{} = filter) do | ||
case new(filter) do | ||
{:ok, batch} -> batch | ||
{:error, :out_of_memory} -> raise OutOfMemoryError | ||
{:error, :invalid_batch} -> raise InvalidBatchError | ||
{:error, :batch_full} -> raise BatchFullError | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const std = @import("std"); | ||
const assert = std.debug.assert; | ||
|
||
const batch = @import("batch.zig"); | ||
const beam = @import("beam.zig"); | ||
const scheduler = beam.scheduler; | ||
|
||
const tb = @import("vsr").tigerbeetle; | ||
const AccountFilter = tb.AccountFilter; | ||
pub const AccountFilterBatch = batch.Batch(AccountFilter); | ||
pub const AccountFilterBatchResource = batch.BatchResource(AccountFilter); | ||
|
||
pub fn create(env: beam.Env, capacity: u32) beam.Term { | ||
return batch.create(AccountFilter, env, capacity) catch |err| switch (err) { | ||
error.OutOfMemory => return beam.make_error_atom(env, "out_of_memory"), | ||
}; | ||
} | ||
|
||
pub fn append( | ||
env: beam.Env, | ||
transfer_batch_resource: AccountFilterBatchResource, | ||
transfer_bytes: []const u8, | ||
) !beam.Term { | ||
if (transfer_bytes.len != @sizeOf(AccountFilter)) return beam.raise_badarg(env); | ||
|
||
return batch.append( | ||
AccountFilter, | ||
env, | ||
transfer_batch_resource, | ||
transfer_bytes, | ||
) catch |err| switch (err) { | ||
error.BatchFull => beam.make_error_atom(env, "batch_full"), | ||
error.LockFailed => return error.Yield, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.