Skip to content

Commit

Permalink
chore: wip contract standards
Browse files Browse the repository at this point in the history
  • Loading branch information
mpeterdev committed Oct 7, 2024
1 parent 183e757 commit 07012ab
Show file tree
Hide file tree
Showing 3 changed files with 379 additions and 141 deletions.
191 changes: 137 additions & 54 deletions packages/client/src/contract-standards/storage-management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,96 +2,179 @@
* Tailored functionCall invocations for StorageManagement contract standard
*/

import { functionCall } from '../transactions/actions';
import type { StorageBalance, StorageManagement } from 'near-contract-standards/lib';
import type { FunctionCallParams, /*ViewParams */ } from '../interfaces';
import type { FinalExecutionOutcome } from '@near-js/types';
// import { view } from '../view';
import { functionCall } from "../transactions/actions";
import type {
StorageBalance,
StorageManagement,
StorageBalanceBounds,
} from "near-contract-standards/lib";
import type { FunctionCallParams, ViewParams } from "../interfaces";
import type { FinalExecutionOutcome } from "@near-js/types";
import { view } from "../view";

// import type { StorageManagement, StorageBalance, StorageBalanceBounds } from 'near-contract-standards/lib/storage_management';
// import { AccountId } from 'near-sdk-js';
// import { Option } from 'near-contract-standards/lib/non_fungible_token/utils';

