Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(PE-5758): add signer to ario class #20

Merged
merged 36 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
7e08097
feat(signer): add arweave signer to ario class
Mar 16, 2024
bc7e577
fix(signer): remove jwk use, ignore web example for now
Mar 18, 2024
35ffab6
fix(arbundle version): pin version
Mar 18, 2024
dcdf171
chore: update example
Mar 18, 2024
2508bd9
chore: formatting
Mar 18, 2024
f32f738
chore: simplify constructor
Mar 18, 2024
d2ef573
fix(signer): remove use of JWK, simplify constructor (#22)
dtfiedler Mar 18, 2024
18f2b58
Merge remote-tracking branch 'origin/alpha' into PE-5758
Mar 18, 2024
c7f8eee
fix(signer): update ANT to have signer
Mar 20, 2024
4581b8d
fix(ant): add signer to ant test
Mar 20, 2024
faab4f3
fix(tests): use process vars as priority url
Mar 20, 2024
1395664
Merge branch 'alpha' into PE-5758
atticusofsparta Mar 20, 2024
fddba1e
fix(tests): use http not https in tests
Mar 20, 2024
bf41b60
chore(types): update types and methods on contract instances
Mar 21, 2024
a71befd
fix(tests): update docker compose params
Mar 21, 2024
feb29ba
Merge branch 'alpha' into PE-5758
atticusofsparta Mar 21, 2024
4b3c4c2
fix(ctrl flow): remove else from control flow
Mar 21, 2024
f02d83f
fix(arbundles): update arbundles import
Mar 21, 2024
2cd1b5c
fix(tests): update tests with new name
Mar 21, 2024
740d8b8
fix(contracts): remove write method and type from remote contract
Mar 21, 2024
10f30fe
fix(tests): use angela for testing
Mar 21, 2024
2c02e90
fix(types): update interaction type to only use read for now
Mar 22, 2024
87d6c90
fix(types): rename signer to ContractSigner
Mar 22, 2024
32530eb
fix(eslint): remove eslint comments and use this signer
Mar 22, 2024
1bdcfeb
fix(tests): update tests with constants and update types
Mar 22, 2024
4fae4a2
fix(contracts): add configuration view method and update types
Mar 22, 2024
5618690
Merge branch 'alpha' into PE-5758
atticusofsparta Mar 22, 2024
21224e2
fix(lint): formatting
Mar 22, 2024
b4a7bc3
fix(contract configuration): return cache url as well
Mar 22, 2024
9869415
fix(tests): instantiate new ant to connect in tests
Mar 22, 2024
060ee2c
fix(warp contract): added test for getting state after connecting wit…
Mar 22, 2024
2ac9427
fix(cache): remove cache folder
Mar 22, 2024
2867abc
fix(gitignore): remove cache from gitignore
Mar 22, 2024
a59f05c
fix(types and tests): update evalTo to allow undefined sortKey and bl…
Mar 22, 2024
f76a201
fix(tests): dont make blockHeight or sortKey undefined but rather evalTo
Mar 22, 2024
9d4bd9f
chore(readme): add docs on using connect
Mar 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/node/index.cjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
const Arweave = require('arweave');
const {
ArIO,
ARNS_TESTNET_REGISTRY_TX,
} = require('../../lib/cjs/node/index.js');
const { ArweaveSigner } = require('arbundles');

(async () => {
const arIO = new ArIO();
const jwk = await Arweave.init({}).wallets.generate();
const signer = new ArweaveSigner(jwk);
const arIO = new ArIO({ signer });
// testnet gateways
const testnetGateways = await arIO.getGateways();
const protocolBalance = await arIO.getBalance({
Expand Down
7 changes: 6 additions & 1 deletion examples/node/index.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { ArweaveSigner } from 'arbundles';
import Arweave from 'arweave';

import { ARNS_TESTNET_REGISTRY_TX, ArIO } from '../../lib/esm/node/index.js';

(async () => {
const arIO = new ArIO();
const jwk = await Arweave.init({}).wallets.generate();
const signer = new ArweaveSigner(jwk);
const arIO = new ArIO({ signer });
// testnet gateways
const testnetGateways = await arIO.getGateways();
const protocolBalance = await arIO.getBalance({
Expand Down
8 changes: 6 additions & 2 deletions examples/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,14 @@ <h1 class="text-textPrimary w-full font-bold">View Distribution Data</h1>
</div>

<script type="module">
import { ArconnectSigner } from 'https://unpkg.com/arbundles@0.11.0/build/web/esm/webIndex.js';
import Arweave from 'https://unpkg.com/arweave@1.14.4/web/index.js';

dtfiedler marked this conversation as resolved.
Show resolved Hide resolved
import { ArIO } from './web.bundle.min.js';

// set up our client
const arIO = new ArIO();
const arweave = Arweave.init({});
const signer = new ArconnectSigner(window['arweaveWallet'], arweave);
const arIO = new ArIO({ signer });

// fetch data on page load
async function init() {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"typescript": "^5.1.6"
},
"dependencies": {
"arbundles": "0.11.0",
"arweave": "^1.14.4",
"axios": "1.4.0",
"setimmediate": "^1.0.5",
Expand Down
48 changes: 39 additions & 9 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* 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 { ArconnectSigner, ArweaveSigner } from 'arbundles';

import {
ANTRecord,
ANTState,
Expand All @@ -32,17 +34,21 @@ export type SortKey = string;
export type WalletAddress = string;

// TODO: append this with other configuration options (e.g. local vs. remote evaluation)
export type ContractConfiguration =
export type ContractSigner = ArweaveSigner | ArconnectSigner;
export type ContractConfiguration = {
signer?: ContractSigner; // TODO: optionally allow JWK in place of signer
} & (
| {
contract?: SmartWeaveContract<unknown>;
contract?: BaseContract<unknown> & ReadContract;
}
| {
contractTxId: string;
};
}
);

export function isContractConfiguration<T>(
config: ContractConfiguration,
): config is { contract: SmartWeaveContract<T> } {
): config is { contract: BaseContract<T> & ReadContract } {
return 'contract' in config;
}

Expand All @@ -62,19 +68,44 @@ export type EvaluationParameters<T = NonNullable<unknown>> = {
evaluationOptions?: EvaluationOptions | Record<string, never> | undefined;
} & T;

export interface BaseContract<T> {
getState(params: EvaluationParameters): Promise<T>;
connect(signer: ContractSigner): this;
}

export interface ReadContract {
readInteraction<Input, State>({
functionName,
inputs,
evaluationOptions,
}: EvaluationParameters<{
functionName: string;
inputs?: Input;
}>): Promise<State>;
}

export interface WriteContract {
writeInteraction<Input, State>({
functionName,
inputs,
evaluationOptions,
}: EvaluationParameters<{
functionName: string;
inputs: Input;
}>): Promise<State>;
}

export interface SmartWeaveContract<T> {
getContractState(params: EvaluationParameters): Promise<T>;
readInteraction<I, K>({
functionName,
inputs,
evaluationOptions,
}: EvaluationParameters<{ functionName: string; inputs?: I }>): Promise<K>;
// TODO: write interaction
}

// TODO: extend with additional methods
export interface ArIOContract {
getState({ evaluationOptions }: EvaluationParameters): Promise<ArIOState>;
export interface ArIOContract extends BaseContract<ArIOState> {
getGateway({
address,
evaluationOptions,
Expand Down Expand Up @@ -138,8 +169,7 @@ export interface ArIOContract {
}>): Promise<ArNSAuctionData>;
}

export interface ANTContract {
getState({ evaluationOptions }: EvaluationParameters): Promise<ANTState>;
export interface ANTContract extends BaseContract<ANTState> {
getRecord({
domain,
evaluationOptions,
Expand Down
55 changes: 35 additions & 20 deletions src/common/ant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@ import {
ANTContract,
ANTRecord,
ANTState,
BaseContract,
ContractConfiguration,
ContractSigner,
EvaluationOptions,
EvaluationParameters,
SmartWeaveContract,
isContractConfiguration,
isContractTxIdConfiguration,
} from '../types.js';
import { RemoteContract } from './contracts/remote-contract.js';
import { WarpContract } from './index.js';

export class ANT implements ANTContract {
private contract: SmartWeaveContract<ANTState>;
export class ANT implements ANTContract, BaseContract<ANTState> {
private contract: BaseContract<ANTState>;
private signer: ContractSigner | undefined;

constructor(config: ContractConfiguration) {
constructor({ signer, ...config }: ContractConfiguration) {
this.signer = signer;
if (isContractConfiguration<ANTState>(config)) {
this.contract = config.contract;
} else if (isContractTxIdConfiguration(config)) {
Expand All @@ -40,13 +44,24 @@ export class ANT implements ANTContract {
}
}

connect(signer: ContractSigner): this {
this.signer = signer;
if (this.contract instanceof RemoteContract) {
const config = this.contract.configuration();
this.contract = new WarpContract<ANTState>({
...config,
signer,
});
}
this.contract.connect(this.signer);

return this;
}
/**
* Returns the current state of the contract.
*/
async getState({
evaluationOptions,
}: EvaluationParameters = {}): Promise<ANTState> {
const state = await this.contract.getContractState({ evaluationOptions });
async getState(params: EvaluationParameters): Promise<ANTState> {
const state = await this.contract.getState(params);
return state;
}

Expand All @@ -62,53 +77,53 @@ export class ANT implements ANTContract {
evaluationOptions,
}: {
evaluationOptions?: EvaluationOptions | Record<string, never> | undefined;
} = {}): Promise<Record<string, ANTRecord>> {
const state = await this.contract.getContractState({ evaluationOptions });
}): Promise<Record<string, ANTRecord>> {
const state = await this.contract.getState({ evaluationOptions });
return state.records;
}

async getOwner({
evaluationOptions,
}: {
evaluationOptions?: EvaluationOptions | Record<string, never> | undefined;
} = {}): Promise<string> {
const state = await this.contract.getContractState({ evaluationOptions });
}): Promise<string> {
const state = await this.contract.getState({ evaluationOptions });
return state.owner;
}

async getControllers({
evaluationOptions,
}: {
evaluationOptions?: EvaluationOptions | Record<string, never> | undefined;
} = {}): Promise<string[]> {
const state = await this.contract.getContractState({ evaluationOptions });
}): Promise<string[]> {
const state = await this.contract.getState({ evaluationOptions });
return state.controllers;
}

async getName({
evaluationOptions,
}: {
evaluationOptions?: EvaluationOptions | Record<string, never> | undefined;
} = {}): Promise<string> {
const state = await this.contract.getContractState({ evaluationOptions });
}): Promise<string> {
const state = await this.contract.getState({ evaluationOptions });
return state.name;
}

async getTicker({
evaluationOptions,
}: {
evaluationOptions?: EvaluationOptions | Record<string, never> | undefined;
} = {}): Promise<string> {
const state = await this.contract.getContractState({ evaluationOptions });
}): Promise<string> {
const state = await this.contract.getState({ evaluationOptions });
return state.ticker;
}

async getBalances({
evaluationOptions,
}: {
evaluationOptions?: EvaluationOptions | Record<string, never> | undefined;
} = {}): Promise<Record<string, number>> {
const state = await this.contract.getContractState({ evaluationOptions });
}): Promise<Record<string, number>> {
const state = await this.contract.getState({ evaluationOptions });
return state.balances;
}

Expand Down
55 changes: 31 additions & 24 deletions src/common/ar-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,36 @@ import {
ArIOState,
ArNSAuctionData,
ArNSNameData,
BaseContract,
ContractConfiguration,
ContractSigner,
EpochDistributionData,
EvaluationOptions,
EvaluationParameters,
Gateway,
Observations,
ReadContract,
RegistrationType,
SmartWeaveContract,
WeightedObserver,
isContractConfiguration,
isContractTxIdConfiguration,
} from '../types.js';
import { RemoteContract } from './contracts/remote-contract.js';
import { WarpContract } from './index.js';

function isContractConfiguration<T>(
config: ContractConfiguration,
): config is { contract: SmartWeaveContract<T> } {
return 'contract' in config;
}

function isContractTxIdConfiguration(
config: ContractConfiguration,
): config is { contractTxId: string } {
return 'contractTxId' in config;
}

export class ArIO implements ArIOContract {
private contract: SmartWeaveContract<ArIOState>;
export class ArIO implements ArIOContract, BaseContract<ArIOState> {
dtfiedler marked this conversation as resolved.
Show resolved Hide resolved
private contract: BaseContract<ArIOState> & ReadContract;
private signer: ContractSigner | undefined;

constructor(
config: ContractConfiguration = {
// default to a contract that uses the arns service to do the evaluation
{ signer, ...config }: ContractConfiguration = {
contract: new RemoteContract<ArIOState>({
contractTxId: ARNS_TESTNET_REGISTRY_TX,
}),
},
) {
this.signer = signer;

if (isContractConfiguration<ArIOState>(config)) {
this.contract = config.contract;
} else if (isContractTxIdConfiguration(config)) {
Expand All @@ -66,14 +61,26 @@ export class ArIO implements ArIOContract {
}
}

connect(signer: ContractSigner): this {
this.signer = signer;
if (this.contract instanceof RemoteContract) {
const config = this.contract.configuration();
this.contract = new WarpContract<ArIOState>({
...config,
signer,
});
}
this.contract.connect(this.signer);

return this;
}
/**
* Returns the current state of the contract.
*/
async getState(params: EvaluationParameters): Promise<ArIOState> {
const state = await this.contract.getContractState(params);
const state = await this.contract.getState(params);
return state;
}

/**
* Returns the ARNS record for the given domain.
*/
Expand All @@ -93,7 +100,7 @@ export class ArIO implements ArIOContract {
async getArNSRecords({
evaluationOptions,
}: EvaluationParameters = {}): Promise<Record<string, ArNSNameData>> {
const state = await this.contract.getContractState({ evaluationOptions });
const state = await this.contract.getState({ evaluationOptions });
return state.records;
}

Expand All @@ -114,7 +121,7 @@ export class ArIO implements ArIOContract {
async getBalances({ evaluationOptions }: EvaluationParameters = {}): Promise<
Record<string, number>
> {
const state = await this.contract.getContractState({ evaluationOptions });
const state = await this.contract.getState({ evaluationOptions });
return state.balances;
}

Expand Down Expand Up @@ -199,15 +206,15 @@ export class ArIO implements ArIOContract {
}: EvaluationParameters<{
epochStartHeight?: number;
}> = {}): Promise<Observations> {
const { observations } = await this.contract.getContractState({
const { observations } = await this.contract.getState({
evaluationOptions,
});
return observations;
}
async getDistributions({
evaluationOptions,
}: EvaluationParameters = {}): Promise<EpochDistributionData> {
const { distributions } = await this.contract.getContractState({
const { distributions } = await this.contract.getState({
evaluationOptions,
});
return distributions;
Expand Down Expand Up @@ -235,7 +242,7 @@ export class ArIO implements ArIOContract {
}: {
evaluationOptions?: EvaluationOptions | Record<string, never> | undefined;
}): Promise<Record<string, ArNSAuctionData>> {
const { auctions } = await this.contract.getContractState({
const { auctions } = await this.contract.getState({
evaluationOptions,
});

Expand Down
Loading