Skip to content

Commit

Permalink
Merge pull request #244 from InjectiveLabs/chore/msgGrantAllowance
Browse files Browse the repository at this point in the history
chore: feegrant module
  • Loading branch information
bangjelkoski authored Oct 13, 2023
2 parents 9158df2 + d136734 commit 18f2ddb
Show file tree
Hide file tree
Showing 15 changed files with 714 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitbook/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
* [Core Modules](core-modules/README.md)
* [Auction](core-modules/auction.md)
* [AuthZ](core-modules/authz.md)
- [Feegrant](core-modules/feegrant.md)
* [Bank](core-modules/bank.md)
* [Distribution](core-modules/distribution.md)
* [Exchange](core-modules/exchange.md)
Expand Down
1 change: 1 addition & 0 deletions .gitbook/core-modules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Within this section, we are going to explore the core modules of the Injective c
| ------------------------------- | ------------------------------------------------ |
| [Auction](auction.md) | Use for the buy-back-and-burn on chain mechanism |
| [AuthZ](authz.md) | Used for granting account priveledges |
| [Feegrant](feegrant.md) | Used for granting fee allowance priveledges |
| [Bank](bank.md) | Used for managing users assets (funds) |
| [Exchange](exchange.md) | Used for the exchange primitives |
| [Distribution](distribution.md) | Used for on-chain distribution/minting |
Expand Down
75 changes: 75 additions & 0 deletions .gitbook/core-modules/feegrant.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Fee Grant

The `feegrant` module allows accounts (granters) to grant fee allowances to other accounts (grantees). This allows the grantee to use the granter's funds to pay for transaction fees.

## Messages

### MsgGrantAllowance

A fee allowance grant is created using the `MsgGrantAllowance` message. If there is already a grant for the (granter, grantee) pair, then the new grant will overwrite the previous one.

```ts
import { MsgGrantAllowance, MsgBroadcasterWithPk } from '@injectivelabs/sdk-ts'
import { Network } from '@injectivelabs/networks'


const privateKeyOfGranter = '0x...'

const date = new Date('2023-10-02T00:00:00Z')
const expiration = date.getTime() / 1000
const granter = 'inj...'
const grantee = 'inj...'
const allowance = {
spendLimit: [
{
denom: 'inj',
amount: '10000',
},
],
expiration
}

const msg = MsgGrantAllowance.fromJSON({
granter,
grantee,
allowance,
})

const txHash = await new MsgBroadcasterWithPk({
privateKey: privateKeyOfGranter,
network: Network.Testnet,
}).broadcast({
msgs: msg,
})

console.log(txHash)

```

### MsgRevokeAllowance
A grant can be removed using the MsgRevokeAllowance message. The grantee will no longer be able to use the granter's funds to pay for transaction fees.

```ts
import { MsgRevokeAllowance, MsgBroadcasterWithPk } from '@injectivelabs/sdk-ts'
import { Network } from '@injectivelabs/networks'

const privateKey= "0x..."
const granteeAddress = 'inj...'
const granterAddress = 'inj...'

const params = {
grantee: granteeAddress,
granter: granterAddress,
}

const msg = MsgRevokeAllowance.fromJSON(params);

const txHash = await new MsgBroadcasterWithPk({
privateKey,
network: Network.Testnet,
}).broadcast({
msgs: msg,
})

console.log(txHash)
```
4 changes: 3 additions & 1 deletion .gitbook/core-modules/wasm.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ console.log(txHash);

### MsgExecuteContractCompact

There are some compatibility issue parsing the funds array in the previous example with EIP712, hence we introduced MsgExecuteContractCompact which converts the funds into a string
There are some compatibility issue parsing the funds array in the previous example with EIP712.
Since MsgExecuteContract can't be converted to EIP712 and then signed by ethereum wallets, we introduced MsgExecuteContractCompact which converts the funds into a string and therefore allows for EIP712 transformation.
Note that the MsgExecuteContract and MsgExecuteContractCompat underlying messages are the same. MsgExecuteContractCompat will just format for EIP712 compatibility.

An array of funds:

Expand Down
4 changes: 4 additions & 0 deletions packages/sdk-ts/src/core/modules/feegrant/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import MsgGrantAllowance from './msgs/MsgGrantAllowance'
import MsgRevokeAllowance from './msgs/MsgRevokeAllowance'

