-
Notifications
You must be signed in to change notification settings - Fork 172
Implement IncreaseBalanceTx #888
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
87299c3
feat: add test transactions and complexity constants
erictaylor 8afca52
feat: add IncreaseBalanceTx
erictaylor 5710ba7
chore: update ts-jest and jest config
erictaylor e442228
fix: circular dependencies
erictaylor 96041b6
test: add newIncreaseBalanceTx builder tests
erictaylor cc04f76
docs: add example for increaseBalanceTx
erictaylor 52ba14b
chore: adjust example envs
erictaylor 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
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,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 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
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 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
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
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 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
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 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,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 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
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,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.
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.