-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c396c3d
commit a66e14b
Showing
5 changed files
with
80 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { useAccount } from '@gear-js/react-hooks'; | ||
import { web3FromSource } from '@polkadot/extension-dapp'; | ||
import { TransactionBuilder } from 'sails-js'; | ||
|
||
type FunctionName<T> = { | ||
[K in keyof T]: T[K] extends (...args: any[]) => TransactionBuilder<any> ? K : never; | ||
}[keyof T]; | ||
|
||
type NonServiceKeys = 'api' | 'registry' | 'programId' | 'newCtorFromCode' | 'newCtorFromCodeId'; | ||
|
||
function useTransaction< | ||
TProgram, | ||
TServiceName extends Exclude<keyof TProgram, NonServiceKeys>, | ||
TFunctionName extends FunctionName<TProgram[TServiceName]>, | ||
>(program: TProgram | undefined, serviceName: TServiceName, functionName: TFunctionName) { | ||
const { account } = useAccount(); | ||
|
||
type FunctionType = TProgram[TServiceName][TFunctionName] extends (...args: infer A) => TransactionBuilder<infer R> | ||
? (...args: A) => TransactionBuilder<R> | ||
: never; | ||
|
||
return async (...args: Parameters<FunctionType>) => { | ||
if (!program) throw new Error('Program is not found'); | ||
if (!account) throw new Error('Account is not found'); | ||
|
||
const transaction = (program[serviceName][functionName] as FunctionType)(...args) as ReturnType<FunctionType>; | ||
|
||
const { address, meta } = account; | ||
const { signer } = await web3FromSource(meta.source); | ||
transaction.withAccount(address, { signer }); | ||
|
||
return transaction; | ||
}; | ||
} | ||
|
||
export { useTransaction }; |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { useProgram, UseProgramParameters } from './use-program'; | ||
import { useTransaction } from './use-transaction'; | ||
|
||
export { useProgram }; | ||
export { useProgram, useTransaction }; | ||
export type { UseProgramParameters }; |
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,37 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { useAccount } from '@gear-js/react-hooks'; | ||
import { web3FromSource } from '@polkadot/extension-dapp'; | ||
import { TransactionBuilder } from 'sails-js'; | ||
|
||
type FunctionName<T> = { | ||
[K in keyof T]: T[K] extends (...args: any[]) => TransactionBuilder<any> ? K : never; | ||
}[keyof T]; | ||
|
||
type NonServiceKeys = 'api' | 'registry' | 'programId' | 'newCtorFromCode' | 'newCtorFromCodeId'; | ||
|
||
function useTransaction< | ||
TProgram, | ||
TServiceName extends Exclude<keyof TProgram, NonServiceKeys>, | ||
TFunctionName extends FunctionName<TProgram[TServiceName]>, | ||
>(program: TProgram | undefined, serviceName: TServiceName, functionName: TFunctionName) { | ||
const { account } = useAccount(); | ||
|
||
type FunctionType = TProgram[TServiceName][TFunctionName] extends (...args: infer A) => TransactionBuilder<infer R> | ||
? (...args: A) => TransactionBuilder<R> | ||
: never; | ||
|
||
return async (...args: Parameters<FunctionType>) => { | ||
if (!program) throw new Error('Program is not found'); | ||
if (!account) throw new Error('Account is not found'); | ||
|
||
const transaction = (program[serviceName][functionName] as FunctionType)(...args) as ReturnType<FunctionType>; | ||
|
||
const { address, meta } = account; | ||
const { signer } = await web3FromSource(meta.source); | ||
transaction.withAccount(address, { signer }); | ||
|
||
return transaction; | ||
}; | ||
} | ||
|
||
export { useTransaction }; |
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