-
Notifications
You must be signed in to change notification settings - Fork 196
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 #244 from InjectiveLabs/chore/msgGrantAllowance
chore: feegrant module
- Loading branch information
Showing
15 changed files
with
714 additions
and
1 deletion.
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
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,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) | ||
``` |
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,4 @@ | ||
import MsgGrantAllowance from './msgs/MsgGrantAllowance' | ||
import MsgRevokeAllowance from './msgs/MsgRevokeAllowance' | ||
|
||
export { MsgGrantAllowance, MsgRevokeAllowance } |
151 changes: 151 additions & 0 deletions
151
packages/sdk-ts/src/core/modules/feegrant/msgs/MsgGrantAllowance.spec.ts
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,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, | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.