-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
129 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ dist-ssr | |
server/dist | ||
public/dist | ||
.turbo | ||
.vscode | ||
.vscode | ||
wallet.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.