-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Emurgo/denis/yoext-963/basic-gov-functions
Basic gov functions
- Loading branch information
Showing
19 changed files
with
845 additions
and
217 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import React from 'react' | ||
import { | ||
getAddressFromBech32, | ||
getCslUtxos, | ||
getLargestFirstMultiAsset, | ||
getTransactionFromBytes, | ||
getTransactionOutput, | ||
getTransactionWitnessSetFromBytes, | ||
getTxBuilder, | ||
strToBigNum, | ||
getSignedTransaction, | ||
} from '../../utils/cslTools' | ||
import {bytesToHex} from '../../utils/utils' | ||
|
||
const Cip95BuildSignSubmitCard = (props) => { | ||
const {api, wasm, onWaiting, onError, getters, setters} = props | ||
const {setCertBuilder, setVotingBuilder} = setters | ||
const {certBuilder, votingBuilder, changeAddress, usedAddress, totalRefunds, hexUtxos} = getters | ||
|
||
const buildSignSubmit = () => { | ||
onWaiting(true) | ||
try { | ||
// build Tx | ||
const txBuilder = getTxBuilder(wasm) | ||
// adding certs, votes to the tx | ||
if (certBuilder) { | ||
txBuilder.set_certs_builder(certBuilder) | ||
setCertBuilder(null) | ||
} | ||
if (votingBuilder) { | ||
txBuilder.set_voting_builder(votingBuilder) | ||
setVotingBuilder(null) | ||
} | ||
// gov actions will be here in future | ||
|
||
// Set output and change addresses to those of our wallet | ||
const shelleyOutputAddress = getAddressFromBech32(wasm, usedAddress) | ||
const shelleyChangeAddress = getAddressFromBech32(wasm, changeAddress) | ||
|
||
// Add output of 1 ADA plus total needed for refunds | ||
let outputValue = strToBigNum(wasm, '1000000') | ||
if (totalRefunds.length > 0) { | ||
outputValue = outputValue.checked_add(strToBigNum(wasm, totalRefunds)) | ||
} | ||
|
||
txBuilder.add_output(getTransactionOutput(wasm, shelleyOutputAddress, outputValue)) | ||
// Find the available UTxOs in the wallet and use them as Inputs for the transaction | ||
const wasmUtxos = getCslUtxos(wasm, hexUtxos) | ||
txBuilder.add_inputs_from(wasmUtxos, getLargestFirstMultiAsset(wasm)) | ||
// Set change address, incase too much ADA provided for fee | ||
txBuilder.add_change_if_needed(shelleyChangeAddress) | ||
const wasmUnsignedTransaction = txBuilder.build_tx() | ||
// sign Tx | ||
const unsignedTxHex = bytesToHex(wasmUnsignedTransaction.to_bytes()) | ||
|
||
api | ||
.signTx(unsignedTxHex) | ||
.then((witnessHex) => { | ||
const wasmUnsignedTransaction = getTransactionFromBytes(wasm, unsignedTxHex) | ||
const wasmWitnessSet = getTransactionWitnessSetFromBytes(wasm, witnessHex) | ||
const wasmSignedTransaction = getSignedTransaction(wasm, wasmUnsignedTransaction, wasmWitnessSet) | ||
const signedTxHex = bytesToHex(wasmSignedTransaction.to_bytes()) | ||
|
||
// submit tx | ||
api | ||
.submitTx(signedTxHex) | ||
.then((txId) => { | ||
onWaiting(false) | ||
console.log('The transaction is sent:', txId) | ||
}) | ||
.catch((e) => { | ||
onWaiting(false) | ||
console.log(e) | ||
}) | ||
}) | ||
.catch((e) => { | ||
onWaiting(false) | ||
console.log(e) | ||
}) | ||
|
||
onWaiting(false) | ||
} catch (e) { | ||
console.error(e) | ||
onError() | ||
onWaiting(false) | ||
} | ||
} | ||
|
||
return ( | ||
<div className="flex"> | ||
<div className="flex-auto mt-5"> | ||
<button | ||
className="w-full py-1 rounded-md text-xl text-white font-semibold bg-green-700 hover:bg-green-800 active:bg-green-500" | ||
onClick={buildSignSubmit} | ||
> | ||
Build, Sign, Submit | ||
</button> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default Cip95BuildSignSubmitCard |
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,83 @@ | ||
import React, {useState} from 'react' | ||
import GovToolsPanel from '../govToolsPanel' | ||
import InputWithLabel from '../../inputWithLabel' | ||
import {getAnchor, getCertOfNewDRepReg, getDRepRegCert, getDRepRegWithAnchorCert} from '../../../utils/cslTools' | ||
import {bytesToHex} from '../../../utils/utils' | ||
|
||
const DRepRegistrationPanel = (props) => { | ||
const {wasm, onWaiting, onError, getters, setters, handleInputCreds} = props | ||
|
||
const {handleAddingCertInTx, setDRepIdInputValue} = setters | ||
const {dRepIdInputValue, getCertBuilder} = getters | ||
|
||
const [depositAmount, setDepositAmount] = useState('2000000') | ||
const [metadataURL, setMetadataURL] = useState('') | ||
const [metadataHash, setMetadataHash] = useState('') | ||
|
||
const buildDRepRegistrationCert = () => { | ||
onWaiting(true) | ||
const certBuilder = getCertBuilder(wasm) | ||
try { | ||
const dRepCred = handleInputCreds(dRepIdInputValue) | ||
let dRepRegCert = null | ||
if (metadataURL.length > 0) { | ||
const dataHash = | ||
metadataHash.length > 0 | ||
? metadataHash | ||
: bytesToHex(new TextEncoder().encode('{"testField": "_test__message_"}')) | ||
const anchor = getAnchor(wasm, metadataURL, dataHash) | ||
dRepRegCert = getDRepRegWithAnchorCert(wasm, dRepCred, depositAmount, anchor) | ||
} else { | ||
dRepRegCert = getDRepRegCert(wasm, dRepCred, depositAmount) | ||
} | ||
certBuilder.add(getCertOfNewDRepReg(wasm, dRepRegCert)) | ||
handleAddingCertInTx(certBuilder) | ||
onWaiting(false) | ||
} catch (error) { | ||
console.error(error) | ||
onWaiting(false) | ||
onError() | ||
} | ||
} | ||
|
||
const panelProps = { | ||
buttonName: 'Build Cert', | ||
certLabel: 'dRepRegistration', | ||
clickFunction: buildDRepRegistrationCert, | ||
} | ||
|
||
return ( | ||
<GovToolsPanel {...panelProps}> | ||
<InputWithLabel | ||
inputName="DRep ID (Bech32 or Hex encoded)" | ||
inputValue={dRepIdInputValue} | ||
onChangeFunction={(event) => { | ||
setDRepIdInputValue(event.target.value) | ||
}} | ||
/> | ||
<InputWithLabel | ||
inputName="DRep Registration Deposit Amount (lovelaces)" | ||
inputValue={depositAmount} | ||
onChangeFunction={(event) => { | ||
setDepositAmount(event.target.value) | ||
}} | ||
/> | ||
<InputWithLabel | ||
inputName="Metadata URL (Optional)" | ||
inputValue={metadataURL} | ||
onChangeFunction={(event) => { | ||
setMetadataURL(event.target.value) | ||
}} | ||
/> | ||
<InputWithLabel | ||
inputName="Metadata Hash (Optional)" | ||
inputValue={metadataHash} | ||
onChangeFunction={(event) => { | ||
setMetadataHash(event.target.value) | ||
}} | ||
/> | ||
</GovToolsPanel> | ||
) | ||
} | ||
|
||
export default DRepRegistrationPanel |
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,55 @@ | ||
import React, {useState} from 'react' | ||
import GovToolsPanel from '../govToolsPanel' | ||
import InputWithLabel from '../../inputWithLabel' | ||
import {getCertOfNewDRepRetirement, getDRepRetirementCert} from '../../../utils/cslTools' | ||
|
||
const DRepRetirementPanel = (props) => { | ||
const {wasm, onWaiting, onError, getters, setters, handleInputCreds} = props | ||
|
||
const {handleAddingCertInTx, setDRepIdInputValue} = setters | ||
const {dRepIdInputValue, getCertBuilder} = getters | ||
const [depositRefundAmount, setDepositRefundAmount] = useState('2000000') | ||
|
||
const buildDRepRetirementCert = () => { | ||
onWaiting(true) | ||
const certBuilder = getCertBuilder(wasm) | ||
try { | ||
const dRepCred = handleInputCreds(dRepIdInputValue) | ||
const dRepRetirementCert = getDRepRetirementCert(wasm, dRepCred, depositRefundAmount) | ||
certBuilder.add(getCertOfNewDRepRetirement(wasm, dRepRetirementCert)) | ||
handleAddingCertInTx(certBuilder) | ||
onWaiting(false) | ||
} catch (error) { | ||
console.error(error) | ||
onWaiting(false) | ||
onError() | ||
} | ||
} | ||
|
||
const panelProps = { | ||
buttonName: 'Build Cert', | ||
certLabel: 'dRepRetirement', | ||
clickFunction: buildDRepRetirementCert, | ||
} | ||
|
||
return ( | ||
<GovToolsPanel {...panelProps}> | ||
<InputWithLabel | ||
inputName="DRep ID (Bech32 or Hex encoded)" | ||
inputValue={dRepIdInputValue} | ||
onChangeFunction={(event) => { | ||
setDRepIdInputValue(event.target.value) | ||
}} | ||
/> | ||
<InputWithLabel | ||
inputName="DRep Registration Deposit Refund Amount (lovelaces)" | ||
inputValue={depositRefundAmount} | ||
onChangeFunction={(event) => { | ||
setDepositRefundAmount(event.target.value) | ||
}} | ||
/> | ||
</GovToolsPanel> | ||
) | ||
} | ||
|
||
export default DRepRetirementPanel |
Oops, something went wrong.