-
Notifications
You must be signed in to change notification settings - Fork 295
feat(sdk-coin-vet): add token transaction builder for vechain #6487
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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export const VET_TRANSACTION_ID_LENGTH = 64; | ||
export const VET_ADDRESS_LENGTH = 40; | ||
export const VET_BLOCK_ID_LENGTH = 64; | ||
|
||
export const TRANSFER_TOKEN_METHOD_ID = '0xa9059cbb'; |
This file contains hidden or 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
104 changes: 104 additions & 0 deletions
104
modules/sdk-coin-vet/src/lib/transaction/tokenTransaction.ts
This file contains hidden or 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,104 @@ | ||
import assert from 'assert'; | ||
import { Secp256k1, Transaction as VetTransaction } from '@vechain/sdk-core'; | ||
|
||
import { BaseCoin as CoinConfig } from '@bitgo/statics'; | ||
import { InvalidTransactionError, TransactionType } from '@bitgo/sdk-core'; | ||
import { Transaction } from './transaction'; | ||
import utils from '../utils'; | ||
|
||
import { VetTransactionData } from '../iface'; | ||
|
||
export class TokenTransaction extends Transaction { | ||
private _tokenAddress: string; | ||
|
||
constructor(_coinConfig: Readonly<CoinConfig>) { | ||
super(_coinConfig); | ||
this._type = TransactionType.Send; | ||
} | ||
|
||
get tokenAddress(): string { | ||
return this._tokenAddress; | ||
} | ||
|
||
set tokenAddress(address: string) { | ||
this._tokenAddress = address; | ||
} | ||
|
||
buildClauses(): void { | ||
if (!this.tokenAddress) { | ||
throw new Error('Token address is not set'); | ||
} | ||
this.clauses = this.recipients.map((recipient) => { | ||
const data = utils.getTransferTokenData(recipient.address, String(recipient.amount)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be imported from abstract-eth if possible as the code for data generation is same |
||
return { | ||
to: this.tokenAddress, | ||
value: '0x0', | ||
data, | ||
}; | ||
}); | ||
} | ||
|
||
toJson(): VetTransactionData { | ||
const json: VetTransactionData = { | ||
id: this.id, | ||
chainTag: this.chainTag, | ||
blockRef: this.blockRef, | ||
expiration: this.expiration, | ||
recipients: this.recipients, | ||
gasPriceCoef: this.gasPriceCoef, | ||
gas: this.gas, | ||
dependsOn: this.dependsOn, | ||
nonce: this.nonce, | ||
sender: this.sender, | ||
feePayer: this.feePayerAddress, | ||
tokenAddress: this.tokenAddress, | ||
}; | ||
|
||
return json; | ||
} | ||
|
||
fromDeserializedSignedTransaction(signedTx: VetTransaction): void { | ||
try { | ||
if (!signedTx || !signedTx.body) { | ||
throw new InvalidTransactionError('Invalid transaction: missing transaction body'); | ||
} | ||
|
||
// Store the raw transaction | ||
this.rawTransaction = signedTx; | ||
|
||
// Set transaction body properties | ||
const body = signedTx.body; | ||
this.chainTag = body.chainTag; | ||
this.blockRef = body.blockRef; | ||
this.expiration = body.expiration; | ||
this.clauses = body.clauses; | ||
this.gasPriceCoef = typeof body.gasPriceCoef === 'number' ? body.gasPriceCoef : 128; | ||
this.gas = Number(body.gas); | ||
this.dependsOn = body.dependsOn; | ||
this.nonce = String(body.nonce); | ||
// Set recipients from clauses | ||
assert(body.clauses[0].to, 'token address not found in the clauses'); | ||
this.tokenAddress = body.clauses[0].to; | ||
this.recipients = body.clauses.map((clause) => utils.decodeTransferTokenData(clause.data)); | ||
this.loadInputsAndOutputs(); | ||
|
||
// Set sender address | ||
if (signedTx.signature && signedTx.origin) { | ||
this.sender = signedTx.origin.toString().toLowerCase(); | ||
} | ||
|
||
// Set signatures if present | ||
if (signedTx.signature) { | ||
// First signature is sender's signature | ||
this.senderSignature = Buffer.from(signedTx.signature.slice(0, Secp256k1.SIGNATURE_LENGTH)); | ||
|
||
// If there's additional signature data, it's the fee payer's signature | ||
if (signedTx.signature.length > Secp256k1.SIGNATURE_LENGTH) { | ||
this.feePayerSignature = Buffer.from(signedTx.signature.slice(Secp256k1.SIGNATURE_LENGTH)); | ||
} | ||
} | ||
} catch (e) { | ||
throw new InvalidTransactionError(`Failed to deserialize transaction: ${e.message}`); | ||
} | ||
} | ||
} |
This file contains hidden or 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
80 changes: 80 additions & 0 deletions
80
modules/sdk-coin-vet/src/lib/transactionBuilder/tokenTransactionBuilder.ts
This file contains hidden or 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,80 @@ | ||
import { addHexPrefix } from 'ethereumjs-util'; | ||
import { BaseCoin as CoinConfig } from '@bitgo/statics'; | ||
import { TransactionType } from '@bitgo/sdk-core'; | ||
import { TransactionClause } from '@vechain/sdk-core'; | ||
|
||
import { TransactionBuilder } from './transactionBuilder'; | ||
import { TokenTransaction } from '../transaction/tokenTransaction'; | ||
import utils from '../utils'; | ||
|
||
export class TokenTransactionBuilder extends TransactionBuilder { | ||
constructor(_coinConfig: Readonly<CoinConfig>) { | ||
super(_coinConfig); | ||
} | ||
|
||
initBuilder(tx: TokenTransaction): void { | ||
this._transaction = tx; | ||
} | ||
|
||
get tokenTransaction(): TokenTransaction { | ||
return this._transaction as TokenTransaction; | ||
} | ||
|
||
protected get transactionType(): TransactionType { | ||
return TransactionType.Send; | ||
} | ||
|
||
/** | ||
* Validates the transaction clauses for flush token transaction. | ||
* @param {TransactionClause[]} clauses - The transaction clauses to validate. | ||
* @returns {boolean} - Returns true if the clauses are valid, false otherwise. | ||
*/ | ||
protected isValidTransactionClauses(clauses: TransactionClause[]): boolean { | ||
try { | ||
if (!clauses || !Array.isArray(clauses) || clauses.length === 0) { | ||
return false; | ||
} | ||
|
||
const clause = clauses[0]; | ||
|
||
if (!clause.to || !utils.isValidAddress(clause.to)) { | ||
return false; | ||
} | ||
|
||
// For token transactions, the value should be 0 | ||
if (clause.value !== 0) { | ||
return false; | ||
} | ||
|
||
const { address } = utils.decodeTransferTokenData(clause.data); | ||
const recipientAddress = addHexPrefix(address.toString()).toLowerCase(); | ||
|
||
if (!recipientAddress || !utils.isValidAddress(recipientAddress)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
tokenAddress(address: string): this { | ||
this.validateAddress({ address }); | ||
this.tokenTransaction.tokenAddress = address; | ||
return this; | ||
} | ||
|
||
/** @inheritdoc */ | ||
validateTransaction(transaction?: TokenTransaction): void { | ||
if (!transaction) { | ||
throw new Error('transaction not defined'); | ||
} | ||
|
||
if (!transaction.tokenAddress) { | ||
throw new Error('Token address is required'); | ||
} | ||
|
||
this.validateAddress({ address: transaction.tokenAddress }); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
8 changes: 3 additions & 5 deletions
8
modules/sdk-coin-vet/test/transactionBuilder/addressInitializationBuilder.ts
This file contains hidden or 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
6 changes: 2 additions & 4 deletions
6
modules/sdk-coin-vet/test/transactionBuilder/flushTokenTransactionBuilder.ts
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.