Skip to content

Commit

Permalink
reset lastBlock on config change
Browse files Browse the repository at this point in the history
add method estimate gasLimit for token transfers
  • Loading branch information
ffox77 committed Sep 3, 2021
1 parent 738b3ca commit af6f9e2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/dag4-xchain-ethereum/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stardust-collective/dag4-xchain-ethereum",
"version": "0.1.12",
"version": "0.1.13",
"description": "Cross chain communication with Ethereum",
"author": "Frank Fox",
"email": "ffox77@gmail.com",
Expand Down
1 change: 1 addition & 0 deletions packages/dag4-xchain-ethereum/src/account-tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class AccountTracker {
clearTimeout(this.timeoutId);
this.isRunning = false;
this.provider = null;
this.lastBlock = null;
}

private async getTokenBalances () {
Expand Down
15 changes: 15 additions & 0 deletions packages/dag4-xchain-ethereum/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { Network} from '@xchainjs/xchain-client';
import {BigNumber, ethers, FixedNumber} from 'ethers';
import {Client} from '@xchainjs/xchain-ethereum';
import {tokenContractService} from './token-contract-service';
import ERC_20_ABI from 'erc-20-abi';

import * as utils from '@xchainjs/xchain-util';
import {formatUnits} from "ethers/lib/utils";

export {utils};

Expand Down Expand Up @@ -32,6 +34,19 @@ export class XChainEthClient extends Client {
this['changeWallet'](new ethers.Wallet(privateKey, this.getProvider()));
}

async estimateTokenTransferGasLimit (recipient: string, contractAddress: string, txAmount: BigNumber, defaultValue?: number) {

try {
const contract = new ethers.Contract(contractAddress, ERC_20_ABI, this.getProvider());
const gasLimit: BigNumber = await contract.estimateGas.transfer(recipient, txAmount, {from: this.getAddress()});

return gasLimit.toNumber();
}
catch(e) {
return defaultValue;
}
}

isValidEthereumAddress (address: string) {
return ethers.utils.isAddress(address);
}
Expand Down
18 changes: 11 additions & 7 deletions packages/dag4-xchain-ethereum/test/nodejs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const xchainUtil = require("@xchainjs/xchain-util");
const {getTokenAddress} = require("@xchainjs/xchain-ethereum");

const {XChainEthClient} = require("../../dist/cjs/client");
const {ethers} = require("ethers");

const PRIVATE_KEY = "114d7d51019c7f704388e03fb5c74174f27e2fc68ac88c82832ec9b1174b17fd";

Expand Down Expand Up @@ -29,23 +30,25 @@ async function testGetTokenBalance() {
const tokenInfo =
{ address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', symbol: 'USDC', decimals: 6 };


const tokens = await ethClient.getTokenBalance('0xcd4328383abc5399a910b7e01c8047d95b3afa8a', tokenInfo);

console.log(JSON.stringify(tokens, null, 2))

}

async function testGetTokenBalanceTestnet() {
async function getEthBalance () {
const provider = new ethers.providers.InfuraProvider();
const result = await provider.getBalance('0x70504ae3853e42533a22BBAA96915Af95178aBe6');
console.log(ethers.utils.formatEther(result));
}

const tokenInfo =
{ address: '0xeb349b537d77eec95d2761177c7581d6535630a1', symbol: 'FOO', decimals: 18 };
async function testGetTokenBalanceTestnet() {

const tokenInfo = { address: '0xeb349b537d77eec95d2761177c7581d6535630a1', symbol: 'FOO', decimals: 18 };

const tokens = await ethClient.getTokenBalance('0xC769323999C7b5cAD4c125bE0F33e83Ee4FB25c0', tokenInfo, 3);

console.log(JSON.stringify(tokens, null, 2))

}

async function testGetTokenInfo() {
Expand All @@ -66,5 +69,6 @@ async function testXChainUtils () {
}

// testXChainUtils();
testGetTokenBalance();
testGetTokenInfo()
// testGetTokenBalance();
// testGetTokenInfo()
getEthBalance();

0 comments on commit af6f9e2

Please sign in to comment.