export { MsgGrantAllowance, MsgRevokeAllowance }
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import MsgGrantAllowance from './MsgGrantAllowance'
import { mockFactory } from '@injectivelabs/test-utils'
import { CosmosFeegrantV1Beta1Feegrant } from '@injectivelabs/core-proto-ts'
import snakecaseKeys from 'snakecase-keys'

const { injectiveAddress, injectiveAddress2 } = mockFactory

const params: MsgGrantAllowance['params'] = {
grantee: injectiveAddress,
granter: injectiveAddress2,
allowance: {
spendLimit: [
{
denom: 'inj',
amount: '1000',
},
],
expiration: 1679416772,
},
}

const protoType = '/cosmos.feegrant.v1beta1.MsgGrantAllowance'
const protoTypeShort = 'cosmos-sdk/MsgGrantAllowance'
const protoParams = {
grantee: params.grantee,
granter: params.granter,
allowance: {
typeUrl: '/cosmos.feegrant.v1beta1.BasicAllowance',
value: Uint8Array.from(
CosmosFeegrantV1Beta1Feegrant.BasicAllowance.encode({
spendLimit: params.allowance.spendLimit,
expiration: new Date(params.allowance.expiration! * 1000),
}).finish(),
),
},
}

const protoParamsAmino = snakecaseKeys({
grantee: params.grantee,
granter: params.granter,
allowance: {
type: 'cosmos-sdk/BasicAllowance',
value: {
spendLimit: params.allowance.spendLimit,
expiration: new Date(params.allowance.expiration! * 1000),
},
},
})

const protoParamsWeb3 = {
grantee: params.grantee,
granter: params.granter,
allowance: {
'@type': '/cosmos.feegrant.v1beta1.BasicAllowance',
spendLimit: params.allowance.spendLimit,
expiration: new Date(params.allowance.expiration! * 1000),
},
}
const message = MsgGrantAllowance.fromJSON(params)

describe('MsgGrantAllowance', () => {
it('generates proper proto', () => {
const message = MsgGrantAllowance.fromJSON(params)
const proto = message.toProto()

expect(proto).toStrictEqual({
...protoParams,
})
})

it('generates proper data', () => {
const data = message.toData()

expect(data).toStrictEqual({
'@type': protoType,
...protoParams,
})
})

it('generates proper amino', () => {
const amino = message.toAmino()

expect(amino).toStrictEqual({
type: protoTypeShort,
value: protoParamsAmino,
})
})

it('generates proper Eip712 types', () => {
const eip712Types = message.toEip712Types()

expect(Object.fromEntries(eip712Types)).toStrictEqual({
TypeAllowance: [
{ name: 'type', type: 'string' },
{ name: 'value', type: 'TypeAllowanceValue' },
],
TypeAllowanceValue: [
{ name: 'spend_limit', type: 'TypeAllowanceValueSpendLimit[]' },
{ name: 'expiration', type: 'string' },
],
TypeAllowanceValueSpendLimit: [
{ name: 'denom', type: 'string' },
{ name: 'amount', type: 'string' },
],
MsgValue: [
{ name: 'granter', type: 'string' },
{ name: 'grantee', type: 'string' },
{ name: 'allowance', type: 'TypeAllowance' },
],
})
})

it('generates proper Eip712 values', () => {
const eip712 = message.toEip712()

expect(eip712).toStrictEqual({
type: protoTypeShort,
value: snakecaseKeys({
...protoParamsAmino,
allowance: {
...protoParamsAmino.allowance,
value: {
...protoParamsAmino.allowance.value,
expiration:
protoParamsAmino.allowance.value.expiration
.toJSON()
.split('.')[0] + 'Z',
},
},
}),
})
})

it('generates proper direct sign', () => {
const directSign = message.toDirectSign()

expect(directSign).toStrictEqual({
type: protoType,
message: protoParams,
})
})

it('generates proper web3', () => {
const web3 = message.toWeb3()

expect(web3).toStrictEqual({
'@type': protoType,
...protoParamsWeb3,
})
})
})
Loading

0 comments on commit 18f2ddb

Please sign in to comment.