type StorageManagementParams<T extends keyof StorageManagement> = Omit<FunctionCallParams, 'method' | 'args'> & {
args: Parameters<StorageManagement[T]>[0]
type StorageManagementParams<T extends keyof StorageManagement> = Omit<
FunctionCallParams,
"method" | "args"
> & {
args: Parameters<StorageManagement[T]>[0];
};

// type StorageManagementViewParams<T extends keyof StorageManagement> = Omit<ViewParams, 'method' | 'args'> & {
// args: Parameters<StorageManagement[T]>[0]
// };
type StorageManagementViewParams<T extends keyof StorageManagement> = Omit<
ViewParams,
"method" | "args"
> & {
args: Parameters<StorageManagement[T]>[0];
};

type StorageManagementReturn<T extends keyof StorageManagement> = {
outcome: FinalExecutionOutcome;
result: ReturnType<StorageManagement[T]>;
};
type StorageManagementViewReturn<T extends keyof StorageManagement> =
ReturnType<StorageManagement[T]>;

export async function storageDeposit(
params: StorageManagementParams<'storage_deposit'>
): Promise<StorageManagementReturn<'storage_deposit'>> {
const {outcome,result} = await functionCall({
params: StorageManagementParams<"storage_deposit">
): Promise<StorageManagementReturn<"storage_deposit">> {
const { outcome, result } = await functionCall({
...params,
method: 'storage_deposit'
method: "storage_deposit",
});


// Ensure the result matches the StorageBalance structure
if (typeof result === 'object' && typeof result?.['total'] === 'string' && typeof result?.['available'] === 'string') {
if (
typeof result === "object" &&
typeof result?.["total"] === "string" &&
typeof result?.["available"] === "string"
) {
const storageBalance = result as StorageBalance;

// cast string bigints to bigint literals
Object.assign(storageBalance, {
total: BigInt(storageBalance.total),
available: BigInt(storageBalance.available)
available: BigInt(storageBalance.available),
});

return {
outcome,
result: storageBalance
}
result: storageBalance,
};
}

throw new Error('Unexpected result format from storage_deposit');
}

// export async function storageWithdraw(
// params: StorageManagementParams<'storage_withdraw'>
// ): Promise<StorageManagementReturn<'storage_withdraw'>> {
// return functionCall({
// ...params,
// method: 'storage_withdraw'
// });
// }
throw new Error("Unexpected result format from storage_deposit");
}

export async function storageUnregister(
params: StorageManagementParams<'storage_unregister'>
): Promise<StorageManagementReturn<'storage_unregister'>> {
const {outcome,result} = await functionCall({
params: StorageManagementParams<"storage_unregister">
): Promise<StorageManagementReturn<"storage_unregister">> {
const { outcome, result } = await functionCall({
...params,
method: 'storage_unregister'
method: "storage_unregister",
});

console.log(result);
console.log('type', typeof result);
return {
outcome,
result: Boolean(result)
result: Boolean(result),
};
}

export async function storageWithdraw(
params: StorageManagementParams<"storage_withdraw">
): Promise<StorageManagementReturn<"storage_withdraw">> {

// bigints go over the wire as strings
const {args, ...otherParams} = params;
const convertedArgs = {
...args,
amount: args.amount.toString(),
};

const { outcome, result } = await functionCall({
args: convertedArgs,
...otherParams,
method: "storage_withdraw",
});

console.log("result", result);
console.log("outcome", outcome);
if (
typeof result === "object" &&
typeof result?.["total"] === "string" &&
typeof result?.["available"] === "string"
) {
const storageBalance = result as StorageBalance;

// cast string bigints to bigint literals
Object.assign(storageBalance, {
total: BigInt(storageBalance.total),
available: BigInt(storageBalance.available),
});

return {
outcome,
result: storageBalance,
};
} else {
throw new Error("Unexpected result format from storage_withdraw");
}
}

// export async function storageBalanceBounds(
// params: StorageManagementViewParams<'storage_balance_bounds'>
// ): Promise<StorageManagementReturn<'storage_balance_bounds'>> {
// return view({
// ...params,
// method: 'storage_balance_bounds',
// });
// }

// export async function storageBalanceOf(
// params: StorageManagementViewParams<'storage_balance_of'>
// ): Promise<StorageManagementReturn<'storage_balance_of'>> {
// return view({
// ...params,
// method: 'storage_balance_of'
// });
// }
export async function storageBalanceBounds(
params: StorageManagementViewParams<"storage_balance_bounds">
): Promise<StorageManagementViewReturn<"storage_balance_bounds">> {
const result = await view({
...params,
method: "storage_balance_bounds",
});

if (
typeof result === "object" &&
typeof result?.["min"] === "string" &&
typeof result?.["max"] === "string"
) {
const storageBalanceBounds = result as StorageBalanceBounds;

// cast string bigints to bigint literals
Object.assign(storageBalanceBounds, {
min: BigInt(storageBalanceBounds.min),
max: BigInt(storageBalanceBounds.max),
});

return storageBalanceBounds;
} else {
throw new Error("Unexpected result format from storage_balance_bounds");
}
}

export async function storageBalanceOf(
params: StorageManagementViewParams<"storage_balance_of">
): Promise<StorageManagementViewReturn<"storage_balance_of">> {
const result = await view({
...params,
method: "storage_balance_of",
});

if (
typeof result === "object" &&
typeof result?.["total"] === "string" &&
typeof result?.["available"] === "string"
) {
const storageBalance = result as StorageBalance;

// cast string bigints to bigint literals
Object.assign(storageBalance, {
total: BigInt(storageBalance.total),
available: BigInt(storageBalance.available),
});

return storageBalance;
} else if (result === null) {
return null;
} else {
throw new Error("Unexpected result format from storage_balance_of");
}
}
11 changes: 9 additions & 2 deletions packages/client/src/transactions/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ import { SignedTransactionComposer } from './composers';
* @param blockReference block ID/finality
* @param deps sign-and-send dependencies
*/
export function functionCall({ sender, receiver, method, args, gas, deposit, blockReference, deps }: FunctionCallParams) {
return SignedTransactionComposer.init({ sender, receiver, deps })
export async function functionCall({ sender, receiver, method, args, gas, deposit, blockReference, deps }: FunctionCallParams) {
const {outcome, result} = await SignedTransactionComposer.init({ sender, receiver, deps })
.functionCall(method, args, gas, deposit)
.signAndSend(blockReference);

if (typeof outcome.status === 'object' && 'Failure' in outcome.status) {
console.log(JSON.stringify(outcome.status, null, 2));
throw new Error(outcome.status.Failure.error_message);
}

return {outcome, result};
}

/**
Expand Down
Loading

0 comments on commit 07012ab

Please sign in to comment.