-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lockADA-transaction): Moved transaction building to a separate m…
…odule BREAKING CHANGE: #101
- Loading branch information
Showing
2 changed files
with
110 additions
and
1 deletion.
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,101 @@ | ||
import { promises as fs } from 'fs'; | ||
import { | ||
Address, | ||
Assets, | ||
ByteArrayData, | ||
ConstrData, | ||
Datum, | ||
hexToBytes, | ||
IntData, | ||
ListData, | ||
NetworkEmulator, | ||
NetworkParams, | ||
Program, | ||
Tx, | ||
TxOutput, | ||
Value, | ||
} from "@hyperionbt/helios"; | ||
|
||
export const lockAda = async (params : any) => { | ||
|
||
const alice = params[0] as object; | ||
const bob = params[1] as object; | ||
const adaQty = params[2] as number; | ||
const duration = params[3] as number; | ||
|
||
const benAddr = bob.address; | ||
const emulatorDate = 1677108984000; | ||
const deadline = new Date(emulatorDate + duration); | ||
const benPkh = bob.pubKeyHash; | ||
const ownerPkh = alice.pubKeyHash; | ||
|
||
const lovelaceAmt = Number(adaQty) * 1000000; | ||
const adaAmountVal = new Value(BigInt(lovelaceAmt)); | ||
|
||
const datum = new ListData([new ByteArrayData(ownerPkh.bytes), | ||
new ByteArrayData(benPkh.bytes), | ||
new IntData(BigInt(deadline.getTime()))]); | ||
const inlineDatum = Datum.inline(datum); | ||
|
||
const inputUtxos = await alice.utxos; | ||
|
||
const tx = new Tx(); | ||
|
||
tx.addInputs(inputUtxos); | ||
|
||
const mintScript =`minting nft | ||
const TX_ID: ByteArray = #` + inputUtxos[0].txId.hex + ` | ||
const txId: TxId = TxId::new(TX_ID) | ||
const outputId: TxOutputId = TxOutputId::new(txId, ` + inputUtxos[0].utxoIdx + `) | ||
func main(ctx: ScriptContext) -> Bool { | ||
tx: Tx = ctx.tx; | ||
mph: MintingPolicyHash = ctx.get_current_minting_policy_hash(); | ||
assetclass: AssetClass = AssetClass::new( | ||
mph, | ||
"Vesting Key".encode_utf8() | ||
); | ||
value_minted: Value = tx.minted; | ||
// Validator logic starts | ||
(value_minted == Value::new(assetclass, 1)).trace("NFT1: ") && | ||
tx.inputs.any((input: TxInput) -> Bool { | ||
(input.output_id == outputId).trace("NFT2: ") | ||
} | ||
) | ||
}` | ||
|
||
const optimize = false; //maybe add to test context? | ||
const mintProgram = Program.new(mintScript).compile(optimize); | ||
|
||
tx.attachScript(mintProgram); | ||
|
||
// Construct the NFT that we will want to send as an output | ||
const nftTokenName = ByteArrayData.fromString("Vesting Key").toHex(); | ||
const tokens: [number[], bigint][] = [[hexToBytes(nftTokenName), BigInt(1)]]; | ||
|
||
// Create an empty Redeemer because we must always send a Redeemer with | ||
// a plutus script transaction even if we don't actually use it. | ||
const mintRedeemer = new ConstrData(0, []); | ||
|
||
// Indicate the minting we want to include as part of this transaction | ||
tx.mintTokens( | ||
mintProgram.mintingPolicyHash, | ||
tokens, | ||
mintRedeemer | ||
) | ||
|
||
const lockedVal = new Value(adaAmountVal.lovelace, new Assets([[mintProgram.mintingPolicyHash, tokens]])); | ||
|
||
// Add the destination address and the amount of Ada to lock including a datum | ||
tx.addOutput(new TxOutput(validatorAddress, lockedVal, inlineDatum)); | ||
|
||
// beforeAll? | ||
const networkParamsFile = await fs.readFile('./src/preprod.json', 'utf8'); | ||
const networkParams = new NetworkParams(JSON.parse(networkParamsFile.toString())); | ||
|
||
await tx.finalize(networkParams, alice.address); | ||
const txId = await network.submitTx(tx); | ||
} |
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