Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: chang #150

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"devDependencies": {
"@blaze-cardano/eslint-config": "workspace:*",
"@changesets/cli": "^2.27.7",
"@types/node": "^20.14.12",
"@types/node": "^20.16.4",
"eslint": "^8.57.0",
"jest": "^29.7.0",
"prettier": "^3.3.3",
Expand Down
16 changes: 8 additions & 8 deletions packages/blaze-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@
"@blaze-cardano/eslint-config": "workspace:*",
"@blaze-cardano/tsconfig": "workspace:*",
"eslint": "^8.57.0",
"terser": "^5.31.3",
"tsup": "^8.2.3",
"terser": "^5.31.6",
"tsup": "^8.2.4",
"typescript": "^5.5.4"
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"@cardano-sdk/core": "^0.35.4",
"@cardano-sdk/crypto": "^0.1.28",
"@cardano-sdk/util": "^0.15.4",
"@noble/curves": "^1.4.2",
"@cardano-sdk/core": "^0.39.0",
"@cardano-sdk/crypto": "^0.1.30",
"@cardano-sdk/util": "^0.15.5",
"@noble/curves": "^1.6.0",
"@noble/ed25519": "^2.1.0",
"@noble/hashes": "^1.4.0",
"@scure/bip39": "^1.3.0",
"@noble/hashes": "^1.5.0",
"@scure/bip39": "^1.4.0",
"blakejs": "^1.2.1"
}
}
45 changes: 42 additions & 3 deletions packages/blaze-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ export type TokenMap = C.Cardano.TokenMap;
export const Transaction = C.Serialization.Transaction;
export type Transaction = C.Serialization.Transaction;

export const TxCBOR = C.TxCBOR;
export type TxCBOR = C.TxCBOR;

export const TransactionId = C.Cardano.TransactionId;
export type TransactionId = C.Cardano.TransactionId;

Expand Down Expand Up @@ -258,6 +255,48 @@ export const TransactionMetadatumKind =
C.Serialization.TransactionMetadatumKind;
export type TransactionMetadatumKind = C.Serialization.TransactionMetadatumKind;

export const VotingProcedures = C.Serialization.VotingProcedures;
export type VotingProcedures = C.Serialization.VotingProcedures;

export const ProposalProcedure = C.Serialization.ProposalProcedure;
export type ProposalProcedure = C.Serialization.ProposalProcedure;

export const InfoAction = C.Serialization.InfoAction;
export type InfoAction = C.Serialization.InfoAction;

export const Voter = C.Serialization.Voter;
export type Voter = C.Serialization.Voter

export const GovernanceActionId = C.Serialization.GovernanceActionId
export type GovernanceActionId = C.Serialization.GovernanceActionId

export const Anchor = C.Serialization.Anchor
export type Anchor = C.Serialization.Anchor

export const VotingProcedure = C.Serialization.VotingProcedure
export type VotingProcedure = C.Serialization.VotingProcedure

export const VoteDelegation = C.Serialization.VoteDelegation
export type VoteDelegation = C.Serialization.VoteDelegation

export const StakeVoteDelegation = C.Serialization.StakeVoteDelegation
export type StakeVoteDelegation = C.Serialization.StakeVoteDelegation

export type VoteDelegationCertificate = C.Cardano.VoteDelegationCertificate

export const voteNo = (x?: Anchor) => new VotingProcedure(C.Cardano.Vote.no, x)
export const voteYes = (x?: Anchor) => new VotingProcedure(C.Cardano.Vote.yes, x)
export const voteAbstain = (x?: Anchor) => new VotingProcedure(C.Cardano.Vote.abstain, x)

export const VoterType = C.Cardano.VoterType
export type VoterType = C.Cardano.VoterType

export const DRepID = C.Cardano.DRepID
export type DRepID = C.Cardano.DRepID

export const Drep = C.Serialization.DRep
export type Drep = C.Serialization.DRep

