diff --git a/src/base/web3-connection.ts b/src/base/web3-connection.ts index 4407f1ab..9a26a0ab 100644 --- a/src/base/web3-connection.ts +++ b/src/base/web3-connection.ts @@ -3,7 +3,7 @@ import Web3 from 'web3'; import {Account, provider as Provider} from 'web3-core'; import {HttpProviderOptions, WebsocketProviderOptions} from 'web3-core-helpers'; import {Web3ConnectionOptions} from '@interfaces/web3-connection-options'; -import {Utils} from 'web3-utils'; +import {fromWei, Utils} from 'web3-utils'; import {Eth} from 'web3-eth'; export class Web3Connection { @@ -34,8 +34,8 @@ export class Web3Connection { (await this.eth?.givenProvider?.request({method: 'eth_requestAccounts'}) || [""])[0]; } - async getBalance(): Promise { - return this.eth?.getBalance(await this.getAddress()); + async getBalance(ofAddress?: string): Promise { + return fromWei(await this.eth?.getBalance(ofAddress || await this.getAddress())); } async getETHNetworkId(): Promise { @@ -115,4 +115,8 @@ export class Web3Connection { } /* eslint-enable complexity */ + async sendNativeToken(to: string, amount: number) { + return this.eth.sendTransaction({from: await this.getAddress(), to, value: this.utils.toWei(amount.toString())}) + } + } diff --git a/test/base/web3-connection.spec.ts b/test/base/web3-connection.spec.ts index b8f17b13..3fc6fe59 100644 --- a/test/base/web3-connection.spec.ts +++ b/test/base/web3-connection.spec.ts @@ -43,5 +43,12 @@ describe(`Web3Connection`, () => { it(`Has restartModelOnDeploy as true by default`, () => { expect(web3Connection.options.restartModelOnDeploy).to.be.true; }) + + it(`Sends native token to another address`, async () => { + const AliceAddress = web3Connection.eth.accounts.privateKeyToAccount(getPrivateKeyFromFile(1))?.address; + const balance = await web3Connection.getBalance(AliceAddress); + await web3Connection.sendNativeToken(AliceAddress, 1); + expect(await web3Connection.getBalance(AliceAddress)).to.be.eq((+balance+1).toString()) + }) }) })