-
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.
Merge pull request #872 from ava-labs/erictaylor/p-chain-dynamic-fees
feat: p-chain dynamic fees
- Loading branch information
Showing
75 changed files
with
6,110 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"recommendations": ["streetsidesoftware.code-spell-checker"] | ||
} |
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,3 +1,4 @@ | ||
{ | ||
"typescript.preferences.importModuleSpecifier": "relative", | ||
"typescript.tsdk": "node_modules/typescript/lib" | ||
} | ||
} |
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,23 @@ | ||
{ | ||
"$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json", | ||
"version": "0.2", | ||
"dictionaries": [ | ||
"companies", | ||
"css", | ||
"en_us", | ||
"en-gb", | ||
"fullstack", | ||
"html", | ||
"lorem-ipsum", | ||
"node", | ||
"npm", | ||
"softwareTerms", | ||
"sql", | ||
"typescript" | ||
], | ||
"ignorePaths": ["node_modules", "__generated__", "build", "dist", "out"], | ||
"ignoreRegExpList": ["/.*[0-9].*/"], | ||
"language": "en", | ||
"minWordLength": 5, | ||
"words": ["amounter", "avalabs", "locktime", "stakeable", "unstakeable", "utxo", "utxos"] | ||
} |
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,42 @@ | ||
import { TransferableOutput, addTxSignatures, pvm, utils } from '../../../src'; | ||
import { getEnvVars } from '../../utils/getEnvVars'; | ||
import { getEtnaContextFromURI } from './utils/etna-context'; | ||
|
||
/** | ||
* The amount of AVAX to send to self. | ||
*/ | ||
const SEND_AVAX_AMOUNT: number = 0.001; | ||
|
||
const main = async () => { | ||
const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY } = getEnvVars(); | ||
|
||
const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL); | ||
|
||
const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL); | ||
|
||
const { utxos } = await pvmApi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] }); | ||
|
||
const tx = pvm.e.newBaseTx( | ||
{ | ||
fromAddressesBytes: [utils.bech32ToBytes(P_CHAIN_ADDRESS)], | ||
outputs: [ | ||
TransferableOutput.fromNative( | ||
context.avaxAssetID, | ||
BigInt(SEND_AVAX_AMOUNT * 1e9), | ||
[utils.bech32ToBytes(P_CHAIN_ADDRESS)], | ||
), | ||
], | ||
utxos, | ||
}, | ||
context, | ||
); | ||
|
||
await addTxSignatures({ | ||
unsignedTx: tx, | ||
privateKeys: [utils.hexToBuffer(PRIVATE_KEY)], | ||
}); | ||
|
||
return pvmApi.issueSignedTx(tx.getSignedTx()); | ||
}; | ||
|
||
main().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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { addTxSignatures, networkIDs, pvm, utils } from '../../../src'; | ||
import { getEnvVars } from '../../utils/getEnvVars'; | ||
import { getEtnaContextFromURI } from './utils/etna-context'; | ||
|
||
const AMOUNT_TO_DELEGATE_AVAX: number = 1; | ||
const DAYS_TO_DELEGATE: number = 14; | ||
|
||
const main = async () => { | ||
const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY } = getEnvVars(); | ||
|
||
const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL); | ||
|
||
const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL); | ||
|
||
const { utxos } = await pvmApi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] }); | ||
|
||
const startTime = await pvmApi.getTimestamp(); | ||
const startDate = new Date(startTime.timestamp); | ||
const start: bigint = BigInt(startDate.getTime() / 1_000); | ||
|
||
const endTime = new Date(startTime.timestamp); | ||
endTime.setDate(endTime.getDate() + DAYS_TO_DELEGATE); | ||
const end: bigint = BigInt(endTime.getTime() / 1_000); | ||
|
||
// TODO: Get this from an argument. | ||
const nodeId = 'NodeID-MqgFXT8JhorbEW2LpTDGePBBhv55SSp3M'; | ||
|
||
const tx = pvm.e.newAddPermissionlessDelegatorTx( | ||
{ | ||
end, | ||
fromAddressesBytes: [utils.bech32ToBytes(P_CHAIN_ADDRESS)], | ||
nodeId, | ||
rewardAddresses: [utils.bech32ToBytes(P_CHAIN_ADDRESS)], | ||
start, | ||
subnetId: networkIDs.PrimaryNetworkID.toString(), | ||
utxos, | ||
weight: BigInt(AMOUNT_TO_DELEGATE_AVAX * 1e9), | ||
}, | ||
context, | ||
); | ||
|
||
await addTxSignatures({ | ||
unsignedTx: tx, | ||
privateKeys: [utils.hexToBuffer(PRIVATE_KEY)], | ||
}); | ||
|
||
return pvmApi.issueSignedTx(tx.getSignedTx()); | ||
}; | ||
|
||
main().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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { TransferableOutput, addTxSignatures, pvm, utils } from '../../../src'; | ||
import { getEnvVars } from '../../utils/getEnvVars'; | ||
import { getEtnaContextFromURI } from './utils/etna-context'; | ||
|
||
const AMOUNT_TO_EXPORT_AVAX: number = 0.001; | ||
|
||
const main = async () => { | ||
const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY, X_CHAIN_ADDRESS } = | ||
getEnvVars(); | ||
|
||
const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL); | ||
|
||
const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL); | ||
|
||
const { utxos } = await pvmApi.getUTXOs({ | ||
addresses: [P_CHAIN_ADDRESS], | ||
}); | ||
|
||
const exportTx = pvm.e.newExportTx( | ||
{ | ||
destinationChainId: context.xBlockchainID, | ||
fromAddressesBytes: [utils.bech32ToBytes(P_CHAIN_ADDRESS)], | ||
outputs: [ | ||
TransferableOutput.fromNative( | ||
context.avaxAssetID, | ||
BigInt(AMOUNT_TO_EXPORT_AVAX * 1e9), | ||
[utils.bech32ToBytes(X_CHAIN_ADDRESS)], | ||
), | ||
], | ||
utxos, | ||
}, | ||
context, | ||
); | ||
|
||
await addTxSignatures({ | ||
unsignedTx: exportTx, | ||
privateKeys: [utils.hexToBuffer(PRIVATE_KEY)], | ||
}); | ||
|
||
return pvmApi.issueSignedTx(exportTx.getSignedTx()); | ||
}; | ||
|
||
main().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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { addTxSignatures, pvm, utils } from '../../../src'; | ||
import { getEnvVars } from '../../utils/getEnvVars'; | ||
import { getEtnaContextFromURI } from './utils/etna-context'; | ||
|
||
const main = async () => { | ||
const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY, X_CHAIN_ADDRESS } = | ||
getEnvVars(); | ||
|
||
const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL); | ||
|
||
const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL); | ||
|
||
const { utxos } = await pvmApi.getUTXOs({ | ||
sourceChain: 'X', | ||
addresses: [P_CHAIN_ADDRESS], | ||
}); | ||
|
||
const importTx = pvm.e.newImportTx( | ||
{ | ||
fromAddressesBytes: [utils.bech32ToBytes(X_CHAIN_ADDRESS)], | ||
sourceChainId: context.xBlockchainID, | ||
toAddresses: [utils.bech32ToBytes(P_CHAIN_ADDRESS)], | ||
utxos, | ||
}, | ||
context, | ||
); | ||
|
||
await addTxSignatures({ | ||
unsignedTx: importTx, | ||
privateKeys: [utils.hexToBuffer(PRIVATE_KEY)], | ||
}); | ||
|
||
return pvmApi.issueSignedTx(importTx.getSignedTx()); | ||
}; | ||
|
||
main().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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Context } from '../../../../src'; | ||
|
||
/** | ||
* Gets the context from URI and then modifies the context | ||
* to be used for testing example Etna transactions until Etna is enabled. | ||
*/ | ||
export const getEtnaContextFromURI = async ( | ||
uri: string, | ||
): Promise<Context.Context> => { | ||
const context = await Context.getContextFromURI(uri); | ||
|
||
return { | ||
...context, | ||
gasPrice: 10_000n, | ||
}; | ||
}; |
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,10 @@ | ||
import { base58check } from '../../../../src/utils'; | ||
|
||
export const getRandomNodeId = (): string => { | ||
const buffer = new Uint8Array(20); | ||
const randomBuffer = crypto.getRandomValues(buffer); | ||
|
||
const nodeId = `NodeID-${base58check.encode(randomBuffer)}`; | ||
|
||
return nodeId; | ||
}; |
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,64 @@ | ||
import { addTxSignatures, networkIDs, pvm, utils } from '../../../src'; | ||
import { getEnvVars } from '../../utils/getEnvVars'; | ||
import { getEtnaContextFromURI } from './utils/etna-context'; | ||
import { getRandomNodeId } from './utils/random-node-id'; | ||
|
||
const AMOUNT_TO_VALIDATE_AVAX: number = 1; | ||
const DAYS_TO_VALIDATE: number = 21; | ||
|
||
const nodeId = getRandomNodeId(); | ||
|
||
const main = async () => { | ||
const { AVAX_PUBLIC_URL, P_CHAIN_ADDRESS, PRIVATE_KEY } = getEnvVars(); | ||
|
||
const pvmApi = new pvm.PVMApi(AVAX_PUBLIC_URL); | ||
|
||
const context = await getEtnaContextFromURI(AVAX_PUBLIC_URL); | ||
|
||
const { utxos } = await pvmApi.getUTXOs({ addresses: [P_CHAIN_ADDRESS] }); | ||
|
||
const startTime = await pvmApi.getTimestamp(); | ||
const startDate = new Date(startTime.timestamp); | ||
const start: bigint = BigInt(startDate.getTime() / 1_000); | ||
|
||
const endTime = new Date(startTime.timestamp); | ||
endTime.setDate(endTime.getDate() + DAYS_TO_VALIDATE); | ||
const end: bigint = BigInt(endTime.getTime() / 1_000); | ||
|
||
const publicKey = utils.hexToBuffer( | ||
'0x8f95423f7142d00a48e1014a3de8d28907d420dc33b3052a6dee03a3f2941a393c2351e354704ca66a3fc29870282e15', | ||
); | ||
|
||
const signature = utils.hexToBuffer( | ||
'0x86a3ab4c45cfe31cae34c1d06f212434ac71b1be6cfe046c80c162e057614a94a5bc9f1ded1a7029deb0ba4ca7c9b71411e293438691be79c2dbf19d1ca7c3eadb9c756246fc5de5b7b89511c7d7302ae051d9e03d7991138299b5ed6a570a98', | ||
); | ||
|
||
const tx = pvm.e.newAddPermissionlessValidatorTx( | ||
{ | ||
end, | ||
delegatorRewardsOwner: [utils.bech32ToBytes(P_CHAIN_ADDRESS)], | ||
fromAddressesBytes: [utils.bech32ToBytes(P_CHAIN_ADDRESS)], | ||
nodeId, | ||
publicKey, | ||
rewardAddresses: [utils.bech32ToBytes(P_CHAIN_ADDRESS)], | ||
shares: 20 * 1e4, | ||
signature, | ||
start, | ||
subnetId: networkIDs.PrimaryNetworkID.toString(), | ||
utxos, | ||
weight: BigInt(AMOUNT_TO_VALIDATE_AVAX * 1e9), | ||
}, | ||
context, | ||
); | ||
|
||
await addTxSignatures({ | ||
unsignedTx: tx, | ||
privateKeys: [utils.hexToBuffer(PRIVATE_KEY)], | ||
}); | ||
|
||
return pvmApi.issueSignedTx(tx.getSignedTx()); | ||
}; | ||
|
||
main() | ||
.then(console.log) | ||
.then(() => console.log('Validate node ID:', nodeId)); |
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,17 @@ | ||
const AVAX_PUBLIC_URL = process.env['AVAX_PUBLIC_URL']; | ||
const P_CHAIN_ADDRESS = process.env['P_CHAIN_ADDRESS']; | ||
const PRIVATE_KEY = process.env['PRIVATE_KEY']; | ||
const X_CHAIN_ADDRESS = process.env['X_CHAIN_ADDRESS']; | ||
|
||
export const getEnvVars = () => { | ||
if (!(AVAX_PUBLIC_URL && P_CHAIN_ADDRESS && PRIVATE_KEY && X_CHAIN_ADDRESS)) { | ||
throw new Error('Missing environment variable(s).'); | ||
} | ||
|
||
return { | ||
AVAX_PUBLIC_URL, | ||
P_CHAIN_ADDRESS, | ||
PRIVATE_KEY, | ||
X_CHAIN_ADDRESS, | ||
}; | ||
}; |
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
Oops, something went wrong.