Skip to content

Commit

Permalink
Merge pull request #3 from ar-io/PE-5691-ario-network-interface
Browse files Browse the repository at this point in the history
chore(PE-5691): init ario network contract interface
  • Loading branch information
atticusofsparta authored Feb 24, 2024
2 parents e5e9d1d + fb47de0 commit e92da07
Show file tree
Hide file tree
Showing 27 changed files with 583 additions and 251 deletions.
47 changes: 36 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# ar-io/sdk
# @ar-io/sdk

This is the home of ar.io SDK. This SDK provides functionality for interacting with the ArNS and ar.io ecosystem. It is available for both NodeJS and Web environments.

Expand Down Expand Up @@ -35,11 +35,16 @@ yarn add @ar-io/sdk
## Quick Start

```typescript
const arIO = new ArIO();

const state = arIO.getContractState({
contractTxId: 'bLAgYxAdX2Ry-nt6aH2ixgvJXbpsEYm28NgJgyqfs-U',
});
import { ArIO } from '@ar-io/sdk';

const arIO = new ArIO({});
const address = 'QGWqtJdLLgm2ehFWiiPzMaoFLD50CnGuzZIPEdoDRGQ';
// testnet
const testnetBalance = await arIO.testnet.getBalance({ address });
const testnetGateway = await arIO.testnet.getGateway({ address });
// mainnet
const balance = await arIO.mainnet.getBalance({ address });
const gateway = await arIO.mainnet.getGateway({ address });
```

## Usage
Expand All @@ -51,19 +56,32 @@ The SDK is provided in both CommonJS and ESM formats, and it's compatible with b
#### Bundlers (Webpack, Rollup, ESbuild, etc.)

```javascript
// INSERT EXAMPLE FOR USING IN ESM PROJECT
import { ArIO } from '@ar-io/sdk';

const arIO = new ArIO({});
const gateways = arIO.mainnet.getGateways();
```

#### Browser

```javascript
// INSERT EXAMPLE FOR USING IN BROWSER PROJECT
```html
<script type="module">
import { ArIO } from 'https://unpkg.com/@ar-io/sdk';
// set up our client
const arIO = new ArIO({});
// fetch mainnet gateways
const gateways = await arIO.mainnet.getGateways();
</script>
```

### Node

```javascript
// INSERT EXAMPLE FOR USING IN CJS PROJECT
const { ArIO } = require('@ar-io/sdk');

