-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement IncreaseBalanceTx (#888) * feat: add test transactions and complexity constants * feat: add IncreaseBalanceTx * chore: update ts-jest and jest config * fix: circular dependencies Closes #873. * test: add newIncreaseBalanceTx builder tests * docs: add example for increaseBalanceTx * chore: adjust example envs
- Loading branch information
1 parent
0097d86
commit 683b56f
Showing
31 changed files
with
737 additions
and
215 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
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,35 @@ | ||
import { addTxSignatures, pvm, utils } from '../../../src'; | ||
import { setupEtnaExample } from './utils/etna-helper'; | ||
import { getEnvVars } from '../../utils/getEnvVars'; | ||
|
||
const BALANCE_AVAX: number = 1; | ||
const VALIDATION_ID: string = ''; | ||
|
||
const increaseBalanceTx = async () => { | ||
const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY } = getEnvVars(); | ||
const { context, feeState, pvmApi } = await setupEtnaExample(AVAX_PUBLIC_URL); | ||
|
||
const { utxos } = await pvmApi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] }); | ||
|
||
const testPAddr = utils.bech32ToBytes(P_CHAIN_ADDRESS); | ||
|
||
const unsignedTx = pvm.e.newIncreaseBalanceTx( | ||
{ | ||
balance: BigInt(BALANCE_AVAX * 1e9), | ||
feeState, | ||
fromAddressesBytes: [testPAddr], | ||
utxos, | ||
validationId: VALIDATION_ID, | ||
}, | ||
context, | ||
); | ||
|
||
await addTxSignatures({ | ||
unsignedTx, | ||
privateKeys: [utils.hexToBuffer(PRIVATE_KEY)], | ||
}); | ||
|
||
return pvmApi.issueSignedTx(unsignedTx.getSignedTx()); | ||
}; | ||
|
||
await increaseBalanceTx().then(console.log); |
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 |
---|---|---|
@@ -1,21 +1,19 @@ | ||
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */ | ||
module.exports = { | ||
import { createDefaultEsmPreset, type JestConfigWithTsJest } from 'ts-jest'; | ||
|
||
const preset = createDefaultEsmPreset(); | ||
|
||
const jestConfig: JestConfigWithTsJest = { | ||
...preset, | ||
moduleFileExtensions: ['js', 'json', 'ts'], | ||
rootDir: 'src', | ||
transform: { | ||
'^.+\\.tsx?$': [ | ||
'ts-jest', | ||
{ | ||
useESM: true, | ||
}, | ||
], | ||
}, | ||
collectCoverageFrom: ['**/*.(t|j)s'], | ||
coverageDirectory: '../coverage', | ||
testEnvironment: 'node', | ||
coverageProvider: 'v8', | ||
extensionsToTreatAsEsm: ['.ts'], | ||
// Experimental to fix issues with BigInt serialization | ||
// See: https://jestjs.io/docs/configuration#workerthreads | ||
// @ts-expect-error - workerThreads is not in the type definition | ||
workerThreads: true, | ||
}; | ||
|
||
export default jestConfig; |
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
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
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 |
---|---|---|
@@ -1,5 +1,12 @@ | ||
import { testPVMCodec } from '../../../fixtures/codec'; | ||
import { pChainOwner, pChainOwnerBytes } from '../../../fixtures/pvm'; | ||
import { testSerialization } from '../../../fixtures/utils/serializable'; | ||
import { PChainOwner } from './pChainOwner'; | ||
|
||
testSerialization('PChainOwner', PChainOwner, pChainOwner, pChainOwnerBytes); | ||
testSerialization( | ||
'PChainOwner', | ||
PChainOwner, | ||
pChainOwner, | ||
pChainOwnerBytes, | ||
testPVMCodec, | ||
); |
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,12 @@ | ||
import { testPVMCodec } from '../../fixtures/codec'; | ||
import { increaseBalanceTx, increaseBalanceTxBytes } from '../../fixtures/pvm'; | ||
import { testSerialization } from '../../fixtures/utils/serializable'; | ||
import { IncreaseBalanceTx } from './increaseBalanceTx'; | ||
|
||
testSerialization( | ||
'IncreaseBalanceTx', | ||
IncreaseBalanceTx, | ||
increaseBalanceTx, | ||
increaseBalanceTxBytes, | ||
testPVMCodec, | ||
); |
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,44 @@ | ||
import { pack, unpack } from '../../utils/struct'; | ||
import { BaseTx } from '../avax/baseTx'; | ||
import type { Codec } from '../codec'; | ||
import { serializable } from '../common/types'; | ||
import { TypeSymbols } from '../constants'; | ||
import { Id } from '../fxs/common'; | ||
import { BigIntPr } from '../primitives'; | ||
import { PVMTx } from './abstractTx'; | ||
|
||
@serializable() | ||
export class IncreaseBalanceTx extends PVMTx { | ||
_type = TypeSymbols.IncreaseBalanceTx; | ||
|
||
constructor( | ||
public readonly baseTx: BaseTx, | ||
/** | ||
* The corresponding Validator ID. | ||
*/ | ||
public readonly validationId: Id, | ||
/** | ||
* Balance <= sum of AVAX inputs - sum of AVAX outputs - Tx fee | ||
*/ | ||
public readonly balance: BigIntPr, | ||
) { | ||
super(); | ||
} | ||
|
||
static fromBytes( | ||
bytes: Uint8Array, | ||
codec: Codec, | ||
): [increaseBalanceTx: IncreaseBalanceTx, rest: Uint8Array] { | ||
const [baseTx, validationId, balance, rest] = unpack( | ||
bytes, | ||
[BaseTx, Id, BigIntPr], | ||
codec, | ||
); | ||
|
||
return [new IncreaseBalanceTx(baseTx, validationId, balance), rest]; | ||
} | ||
|
||
toBytes(codec: Codec) { | ||
return pack([this.baseTx, this.validationId, this.balance], codec); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,5 +1,12 @@ | ||
import { testPVMCodec } from '../../fixtures/codec'; | ||
import { validator, validatorBytes } from '../../fixtures/pvm'; | ||
import { testSerialization } from '../../fixtures/utils/serializable'; | ||
import { Validator } from './validator'; | ||
|
||
testSerialization('Validator', Validator, validator, validatorBytes); | ||
testSerialization( | ||
'Validator', | ||
Validator, | ||
validator, | ||
validatorBytes, | ||
testPVMCodec, | ||
); |
Oops, something went wrong.