Skip to content

Commit

Permalink
fix: BNC part 2 - network refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lucanicoladebiasi committed Feb 7, 2025
1 parent c5ea205 commit 105ca6c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 276 deletions.
6 changes: 3 additions & 3 deletions docs/templates/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ In the following complete examples, we will explore the entire lifecycle of a Ve

1. **No Delegation (Signing Only with an Origin Private Key)**: In this scenario, we'll demonstrate the basic process of creating a transaction, signing it with the origin private key, and sending it to the VeChainThor blockchain without involving fee delegation.

[FullFlowNoDelegatorSnippet](examples/transactions/full-flow-no-gas-payer.ts)
[FullFlowNoGasPayerSnippet](examples/transactions/full-flow-no-gas-payer.ts)

2. **Delegation with Private Key**: Here, we'll extend the previous example by incorporating fee delegation. The transaction sender will delegate the transaction fee payment to another entity (gasPayer), and we'll guide you through the steps of building, signing, and sending such a transaction.

[FullFlowDelegatorPrivateKeySnippet](examples/transactions/full-flow-gas-payer-private-key.ts)
[FullFlowGasPayerPrivateKeySnippet](examples/transactions/full-flow-gas-payer-private-key.ts)

3. **Delegation with URL**: This example will showcase the use of a delegation URL for fee delegation. The sender will specify a delegation URL in the `signTransaction` options, allowing a designated sponsor to pay the transaction fee. We'll cover the full process, from building clauses to verifying the transaction on-chain.

[FullFlowDelegatorUrlSnippet](examples/transactions/full-flow-gas-payer-url.ts)
[FullFlowGasPayerUrlSnippet](examples/transactions/full-flow-gas-payer-url.ts)

By examining these complete examples, developers can gain a comprehensive understanding of transaction handling in the VeChain SDK. Each example demonstrates the steps involved in initiating, signing, and sending transactions, as well as the nuances associated with fee delegation.

Expand Down
276 changes: 3 additions & 273 deletions docs/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,289 +369,19 @@ In the following complete examples, we will explore the entire lifecycle of a Ve
1. **No Delegation (Signing Only with an Origin Private Key)**: In this scenario, we'll demonstrate the basic process of creating a transaction, signing it with the origin private key, and sending it to the VeChainThor blockchain without involving fee delegation.

```typescript { name=full-flow-no-gas-payer, category=example }
// 1 - Create the thor client
const thorSoloClient = ThorClient.at(THOR_SOLO_URL, {
isPollingEnabled: false
});

// Sender account with private key
const senderAccount: { privateKey: string; address: string } = {
privateKey:
'f9fc826b63a35413541d92d2bfb6661128cd5075fcdca583446d20c59994ba26',
address: '0x7a28e7361fd10f4f058f9fefc77544349ecff5d6'
};

// Create the provider (used in this case to sign the transaction with getSigner() method)
const provider = new VeChainProvider(
// Thor client used by the provider
thorSoloClient,

// Internal wallet used by the provider (needed to call the getSigner() method)
new ProviderInternalBaseWallet([
{
privateKey: HexUInt.of(senderAccount.privateKey).bytes,
address: senderAccount.address
}
]),

// Disable fee delegation (BY DEFAULT IT IS DISABLED)
false
);

// 2 - Create the transaction clauses
const transaction = {
clauses: [
Clause.transferVET(
Address.of('0xb717b660cd51109334bd10b2c168986055f58c1a'),
VET.of(1)
) as TransactionClause
],
simulateTransactionOptions: {
caller: senderAccount.address
}
};

// 3 - Estimate gas
const gasResult = await thorSoloClient.gas.estimateGas(
transaction.clauses,
transaction.simulateTransactionOptions.caller
);

// 4 - Build transaction body
const txBody = await thorSoloClient.transactions.buildTransactionBody(
transaction.clauses,
gasResult.totalGas
);

// 4 - Sign the transaction
const signer = await provider.getSigner(senderAccount.address);

const rawSignedTransaction = await signer.signTransaction(
signerUtils.transactionBodyToTransactionRequestInput(
txBody,
senderAccount.address
)
);

const signedTransaction = Transaction.decode(
HexUInt.of(rawSignedTransaction.slice(2)).bytes,
true
);

// 5 - Send the transaction
const sendTransactionResult =
await thorSoloClient.transactions.sendTransaction(signedTransaction);

// 6 - Wait for transaction receipt
const txReceipt = await thorSoloClient.transactions.waitForTransaction(
sendTransactionResult.id
);
Content not found between specified comments.
```

2. **Delegation with Private Key**: Here, we'll extend the previous example by incorporating fee delegation. The transaction sender will delegate the transaction fee payment to another entity (gasPayer), and we'll guide you through the steps of building, signing, and sending such a transaction.

