49 lines
1.1 KiB
TypeScript
49 lines
1.1 KiB
TypeScript
import Web3 from 'web3'
|
|
import { useAppStore } from '@/store/app'
|
|
import { ERC20ABI } from '@/configs/contracts'
|
|
import pinia from '@/store';
|
|
|
|
const AppModule = useAppStore(pinia);
|
|
|
|
export class Chain {
|
|
private web3: Web3
|
|
|
|
constructor(provider: any) {
|
|
this.web3 = new Web3(provider)
|
|
}
|
|
|
|
public async initInstance({ abi, address, account }: {abi: any, address: string, account: string}) {
|
|
return new this.web3.eth.Contract(
|
|
abi,
|
|
address,
|
|
{ from: account }
|
|
)
|
|
}
|
|
|
|
public async initContractInstance(address: string, abi: any = ERC20ABI) {
|
|
return this.initInstance({
|
|
abi,
|
|
address,
|
|
account: AppModule.accountId
|
|
})
|
|
}
|
|
|
|
public async sendCmd({ method, params, from }: any) {
|
|
return new Promise((resolve, reject) => {
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
// @ts-ignore
|
|
this.web3.currentProvider.send({
|
|
method,
|
|
params,
|
|
from
|
|
}, async function(err: any, result: any) {
|
|
if (err) {
|
|
reject && reject(err)
|
|
return
|
|
}
|
|
resolve && resolve(result)
|
|
})
|
|
})
|
|
}
|
|
}
|