Skip to content

Commit

Permalink
feat: add data cost utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Envoy-VC committed Dec 29, 2023
1 parent bfd1df7 commit 2dd0ae1
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ dist-ssr
server/dist
public/dist
.turbo
.vscode
.vscode
wallet.json
1 change: 1 addition & 0 deletions packages/atomic-toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@types/lodash": "^4.14.202",
"@types/node": "^18.19.3",
"@urql/core": "^4.2.2",
"bignumber.js": "^9.1.2",
"dotenv": "^16.3.1",
"ethers": "v5",
"mime": "^4.0.0",
Expand Down
5 changes: 3 additions & 2 deletions packages/atomic-toolkit/src/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Warp } from 'warp-contracts';
import Irys from '@irys/sdk';
import Irys, { WebIrys } from '@irys/sdk';
import Arweave from 'arweave';

// Warp
Expand All @@ -22,7 +22,7 @@ import { UploadResponse } from '@irys/sdk/build/cjs/common/types';
class AtomicToolkitBase {
protected warp: Warp;
protected arweaveInstance: Arweave;
protected irys: Irys | null | undefined;
protected irys: WebIrys | Irys | null | undefined;
protected key: JWKInterface | 'use_wallet' | null;
public gql: Client;

Expand Down Expand Up @@ -54,6 +54,7 @@ class AtomicToolkitBase {
this.key = key ?? 'use_wallet';
this.irys = null;
}

this.gql = new Client({
url: 'https://arweave.net/graphql',
exchanges: [cacheExchange, fetchExchange],
Expand Down
78 changes: 78 additions & 0 deletions packages/atomic-toolkit/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Types
import * as Types from '../../types';
import AtomicToolkitBase from '../../base';

import BigNumber from 'bignumber.js';

class Utilities extends AtomicToolkitBase {
constructor(
opts: Types.AtomicToolkitNodeOpts | Types.AtomicToolkitWebOpts,
) {
super(opts);
}

public async getUploadCost(size: number) {
let token: string;
let balance: { atomic: string; formatted: string };
let cost: { atomic: string; formatted: string };
let additional: { atomic: string; formatted: string };

if (this.irys) {
await this.irys.ready();
const b = await this.irys.getLoadedBalance();
const c = await this.irys.getPrice(size);
const a = c.minus(b);

token = this.irys.utils.token;
balance = {
atomic: b.toString(),
formatted: this.irys.utils.fromAtomic(b).toString(),
};
cost = {
atomic: c.toString(),
formatted: this.irys.utils.fromAtomic(c).toString(),
};
additional = {
atomic: a.isGreaterThan(0) ? a.toString() : '0',
formatted: a.isGreaterThan(0)
? this.irys.utils.fromAtomic(a).toString()
: '0',
};
} else {
if (!this.key) {
throw new Error('No key provided');
}
const wallet = this.arweaveInstance.wallets;
const address = await wallet.getAddress(this.key);
const b = BigNumber(await wallet.getBalance(address));
const c = BigNumber(
await this.arweaveInstance.transactions.getPrice(size, address),
);
const a = c.minus(b);

token = 'arweave';
balance = {
atomic: b.toString(),
formatted: this.arweaveInstance.ar.winstonToAr(b.toString()),
};
cost = {
atomic: c.toString(),
formatted: this.arweaveInstance.ar.winstonToAr(c.toString()),
};
additional = {
atomic: a.isGreaterThan(0) ? a.toString() : '0',
formatted: a.isGreaterThan(0)
? this.arweaveInstance.ar.winstonToAr(a.toString())
: '0',
};
}
return {
token,
cost,
balance,
additional,
};
}
}

export default Utilities;
3 changes: 3 additions & 0 deletions packages/atomic-toolkit/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ import AtomicToolkitBase from './base';

import AtomicAssets from './lib/assets';
import Collection from './lib/collection';
import Utilities from './lib/utils';

// Types
import * as Types from './types';

class AtomicToolkit extends AtomicToolkitBase {
public assets: AtomicAssets;
public collection: Collection;
public utils: Utilities;

constructor(opts: Types.AtomicToolkitNodeOpts) {
super(opts);
this.assets = new AtomicAssets(opts);
this.collection = new Collection(opts);
this.utils = new Utilities(opts);
}
}

Expand Down
3 changes: 3 additions & 0 deletions packages/atomic-toolkit/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ import AtomicToolkitBase from './base';

import AtomicAssets from './lib/assets';
import Collection from './lib/collection';
import Utilities from './lib/utils';

// Types
import * as Types from './types';

class AtomicToolkitWeb extends AtomicToolkitBase {
public assets: AtomicAssets;
public collection: Collection;
public utils: Utilities;
public arweave: Arweave;

constructor(opts: Types.AtomicToolkitWebOpts) {
super(opts);
this.assets = new AtomicAssets(opts);
this.collection = new Collection(opts);
this.arweave = this.arweaveInstance;
this.utils = new Utilities(opts);
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/atomic-toolkit/tests/collection/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ describe('Collection GraphQL', () => {
const res = await toolkit.collection.getCollection(
'naUSbwwYgZo1Zo8wD1jsWNTulydnccOTnx6eOyzy5AM',
);
console.log(res);
expect(res).toBeDefined();
});
});
1 change: 0 additions & 1 deletion packages/atomic-toolkit/tests/tags/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ describe('Content-Type Tags', () => {
'Contract-Src',
'Init-State',
'Contract-Manifest',
'Indexed-By',
'License',
];

Expand Down
36 changes: 36 additions & 0 deletions packages/atomic-toolkit/tests/utils/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { describe, expect, it } from 'vitest';

import AtomicToolkit from '../../src';

import Irys from '@irys/sdk';
import * as dotenv from 'dotenv';
dotenv.config({ path: '.env.local' });

import { readFileSync } from 'fs';
const key = JSON.parse(readFileSync('./wallet.json').toString());

describe('Utilities', () => {
it('should return cost to upload data using arweave', async () => {
const toolkit = new AtomicToolkit({
key,
});

const cost = await toolkit.utils.getUploadCost(10000);
expect(cost).toBeDefined();
});
it('should return cost to upload data using irys', async () => {
const irys = new Irys({
url: 'https://node2.irys.xyz',
token: 'matic',
key: process.env.PRIVATE_KEY,
});

await irys.ready();
const toolkit = new AtomicToolkit({
irys,
});

const cost = await toolkit.utils.getUploadCost(131434143);
expect(cost).toBeDefined();
});
});
4 changes: 3 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2dd0ae1

Please sign in to comment.