```typescript { name=full-flow-gas-payer-private-key, category=example }
// 1 - Create the thor client
const thorSoloClient = ThorClient.at(THOR_SOLO_URL, {
isPollingEnabled: false
});

// Sender account with private key
const senderAccount: { privateKey: string; address: string } = {
privateKey:
'f9fc826b63a35413541d92d2bfb6661128cd5075fcdca583446d20c59994ba26',
address: '0x7a28e7361fd10f4f058f9fefc77544349ecff5d6'
};

// Gas-payer account with private key
const gasPayerAccount: { privateKey: string; address: string } = {
privateKey:
'521b7793c6eb27d137b617627c6b85d57c0aa303380e9ca4e30a30302fbc6676',
address: '0x062F167A905C1484DE7e75B88EDC7439f82117DE'
};

// Create the provider (used in this case to sign the transaction with getSigner() method)
const providerWithDelegationEnabled = new VeChainProvider(
// Thor client used by the provider
thorSoloClient,

// Internal wallet used by the provider (needed to call the getSigner() method)
new ProviderInternalBaseWallet(
[
{
privateKey: HexUInt.of(senderAccount.privateKey).bytes,
address: senderAccount.address
}
],
{
gasPayer: {
gasPayerPrivateKey: gasPayerAccount.privateKey
}
}
),

// Enable fee delegation
true
);

// 2 - Create the transaction clauses
const transaction = {
clauses: [
Clause.transferVET(
Address.of('0xb717b660cd51109334bd10b2c168986055f58c1a'),
VET.of(1)
) as TransactionClause
],
simulateTransactionOptions: {
caller: senderAccount.address
}
};

// 3 - Estimate gas
const gasResult = await thorSoloClient.gas.estimateGas(
transaction.clauses,
transaction.simulateTransactionOptions.caller
);

// 4 - Build transaction body
const txBody = await thorSoloClient.transactions.buildTransactionBody(
transaction.clauses,
gasResult.totalGas,
{
isDelegated: true
}
);

// 4 - Sign the transaction
const signer = await providerWithDelegationEnabled.getSigner(
senderAccount.address
);

const rawDelegateSigned = await signer.signTransaction(
signerUtils.transactionBodyToTransactionRequestInput(
txBody,
senderAccount.address
)
);

const delegatedSigned = Transaction.decode(
HexUInt.of(rawDelegateSigned.slice(2)).bytes,
true
);

// 5 - Send the transaction
const sendTransactionResult =
await thorSoloClient.transactions.sendTransaction(delegatedSigned);

// 6 - Wait for transaction receipt
const txReceipt = await thorSoloClient.transactions.waitForTransaction(
sendTransactionResult.id
);
Content not found between specified comments.
```

3. **Delegation with URL**: This example will showcase the use of a delegation URL for fee delegation. The sender will specify a delegation URL in the `signTransaction` options, allowing a designated sponsor to pay the transaction fee. We'll cover the full process, from building clauses to verifying the transaction on-chain.

```typescript { name=full-flow-gas-payer-url, category=example }
// 1 - Create the thor client
const thorClient = ThorClient.at(TESTNET_URL, {
isPollingEnabled: false
});

// Sender account with private key
const senderAccount: {
mnemonic: string;
privateKey: string;
address: string;
} = {
mnemonic:
'fat draw position use tenant force south job notice soul time fruit',
privateKey:
'2153c1e49c14d92e8b558750e4ec3dc9b5a6ac4c13d24a71e0fa4f90f4a384b5',
address: '0x571E3E1fBE342891778151f037967E107fb89bd0'
};

// Gas-payer account with private key
const gasPayerAccount = {
URL: 'https://sponsor-testnet.vechain.energy/by/269'
};

// Create the provider (used in this case to sign the transaction with getSigner() method)
const providerWithDelegationEnabled = new VeChainProvider(
// Thor client used by the provider
thorClient,

// Internal wallet used by the provider (needed to call the getSigner() method)
new ProviderInternalBaseWallet(
[
{
privateKey: HexUInt.of(senderAccount.privateKey).bytes,
address: senderAccount.address
}
],
{
gasPayer: {
gasPayerServiceUrl: gasPayerAccount.URL
}
}
),

// Enable fee delegation
true
);

// 2 - Create the transaction clauses
const transaction = {
clauses: [
Clause.transferVET(
Address.of('0xb717b660cd51109334bd10b2c168986055f58c1a'),
VET.of(1)
) as TransactionClause
],
simulateTransactionOptions: {
caller: senderAccount.address
}
};

// 3 - Estimate gas
const gasResult = await thorClient.gas.estimateGas(
transaction.clauses,
senderAccount.address
);

// 4 - Build transaction body
const txBody = await thorClient.transactions.buildTransactionBody(
transaction.clauses,
gasResult.totalGas,
{
isDelegated: true
}
);

// 4 - Sign the transaction
const signer = await providerWithDelegationEnabled.getSigner(
senderAccount.address
);

const rawDelegateSigned = await signer.signTransaction(
signerUtils.transactionBodyToTransactionRequestInput(
txBody,
senderAccount.address
)
);

const delegatedSigned = Transaction.decode(
HexUInt.of(rawDelegateSigned.slice(2)).bytes,
true
);

// 5 - Send the transaction
const sendTransactionResult =
await thorClient.transactions.sendTransaction(delegatedSigned);

// 6 - Wait for transaction receipt
const txReceipt = await thorClient.transactions.waitForTransaction(
sendTransactionResult.id
);
Content not found between specified comments.
```

By examining these complete examples, developers can gain a comprehensive understanding of transaction handling in the VeChain SDK. Each example demonstrates the steps involved in initiating, signing, and sending transactions, as well as the nuances associated with fee delegation.
Expand Down

0 comments on commit 105ca6c

Please sign in to comment.