Skip to content

Commit

Permalink
[construction] Add support for balances.transfer (#59)
Browse files Browse the repository at this point in the history
Co-authored-by: Dmitryii Osipov <[email protected]>
  • Loading branch information
stefanmendoza-cb and osipov-mit authored Aug 14, 2023
1 parent 6253224 commit dd3e939
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
with:
node-version: 18.x

- name: Download Gear node
- name: Download Gear node [release=build]
run: wget -O ./gear https://github.com/gear-tech/gear/releases/download/build/gear

- name: Build spec
Expand Down
2 changes: 1 addition & 1 deletion config/gear-testnet.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gear_testnet",
"blockchain": "gear",
"network": "gear testnet",
"network": "testnet",
"wsAddress": "wss://archive-node.gear-tech.io",
"httpAddress": "https://archive-node.gear-tech.io",
"ss58Format": 42,
Expand Down
4 changes: 3 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"watch:vara": "nodemon src/index.ts --config-name vara",
"watch:vara-test": "nodemon src/index.ts --config-name vara_testnet",
"watch:offline": "nodemon src/index.ts --mode offline",
"build": "rm -rf dist && tsc"
"build": "rm -rf dist && tsc",
"lint": "yarn run eslint src/**/*.ts"
},
"keywords": [
"openapi-generator",
Expand Down Expand Up @@ -47,6 +48,7 @@
"winston": "3.10.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "latest",
"@typescript-eslint/parser": "^6.2.0",
"axios": "^1.4.0",
"chai": "^5.0.0-alpha.1",
Expand Down
52 changes: 31 additions & 21 deletions server/src/helpers/construct-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,54 @@ import logger from '../logger';
import { GearNetworkIdentifier } from '../networks';

export interface TxParams {
dest: string;
fromAddress: string;
toAddress: string;
value: string;
source: string;
blockHash: string;
blockNumber: number;
eraPeriod: number;
nonce: number;
tip: number;
specVersion: number;
transactionVersion: number;
disableKeepAlive: boolean,
networkIdent: GearNetworkIdentifier;
}

export function constructTx({
dest,
fromAddress,
toAddress,
value,
source,
blockHash,
blockNumber,
eraPeriod,
nonce,
networkIdent: { genesis, registry, specVersion, transactionVersion, metadataRpc, ss58Format },
tip,
specVersion,
transactionVersion,
disableKeepAlive,
networkIdent: { genesis, registry, metadataRpc, ss58Format },
}: TxParams) {
const unsigned = methods.balances.transferKeepAlive(
{ dest: { id: dest }, value },
{
address: deriveAddress(source, ss58Format),
blockHash,
blockNumber,
eraPeriod,
nonce,
genesisHash: genesis,
metadataRpc: metadataRpc,
specVersion,
transactionVersion,
},
{
const args = { dest: { id: toAddress }, value };
const info = {
address: deriveAddress(fromAddress, ss58Format),
blockHash,
blockNumber,
eraPeriod,
nonce,
tip,
genesisHash: genesis,
metadataRpc: metadataRpc,
specVersion,
transactionVersion,
};
const options = {
metadataRpc: metadataRpc,
registry,
},
);
};
const unsigned = disableKeepAlive
? methods.balances.transfer(args, info, options)
: methods.balances.transferKeepAlive(args, info, options);

const loggedUnsignedTx = unsigned;
loggedUnsignedTx.metadataRpc = "0x...truncated...";
Expand Down
8 changes: 7 additions & 1 deletion server/src/helpers/gear.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiPromise, WsProvider } from '@polkadot/api';
import { ApiDecoration } from '@polkadot/api/types';
import { Header, Index, SignedBlock, SyncState } from '@polkadot/types/interfaces';
import { Header, Index, SignedBlock } from '@polkadot/types/interfaces';
import { BlockIdentifier, Peer, SyncStatus } from 'rosetta-client';

import { NetworkConfig } from 'types';
Expand Down Expand Up @@ -95,11 +95,17 @@ export class GearApi {
const blockHash = await this.api.rpc.chain.getBlockHash(signingInfo.header.number.unwrap());
const eraPeriod = signingInfo.mortalLength;

const runtimeInfo = this.api.runtimeVersion;
const specVersion: number = runtimeInfo.specVersion.toNumber();
const transactionVersion: number = runtimeInfo.transactionVersion.toNumber();

return {
nonce,
blockHash,
blockNumber,
eraPeriod,
specVersion,
transactionVersion
};
}

Expand Down
25 changes: 19 additions & 6 deletions server/src/services/ConstructionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,16 @@ const constructionMetadata = async ({
const constructionPayloads = async ({
body: { operations, network_identifier, metadata },
}: ApiRequest<ConstructionPayloadsRequest>) => {
const { nonce, blockHash, blockNumber, eraPeriod } = metadata;
const {
blockHash,
blockNumber,
eraPeriod,
nonce,
tip,
specVersion,
transactionVersion,
disableKeepAlive
} = metadata;

const networkIdent = getNetworkIdent(network_identifier);

Expand All @@ -147,17 +156,21 @@ const constructionPayloads = async ({

const { value } = toOp.amount;

const dest = toOp.account.address;
let source = fromOp.account.address;
const fromAddress = fromOp.account.address;
const toAddress = toOp.account.address;

const txParams = {
dest,
fromAddress,
toAddress,
value,
source,
blockHash,
blockNumber,
eraPeriod,
nonce,
tip,
specVersion,
transactionVersion,
disableKeepAlive
};

const { unsignedTx, signingPayload } = constructTx({
Expand All @@ -167,7 +180,7 @@ const constructionPayloads = async ({

const payloads = [
{
account_identifier: new AccountIdentifier(source),
account_identifier: new AccountIdentifier(fromAddress),
hex_bytes: signingPayload,
signature_type: 'ed25519',
},
Expand Down
4 changes: 2 additions & 2 deletions test/development/gear.ros
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
create_account(1){
create{
network = {"network":"Development", "blockchain":"Gear"};
network = {"network":"development", "blockchain":"gear"};
key = generate_key({"curve_type": "edwards25519"});
account = derive({
"network_identifier": {{network}},
Expand All @@ -16,7 +16,7 @@ create_account(1){

transfer(1){
transfer{
transfer.network = {"network":"Development", "blockchain":"Gear"};
transfer.network = {"network":"development", "blockchain":"gear"};
currency = {"symbol":"GRT", "decimals":12};

sender = find_balance({
Expand Down
4 changes: 2 additions & 2 deletions test/testnet/gear.ros
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
create_account(1){
create{
network = {"network":"Gear Staging Testnet V7", "blockchain":"Gear"};
network = {"network":"testnet", "blockchain":"gear"};
key = generate_key({"curve_type": "edwards25519"});
account = derive({
"network_identifier": {{network}},
Expand All @@ -16,7 +16,7 @@ create_account(1){

transfer(1){
transfer{
transfer.network = {"network":"Gear Staging Testnet V7", "blockchain":"Gear"};
transfer.network = {"network":"testnet", "blockchain":"gear"};
currency = {"symbol":"GRT", "decimals":12};

sender = find_balance({
Expand Down
Loading

0 comments on commit dd3e939

Please sign in to comment.