Skip to content

Commit c2479f1

Browse files
committed
feat: storage management contract standards functions
1 parent 5db9b63 commit c2479f1

File tree

7 files changed

+1395
-27
lines changed

7 files changed

+1395
-27
lines changed

packages/client/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
"@near-js/utils": "workspace:*",
2424
"@noble/hashes": "1.3.3",
2525
"isomorphic-fetch": "3.0.0",
26-
"near-ledger-js": "0.2.1"
26+
"near-contract-standards": "^2.0.0",
27+
"near-ledger-js": "0.2.1",
28+
"near-sdk-js": "^2.0.0"
2729
},
2830
"keywords": [],
2931
"author": "",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './storage-management';
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* Tailored functionCall invocations for StorageManagement contract standard
3+
*/
4+
5+
import { functionCall } from '../transactions/actions';
6+
import type { StorageBalance, StorageManagement } from 'near-contract-standards/lib';
7+
import type { FunctionCallParams, /*ViewParams */ } from '../interfaces';
8+
import type { FinalExecutionOutcome } from '@near-js/types';
9+
// import { view } from '../view';
10+
11+
// import type { StorageManagement, StorageBalance, StorageBalanceBounds } from 'near-contract-standards/lib/storage_management';
12+
// import { AccountId } from 'near-sdk-js';
13+
// import { Option } from 'near-contract-standards/lib/non_fungible_token/utils';
14+
15+
type StorageManagementParams<T extends keyof StorageManagement> = Omit<FunctionCallParams, 'method' | 'args'> & {
16+
args: Parameters<StorageManagement[T]>[0]
17+
};
18+
19+
// type StorageManagementViewParams<T extends keyof StorageManagement> = Omit<ViewParams, 'method' | 'args'> & {
20+
// args: Parameters<StorageManagement[T]>[0]
21+
// };
22+
23+
type StorageManagementReturn<T extends keyof StorageManagement> = {
24+
outcome: FinalExecutionOutcome;
25+
result: ReturnType<StorageManagement[T]>;
26+
};
27+
28+
export async function storageDeposit(
29+
params: StorageManagementParams<'storage_deposit'>
30+
): Promise<StorageManagementReturn<'storage_deposit'>> {
31+
const {outcome,result} = await functionCall({
32+
...params,
33+
method: 'storage_deposit'
34+
});
35+
36+
37+
// Ensure the result matches the StorageBalance structure
38+
if (typeof result === 'object' && typeof result?.['total'] === 'string' && typeof result?.['available'] === 'string') {
39+
const storageBalance = result as StorageBalance;
40+
41+
// cast string bigints to bigint literals
42+
Object.assign(storageBalance, {
43+
total: BigInt(storageBalance.total),
44+
available: BigInt(storageBalance.available)
45+
});
46+
47+
return {
48+
outcome,
49+
result: storageBalance
50+
}
51+
}
52+
53+
throw new Error('Unexpected result format from storage_deposit');
54+
}
55+
56+
// export async function storageWithdraw(
57+
// params: StorageManagementParams<'storage_withdraw'>
58+
// ): Promise<StorageManagementReturn<'storage_withdraw'>> {
59+
// return functionCall({
60+
// ...params,
61+
// method: 'storage_withdraw'
62+
// });
63+
// }
64+
65+
export async function storageUnregister(
66+
params: StorageManagementParams<'storage_unregister'>
67+
): Promise<StorageManagementReturn<'storage_unregister'>> {
68+
const {outcome,result} = await functionCall({
69+
...params,
70+
method: 'storage_unregister'
71+
});
72+
73+
console.log(result);
74+
console.log('type', typeof result);
75+
return {
76+
outcome,
77+
result: Boolean(result)
78+
}
79+
}
80+
81+
// export async function storageBalanceBounds(
82+
// params: StorageManagementViewParams<'storage_balance_bounds'>
83+
// ): Promise<StorageManagementReturn<'storage_balance_bounds'>> {
84+
// return view({
85+
// ...params,
86+
// method: 'storage_balance_bounds',
87+
// });
88+
// }
89+
90+
// export async function storageBalanceOf(
91+
// params: StorageManagementViewParams<'storage_balance_of'>
92+
// ): Promise<StorageManagementReturn<'storage_balance_of'>> {
93+
// return view({
94+
// ...params,
95+
// method: 'storage_balance_of'
96+
// });
97+
// }

