Skip to content

Commit

Permalink
Merge branch 'main' into toshi/provider-basic-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
msmania authored Mar 10, 2024
2 parents db5a3bb + 96809f5 commit d5f6c26
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 87 deletions.
2 changes: 1 addition & 1 deletion packages/provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@
"abort-controller": "^3.0.0",
"buffer": "^6.0.3",
"debug": "^4.3.3",
"undici": "^5.0.0"
"undici": "^5.26.2"
}
}
11 changes: 11 additions & 0 deletions packages/transaction-builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ Returns a `MsgProtoAppStake`: The unsigned App Stake message.
| chains | `string[]` | Chains that the apps wants access to by staking POKT. Throughput will be equally divided through all chains. |
| amount | `string` | Amount of uPOKT to stake. |

#### appTransfer({ appPubKey }): MsgProtoAppStake

Builds a transaction message to transfer the slot of a staked app.
Signer must be an existing staked app to transfer the slot from.

Returns a `MsgProtoAppTransfer`: The unsigned AppTransfer message

|Param|Type|Description|
|--|--|--|
|appPubKey|`string`|Application public key to be transferred to|

#### appUnstake(address): MsgProtoAppUnstake

Adds a MsgProtoAppUnstake TxMsg for this transaction.
Expand Down
1 change: 1 addition & 0 deletions packages/transaction-builder/src/models/msgs/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './msg-proto-app-stake'
export * from './msg-proto-app-transfer'
export * from './msg-proto-app-unstake'
export * from './msg-proto-node-stake'
export * from './msg-proto-node-unjail'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Buffer } from 'buffer'
import { MsgProtoStake } from '../proto/generated/tx-signer'
import { Any } from '../proto/generated/google/protobuf/any'
import { TxMsg } from './tx-msg'

/**
* MsgProtoAppTransfer is a special case of MsgProtoAppStake where
* chains and amount are not set
*/
export class MsgProtoAppTransfer extends TxMsg {
public readonly KEY: string = '/x.apps.MsgProtoStake'
public readonly AMINO_KEY: string = 'apps/MsgAppStake'
public readonly pubKey: Buffer

/**
* Constructor for this class
* @param {Buffer} pubKey - Application public key to be transferred to
*/
constructor(pubKey: string) {
super()
this.pubKey = Buffer.from(pubKey, 'hex')
}

/**
* Converts an Msg Object to StdSignDoc
* @returns {object} - Msg type key value.
* @memberof MsgAppStake
*/
public toStdSignDocMsgObj(): object {
return {
type: this.AMINO_KEY,
value: {
chains: null,
pubkey: {
type: 'crypto/ed25519_public_key',
value: this.pubKey.toString('hex'),
},
value: '0',
},
}
}

/**
* Converts an Msg Object for StdTx encoding
* @returns {any} - Msg type key value.
* @memberof MsgAppStake
*/
public toStdTxMsgObj(): any {
const data : MsgProtoStake = {
pubKey: this.pubKey,
chains: [],
value: '0',
}

return Any.fromJSON({
typeUrl: this.KEY,
value: Buffer.from(MsgProtoStake.encode(data).finish()).toString(
'base64'
),
})
}
}
16 changes: 15 additions & 1 deletion packages/transaction-builder/src/tx-builder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Buffer } from 'buffer'
import {
MsgProtoAppStake,
MsgProtoAppTransfer,
MsgProtoAppUnstake,
MsgProtoNodeStakeTx,
MsgProtoNodeUnjail,
Expand Down Expand Up @@ -28,7 +29,6 @@ import { InvalidChainIDError, NoProviderError, NoSignerError } from './errors'
import { AbstractBuilder } from './abstract-tx-builder'
import { MsgProtoGovDAOTransfer } from './models/msgs/msg-proto-gov-dao-transfer'
import { MsgProtoGovChangeParam } from './models/msgs/msg-proto-gov-change-param'
import { Upgrade } from './models/proto/generated/tx-signer'
import { MsgProtoGovUpgrade } from './models/msgs/msg-proto-gov-upgrade'

export type ChainID = 'mainnet' | 'testnet' | 'localnet'
Expand Down Expand Up @@ -207,6 +207,20 @@ export class TransactionBuilder implements AbstractBuilder {
return new MsgProtoAppStake(appPubKey, chains, amount)
}

/**
* Builds a transaction message to transfer the slot of a staked app.
* Signer must be an existing staked app to transfer the slot from.
* @param {string} appPubKey - Application public key to be transferred to
* @returns {MsgProtoAppTransfer} - The unsigned AppTransfer message
*/
public appTransfer({
appPubKey,
}: {
appPubKey: string
}): MsgProtoAppTransfer {
return new MsgProtoAppTransfer(appPubKey)
}

/**
* Adds a MsgBeginAppUnstake TxMsg for this transaction
* @param {string} address - Address of the Application to unstake for
Expand Down
6 changes: 6 additions & 0 deletions packages/transaction-builder/tests/transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { RawTxRequest } from '@pokt-foundation/pocketjs-types'
import { MsgProtoGovUpgrade } from '../src/models/msgs/msg-proto-gov-upgrade'
import { MsgProtoGovChangeParam } from '../src/models/msgs/msg-proto-gov-change-param'
import { MsgProtoGovDAOTransfer } from '../src/models/msgs/msg-proto-gov-dao-transfer'
import { MsgProtoAppTransfer } from '../src/models/msgs/msg-proto-app-transfer'

const PRIVATE_KEY =
'1f8cbde30ef5a9db0a5a9d5eb40536fc9defc318b8581d543808b7504e0902bcb243b27bc9fbe5580457a46370ae5f03a6f6753633e51efdaf2cf534fdc26cc3'
Expand Down Expand Up @@ -84,6 +85,11 @@ describe('TransactionBuilder Tests', () => {
})
expect(appStakeMsg instanceof MsgProtoAppStake).toBe(true)

const appTransferMsg = transactionBuilder.appTransfer({
appPubKey: (await KeyManager.createRandom()).getPublicKey(),
})
expect(appTransferMsg instanceof MsgProtoAppTransfer).toBe(true)

const appUnstakeMsg = transactionBuilder.appUnstake(ADDRESS)
expect(appUnstakeMsg instanceof MsgProtoAppUnstake).toBe(true)

Expand Down
Loading

0 comments on commit d5f6c26

Please sign in to comment.