const arIO = new ArIO({});
const gateways = await arIO.mainnet.getGateways();
```

### Typescript
Expand All @@ -74,7 +92,14 @@ Types are exported from `./lib/types/[node/web]/index.d.ts` and should be automa

## APIs

[INSERT A LIST OF ALL THE APIS PROVIDED BY THE SDK AND HOW TO USE THEM]
The contract that the following methods retrieve data from are determined by the `testnet` or `devnet` clients - see examples above for implementation details.

| Method Name | Description |
| ------------------------- | ----------------------------------------------- |
| `getBalance({ address })` | Retrieves the balance of the specified address. |
| `getBalances()` | Retrieves all balances on the ArIO contract. |
| `getGateway({ address })` | Retrieves the specified gateway by address. |
| `getGateways()` | Retrieves all gateways. |

## Developers

Expand Down
34 changes: 0 additions & 34 deletions bundle.cjs

This file was deleted.

8 changes: 1 addition & 7 deletions bundle.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ const bundle = () => {
target: ['esnext'],
format: 'esm',
globalName: 'ar.io',
plugins: [
polyfillNode({
polyfills: {
crypto: true,
},
}),
],
plugins: [polyfillNode()],
tsconfig: './tsconfig.web.json',
outfile: './bundles/web.bundle.min.js',
})
Expand Down
11 changes: 8 additions & 3 deletions examples/node/index.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const { DefaultClient } = require('../../lib/index.js');
const { ArIO } = require('../../lib/cjs/node/index.js');

(async () => {
const client = new DefaultClient();
console.log(client);
const arIO = new ArIO({});
// testnet gateways
const testnetGateways = await arIO.testnet.getGateways();
// devnet gateways
const devnetGateways = await arIO.devnet.getGateways();

console.dir({ testnetGateways, devnetGateways }, { depth: 2 });
})();
11 changes: 8 additions & 3 deletions examples/node/index.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { DefaultClient } from '../../lib/index.js';
import { ArIO } from '../../lib/esm/node/index.js';

(async () => {
const client = new DefaultClient();
console.log(client);
const arIO = new ArIO({});
// testnet gateways
const testnetGateways = await arIO.testnet.getGateways();
// devnet gateways
const devnetGateways = await arIO.devnet.getGateways();

console.dir({ testnetGateways, devnetGateways }, { depth: 2 });
})();
90 changes: 78 additions & 12 deletions examples/web/index.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,88 @@
<html>
<head>
<script type="module" src="./web.bundle.min.js"></script>
<style>
/* Center the button */
body {
padding: 10px;
}
</style>
<!-- import permanent tailwind -->
<script src="https://arweave.net/RjXXBjP2GCZZUg4gSW1Hyr9GtLG-o59QCHrKiITtpQw"></script>
<script>
tailwind.config = {
theme: {
borderWidth: {
1: '1px',
},
extend: {
colors: {
primary: '#FFBB38',
background: '#141416',
surface: '#222224',
surfaceSecondary: '#2E2E30',
textPrimary: '#F5F5F5',
textSubtle: '#7A7A7C',
},
},
},
};
</script>
</head>
<body>
<body class="bg-background flex flex-col items-center p-10">
<div
class="bg-surface flex flex-col gap-5 items-end justify-center p-5 rounded-md h-full"
style="width: 750px"
>
<div class="h-full w-full" style="overflow-y: scroll">
<table class="w-full bg-background text-textPrimary">
<thead>
<tr>
<th class="px-4 py-2">Domain</th>
<th class="px-4 py-2">Owner</th>
<th class="px-4 py-2">Stake</th>
</tr>
</thead>
<tbody id="table-body">
<!-- Add more rows as needed -->
</tbody>
</table>
</div>
<button
id="fetch-gateways-button"
class="animate-bounce bg-primary text-textPrimary p-2 rounded-md w-full text-surface hover:shadow-xl"
style="color: black"
>
Fetch Gateways
</button>
</div>
<script type="module">
import { DefaultClient } from './web.bundle.min.js';
import { ArIO } from './web.bundle.min.js';

// set up our client
const client = new DefaultClient();
const arIO = new ArIO({}).testnet;

async function setGateways() {
const tableBody = document.getElementById('table-body');
tableBody.innerHTML = `
<tr>
<td class="border border-surface px-4 py-2">Loading...</td>
</tr>
`;
const gateways = await arIO.getGateways();

tableBody.innerHTML = Object.entries(gateways)
.map(([gatewayOwner, gateway]) => {
return `
<tr>
<td class="border border-surface px-4 py-2 text-primary"><a href="https://${gateway.settings.fqdn}" target="_blank">${gateway.settings.fqdn}</a></td>
<td class="border border-surface px-4 py-2 text-primary"><a href="https://arscan.io/address/${gatewayOwner}" target="_blank">${gatewayOwner}</a></td>
<td class="border border-surface px-4 py-2">${gateway.operatorStake} IO</td>
</tr>
`;
})
.join('');
}

// print the client to the console
console.log(client);
const fetchGatewaysButton = document.getElementById(
'fetch-gateways-button',
);
fetchGatewaysButton.addEventListener('click', () => {
setGateways();
});
</script>
</body>
</html>
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{
"name": "@ar-io/sdk",
"version": "0.0.1",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"main": "./lib/cjs/node/index.js",
"module": "./lib/esm/node/index.js",
"types": "./lib/types/node/index.d.ts",
"browser": "./bundles/web.bundle.min.js",
"type": "module",
"files": [
"lib",
"bundles",
"LICENSE",
"README.md"
"README.md",
"package.json"
],
"author": {
"name": "Permanent Data Solutions Inc",
Expand Down Expand Up @@ -41,19 +44,17 @@
"scripts": {
"build:web": "node bundle.mjs",
"build:esm": "yarn tsc -p tsconfig.json",
"build:cjs": "yarn tsc -p tsconfig.cjs.json && echo '{\"type\": \"commonjs\"}' > lib/cjs/package.json",
"build:types": "yarn tsc -p tsconfig.types.json",
"build": "yarn clean && yarn build:web && yarn build:esm && yarn build:cjs && yarn build:types",
"build:cjs": "yarn tsc -p tsconfig.cjs.json && echo \"{\\\"type\\\": \\\"commonjs\\\"}\" > lib/cjs/package.json",
"build": "yarn clean && yarn build:web && yarn build:esm && yarn build:cjs",
"clean": "rimraf [ lib coverage bundles ]",
"postinstall": "husky install",
"lint": "eslint src",
"lint:fix": "eslint src --fix",
"format": "prettier --check .",
"format:fix": "prettier --write .",
"test": "c8 jest .",
"test": "yarn clean && c8 jest .",
"prepare": "husky install",
"example:mjs": "yarn build:esm && node examples/node/index.mjs",
"example:cjs": "yarn build:esm && node examples/node/index.mjs",
"example:cjs": "yarn build:cjs && node examples/node/index.cjs",
"example:web": "yarn build:web && cp -r bundles/* examples/web && http-server --port 8080 --host -o examples/web"
},
"devDependencies": {
Expand Down Expand Up @@ -88,6 +89,7 @@
"prettier": "^3.0.2",
"rimraf": "^5.0.1",
"semantic-release": "^21.0.7",
"setimmediate": "^1.0.5",
"sinon": "^15.2.0",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.1",
Expand Down
35 changes: 13 additions & 22 deletions src/common/ar-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,22 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { ContractCache } from '../types.js';
import {
ARNS_DEVNET_REGISTRY_TX,
ARNS_TESTNET_REGISTRY_TX,
} from '../constants.js';
import { ArIOContract, ContractCache } from '../types/index.js';
import { ArNSRemoteCache } from './index.js';

export class ArIO implements ContractCache {
private contractStateProvider: ContractCache;
export class ArIO {
protected cache: ContractCache = new ArNSRemoteCache({});

constructor({
contractStateProvider = new ArNSRemoteCache({}),
}: {
contractStateProvider?: ContractCache;
}) {
this.contractStateProvider = contractStateProvider;
}
public testnet: ArIOContract;
public devnet: ArIOContract;

/**
* Fetches the state of a contract.
* @param {string} contractTxId - The Arweave transaction id of the contract.
*/
async getContractState<ContractState>({
contractTxId,
}: {
contractTxId: string;
}): Promise<ContractState> {
return this.contractStateProvider.getContractState<ContractState>({
contractTxId,
});
constructor({ remoteCacheUrl }: { remoteCacheUrl?: string }) {
this.cache = new ArNSRemoteCache({ url: remoteCacheUrl });
this.testnet = this.cache.setContractTxId(ARNS_TESTNET_REGISTRY_TX);
this.devnet = this.cache.setContractTxId(ARNS_DEVNET_REGISTRY_TX);
}
}
Loading

0 comments on commit e92da07

Please sign in to comment.