Skip to content

Commit

Permalink
Replace CTC string parsing with FixedNumber implementation; Export
Browse files Browse the repository at this point in the history
additional utilities from Ethers via creditcoin-js
  • Loading branch information
AdaJane committed Jun 22, 2023
1 parent a9c82b7 commit e179b91
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 35 deletions.
3 changes: 2 additions & 1 deletion creditcoin-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export * from './creditcoin-api';
export * from './types';
export * from './model';

export { providers, Wallet } from 'ethers';
export { providers, Wallet, FixedNumber, BigNumber } from 'ethers';
export { parseUnits } from 'ethers/lib/utils';
export { Guid } from 'js-guid';

export { ApiPromise, WsProvider, Keyring } from '@polkadot/api';
Expand Down
27 changes: 6 additions & 21 deletions scripts/cc-cli/src/utils/balance.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
import { BN } from "creditcoin-js";
import { BN, FixedNumber } from "creditcoin-js";

export const MICROUNITS_PER_CTC = new BN("1000000000000000000");

export function parseCTCString(amount: string): BN {
const split = amount.split(".");
const CTC = split[0];
const ctcInMicrounits = new BN(CTC).mul(MICROUNITS_PER_CTC);

let decimalInMicrounits = new BN(0);

if (split.length === 1) {
return ctcInMicrounits;
} else {
const decimal = amount.split(".")[1];
const decimals = decimal.length ? decimal.length : 0;
decimalInMicrounits = new BN(decimal)
.mul(MICROUNITS_PER_CTC)
.div(new BN(10).pow(new BN(decimals)));

return ctcInMicrounits.add(decimalInMicrounits);
try {
return new BN(FixedNumber.fromString(amount, 18).toString())
} catch (e) {
console.error(`Unable to parse CTC amount: ${amount}`)
process.exit(1)
}
}

export function toMicrounits(amount: number | BN): BN {
return new BN(amount).mul(MICROUNITS_PER_CTC);
}

export function toCTCString(amount: BN): string {
const CTC = amount.div(MICROUNITS_PER_CTC);
const remainder = amount.mod(MICROUNITS_PER_CTC);
Expand Down
26 changes: 13 additions & 13 deletions scripts/cc-cli/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@
"@babel/helper-plugin-utils" "^7.20.2"

"@babel/runtime@^7.20.13", "@babel/runtime@^7.20.6":
version "7.21.5"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.21.5.tgz#8492dddda9644ae3bda3b45eabe87382caee7200"
integrity sha512-8jI69toZqqcsnqGGqwGS4Qb1VwLOEp4hz+CXPywcvjs60u3B4Pom/U/7rm4W8tMOYEB+E9wgD0mW1l3r8qlI9Q==
version "7.22.5"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.22.5.tgz#8564dd588182ce0047d55d7a75e93921107b57ec"
integrity sha512-ecjvYlnAaZ/KVneE/OdKYBYfgXV3Ptu6zQWmgEF7vwKhQnvVS6bjMD2XYgj+SNvQ1GfK/pjgokfPkC/2CO8CuA==
dependencies:
regenerator-runtime "^0.13.11"

Expand Down Expand Up @@ -1472,17 +1472,17 @@
integrity sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==

"@types/node-fetch@^2.6.2":
version "2.6.3"
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.3.tgz#175d977f5e24d93ad0f57602693c435c57ad7e80"
integrity sha512-ETTL1mOEdq/sxUtgtOhKjyB2Irra4cjxksvcMUR5Zr4n+PxVhsCD9WS46oPbHL3et9Zde7CNRr+WUNlcHvsX+w==
version "2.6.4"
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.6.4.tgz#1bc3a26de814f6bf466b25aeb1473fa1afe6a660"
integrity sha512-1ZX9fcN4Rvkvgv4E6PAY5WXUFWFcRWxZa3EW83UjycOB9ljJCedb2CupIP4RZMEwF/M3eTcCihbBRgwtGbg5Rg==
dependencies:
"@types/node" "*"
form-data "^3.0.0"

"@types/node@*":
version "20.1.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.1.0.tgz#258805edc37c327cf706e64c6957f241ca4c4c20"
integrity sha512-O+z53uwx64xY7D6roOi4+jApDGFg0qn6WHcxe5QeqjMaTezBO/mxdfFXIVAVVyNWKx84OmPB3L8kbVYOTeN34A==
version "20.3.1"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.3.1.tgz#e8a83f1aa8b649377bb1fb5d7bac5cb90e784dfe"
integrity sha512-EhcH/wvidPy1WeML3TtYFGR83UzjxeWRen9V402T8aUGYsCHOmfoisV3ZSg03gAFIbLq8TnWOJ0f4cALtnSEUg==

"@types/prettier@^2.1.5":
version "2.7.2"
Expand Down Expand Up @@ -1971,7 +1971,7 @@ create-require@^1.1.0:

"creditcoin-js@file:../../creditcoin-js/creditcoin-js-v0.9.5.tgz":
version "0.9.5"
resolved "file:../../creditcoin-js/creditcoin-js-v0.9.5.tgz#db0b4ed75435b0886b4ecbebe34fe9eb8e6f6b1b"
resolved "file:../../creditcoin-js/creditcoin-js-v0.9.5.tgz#699b5c8cf04475e6455b689a6794e2b73947dced"
dependencies:
"@polkadot/api" "9.14.2"
ethers "^5.7.1"
Expand Down Expand Up @@ -3829,9 +3829,9 @@ tslib@^1.8.1:
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==

tslib@^2.1.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.5.0.tgz#42bfed86f5787aeb41d031866c8f402429e0fddf"
integrity sha512-336iVw3rtn2BUK7ORdIAHTyxHGRIHVReokCR3XjbckJMK7ms8FysBfhLR8IXnAgy7T0PTPNBWKiH514FOW/WSg==
version "2.5.3"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.5.3.tgz#24944ba2d990940e6e982c4bea147aba80209913"
integrity sha512-mSxlJJwl3BMEQCUNnxXBU9jP4JBktcEGhURcPR6VQVlnP0FdDEsIaz0C35dXNGLyRfrATNofF0F5p2KPxQgB+w==

tsutils@^3.21.0:
version "3.21.0"
Expand Down

0 comments on commit e179b91

Please sign in to comment.