Skip to content

Commit

Permalink
feat: Add proxy methods to txwrapper-{substrate, polkadot} (#21)
Browse files Browse the repository at this point in the history
General clean up
  • Loading branch information
emostov committed Nov 30, 2020
1 parent 95825c7 commit 1c09a0e
Show file tree
Hide file tree
Showing 24 changed files with 745 additions and 222 deletions.
11 changes: 7 additions & 4 deletions package.json
Expand Up @@ -15,16 +15,19 @@
"test": "jest",
"docs": "typedoc"
},
"resolutions": {
"@polkadot/api": "^2.8.1"
},
"devDependencies": {
"@types/jest": "^26.0.0",
"@typescript-eslint/eslint-plugin": "^4.8.1",
"@typescript-eslint/parser": "^4.8.1",
"eslint": "^7.13.0",
"@typescript-eslint/eslint-plugin": "^4.8.2",
"@typescript-eslint/parser": "^4.8.2",
"eslint": "^7.14.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-simple-import-sort": "^6.0.1",
"jest": "^26.6.3",
"prettier": "^2.0.5",
"prettier": "^2.2.0",
"standard-version": "^9.0.0",
"ts-jest": "^26.4.4",
"ts-node": "^9.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/package.json
Expand Up @@ -12,7 +12,7 @@
"polkadot": "../../node_modules/.bin/ts-node src/polkadot.ts"
},
"dependencies": {
"@polkadot/api": "^2.4.1",
"@polkadot/api": "^2.8.1",
"@substrate/txwrapper-polkadot": "^0.0.0",
"ts-node": "9.0.0"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/txwrapper-core/package.json
Expand Up @@ -18,7 +18,7 @@
"test": "jest"
},
"dependencies": {
"@polkadot/api": "^2.4.1",
"@polkadot/api": "^2.8.1",
"@types/memoizee": "^0.4.3"
}
}
2 changes: 0 additions & 2 deletions packages/txwrapper-core/src/core/decode/decode.spec.ts
Expand Up @@ -20,13 +20,11 @@ describe('decode', () => {
TEST_BASE_TX_INFO,
POLKADOT_25_TEST_OPTIONS
);

const signingPayload = construct.signingPayload(
unsigned,
POLKADOT_25_TEST_OPTIONS
);
const signature = await signWithAlice(signingPayload);

const signedTx = construct.signedTx(
unsigned,
signature,
Expand Down
12 changes: 10 additions & 2 deletions packages/txwrapper-polkadot/src/index.ts
@@ -1,17 +1,25 @@
import { balances, utility } from '@substrate/txwrapper-substrate/src/methods';
import {
balances,
proxy,
utility,
} from '@substrate/txwrapper-substrate/src/methods';

// Export methods of pallets included in the Polkadot/ Kusama/ Westend runtimes.
// Note: in the future this may also include methods defined within this package
// that do not exist in Substrate.
export const methods = {
balances,
utility,
proxy,
};

// Rexport all of txwrapper-core so users have access to utilities, construct functions,
// decode function, and types.
export * from '@substrate/txwrapper-core';

// SS58 is not exported at the top level since most txwrapper-core users don't need it,
// so we have to reach in and grab it
export { PolkadotSS58Format } from '@substrate/txwrapper-core/src/polkadot';
// getRegistry{Polkadot, Westend, Kusama}

// exports getRegistry{Polkadot, Westend, Kusama}
export * from './getRegistry';
1 change: 1 addition & 0 deletions packages/txwrapper-substrate/src/methods/index.ts
@@ -1,3 +1,4 @@
// Name exports to create namespaces that map to pallets
export * as balances from './balances';
export * as proxy from './proxy';
export * as utility from './utility';
24 changes: 24 additions & 0 deletions packages/txwrapper-substrate/src/methods/proxy/addProxy.spec.ts
@@ -0,0 +1,24 @@
import {
itHasCorrectBaseTxInfo,
POLKADOT_25_TEST_OPTIONS,
TEST_BASE_TX_INFO,
} from '@substrate/txwrapper-core/src/test-helpers';

import { TEST_METHOD_ARGS } from '../../test-helpers';
import { addProxy } from './addProxy';

describe('proxy::addProxy', () => {
it('should work', () => {
const unsigned = addProxy(
TEST_METHOD_ARGS.proxy.addProxy,
TEST_BASE_TX_INFO,
POLKADOT_25_TEST_OPTIONS
);

itHasCorrectBaseTxInfo(unsigned);

expect(unsigned.method).toBe(
'0x1d018eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a480039300000'
);
});
});
48 changes: 48 additions & 0 deletions packages/txwrapper-substrate/src/methods/proxy/addProxy.ts
@@ -0,0 +1,48 @@
import {
Args,
BaseTxInfo,
defineMethod,
OptionsWithMeta,
UnsignedTransaction,
} from '@substrate/txwrapper-core';

export interface ProxyAddProxy extends Args {
/**
* The account that the `caller` would like to make a proxy.
*/
delegate: string;
/**
* The permissions for this proxy account. See the chain's runtime for the `call` filters.
*/
proxyType: string;
/**
* The number of blocks that an announcement must be in place for before the corresponding call
* may be dispatched. If zero, then no announcement is needed.
*/
delay: number | string;
}

/**
* Register a proxy account for the sender that is able to make calls on its behalf.
*
* @param args - Arguments specific to this method.
* @param info - Information required to construct the transaction.
* @param options - Registry and metadata used for constructing the method.
*/
export function addProxy(
args: ProxyAddProxy,
info: BaseTxInfo,
options: OptionsWithMeta
): UnsignedTransaction {
return defineMethod(
{
method: {
args,
name: 'addProxy',
pallet: 'proxy',
},
...info,
},
options
);
}
24 changes: 24 additions & 0 deletions packages/txwrapper-substrate/src/methods/proxy/announce.spec.ts
@@ -0,0 +1,24 @@
import {
itHasCorrectBaseTxInfo,
POLKADOT_25_TEST_OPTIONS,
TEST_BASE_TX_INFO,
} from '@substrate/txwrapper-core/src/test-helpers';

import { TEST_METHOD_ARGS } from '../../test-helpers';
import { announce } from './announce';

describe('proxy::announce', () => {
it('should work', () => {
const unsigned = announce(
TEST_METHOD_ARGS.proxy.announce,
TEST_BASE_TX_INFO,
POLKADOT_25_TEST_OPTIONS
);

itHasCorrectBaseTxInfo(unsigned);

expect(unsigned.method).toBe(
'0x1d068eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48474235772ae94433aee7d1befac0bfcc35fd0b5dfcf0cfc14bba7d5bbe35b778'
);
});
});
56 changes: 56 additions & 0 deletions packages/txwrapper-substrate/src/methods/proxy/announce.ts
@@ -0,0 +1,56 @@
import {
Args,
BaseTxInfo,
defineMethod,
OptionsWithMeta,
UnsignedTransaction,
} from '@substrate/txwrapper-core';

interface ProxyAnnounceArgs extends Args {
/**
* The account that the proxy will make a call on behalf of.
*/
real: string;
/**
* The hash of the call to be made by the `real` account.
*/
callHash: string;
}

/**
* Publish the hash of a proxy-call that will be made in the future.
*
* This must be called `ProxyDefinition.delay` blocks before the corresponding
* `proxy` is attempted if the delay associated with the proxy relationship is
* greater than zero. When a `ProxyDefinition.delay` is 0 `announce` is not neccesary
* and `proxy` can be called at any time.
*
* No more than `MaxPending` announcements may be made at any one time. On Kusama and Polkadot
* `MaxPending` is set to 32.
*
* This will take a deposit of `AnnouncementDepositFactor` as well as
* `AnnouncementDepositBase` if there are no other pending announcements.
*
* The dispatch origin for this call must be _Signed_ and a proxy of `real`.
*
* @param args - Arguments specific to this method.
* @param info - Information required to construct the transaction.
* @param options - Registry and metadata used for constructing the method.
*/
export function announce(
args: ProxyAnnounceArgs,
info: BaseTxInfo,
options: OptionsWithMeta
): UnsignedTransaction {
return defineMethod(
{
method: {
args,
name: 'announce',
pallet: 'proxy',
},
...info,
},
options
);
}
7 changes: 7 additions & 0 deletions packages/txwrapper-substrate/src/methods/proxy/index.ts
@@ -0,0 +1,7 @@
export * from './addProxy';
export * from './announce';
export * from './proxy';
export * from './proxyAnnounced';
export * from './rejectAnnouncement';
export * from './removeProxies';
export * from './removeProxy';
24 changes: 24 additions & 0 deletions packages/txwrapper-substrate/src/methods/proxy/proxy.spec.ts
@@ -0,0 +1,24 @@
import {
itHasCorrectBaseTxInfo,
POLKADOT_25_TEST_OPTIONS,
TEST_BASE_TX_INFO,
} from '@substrate/txwrapper-core/src/test-helpers';

import { TEST_METHOD_ARGS } from '../../test-helpers';
import { proxy } from './proxy';

describe('proxy::proxy', () => {
it('should work', () => {
const unsigned = proxy(
TEST_METHOD_ARGS.proxy.proxy,
TEST_BASE_TX_INFO,
POLKADOT_25_TEST_OPTIONS
);

itHasCorrectBaseTxInfo(unsigned);

expect(unsigned.method).toBe(
'0x1d008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a480100050030672121'
);
});
});
49 changes: 49 additions & 0 deletions packages/txwrapper-substrate/src/methods/proxy/proxy.ts
@@ -0,0 +1,49 @@
import {
Args,
BaseTxInfo,
defineMethod,
OptionsWithMeta,
UnsignedTransaction,
} from '@substrate/txwrapper-core';

export interface ProxyProxy extends Args {
/**
* Dispatch the given `call` from an account that the sender is authorized for
* through, `add_proxy`.
*/
real: string;
/**
* Specify the exact proxy type to be used and checked for this call.
*/
forceProxyType: string;
/**
* The call to be made by the `real` account.
* To take advantage of txwrapper methods, this could be UnsignedTransaction.method.
*/
call: { callIndex?: string; args?: string } | string;
}

/**
* Dispatch the given `call` from an account for which the sender is authorized.
*
* @param args - Arguments specific to this method.
* @param info - Information required to construct the transaction.
* @param options - Registry and metadata used for constructing the method.
*/
export function proxy(
args: ProxyProxy,
info: BaseTxInfo,
options: OptionsWithMeta
): UnsignedTransaction {
return defineMethod(
{
method: {
args,
name: 'proxy',
pallet: 'proxy',
},
...info,
},
options
);
}
@@ -0,0 +1,24 @@
import {
itHasCorrectBaseTxInfo,
POLKADOT_25_TEST_OPTIONS,
TEST_BASE_TX_INFO,
} from '@substrate/txwrapper-core/src/test-helpers';

import { TEST_METHOD_ARGS } from '../../test-helpers';
import { proxyAnnounced } from './proxyAnnounced';

describe('proxy::proxyAnnounced', () => {
it('should work', () => {
const unsigned = proxyAnnounced(
TEST_METHOD_ARGS.proxy.proxyAnnounced,
TEST_BASE_TX_INFO,
POLKADOT_25_TEST_OPTIONS
);

itHasCorrectBaseTxInfo(unsigned);

expect(unsigned.method).toBe(
'0x1d09d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a480100050030672121'
);
});
});

0 comments on commit 1c09a0e

Please sign in to comment.