Skip to content

Commit

Permalink
add sc deploy hook
Browse files Browse the repository at this point in the history
  • Loading branch information
juliancwirko committed Dec 3, 2023
1 parent 19def04 commit 3107ad5
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 32 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
### [0.13.0](https://github.com/useElven/core/releases/tag/v0.13.0) (2023-12-03)
- add useScDeploy hook for smart contract deployments - check [docs](https://www.useElven.com/docs/sdk-reference.html#usescdeploy()) for more informations

### [0.12.0](https://github.com/useElven/core/releases/tag/v0.12.0) (2023-11-30)
- add useSignMessage hook - check [docs](https://www.useElven.com) for more informations
- add useSignMessage hook - check [docs](https://www.useElven.com/docs/sdk-reference.html#usesignmessage()) for more informations
- min Node version is 18
- update dependencies

Expand Down
52 changes: 26 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@useelven/core",
"version": "0.12.0",
"version": "0.13.0",
"description": "Core React hooks for MultiversX DApps",
"license": "MIT",
"author": "Julian Ćwirko <julian.io>",
Expand Down Expand Up @@ -78,12 +78,12 @@
},
"devDependencies": {
"@types/lodash.clonedeep": "4.5.9",
"@types/node": "20.10.1",
"@types/react": "18.2.39",
"@types/node": "20.10.2",
"@types/react": "18.2.41",
"@typescript-eslint/eslint-plugin": "6.13.1",
"@typescript-eslint/parser": "6.13.1",
"eslint": "8.54.0",
"eslint-config-prettier": "9.0.0",
"eslint": "8.55.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-prettier": "5.0.1",
"eslint-plugin-react": "7.33.2",
"eslint-plugin-react-hooks": "4.6.0",
Expand Down
91 changes: 91 additions & 0 deletions src/hooks/useScDeploy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {
Address,
Code,
SmartContract,
CodeMetadata,
TypedValue,
} from '@multiversx/sdk-core';
import { useTransaction, TransactionArgs } from './useTransaction';
import { useAccount } from './useAccount';
import { useConfig } from './useConfig';
import { errorParse } from '../utils/errorParse';
import { useState } from 'react';

export interface ScDeployHookProps {
webWalletRedirectUrl?: TransactionArgs['webWalletRedirectUrl'];
cb?: TransactionArgs['cb'];
}

export interface ScDeployArgs {
source: Buffer | string;
gasLimit?: number;
codeMetadata?: [boolean, boolean, boolean, boolean];
initArguments?: TypedValue[];
}

export const useScDeploy = (
{ webWalletRedirectUrl, cb }: ScDeployHookProps = {
webWalletRedirectUrl: undefined,
cb: undefined,
}
) => {
const [scAddress, setScAddress] = useState<string>();
const { address: accountAddress, nonce } = useAccount();
const { shortId } = useConfig();

const { triggerTx, pending, transaction, txResult, error } = useTransaction({
webWalletRedirectUrl,
cb,
});

const deploy = async ({
source,
gasLimit = 10_000_000,
codeMetadata = [true, false, false, false],
initArguments = [],
}: ScDeployArgs) => {
try {
let code: Code;

if (Buffer.isBuffer(source)) {
code = Code.fromBuffer(source);
} else {
const response = await fetch(source);
const bytes = await response.arrayBuffer();
code = Code.fromBuffer(Buffer.from(bytes));
}

const smartContract = new SmartContract();
const tx = smartContract.deploy({
deployer: new Address(accountAddress),
code,
codeMetadata: new CodeMetadata(...codeMetadata),
initArguments,
gasLimit,
chainID: shortId || 'D',
});

setScAddress(
SmartContract.computeAddress(
new Address(accountAddress),
nonce
).bech32()
);

tx.setNonce(nonce);

triggerTx({ tx });
} catch (e) {
throw new Error(errorParse(e));
}
};

return {
deploy,
pending,
transaction,
txResult,
scAddress,
error,
};
};
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from './hooks/useNetwork';
export * from './hooks/useTokenTransfer';
export * from './hooks/useMultiTokenTransfer';
export * from './hooks/useSignMessage';
export * from './hooks/useScDeploy';
export { useApiCall } from './hooks/useApiCall';
export { useScQuery } from './hooks/useScQuery';

Expand Down

0 comments on commit 3107ad5

Please sign in to comment.