/**
* The SlotConfig interface defines the configuration for slots.
* @interface SlotConfig
Expand Down
110 changes: 108 additions & 2 deletions packages/blaze-tx/src/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import type {
StakeDelegationCertificate,
NetworkId,
AuxiliaryData,
Voter,
GovernanceActionId,
VotingProcedure
} from "@blaze-cardano/core";
import {
CborSet,
Expand Down Expand Up @@ -57,6 +60,9 @@ import {
blake2b_256,
RedeemerTag,
StakeRegistration,
VotingProcedures,
ProposalProcedure,
VoteDelegation
} from "@blaze-cardano/core";
import * as value from "./value";
import { micahsSelector, type SelectionResult } from "./coinSelection";
Expand Down Expand Up @@ -858,6 +864,7 @@ export class TxBuilder {
}
plutusData.setValues(plutusDataList);
tw.setPlutusData(plutusData);
console.log("draft tw", tw.toCore())
return tw;
}

Expand All @@ -878,8 +885,15 @@ export class TxBuilder {
withdrawalAmount += withdrawals.get(account)!;
}
}
let proposalAmount = 0n;
const proposals = this.body.proposalProcedures();
if (proposals !== undefined) {
for (const proposal of proposals.values()) {
proposalAmount += proposal.deposit();
}
}
// Initialize values for input, output, and minted amounts.
let inputValue = new Value(withdrawalAmount);
let inputValue = new Value(withdrawalAmount+proposalAmount);
let outputValue = new Value(bigintMax(this.fee, this.minimumFee));
const mintValue = new Value(0n, this.body.mint());

Expand Down Expand Up @@ -1068,6 +1082,7 @@ export class TxBuilder {
this.params.minFeeConstant +
fromHex(draft_tx.toCbor()).length * this.params.minFeeCoefficient;

console.log("minFeeCoefficient", this.params.minFeeCoefficient)
if (this.params.minFeeReferenceScripts) {
const utxoScope = [...this.utxoScope.values()];
const allInputs = [
Expand Down Expand Up @@ -1354,6 +1369,7 @@ export class TxBuilder {
this.balanceChange(excessValue);
// Create a draft transaction for fee calculation.
const draft_tx = new Transaction(this.body, tw, auxiliaryData);
console.log("draft length", draft_tx.toCbor().length)
// Calculate and set the transaction fee.
let draft_size = draft_tx.toCbor().length / 2;
this.calculateFees(draft_tx);
Expand Down Expand Up @@ -1417,7 +1433,9 @@ export class TxBuilder {
} while (final_size != draft_size);
// Return the fully constructed transaction.
tw.setVkeys(CborSet.fromCore([], VkeyWitness.fromCore));
return new Transaction(this.body, tw, this.auxiliaryData);
const tx = new Transaction(this.body, tw, this.auxiliaryData)
console.log("final length", tx.toCbor().length)
return tx;
}

/**
Expand Down Expand Up @@ -1476,6 +1494,60 @@ export class TxBuilder {
return this;
}

/**
* Adds a certificate to delegate a staker to a pool
*
* @param {Credential} delegator - The credential of the staker to delegate.
* @param {Credential} drep - The credential of the drep to delegate to.
* @param {PlutusData} [redeemer] - Optional. A redeemer to be used if the delegation requires Plutus script validation.
* @returns {TxBuilder} The updated transaction builder.
*/
addVoteDelegation(
delegator: Credential,
drep: Credential,
redeemer?: PlutusData,
): TxBuilder {
const delegatorCredential = delegator.toCore();
const voteDelegation: VoteDelegation = VoteDelegation.fromCore({
__typename: CertificateType.VoteDelegation,
stakeCredential: delegatorCredential,
dRep: drep.toCore(),
});
const delegationCertificate: Certificate = Certificate.newVoteDelegationCert(voteDelegation);
const certs =
this.body.certs() ?? CborSet.fromCore([], Certificate.fromCore);
const vals = [...certs.values(), delegationCertificate];
certs.setValues(vals);
this.body.setCerts(certs);
if (delegatorCredential.type == CredentialType.ScriptHash) {
if (redeemer) {
this.requiredPlutusScripts.add(delegatorCredential.hash);
const redeemers = [...this.redeemers.values()];
redeemers.push(
Redeemer.fromCore({
index: 256, // todo: fix
purpose: RedeemerPurpose["certificate"],
data: redeemer.toCore(),
executionUnits: {
memory: this.params.maxExecutionUnitsPerTransaction.memory,
steps: this.params.maxExecutionUnitsPerTransaction.steps,
},
}),
);
this.redeemers.setValues(redeemers);
} else {
this.requiredNativeScripts.add(delegatorCredential.hash);
}
} else if (redeemer) {
throw new Error(
"TxBuilder addDelegation: failing to attach redeemer to a non-script delegation!",
);
} else {
this.requiredWitnesses.add(HashAsPubKeyHex(delegatorCredential.hash));
}
return this;
}

/**
* This method delegates the selected reward address to a pool.
* It first checks if the reward address is set and if it has a stake component.
Expand Down Expand Up @@ -1652,6 +1724,40 @@ export class TxBuilder {
return this;
}

/**
* This method delegates the selected reward address to a pool.
* It first checks if the reward address is set and if it has a stake component.
* If both conditions are met, it adds a delegation to the transaction.
*
* @param {PoolId} poolId - The ID of the pool to delegate the reward address to.
* @throws {Error} If the reward address is not set or if the method is unimplemented.
*/
governanceVote(voter: Voter, actionId: GovernanceActionId, votingProcedure: VotingProcedure) {
const votingProcedures = this.body.votingProcedures() ?? new VotingProcedures()
votingProcedures.insert(voter, actionId, votingProcedure)
this.body.setVotingProcedures(votingProcedures)
return this
}

/**
* Adds a governance proposal to the transaction.
*
* This method adds a proposal procedure to the transaction's body. If there are existing
* proposal procedures, it appends the new one to the list. If there are no existing
* proposal procedures, it creates a new list with the provided procedure.
*
* @param {ProposalProcedure} proposalProcedure - The proposal procedure to be added to the transaction.
* @returns {TxBuilder} The same transaction builder instance, allowing for method chaining.
*/
governanceProposal(proposalProcedure: ProposalProcedure) {
const proposalProcedures = this.body.proposalProcedures() ?? CborSet.fromCore([], ProposalProcedure.fromCore)
const values = [...proposalProcedures.values()]
values.push(proposalProcedure)
proposalProcedures.setValues(values)
this.body.setProposalProcedures(proposalProcedures)
return this
}

/**
* Adds a required signer to the transaction. This is necessary for transactions that must be explicitly signed by a particular key.
*
Expand Down
Loading
Loading