-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19def04
commit 3107ad5
Showing
5 changed files
with
127 additions
and
32 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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, | ||
}; | ||
}; |
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