-
Notifications
You must be signed in to change notification settings - Fork 11
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 #64 from NillionNetwork/feat/top-up-payment-hooks
feat: add hooks for top-up payments operations
- Loading branch information
Showing
9 changed files
with
134 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
"name": "@nillion/client-react-hooks", | ||
"license": "MIT", | ||
"author": "[email protected]", | ||
"version": "0.2.1", | ||
"version": "0.3.0-rc.0", | ||
"homepage": "https://nillion.pub/client-ts", | ||
"repository": { | ||
"type": "git", | ||
|
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,29 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { type UseNilHook, nilHookBaseResult } from "./nil-hook-base"; | ||
import { useNillion } from "./use-nillion"; | ||
|
||
type ExecuteArgs = undefined; | ||
type ExecuteResult = bigint; | ||
|
||
type UseNilAccountBalance = UseNilHook<ExecuteArgs, ExecuteResult>; | ||
|
||
export const useNilAccountBalance = (): UseNilAccountBalance => { | ||
const { client } = useNillion(); | ||
|
||
const mutationFn = async (): Promise<ExecuteResult> => { | ||
const response = await client.payer.accountBalance(); | ||
return response.balance; | ||
}; | ||
|
||
const mutate = useMutation({ | ||
mutationFn, | ||
}); | ||
|
||
return { | ||
execute: (): void => { | ||
mutate.mutate(); | ||
}, | ||
executeAsync: async (): Promise<ExecuteResult> => mutate.mutateAsync(), | ||
...nilHookBaseResult(mutate), | ||
}; | ||
}; |
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,31 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { type UseNilHook, nilHookBaseResult } from "./nil-hook-base"; | ||
import { useNillion } from "./use-nillion"; | ||
|
||
type ExecuteArgs = { | ||
amount: bigint; | ||
}; | ||
type ExecuteResult = undefined; | ||
|
||
type UseNilAddFunds = UseNilHook<ExecuteArgs, ExecuteResult>; | ||
|
||
export const useNilAddFunds = (): UseNilAddFunds => { | ||
const { client } = useNillion(); | ||
|
||
const mutationFn = async (args: ExecuteArgs): Promise<ExecuteResult> => { | ||
await client.payer.addFunds(args.amount); | ||
}; | ||
|
||
const mutate = useMutation({ | ||
mutationFn, | ||
}); | ||
|
||
return { | ||
execute: (args: ExecuteArgs): void => { | ||
mutate.mutate(args); | ||
}, | ||
executeAsync: async (args: ExecuteArgs): Promise<ExecuteResult> => | ||
mutate.mutateAsync(args), | ||
...nilHookBaseResult(mutate), | ||
}; | ||
}; |
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,31 @@ | ||
"use client"; | ||
|
||
import { useNilAccountBalance } from "@nillion/client-react-hooks"; | ||
import type { FC } from "react"; | ||
|
||
export const AccountBalance: FC = () => { | ||
const mutation = useNilAccountBalance(); | ||
|
||
let data = ""; | ||
if (mutation.isSuccess) { | ||
// stringify cannot handle BigInts | ||
data = JSON.stringify(mutation.data, (_, v) => | ||
typeof v === "bigint" ? v.toString() : v, | ||
); | ||
} else if (mutation.isError) { | ||
data = mutation.error.message; | ||
} | ||
|
||
return ( | ||
<div> | ||
<h2>Account Balance</h2> | ||
<ol> | ||
<li>Status: {mutation.status}</li> | ||
<li>Result: {data}</li> | ||
</ol> | ||
<button type="button" onClick={(): void => mutation.execute()}> | ||
Execute | ||
</button> | ||
</div> | ||
); | ||
}; |
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,30 @@ | ||
"use client"; | ||
|
||
import { useNilAddFunds } from "@nillion/client-react-hooks"; | ||
import type { FC } from "react"; | ||
|
||
export const AddFunds: FC = () => { | ||
const mutation = useNilAddFunds(); | ||
|
||
let result = ""; | ||
if (mutation.isSuccess) { | ||
result = "Account has been funded"; | ||
} else if (mutation.isError) { | ||
result = mutation.error.message; | ||
} | ||
|
||
const args = { amount: BigInt(10) }; | ||
|
||
return ( | ||
<div> | ||
<h2>Add Funds</h2> | ||
<ol> | ||
<li>Status: {mutation.status}</li> | ||
<li>Result: {result}</li> | ||
</ol> | ||
<button type="button" onClick={(): void => mutation.execute(args)}> | ||
Execute | ||
</button> | ||
</div> | ||
); | ||
}; |
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