-
Notifications
You must be signed in to change notification settings - Fork 0
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 #39 from ainize-team/feature/yoojin/seperate_ain
Add ainModule
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 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,48 @@ | ||
import Ain from "@ainblockchain/ain-js"; | ||
import { getBlockChainEndpoint } from "./constants"; | ||
import { TransactionBody } from "@ainblockchain/ain-util"; | ||
|
||
// NOTE(yoojin): Plz suggest a good name. | ||
export default class AinModule { | ||
private ain?: Ain; | ||
private static instance: AinModule; | ||
|
||
static getInstance() { | ||
if (!AinModule.instance) { | ||
AinModule.instance = new AinModule(); | ||
} | ||
return AinModule.instance; | ||
} | ||
|
||
initAin(chainId: 0 | 1) { | ||
const blockchainEndpoint = getBlockChainEndpoint(chainId); | ||
this.ain = new Ain(blockchainEndpoint, chainId); | ||
} | ||
|
||
checkAinInitiated(): boolean { | ||
if (!this.ain) | ||
throw new Error('Set initAin(chainId) First.'); | ||
return true; | ||
} | ||
|
||
isDefaultAccountExist(): boolean { | ||
if (this.getDefaultAccount()) | ||
return false; | ||
return true; | ||
} | ||
|
||
setDefaultAccount(privateKey: string) { | ||
this.checkAinInitiated(); | ||
this.ain!.wallet.addAndSetDefaultAccount(privateKey); | ||
} | ||
|
||
getDefaultAccount() { | ||
this.checkAinInitiated(); | ||
return this.ain!.wallet.defaultAccount; | ||
} | ||
|
||
async sendTransaction(data: TransactionBody) { | ||
this.checkAinInitiated() | ||
return await this.ain!.sendTransaction(data); | ||
} | ||
} |