@galacticcouncil/common 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.cjs +1 -1
- package/build/index.mjs +1 -1
- package/build/types/utils/big.d.ts +4 -2
- package/build/types/utils/enums.d.ts +6 -0
- package/build/types/utils/hex.d.ts +5 -0
- package/build/types/utils/index.d.ts +4 -0
- package/build/types/utils/xcm.d.ts +18 -0
- package/package.json +3 -12
- package/src/consts.ts +0 -5
- package/src/index.ts +0 -8
- package/src/utils/big.spec.ts +0 -73
- package/src/utils/big.ts +0 -26
- package/src/utils/erc20.ts +0 -29
- package/src/utils/h160.spec.ts +0 -39
- package/src/utils/h160.ts +0 -79
- package/src/utils/index.ts +0 -4
- package/src/utils/xc.spec.ts +0 -8
- package/src/utils/xc.ts +0 -7
package/build/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var K=Object.create;var p=Object.defineProperty;var O=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var L=Object.getPrototypeOf,M=Object.prototype.hasOwnProperty;var u=(t,r)=>{for(var e in r)p(t,e,{get:r[e],enumerable:!0})},k=(t,r,e,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of j(r))!M.call(t,o)&&o!==e&&p(t,o,{get:()=>r[o],enumerable:!(n=O(r,o))||n.enumerable});return t};var $=(t,r,e)=>(e=t!=null?K(L(t)):{},k(r||!t||!t.__esModule?p(e,"default",{value:t,enumerable:!0}):e,t)),G=t=>k(p({},"__esModule",{value:!0}),t);var et={};u(et,{HYDRATION_SS58_PREFIX:()=>m,RUNTIME_DECIMALS:()=>ot,big:()=>y,encodeAssetId:()=>I,encodeLocation:()=>E,enums:()=>d,erc20:()=>g,h160:()=>b,hex:()=>v,transform:()=>i,version:()=>tt,xc:()=>S,xcm:()=>w});module.exports=G(et);var ot=18,m=63;var y={};u(y,{asBigInt:()=>q,convertDecimals:()=>z,hasDecimalOverflow:()=>C,toBigInt:()=>F,toDecimal:()=>H});var f=$(require("big.js"));f.default.NE=-18;function H(t,r,e=6,n){let o=(0,f.default)(t.toString().replace(/[^0-9]/g,"")),l=(0,f.default)(10).pow(r);return o.div(l).round(e,n).toString()}function F(t,r){if(typeof t=="bigint")return t;let e=(0,f.default)(10).pow(r),n=(0,f.default)(t).mul(e);return BigInt(n.toFixed(0,f.default.roundDown))}function q(t){return BigInt(t.round(0,f.default.roundDown).toFixed(0))}function z(t,r,e){let n=H(t,r,r);return F(n.toString(),e)}function C(t,r){let e=t.toString().split(".");return e.length>1&&e[1].length>r}var d={};u(d,{enumPath:()=>W});function W(t){let r=[],e=t;for(;e&&typeof e=="object"&&"type"in e;)r.push(e.type),e=e.value;return r.join(".")}var g={};u(g,{ERC20:()=>x});var x=class{static fromAssetId(r){let e=Buffer.alloc(20,0);return e[15]=1,e.writeUInt32BE(r,16),"0x"+e.toString("hex")}static toAssetId(r){let e=Buffer.from(r.replace("0x",""),"hex");return e.length!==20||!this.isAssetAddress(r)?null:e.readUInt32BE(16)}static isAssetAddress(r){let e=Buffer.from("0000000000000000000000000000000100000000","hex"),n=Buffer.from(r.replace("0x",""),"hex");return n.length!==20?!1:n.subarray(0,16).equals(e.subarray(0,16))}};var b={};u(b,{H160:()=>A,isEvmAccount:()=>R,isEvmAddress:()=>N,isSs58Address:()=>P});var a=require("polkadot-api"),B=require("@polkadot-api/utils"),s=require("buffer");var h="ETH\0";function R(t){if(!t)return!1;try{let r=(0,a.AccountId)().enc(t),e=s.Buffer.from(h);return s.Buffer.from(r.subarray(0,e.length)).equals(e)}catch{return!1}}function N(t){return!!/^0x[a-fA-F0-9]{40}$/.test(t)}function P(t){try{return(0,a.AccountId)(63).enc(t),!0}catch{return!1}}var A=class t{static toAccount=r=>{let e=s.Buffer.from(r.slice(2),"hex"),n=s.Buffer.from(h),o=Uint8Array.from(s.Buffer.concat([n,e,s.Buffer.alloc(8)])),l=(0,B.toHex)(o);return(0,a.AccountId)(63).dec(l)};static fromAccount=r=>{let e=(0,a.AccountId)().enc(r),n=s.Buffer.from(h),o=e.slice(n.length,-8);return"0x"+s.Buffer.from(o).toString("hex")};static fromSS58=r=>{let n=(0,a.AccountId)().enc(r).slice(0,20);return(0,B.toHex)(n)};static fromAny=r=>{if(N(r))return r;if(R(r))return t.fromAccount(r);if(P(r))return t.fromSS58(r);throw new Error("Unknown address type")}};var v={};u(v,{assertHexLen:()=>V,ensureHexPrefix:()=>_,hexNormalize:()=>Q,isHex:()=>D,stripHexPrefix:()=>J});var Y=/^0x[0-9a-fA-F]*$/;function D(t,r=!1){return t==="0x"?!0:!Y.test(t)||!r&&t.length<4?!1:r||t.length%2===0}function J(t){return t.startsWith("0x")?t.slice(2):t}function _(t){return t.startsWith("0x")?t:`0x${t}`}function Q(t){return _(t.toLowerCase())}function V(t,r){if(!D(t)||t.length-2!==r*2)throw new Error(`Expected 0x hex of ${r} bytes`)}var w={};u(w,{encodeAssetId:()=>I,encodeLocation:()=>E,toAccountId32:()=>U,toAccountKey20:()=>T,transform:()=>i});var c=require("polkadot-api"),U=t=>{let r=(0,c.AccountId)().enc(t);return{type:"AccountId32",value:{network:void 0,id:c.FixedSizeBinary.fromBytes(r)}}},T=t=>({type:"AccountKey20",value:{network:void 0,key:c.FixedSizeBinary.fromHex(t)}}),i=t=>{if(Array.isArray(t))return t.map(i);if(typeof t=="object"&&t!==null){let r=Object.keys(t);if(r.length===1){let e=r[0],n=t[e];if(e==="AccountKey20"&&typeof n=="object"){let o=T(n.key);return n.network&&(o.value.network=i(n.network)),o}if(e==="AccountId32"&&typeof n=="object"){let o=U(n.id);return n.network&&(o.value.network=i(n.network)),o}return e==="GeneralKey"&&typeof n=="object"&&"data"in n?{type:e,value:{length:n.length,data:typeof n.data=="string"?c.Binary.fromHex(n.data):n.data}}:e==="GlobalConsensus"&&typeof n=="object"&&("Polkadot"in n||"Kusama"in n)?{type:e,value:n}:e==="Ethereum"?{type:e,value:{chain_id:BigInt("chainId"in n?n.chainId:n.chain_id)}}:e==="PalletInstance"?{type:e,value:n}:e==="GeneralIndex"?{type:e,value:BigInt(n)}:e==="X1"&&Array.isArray(n)?{type:e,value:n.length===1?i(n[0]):n.map(i)}:typeof n=="string"?{type:e,value:{type:n}}:typeof n=="number"?{type:e,value:n}:{type:e,value:i(n)}}else{let e={};for(let n of r){let o=t[n];e[n]=i(o)}return e}}else return t};function E(t){if(!t||typeof t!="object")return t;let{parents:r,interior:e}=t;return!e||e==="Here"?{parents:r,interior:{type:"Here"}}:{parents:r,interior:i(e)}}function I(t){if(typeof t=="string")return BigInt(t);if(typeof t=="number"||typeof t=="bigint"||t==null)return t;if(typeof t=="object"&&!Array.isArray(t))return i(t)}var S={};u(S,{convertToId:()=>Z});var X=require("buffer");function Z(t){let e=X.Buffer.from(t.replace("0x",""),"hex").subarray(16);return e.readUIntBE(0,e.length)}var tt="0.1.0";0&&(module.exports={HYDRATION_SS58_PREFIX,RUNTIME_DECIMALS,big,encodeAssetId,encodeLocation,enums,erc20,h160,hex,transform,version,xc,xcm});
|
package/build/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var U=Object.defineProperty;var u=(t,r)=>{for(var e in r)U(t,e,{get:r[e],enumerable:!0})};var J=18,p=63;var g={};u(g,{asBigInt:()=>T,convertDecimals:()=>X,hasDecimalOverflow:()=>K,toBigInt:()=>x,toDecimal:()=>d});import f from"big.js";f.NE=-18;function d(t,r,e=6,n){let o=f(t.toString().replace(/[^0-9]/g,"")),a=f(10).pow(r);return o.div(a).round(e,n).toString()}function x(t,r){if(typeof t=="bigint")return t;let e=f(10).pow(r),n=f(t).mul(e);return BigInt(n.toFixed(0,f.roundDown))}function T(t){return BigInt(t.round(0,f.roundDown).toFixed(0))}function X(t,r,e){let n=d(t,r,r);return x(n.toString(),e)}function K(t,r){let e=t.toString().split(".");return e.length>1&&e[1].length>r}var B={};u(B,{enumPath:()=>O});function O(t){let r=[],e=t;for(;e&&typeof e=="object"&&"type"in e;)r.push(e.type),e=e.value;return r.join(".")}var h={};u(h,{ERC20:()=>l});var l=class{static fromAssetId(r){let e=Buffer.alloc(20,0);return e[15]=1,e.writeUInt32BE(r,16),"0x"+e.toString("hex")}static toAssetId(r){let e=Buffer.from(r.replace("0x",""),"hex");return e.length!==20||!this.isAssetAddress(r)?null:e.readUInt32BE(16)}static isAssetAddress(r){let e=Buffer.from("0000000000000000000000000000000100000000","hex"),n=Buffer.from(r.replace("0x",""),"hex");return n.length!==20?!1:n.subarray(0,16).equals(e.subarray(0,16))}};var I={};u(I,{H160:()=>y,isEvmAccount:()=>b,isEvmAddress:()=>v,isSs58Address:()=>E});import{AccountId as c}from"polkadot-api";import{toHex as A}from"@polkadot-api/utils";import{Buffer as s}from"buffer";var m="ETH\0";function b(t){if(!t)return!1;try{let r=c().enc(t),e=s.from(m);return s.from(r.subarray(0,e.length)).equals(e)}catch{return!1}}function v(t){return!!/^0x[a-fA-F0-9]{40}$/.test(t)}function E(t){try{return c(63).enc(t),!0}catch{return!1}}var y=class t{static toAccount=r=>{let e=s.from(r.slice(2),"hex"),n=s.from(m),o=Uint8Array.from(s.concat([n,e,s.alloc(8)])),a=A(o);return c(63).dec(a)};static fromAccount=r=>{let e=c().enc(r),n=s.from(m),o=e.slice(n.length,-8);return"0x"+s.from(o).toString("hex")};static fromSS58=r=>{let n=c().enc(r).slice(0,20);return A(n)};static fromAny=r=>{if(v(r))return r;if(b(r))return t.fromAccount(r);if(E(r))return t.fromSS58(r);throw new Error("Unknown address type")}};var k={};u(k,{assertHexLen:()=>$,ensureHexPrefix:()=>S,hexNormalize:()=>M,isHex:()=>w,stripHexPrefix:()=>L});var j=/^0x[0-9a-fA-F]*$/;function w(t,r=!1){return t==="0x"?!0:!j.test(t)||!r&&t.length<4?!1:r||t.length%2===0}function L(t){return t.startsWith("0x")?t.slice(2):t}function S(t){return t.startsWith("0x")?t:`0x${t}`}function M(t){return S(t.toLowerCase())}function $(t,r){if(!w(t)||t.length-2!==r*2)throw new Error(`Expected 0x hex of ${r} bytes`)}var D={};u(D,{encodeAssetId:()=>P,encodeLocation:()=>N,toAccountId32:()=>F,toAccountKey20:()=>R,transform:()=>i});import{AccountId as G,Binary as q,FixedSizeBinary as H}from"polkadot-api";var F=t=>{let r=G().enc(t);return{type:"AccountId32",value:{network:void 0,id:H.fromBytes(r)}}},R=t=>({type:"AccountKey20",value:{network:void 0,key:H.fromHex(t)}}),i=t=>{if(Array.isArray(t))return t.map(i);if(typeof t=="object"&&t!==null){let r=Object.keys(t);if(r.length===1){let e=r[0],n=t[e];if(e==="AccountKey20"&&typeof n=="object"){let o=R(n.key);return n.network&&(o.value.network=i(n.network)),o}if(e==="AccountId32"&&typeof n=="object"){let o=F(n.id);return n.network&&(o.value.network=i(n.network)),o}return e==="GeneralKey"&&typeof n=="object"&&"data"in n?{type:e,value:{length:n.length,data:typeof n.data=="string"?q.fromHex(n.data):n.data}}:e==="GlobalConsensus"&&typeof n=="object"&&("Polkadot"in n||"Kusama"in n)?{type:e,value:n}:e==="Ethereum"?{type:e,value:{chain_id:BigInt("chainId"in n?n.chainId:n.chain_id)}}:e==="PalletInstance"?{type:e,value:n}:e==="GeneralIndex"?{type:e,value:BigInt(n)}:e==="X1"&&Array.isArray(n)?{type:e,value:n.length===1?i(n[0]):n.map(i)}:typeof n=="string"?{type:e,value:{type:n}}:typeof n=="number"?{type:e,value:n}:{type:e,value:i(n)}}else{let e={};for(let n of r){let o=t[n];e[n]=i(o)}return e}}else return t};function N(t){if(!t||typeof t!="object")return t;let{parents:r,interior:e}=t;return!e||e==="Here"?{parents:r,interior:{type:"Here"}}:{parents:r,interior:i(e)}}function P(t){if(typeof t=="string")return BigInt(t);if(typeof t=="number"||typeof t=="bigint"||t==null)return t;if(typeof t=="object"&&!Array.isArray(t))return i(t)}var _={};u(_,{convertToId:()=>C});import{Buffer as z}from"buffer";function C(t){let e=z.from(t.replace("0x",""),"hex").subarray(16);return e.readUIntBE(0,e.length)}var ft="0.1.0";export{p as HYDRATION_SS58_PREFIX,J as RUNTIME_DECIMALS,g as big,P as encodeAssetId,N as encodeLocation,B as enums,h as erc20,I as h160,k as hex,i as transform,ft as version,_ as xc,D as xcm};
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import Big, { RoundingMode } from 'big.js';
|
|
2
|
-
export declare function toDecimal(
|
|
3
|
-
export declare function toBigInt(amount: string | number, decimals: number): bigint;
|
|
2
|
+
export declare function toDecimal(number: bigint | number | string, decimals: number, maxDecimal?: number, roundType?: RoundingMode): string;
|
|
3
|
+
export declare function toBigInt(amount: bigint | string | number, decimals: number): bigint;
|
|
4
4
|
export declare function asBigInt(value: Big): bigint;
|
|
5
|
+
export declare function convertDecimals(number: string | bigint, decimals: number, targetDecimals: number): bigint;
|
|
6
|
+
export declare function hasDecimalOverflow(fl: number | string, maxDecimal: number): boolean;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function isHex(value: string, ignoreLength?: boolean): boolean;
|
|
2
|
+
export declare function stripHexPrefix(hex: string): string;
|
|
3
|
+
export declare function ensureHexPrefix(hex: string): string;
|
|
4
|
+
export declare function hexNormalize(h: string): string;
|
|
5
|
+
export declare function assertHexLen(h: string, bytes: number): void;
|
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
export * as big from './big';
|
|
2
|
+
export * as enums from './enums';
|
|
2
3
|
export * as erc20 from './erc20';
|
|
3
4
|
export * as h160 from './h160';
|
|
5
|
+
export * as hex from './hex';
|
|
6
|
+
export * as xcm from './xcm';
|
|
4
7
|
export * as xc from './xc';
|
|
8
|
+
export { encodeAssetId, encodeLocation, transform } from './xcm';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Binary } from 'polkadot-api';
|
|
2
|
+
export declare const toAccountId32: (address: string) => {
|
|
3
|
+
type: string;
|
|
4
|
+
value: {
|
|
5
|
+
network: undefined;
|
|
6
|
+
id: Binary;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
export declare const toAccountKey20: (address: string) => {
|
|
10
|
+
type: string;
|
|
11
|
+
value: {
|
|
12
|
+
network: undefined;
|
|
13
|
+
key: Binary;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
export declare const transform: (obj: any) => any;
|
|
17
|
+
export declare function encodeLocation(location: any): any;
|
|
18
|
+
export declare function encodeAssetId(assetId: any): any;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@galacticcouncil/common",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Common utilities shared across Galactic SDK packages",
|
|
5
5
|
"author": "GalacticCouncil",
|
|
6
6
|
"repository": {
|
|
@@ -17,20 +17,11 @@
|
|
|
17
17
|
"url": "https://github.com/galacticcouncil/sdk/issues"
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
|
-
"build"
|
|
21
|
-
"src"
|
|
20
|
+
"build"
|
|
22
21
|
],
|
|
23
22
|
"main": "./build/index.cjs",
|
|
24
23
|
"module": "./build/index.mjs",
|
|
25
24
|
"types": "./build/types/index.d.ts",
|
|
26
|
-
"exports": {
|
|
27
|
-
".": {
|
|
28
|
-
"types": "./build/types/index.d.ts",
|
|
29
|
-
"import": "./build/index.mjs",
|
|
30
|
-
"require": "./build/index.cjs",
|
|
31
|
-
"default": "./src/index.ts"
|
|
32
|
-
}
|
|
33
|
-
},
|
|
34
25
|
"scripts": {
|
|
35
26
|
"build": "npm run clean && node ./esbuild.dist.mjs",
|
|
36
27
|
"build:watch": "node ./esbuild.dev.mjs",
|
|
@@ -46,6 +37,6 @@
|
|
|
46
37
|
"@types/big.js": "^6.2.2"
|
|
47
38
|
},
|
|
48
39
|
"peerDependencies": {
|
|
49
|
-
"polkadot-api": "
|
|
40
|
+
"polkadot-api": "^1.23.0"
|
|
50
41
|
}
|
|
51
42
|
}
|
package/src/consts.ts
DELETED
package/src/index.ts
DELETED
package/src/utils/big.spec.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import Big from 'big.js';
|
|
2
|
-
import { toBigInt, toDecimal } from './big';
|
|
3
|
-
|
|
4
|
-
describe('Big utils', () => {
|
|
5
|
-
describe('toDecimals', () => {
|
|
6
|
-
it('should convert to decimals with 18 decimals', () => {
|
|
7
|
-
expect(toDecimal(1_678_578_001_143_648_100n, 18)).toBe('1.678578');
|
|
8
|
-
expect(toDecimal(4_778_448_942_696_649_000n, 18, 0)).toBe('5');
|
|
9
|
-
expect(toDecimal(9_277_414_529_928_219_000n, 18, 1)).toBe('9.3');
|
|
10
|
-
expect(toDecimal(1_489_926_767_190_488_000n, 18, 2)).toBe('1.49');
|
|
11
|
-
expect(toDecimal(7_111_587_933_365_016_000n, 18, 3)).toBe('7.112');
|
|
12
|
-
expect(toDecimal(7_111_587_933_365_016_000n, 18, 18)).toBe(
|
|
13
|
-
'7.111587933365016'
|
|
14
|
-
);
|
|
15
|
-
expect(toDecimal(4756503344577211177520n, 18, 18)).toBe(
|
|
16
|
-
'4756.50334457721117752'
|
|
17
|
-
);
|
|
18
|
-
expect(toDecimal(198_000_012_317_550_812n, 18, 18)).toBe(
|
|
19
|
-
'0.198000012317550812'
|
|
20
|
-
);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('should convert to decimals with 12 decimals', () => {
|
|
24
|
-
expect(toDecimal(52_912_391_280_480n, 12)).toBe('52.912391');
|
|
25
|
-
expect(toDecimal(796_301_020_21449n, 12, 0)).toBe('80');
|
|
26
|
-
expect(toDecimal(370_024_965_982_774n, 12, 1)).toBe('370');
|
|
27
|
-
expect(toDecimal(776_879_141_948n, 12, 2)).toBe('0.78');
|
|
28
|
-
expect(toDecimal(372_209_875_392n, 12, 3)).toBe('0.372');
|
|
29
|
-
expect(toDecimal(372_209_875_392n, 12, 12)).toBe('0.372209875392');
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('should convert to decimals with the correct rounding types', () => {
|
|
33
|
-
expect(toDecimal(370_024_965_982_774n, 12, 1, Big.roundDown)).toBe('370');
|
|
34
|
-
});
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
describe('toBigInt', () => {
|
|
38
|
-
it('should convert to BigInt', () => {
|
|
39
|
-
expect(toBigInt('1.678578', 18)).toBe(1_678_578_000_000_000_000n);
|
|
40
|
-
expect(toBigInt('5', 18)).toBe(5_000_000_000_000_000_000n);
|
|
41
|
-
expect(toBigInt('9.3', 18)).toBe(9_300_000_000_000_000_000n);
|
|
42
|
-
expect(toBigInt('1.49', 18)).toBe(1_490_000_000_000_000_000n);
|
|
43
|
-
expect(toBigInt('7.112', 18)).toBe(7_112_000_000_000_000_000n);
|
|
44
|
-
expect(toBigInt('7.111587933365016', 18)).toBe(
|
|
45
|
-
7_111_587_933_365_016_000n
|
|
46
|
-
);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('should convert to BigInt with 12 decimals', () => {
|
|
50
|
-
expect(toBigInt('52.912391', 12)).toBe(52_912_391_000_000n);
|
|
51
|
-
expect(toBigInt('80', 12)).toBe(80_000_000_000_000n);
|
|
52
|
-
expect(toBigInt('370', 12)).toBe(370_000_000_000_000n);
|
|
53
|
-
expect(toBigInt('0.78', 12)).toBe(780_000_000_000n);
|
|
54
|
-
expect(toBigInt('0.372', 12)).toBe(372_000_000_000n);
|
|
55
|
-
expect(toBigInt('0.372209875392', 12)).toBe(372_209_875_392n);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('should convert to BigInt from number', () => {
|
|
59
|
-
expect(toBigInt(52.912391, 12)).toBe(52_912_391_000_000n);
|
|
60
|
-
expect(toBigInt(80, 12)).toBe(80_000_000_000_000n);
|
|
61
|
-
expect(toBigInt(370, 12)).toBe(370_000_000_000_000n);
|
|
62
|
-
expect(toBigInt(0.78, 12)).toBe(780_000_000_000n);
|
|
63
|
-
expect(toBigInt(0.372, 12)).toBe(372_000_000_000n);
|
|
64
|
-
expect(toBigInt(0.372209875392, 12)).toBe(372_209_875_392n);
|
|
65
|
-
expect(toBigInt('4756.50334457721117752', 18)).toBe(
|
|
66
|
-
4_756_503_344_577_211_177_520n
|
|
67
|
-
);
|
|
68
|
-
expect(toBigInt('66712.0367386073069948646', 18)).toBe(
|
|
69
|
-
66_712_036_738_607_306_994_864n
|
|
70
|
-
);
|
|
71
|
-
});
|
|
72
|
-
});
|
|
73
|
-
});
|
package/src/utils/big.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import Big, { RoundingMode } from 'big.js';
|
|
2
|
-
|
|
3
|
-
Big.NE = -18;
|
|
4
|
-
|
|
5
|
-
export function toDecimal(
|
|
6
|
-
amount: bigint,
|
|
7
|
-
decimals: number,
|
|
8
|
-
maxDecimal = 6,
|
|
9
|
-
roundType?: RoundingMode
|
|
10
|
-
): string {
|
|
11
|
-
const dividend = Big(amount.toString());
|
|
12
|
-
const divisor = Big(10).pow(decimals);
|
|
13
|
-
const result = dividend.div(divisor).round(maxDecimal, roundType);
|
|
14
|
-
return result.toString();
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function toBigInt(amount: string | number, decimals: number): bigint {
|
|
18
|
-
const multiplier = Big(10).pow(decimals);
|
|
19
|
-
const result = Big(amount).mul(multiplier);
|
|
20
|
-
const fixedPoint = result.toFixed(0, Big.roundDown);
|
|
21
|
-
return BigInt(fixedPoint);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function asBigInt(value: Big): bigint {
|
|
25
|
-
return BigInt(value.round(0, Big.roundDown).toFixed(0));
|
|
26
|
-
}
|
package/src/utils/erc20.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
export class ERC20 {
|
|
2
|
-
static fromAssetId(assetId: number) {
|
|
3
|
-
const evmAddressBuffer = Buffer.alloc(20, 0);
|
|
4
|
-
evmAddressBuffer[15] = 1;
|
|
5
|
-
evmAddressBuffer.writeUInt32BE(assetId, 16);
|
|
6
|
-
|
|
7
|
-
return '0x' + evmAddressBuffer.toString('hex');
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
static toAssetId(address: string) {
|
|
11
|
-
const addressBuffer = Buffer.from(address.replace('0x', ''), 'hex');
|
|
12
|
-
if (addressBuffer.length !== 20 || !this.isAssetAddress(address)) {
|
|
13
|
-
return null;
|
|
14
|
-
}
|
|
15
|
-
return addressBuffer.readUInt32BE(16);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
static isAssetAddress(address: string) {
|
|
19
|
-
const PREFIX_BUFFER = Buffer.from(
|
|
20
|
-
'0000000000000000000000000000000100000000',
|
|
21
|
-
'hex'
|
|
22
|
-
);
|
|
23
|
-
const addressBuffer = Buffer.from(address.replace('0x', ''), 'hex');
|
|
24
|
-
if (addressBuffer.length !== 20) {
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
return addressBuffer.subarray(0, 16).equals(PREFIX_BUFFER.subarray(0, 16));
|
|
28
|
-
}
|
|
29
|
-
}
|
package/src/utils/h160.spec.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { H160, isEvmAddress } from './h160';
|
|
2
|
-
|
|
3
|
-
const ALICE = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
|
|
4
|
-
const ALICE_H160 = '0xd43593c715fdd31c61141abd04a99fd6822c8558';
|
|
5
|
-
|
|
6
|
-
const ALITH_H160 = '0x15fdd31c61141abd04a99fd6822c8558854ccde3';
|
|
7
|
-
const ALITH_SS58 = '7KATdGaiYbPauPtFrTfRj7zMM8KHqLbkjfurUnX6F57aHHqq';
|
|
8
|
-
|
|
9
|
-
describe('H160 Utils', () => {
|
|
10
|
-
it('Convert Alice (Substrate) SS85 => H160', () => {
|
|
11
|
-
const result = H160.fromSS58(ALICE);
|
|
12
|
-
expect(result).toStrictEqual(ALICE_H160);
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
it('Convert Alith (Evm) H160 => SS58', () => {
|
|
16
|
-
const result = H160.toAccount(ALITH_H160);
|
|
17
|
-
expect(result).toStrictEqual(ALITH_SS58);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it('Convert Alith (Evm) SS58 => H160', () => {
|
|
21
|
-
const result = H160.fromAccount(ALITH_SS58);
|
|
22
|
-
expect(result).toStrictEqual(ALITH_H160);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it('is ALICE_H160 valid h160', () => {
|
|
26
|
-
const result = isEvmAddress(ALICE_H160);
|
|
27
|
-
expect(result).toStrictEqual(true);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('is ALITH_H160 valid h160', () => {
|
|
31
|
-
const result = isEvmAddress(ALITH_H160);
|
|
32
|
-
expect(result).toStrictEqual(true);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it('is ALITH_SS58 not valid h160', () => {
|
|
36
|
-
const result = isEvmAddress(ALITH_SS58);
|
|
37
|
-
expect(result).toStrictEqual(false);
|
|
38
|
-
});
|
|
39
|
-
});
|
package/src/utils/h160.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { AccountId } from 'polkadot-api';
|
|
2
|
-
import { toHex } from '@polkadot-api/utils';
|
|
3
|
-
|
|
4
|
-
import { Buffer } from 'buffer';
|
|
5
|
-
|
|
6
|
-
import { HYDRATION_SS58_PREFIX } from '../consts';
|
|
7
|
-
|
|
8
|
-
const ETH_PREFIX = 'ETH\0';
|
|
9
|
-
|
|
10
|
-
export function isEvmAccount(ss58addr: string) {
|
|
11
|
-
if (!ss58addr) return false;
|
|
12
|
-
|
|
13
|
-
try {
|
|
14
|
-
const addressBytes = AccountId().enc(ss58addr);
|
|
15
|
-
const prefixBytes = Buffer.from(ETH_PREFIX);
|
|
16
|
-
return Buffer.from(addressBytes.subarray(0, prefixBytes.length)).equals(
|
|
17
|
-
prefixBytes
|
|
18
|
-
);
|
|
19
|
-
} catch {
|
|
20
|
-
return false;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function isEvmAddress(address: string): boolean {
|
|
25
|
-
if (!/^0x[a-fA-F0-9]{40}$/.test(address)) {
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
return true;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function isSs58Address(address: string): boolean {
|
|
32
|
-
try {
|
|
33
|
-
AccountId(HYDRATION_SS58_PREFIX).enc(address);
|
|
34
|
-
return true;
|
|
35
|
-
} catch (error) {
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export class H160 {
|
|
41
|
-
static toAccount = (address: string) => {
|
|
42
|
-
const addressBytes = Buffer.from(address.slice(2), 'hex');
|
|
43
|
-
const prefixBytes = Buffer.from(ETH_PREFIX);
|
|
44
|
-
const convertBytes = Uint8Array.from(
|
|
45
|
-
Buffer.concat([prefixBytes, addressBytes, Buffer.alloc(8)])
|
|
46
|
-
);
|
|
47
|
-
const addressHex = toHex(convertBytes);
|
|
48
|
-
return AccountId(HYDRATION_SS58_PREFIX).dec(addressHex);
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
static fromAccount = (address: string) => {
|
|
52
|
-
const decodedBytes = AccountId().enc(address);
|
|
53
|
-
const prefixBytes = Buffer.from(ETH_PREFIX);
|
|
54
|
-
const addressBytes = decodedBytes.slice(prefixBytes.length, -8);
|
|
55
|
-
return '0x' + Buffer.from(addressBytes).toString('hex');
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
static fromSS58 = (address: string) => {
|
|
59
|
-
const decodedBytes = AccountId().enc(address);
|
|
60
|
-
const slicedBytes = decodedBytes.slice(0, 20);
|
|
61
|
-
return toHex(slicedBytes);
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
static fromAny = (address: string) => {
|
|
65
|
-
if (isEvmAddress(address)) {
|
|
66
|
-
return address;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (isEvmAccount(address)) {
|
|
70
|
-
return H160.fromAccount(address);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (isSs58Address(address)) {
|
|
74
|
-
return H160.fromSS58(address);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
throw new Error('Unknown address type');
|
|
78
|
-
};
|
|
79
|
-
}
|
package/src/utils/index.ts
DELETED
package/src/utils/xc.spec.ts
DELETED
package/src/utils/xc.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { Buffer } from 'buffer';
|
|
2
|
-
|
|
3
|
-
export function convertToId(xcAddress: string): number {
|
|
4
|
-
const addressBuffer = Buffer.from(xcAddress.replace('0x', ''), 'hex');
|
|
5
|
-
const assetIdBuffer = addressBuffer.subarray(16);
|
|
6
|
-
return assetIdBuffer.readUIntBE(0, assetIdBuffer.length);
|
|
7
|
-
}
|