packages/client/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export * from './providers';
88
export * from './signing';
99
export * from './transactions';
1010
export * from './view';
11+
export * from './contract-standards';
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import {
2+
createFundedTestnetAccount,
3+
generateRandomKeyPair,
4+
getPlaintextFilesystemSigner,
5+
getTestnetRpcProvider,
6+
storageDeposit,
7+
storageUnregister,
8+
} from '@near-js/client';
9+
import chalk from 'chalk';
10+
import { join } from 'node:path';
11+
import { homedir } from 'node:os';
12+
13+
export default async function storageManagementStandard(contractAddress: string, accountId: string) {
14+
// run serially since calls may rely on previous calls
15+
await storageDepositCall(contractAddress, accountId);
16+
await storageUnregisterCall(contractAddress, accountId);
17+
}
18+
19+
export async function storageDepositCall(contractAddress: string, accountId: string) {
20+
if (!contractAddress || !accountId) {
21+
console.log(chalk`{red pnpm storageManagementStorageDeposit -- CONTRACT_ADDRESS ACCOUNT_ID}`);
22+
return;
23+
}
24+
25+
const credentialsPath = join(homedir(), '.near-credentials');
26+
27+
// initialize testnet RPC provider
28+
const rpcProvider = getTestnetRpcProvider();
29+
// initialize the transaction signer using a pre-existing key for `accountId`
30+
const { signer } = getPlaintextFilesystemSigner({ account: accountId, network: 'testnet', filepath: credentialsPath });
31+
32+
const {result} = await storageDeposit({
33+
receiver: contractAddress,
34+
sender: accountId,
35+
args: {
36+
account_id: accountId,
37+
registration_only: false,
38+
},
39+
deps: {
40+
rpcProvider,
41+
signer,
42+
},
43+
deposit: 1000000000000000000000000n,
44+
});
45+
46+
const output = chalk`
47+
{white ------------------------------------------------------------------------ }
48+
{bold.green RESULTS} {white storage_deposit}
49+
{white ------------------------------------------------------------------------ }
50+
{bold.white Total} {white |} {bold.yellow ${result.total}}
51+
{bold.white Available} {white |} {bold.yellow ${result.available}}
52+
{white ------------------------------------------------------------------------ }
53+
`;
54+
console.log(output);
55+
}
56+
57+
export async function storageUnregisterCall(contractAddress: string, accountId: string) {
58+
if (!contractAddress || !accountId) {
59+
console.log(chalk`{red pnpm storageManagementStorageDeposit -- CONTRACT_ADDRESS ACCOUNT_ID}`);
60+
return;
61+
}
62+
63+
const credentialsPath = join(homedir(), '.near-credentials');
64+
65+
// initialize testnet RPC provider
66+
const rpcProvider = getTestnetRpcProvider();
67+
// initialize the transaction signer using a pre-existing key for `accountId`
68+
const { signer } = getPlaintextFilesystemSigner({ account: accountId, network: 'testnet', filepath: credentialsPath });
69+
70+
const {result} = await storageUnregister({
71+
receiver: contractAddress,
72+
sender: accountId,
73+
args: {
74+
force: true,
75+
},
76+
deps: {
77+
rpcProvider,
78+
signer,
79+
},
80+
deposit: 1n,
81+
});
82+
console.log(`unregister result: ${result}`);
83+
console.log(`unregister result type: ${typeof result}`);
84+
85+
const output = chalk`
86+
{white ------------------------------------------------------------------------ }
87+
{bold.green RESULTS} {white storage_unregister}
88+
{white ------------------------------------------------------------------------ }
89+
{bold.white Result} {white |} {bold.yellow ${result}}
90+
{white ------------------------------------------------------------------------ }
91+
`;
92+
console.log(output);
93+
}

packages/cookbook/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"metaTransaction": "tsx -e \"import f from './transactions/meta-transaction.ts'; f(...process.argv.slice(1));\"",
2727
"metaTransactionRelayer": "tsx -e \"import f from './transactions/meta-transaction-relayer.ts'; f(...process.argv.slice(1));\"",
2828
"signWithLedger": "tsx -e \"import f from './utils/ledger-signing.ts'; f(...process.argv.slice(1));\"",
29+
"storageManagementStandard": "tsx -e \"import f from './contract-standards/storage-management.ts'; f(...process.argv.slice(1));\"",
2930
"traverseBlocks": "tsx -e \"import f from './transactions/traverse-blocks.ts'; f(...process.argv.slice(1));\"",
3031
"unwrapNear": "tsx -e \"import f from './utils/unwrap-near.ts'; f(...process.argv.slice(1));\"",
3132
"verifySignature": "tsx -e \"import f from './utils/verify-signature.ts'; f(...process.argv.slice(1));\"",
@@ -42,6 +43,7 @@
4243
"near-api-js": "workspace:*",
4344
"ts-node": "^10.9.2",
4445
"tsconfig": "workspace:*",
46+
"tsx": "^4.19.1",
4547
"typescript": "5.4.5"
4648
}
4749
}

0 commit comments

Comments
 (0)