Skip to content

Commit

Permalink
add sendNativeToken to web3Connection (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
moshmage authored Apr 20, 2023
1 parent e7422d9 commit 15c2d32
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/base/web3-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -34,8 +34,8 @@ export class Web3Connection {
(await this.eth?.givenProvider?.request({method: 'eth_requestAccounts'}) || [""])[0];
}

async getBalance(): Promise<string> {
return this.eth?.getBalance(await this.getAddress());
async getBalance(ofAddress?: string): Promise<string> {
return fromWei(await this.eth?.getBalance(ofAddress || await this.getAddress()));
}

async getETHNetworkId(): Promise<number> {
Expand Down Expand Up @@ -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())})
}

}
7 changes: 7 additions & 0 deletions test/base/web3-connection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
})
})

0 comments on commit 15c2d32

Please # to comment.