-
Notifications
You must be signed in to change notification settings - Fork 249
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: storage management contract standards functions
- Loading branch information
Showing
7 changed files
with
1,395 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './storage-management'; |
97 changes: 97 additions & 0 deletions
97
packages/client/src/contract-standards/storage-management.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/** | ||
* 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 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 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]>; | ||
}; | ||
|
||
export async function storageDeposit( | ||
params: StorageManagementParams<'storage_deposit'> | ||
): Promise<StorageManagementReturn<'storage_deposit'>> { | ||
const {outcome,result} = await functionCall({ | ||
...params, | ||
method: 'storage_deposit' | ||
}); | ||
|
||
|
||
// Ensure the result matches the StorageBalance structure | ||
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 | ||
} | ||
} | ||
|
||
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' | ||
// }); | ||
// } | ||
|
||
export async function storageUnregister( | ||
params: StorageManagementParams<'storage_unregister'> | ||
): Promise<StorageManagementReturn<'storage_unregister'>> { | ||
const {outcome,result} = await functionCall({ | ||
...params, | ||
method: 'storage_unregister' | ||
}); | ||
|
||
console.log(result); | ||
console.log('type', typeof result); | ||
return { | ||
outcome, | ||
result: Boolean(result) | ||
} | ||
} | ||
|
||
// 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' | ||
// }); | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
packages/cookbook/contract-standards/storage-management.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { | ||
createFundedTestnetAccount, | ||
generateRandomKeyPair, | ||
getPlaintextFilesystemSigner, | ||
getTestnetRpcProvider, | ||
storageDeposit, | ||
storageUnregister, | ||
} from '@near-js/client'; | ||
import chalk from 'chalk'; | ||
import { join } from 'node:path'; | ||
import { homedir } from 'node:os'; | ||
|
||
export default async function storageManagementStandard(contractAddress: string, accountId: string) { | ||
// run serially since calls may rely on previous calls | ||
await storageDepositCall(contractAddress, accountId); | ||
await storageUnregisterCall(contractAddress, accountId); | ||
} | ||
|
||
export async function storageDepositCall(contractAddress: string, accountId: string) { | ||
if (!contractAddress || !accountId) { | ||
console.log(chalk`{red pnpm storageManagementStorageDeposit -- CONTRACT_ADDRESS ACCOUNT_ID}`); | ||
return; | ||
} | ||
|
||
const credentialsPath = join(homedir(), '.near-credentials'); | ||
|
||
// initialize testnet RPC provider | ||
const rpcProvider = getTestnetRpcProvider(); | ||
// initialize the transaction signer using a pre-existing key for `accountId` | ||
const { signer } = getPlaintextFilesystemSigner({ account: accountId, network: 'testnet', filepath: credentialsPath }); | ||
|
||
const {result} = await storageDeposit({ | ||
receiver: contractAddress, | ||
sender: accountId, | ||
args: { | ||
account_id: accountId, | ||
registration_only: false, | ||
}, | ||
deps: { | ||
rpcProvider, | ||
signer, | ||
}, | ||
deposit: 1000000000000000000000000n, | ||
}); | ||
|
||
const output = chalk` | ||
{white ------------------------------------------------------------------------ } | ||
{bold.green RESULTS} {white storage_deposit} | ||
{white ------------------------------------------------------------------------ } | ||
{bold.white Total} {white |} {bold.yellow ${result.total}} | ||
{bold.white Available} {white |} {bold.yellow ${result.available}} | ||
{white ------------------------------------------------------------------------ } | ||
`; | ||
console.log(output); | ||
} | ||
|
||
export async function storageUnregisterCall(contractAddress: string, accountId: string) { | ||
if (!contractAddress || !accountId) { | ||
console.log(chalk`{red pnpm storageManagementStorageDeposit -- CONTRACT_ADDRESS ACCOUNT_ID}`); | ||
return; | ||
} | ||
|
||
const credentialsPath = join(homedir(), '.near-credentials'); | ||
|
||
// initialize testnet RPC provider | ||
const rpcProvider = getTestnetRpcProvider(); | ||
// initialize the transaction signer using a pre-existing key for `accountId` | ||
const { signer } = getPlaintextFilesystemSigner({ account: accountId, network: 'testnet', filepath: credentialsPath }); | ||
|
||
const {result} = await storageUnregister({ | ||
receiver: contractAddress, | ||
sender: accountId, | ||
args: { | ||
force: true, | ||
}, | ||
deps: { | ||
rpcProvider, | ||
signer, | ||
}, | ||
deposit: 1n, | ||
}); | ||
console.log(`unregister result: ${result}`); | ||
console.log(`unregister result type: ${typeof result}`); | ||
|
||
const output = chalk` | ||
{white ------------------------------------------------------------------------ } | ||
{bold.green RESULTS} {white storage_unregister} | ||
{white ------------------------------------------------------------------------ } | ||
{bold.white Result} {white |} {bold.yellow ${result}} | ||
{white ------------------------------------------------------------------------ } | ||
`; | ||
console.log(output); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.