Alberto Cuesta Cañada
2 min readSep 5, 2019

--

Hi Steven,

if you import ABDK as a library from a contract it you would call its methods normally.

In my case, since I wanted to measure gas usage I had to call the methods from web3 with as little interference as possible, so I created a project that included ABDK.sol and a mock contract enveloping the library. I sent an event from each method to make them into transactions. Otherwise the methods from ABDK wouldn’t use gas.

pragma solidity ^0.5.0;import "../ABDKMath64x64.sol";contract ABDKMathMock {  event ValueCalculated(int256 output);  function fromUInt (uint256 x) public pure returns (int128) {    return ABDKMath64x64.fromUInt(x);  }  function add (int128 x, int128 y) public {    emit ValueCalculated(ABDKMath64x64.add(x, y));  }  function mul (int128 x, int128 y) public {    emit ValueCalculated(ABDKMath64x64.mul(x, y));  }  function log_2 (int128 x) public {    emit ValueCalculated(ABDKMath64x64.log_2(x));  }  function ln (int128 x) public {    emit ValueCalculated(ABDKMath64x64.ln(x));  }}

Then I called the methods in the mock contract from a web3 app:

const ABDKMathMock = artifacts.require('./ABDKMathMock.sol');const BigNumber = require('bignumber.js');const chai = require('chai');const { itShouldThrow } = require('./utils');// use default BigNumberchai.use(require('chai-bignumber')()).should();contract('ABDKMath', () => {  let abdkMathMock;  before(async () => {    abdkMathMock = await ABDKMathMock.deployed();  });  it('add(x, y)', async () => {    var values = [      new BigNumber(1).toString(10),      new BigNumber(11).toString(10),      new BigNumber(111).toString(10),      new BigNumber(1111).toString(10),      new BigNumber(11111).toString(10),      new BigNumber(111111000000000).toString(10),      new BigNumber(1111111000000000).toString(10),      new BigNumber(11111111000000000).toString(10),      new BigNumber(111111111000000000).toString(10),      new BigNumber(1111111111000000000).toString(10)];    for (var i = 0; i < values.length; i += 1){      var x = new BigNumber(
await (abdkMathMock.fromUInt(values[i]))).toString(10);
var y = new BigNumber(
await (abdkMathMock.fromUInt(
values[(values.length - 1) - i]
))).toString(10);
const transaction = await (abdkMathMock.add(x, y)); } });});

From there, eth-gas-reporter takes care of showing the gas usage.

--

--

Alberto Cuesta Cañada
Alberto Cuesta Cañada

Written by Alberto Cuesta Cañada

Blockchain Architect | Distributed and High Performance Computing Expert | Chaotic and Complex Systems Fanboy

No responses yet