Skip to content

Commit

Permalink
fix(write): update utils - change error flow - update arweave constru…
Browse files Browse the repository at this point in the history
…ctor props
  • Loading branch information
Atticus committed Mar 25, 2024
1 parent 01e9921 commit 0a81c92
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 33 deletions.
30 changes: 15 additions & 15 deletions src/common/contracts/warp-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class WarpContract<T>
private contract: Contract<T>;
private contractTxId: string;
private cacheUrl: string | undefined;
private arweave = defaultArweave;
private arweave;
private log = new DefaultLogger({
level: 'debug',
});
Expand All @@ -65,16 +65,18 @@ export class WarpContract<T>
},
true,
),
arweave = defaultArweave,
}: {
contractTxId: string;
cacheUrl?: string;
warp?: Warp;
signer?: ContractSigner;
arweave?: Arweave;
}) {
this.contractTxId = contractTxId;
this.contract = warp.contract<T>(contractTxId);
this.cacheUrl = cacheUrl;
this.arweave = warp.arweave as unknown as Arweave;
this.arweave = arweave;
}

configuration(): { contractTxId: string; cacheUrl: string | undefined } {
Expand Down Expand Up @@ -180,17 +182,18 @@ export class WarpContract<T>
})) as InteractionResult<unknown, Input>;
}

const { interactionTx } =
(await this.contract.writeInteraction<Input>({
function: functionName,
...inputs,
})) ?? {};
const writeResult = await this.contract.writeInteraction<Input>({
function: functionName,
...inputs,
});

if (!writeResult) {
throw new Error(`Failed to write contract interaction ${functionName}`);
}
const { interactionTx } = writeResult;

// Flexible way to return information on the transaction, aids in caching and re-deployment if desired by simply refetching tx anchor and resigning.
if (
(interactionTx && isTransaction(interactionTx)) ||
(interactionTx && DataItem.isDataItem(interactionTx))
) {
if (isTransaction(interactionTx) || DataItem.isDataItem(interactionTx)) {
this.log.debug(`Write interaction succesful`, {
contractTxId: this.contractTxId,
functionName,
Expand All @@ -201,12 +204,9 @@ export class WarpContract<T>
});
return interactionTx;
}
throw new Error(`Failed to write contract interaction ${functionName}`);
} catch (error) {
throw new WriteInteractionError(error);
}

throw new WriteInteractionError(
`Failed to write contract interaction ${functionName}`,
);
}
}
20 changes: 2 additions & 18 deletions src/utils/arweave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,10 @@ export function isBlockHeight(height: string | number): height is BlockHeight {
return height !== undefined && !isNaN(parseInt(height.toString()));
}

export const dummyTransaction: Transaction = new Transaction({
format: 2,
id: 'dummy',
last_tx: 'dummy',
owner: 'dummy',
tags: [],
target: 'dummy',
quantity: 'dummy',
data: new Uint8Array(),
reward: 'dummy',
signature: 'dummy',
data_size: 'dummy',
data_root: 'dummy',
});

export const isTransaction = (tx: object): tx is Transaction => {
try {
const testTxKeys = Object.keys(dummyTransaction);
const txKeys = Object.keys(tx);
return txKeys.every((key) => testTxKeys.includes(key));
const requiredTxKeys = ['id', 'owner', 'tags', 'signature'];
return requiredTxKeys.every((key) => key in tx);
} catch (error: unknown) {
return false;
}
Expand Down

0 comments on commit 0a81c92

Please sign in to comment.