use safe erc20 & new logGas

This commit is contained in:
mingda
2020-09-08 20:09:31 +08:00
parent d01c51e85b
commit 2e637f0916
5 changed files with 37 additions and 24 deletions

View File

@@ -101,14 +101,14 @@ describe("DODO ETH PROXY", () => {
describe("buy&sell eth directly", () => {
it("buy", async () => {
const buyAmount = "1";
logGas(
await DODOEthProxy.methods
await logGas(
DODOEthProxy.methods
.buyEthWithToken(
ctx.QUOTE.options.address,
decimalStr(buyAmount),
decimalStr("200")
)
.send(ctx.sendParam(trader)),
),
ctx.sendParam(trader),
"buy ETH with token directly"
);
assert.strictEqual(
@@ -122,14 +122,14 @@ describe("DODO ETH PROXY", () => {
});
it("sell", async () => {
const sellAmount = "1";
logGas(
await DODOEthProxy.methods
await logGas(
DODOEthProxy.methods
.sellEthToToken(
ctx.QUOTE.options.address,
decimalStr(sellAmount),
decimalStr("50")
)
.send(ctx.sendParam(trader, sellAmount)),
),
ctx.sendParam(trader, sellAmount),
"sell ETH to token directly"
);
assert.strictEqual(

View File

@@ -103,14 +103,12 @@ describe("DODO ETH PROXY", () => {
const maxPayEthAmount = "2.1";
const ethInPoolBefore = decimalStr("10");
const traderEthBalanceBefore = await ctx.Web3.eth.getBalance(trader);
const txReceipt: TransactionReceipt = await DODOEthProxy.methods
const txReceipt: TransactionReceipt = await logGas(DODOEthProxy.methods
.buyTokenWithEth(
ctx.BASE.options.address,
decimalStr("200"),
decimalStr(maxPayEthAmount)
)
.send(ctx.sendParam(trader, maxPayEthAmount));
logGas(txReceipt, "buy token with ETH directly");
), ctx.sendParam(trader, maxPayEthAmount), "buy token with ETH directly");
const ethInPoolAfter = "12056338203652739553";
assert.strictEqual(
await ctx.DODO.methods._QUOTE_BALANCE_().call(),
@@ -134,14 +132,14 @@ describe("DODO ETH PROXY", () => {
});
it("sell", async () => {
const minReceiveEthAmount = "0.45";
logGas(
await DODOEthProxy.methods
await logGas(
DODOEthProxy.methods
.sellTokenToEth(
ctx.BASE.options.address,
decimalStr("50"),
decimalStr(minReceiveEthAmount)
)
.send(ctx.sendParam(trader)),
),
ctx.sendParam(trader),
"sell token to ETH directly"
);
assert.strictEqual(

View File

@@ -5,15 +5,15 @@
*/
import { TransactionReceipt } from "web3-core"
export const blueText = x => `\x1b[36m${x}\x1b[0m`;
export const yellowText = x => `\x1b[33m${x}\x1b[0m`;
export const greenText = x => `\x1b[32m${x}\x1b[0m`;
export const redText = x => `\x1b[31m${x}\x1b[0m`;
export const numberWithCommas = x => x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
export function logGas(receipt: TransactionReceipt, desc: string) {
export async function logGas(funcCall: any, params: any, desc: string) {
const estimatedGas = await funcCall.estimateGas(params)
const receipt = await funcCall.send(params)
const gasUsed = receipt.gasUsed;
let colorFn;
@@ -25,5 +25,6 @@ export function logGas(receipt: TransactionReceipt, desc: string) {
colorFn = redText;
}
console.log(("Gas used:").padEnd(60, '.'), blueText(desc) + " ", colorFn(numberWithCommas(gasUsed).padStart(5)));
console.log(("Gas estimated:" + numberWithCommas(estimatedGas)).padEnd(60, '.'), blueText(desc) + " ", colorFn(numberWithCommas(gasUsed).padStart(5)));
return receipt
}