Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/fip 6 fiosdk typescript develop 10132020 #67

Merged
merged 2 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions lib/FIOSDK.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const Transactions_1 = require("./transactions/Transactions");
const constants_1 = require("./utils/constants");
const validation_1 = require("./utils/validation");
const ValidationError_1 = require("./entities/ValidationError");
const AccountName_1 = require("@fioprotocol/fiojs/dist/AccountName");
/**
* @ignore
*/
Expand Down Expand Up @@ -107,6 +108,17 @@ class FIOSDK {
const publicKey = Ecc.privateToPublic(fioPrivateKey);
return { publicKey };
}
/**
* hash a pub key
*
* @param fiopubkey FIO private key.
*
* @returns FIO account derived from pub key.
*/
static accountHash(fiopubkey) {
const accountnm = AccountName_1.accountHash(fiopubkey);
return { accountnm };
}
/**
* Is the Chain Code Valid?
*
Expand Down Expand Up @@ -395,6 +407,20 @@ class FIOSDK {
const removePublicAddresses = new SignedTransactions.RemovePublicAddresses(fioAddress, publicAddresses, maxFee, this.getTechnologyProviderId(technologyProviderId));
return removePublicAddresses.execute(this.privateKey, this.publicKey, this.returnPreparedTrx);
}
/**
* This call allows a user to transfer locked tokens to the specified fio public key
*
* @param payeePublicKey this is the fio public key for the user to receive locked tokens.
* @param canVote true if these locked tokens can be voted, false if these locked tokens are not to be voted.
* @param periods this is an array of lockperiods defining the duration and percent of each period, must be in time order.
* @param amount this is the amount in SUFs to be transfered.
* @param maxFee Maximum amount of SUFs the user is willing to pay for fee. Should be preceded by /get_fee for correct value.
* @param technologyProviderId FIO Address of the wallet which generates this transaction.
*/
transferLockedTokens(payeePublicKey, canVote, periods, amount, maxFee, technologyProviderId = null) {
const transferLockedTokens = new SignedTransactions.TransferLockedTokens(payeePublicKey, canVote, periods, amount, maxFee, this.getTechnologyProviderId(technologyProviderId));
return transferLockedTokens.execute(this.privateKey, this.publicKey);
}
/**
* This call allows a user to remove all addresses from the specified FIO Address, all addresses except the FIO address will be removed.
*
Expand Down Expand Up @@ -516,6 +542,24 @@ class FIOSDK {
return requestNewFunds.execute(this.privateKey, this.publicKey, this.returnPreparedTrx);
});
}
/**
* Retrieves info on locks for this pub key
*
* @param fioPublicKey FIO public key.
*/
getLocks(fioPublicKey) {
const getLocks = new queries.GetLocks(fioPublicKey);
return getLocks.execute(this.publicKey);
}
/*
* Retrieves info on account for this actor
*
* @param account FIO account.
*/
getAccount(actor) {
const getAccount = new queries.GetAccount(actor);
return getAccount.execute(this.publicKey);
}
/**
* Checks if a FIO Address or FIO Domain is available for registration.
*
Expand Down Expand Up @@ -628,6 +672,14 @@ class FIOSDK {
const transferTokens = new SignedTransactions.TransferTokens(payeeFioPublicKey, amount, maxFee, this.getTechnologyProviderId(technologyProviderId));
return transferTokens.execute(this.privateKey, this.publicKey, this.returnPreparedTrx);
}
/**
* Compute and return fee amount for specific call and specific user
*
* @param fioAddress FIO Address incurring the fee and owned by signer.
*/
getFeeForTransferLockedTokens(fioAddress) {
return this.getFee(EndPoint_1.EndPoint.transferLockedTokens, fioAddress);
}
/**
* Compute and return fee amount for specific call and specific user
*
Expand Down Expand Up @@ -759,6 +811,8 @@ class FIOSDK {
switch (action) {
case 'getFioPublicKey':
return this.getFioPublicKey();
case 'getAccount':
return this.getAccount(params.account);
case 'registerFioAddress':
if (params.ownerPublicKey) {
return this.registerOwnerFioAddress(params.fioAddress, params.ownerPublicKey, params.maxFee, params.technologyProviderId);
Expand All @@ -768,6 +822,8 @@ class FIOSDK {
}
case 'registerOwnerFioAddress':
return this.registerOwnerFioAddress(params.fioAddress, params.ownerPublicKey, params.maxFee, params.technologyProviderId);
case 'transferLockedTokens':
return this.transferLockedTokens(params.payeePublicKey, params.canVote, params.periods, params.amount, params.maxFee, params.technologyProviderId);
case 'registerFioDomain':
return this.registerFioDomain(params.fioDomain, params.maxFee, params.technologyProviderId);
case 'registerOwnerFioDomain':
Expand All @@ -788,6 +844,8 @@ class FIOSDK {
return this.addPublicAddresses(params.fioAddress, params.publicAddresses, params.maxFee, params.technologyProviderId);
case 'removePublicAddresses':
return this.removePublicAddresses(params.fioAddress, params.publicAddresses, params.maxFee, params.technologyProviderId);
case 'getLocks':
return this.getLocks(params.fioPublicKey);
case 'cancelFundsRequest':
return this.cancelFundsRequest(params.fioRequestId, params.maxFee, params.technologyProviderId);
case 'removeAllPublicAddresses':
Expand All @@ -796,6 +854,8 @@ class FIOSDK {
return this.setFioDomainVisibility(params.fioDomain, params.isPublic, params.maxFee, params.technologyProviderId);
case 'recordObtData':
return this.recordObtData(params.fioRequestId || null, params.payerFioAddress, params.payeeFioAddress, params.payerTokenPublicAddress, params.payeeTokenPublicAddress, params.amount, params.chainCode, params.tokenCode, params.status || '', params.obtId, params.maxFee, params.technologyProviderId, params.payeeFioPublicKey, params.memo, params.hash, params.offLineUrl);
case 'getFeeForTransferLockedTokens':
return this.getFeeForTransferLockedTokens(params.fioAddress);
case 'getObtData':
return this.getObtData(params.limit, params.offset, params.tokenCode);
case 'rejectFundsRequest':
Expand Down
2 changes: 2 additions & 0 deletions lib/entities/AccountResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
1 change: 1 addition & 0 deletions lib/entities/EndPoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ var EndPoint;
EndPoint["recordObtData"] = "record_obt_data";
EndPoint["transferTokens"] = "transfer_tokens_pub_key";
EndPoint["cancelFundsRequest"] = "cancel_funds_request";
EndPoint["transferLockedTokens"] = "transfer_locked_tokens";
})(EndPoint = exports.EndPoint || (exports.EndPoint = {}));
2 changes: 2 additions & 0 deletions lib/entities/LocksResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
15 changes: 15 additions & 0 deletions lib/transactions/queries/GetAccount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetAccount = void 0;
const Query_1 = require("./Query");
class GetAccount extends Query_1.Query {
constructor(actor) {
super();
this.ENDPOINT = 'chain/get_account';
this.accountToUse = actor;
}
getData() {
return { account_name: this.accountToUse };
}
}
exports.GetAccount = GetAccount;
15 changes: 15 additions & 0 deletions lib/transactions/queries/GetLocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetLocks = void 0;
const Query_1 = require("./Query");
class GetLocks extends Query_1.Query {
constructor(fioPublicKey) {
super();
this.ENDPOINT = 'chain/get_locks';
this.keyToUse = fioPublicKey;
}
getData() {
return { fio_public_key: this.keyToUse };
}
}
exports.GetLocks = GetLocks;
6 changes: 5 additions & 1 deletion lib/transactions/queries/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetDomains = exports.GetAddresses = exports.CancelledFioRequests = exports.GetObtData = exports.GetAbi = exports.GetFee = exports.PendingFioRequests = exports.GetFioBalance = exports.GetNames = exports.SentFioRequests = exports.GetPublicAddress = exports.AvailabilityCheck = void 0;
exports.GetAccount = exports.GetLocks = exports.GetDomains = exports.GetAddresses = exports.CancelledFioRequests = exports.GetObtData = exports.GetAbi = exports.GetFee = exports.PendingFioRequests = exports.GetFioBalance = exports.GetNames = exports.SentFioRequests = exports.GetPublicAddress = exports.AvailabilityCheck = void 0;
var AvailabilityCheck_1 = require("./AvailabilityCheck");
Object.defineProperty(exports, "AvailabilityCheck", { enumerable: true, get: function () { return AvailabilityCheck_1.AvailabilityCheck; } });
var GetPublicAddress_1 = require("./GetPublicAddress");
Expand All @@ -25,3 +25,7 @@ var GetAddresses_1 = require("./GetAddresses");
Object.defineProperty(exports, "GetAddresses", { enumerable: true, get: function () { return GetAddresses_1.GetAddresses; } });
var GetDomains_1 = require("./GetDomains");
Object.defineProperty(exports, "GetDomains", { enumerable: true, get: function () { return GetDomains_1.GetDomains; } });
var GetLocks_1 = require("./GetLocks");
Object.defineProperty(exports, "GetLocks", { enumerable: true, get: function () { return GetLocks_1.GetLocks; } });
var GetAccount_1 = require("./GetAccount");
Object.defineProperty(exports, "GetAccount", { enumerable: true, get: function () { return GetAccount_1.GetAccount; } });
1 change: 1 addition & 0 deletions lib/transactions/signed/TransferLockedTokens.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransferLockedTokens = void 0;
const SignedTransaction_1 = require("./SignedTransaction");
const validation_1 = require("../../utils/validation");
class TransferLockedTokens extends SignedTransaction_1.SignedTransaction {
Expand Down
4 changes: 3 additions & 1 deletion lib/transactions/signed/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CancelFundsRequest = exports.PushTransaction = exports.TransferFioAddress = exports.TransferFioDomain = exports.BurnFioAddress = exports.RenewFioDomain = exports.RenewFioAddress = exports.MockRegisterFioName = exports.RegisterFioDomain = exports.RegisterFioAddress = exports.TransferTokens = exports.RecordObtData = exports.RejectFundsRequest = exports.RequestNewFunds = exports.SetFioDomainVisibility = exports.RemoveAllPublicAddresses = exports.RemovePublicAddresses = exports.AddPublicAddress = void 0;
exports.TransferLockedTokens = exports.CancelFundsRequest = exports.PushTransaction = exports.TransferFioAddress = exports.TransferFioDomain = exports.BurnFioAddress = exports.RenewFioDomain = exports.RenewFioAddress = exports.MockRegisterFioName = exports.RegisterFioDomain = exports.RegisterFioAddress = exports.TransferTokens = exports.RecordObtData = exports.RejectFundsRequest = exports.RequestNewFunds = exports.SetFioDomainVisibility = exports.RemoveAllPublicAddresses = exports.RemovePublicAddresses = exports.AddPublicAddress = void 0;
var AddPublicAddress_1 = require("./AddPublicAddress");
Object.defineProperty(exports, "AddPublicAddress", { enumerable: true, get: function () { return AddPublicAddress_1.AddPublicAddress; } });
var RemovePublicAddresses_1 = require("./RemovePublicAddresses");
Expand Down Expand Up @@ -37,3 +37,5 @@ var PushTransaction_1 = require("./PushTransaction");
Object.defineProperty(exports, "PushTransaction", { enumerable: true, get: function () { return PushTransaction_1.PushTransaction; } });
var CancelFundsRequest_1 = require("./CancelFundsRequest");
Object.defineProperty(exports, "CancelFundsRequest", { enumerable: true, get: function () { return CancelFundsRequest_1.CancelFundsRequest; } });
var TransferLockedTokens_1 = require("./TransferLockedTokens");
Object.defineProperty(exports, "TransferLockedTokens", { enumerable: true, get: function () { return TransferLockedTokens_1.TransferLockedTokens; } });
3 changes: 3 additions & 0 deletions lib/utils/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ exports.validationRules = {
tokenCode: exports.allRules.chain,
tpid: exports.allRules.tpid,
},
transferLockedTokensRequest: {
tpid: exports.allRules.tpid,
},
rejectFunds: {
tpid: exports.allRules.tpid,
},
Expand Down
92 changes: 92 additions & 0 deletions src/FIOSDK.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Fio } from '@fioprotocol/fiojs'
import {LockPeriod} from './entities/LockPeriod'
import {
AbiResponse,
AddPublicAddressResponse,
CancelFundsRequestResponse,
TransferLockedTokensResponse,
AccountResponse,
LocksResponse,
RemovePublicAddressesResponse,
RemoveAllPublicAddressesResponse,
BurnFioAddressResponse,
Expand Down Expand Up @@ -36,6 +40,7 @@ import { Transactions } from './transactions/Transactions'
import { Constants } from './utils/constants'
import { validate, allRules } from './utils/validation'
import { ValidationError } from './entities/ValidationError'
import { accountHash } from '@fioprotocol/fiojs/dist/AccountName'

/**
* @ignore
Expand Down Expand Up @@ -94,6 +99,18 @@ export class FIOSDK {
return { publicKey }
}

/**
* hash a pub key
*
* @param fiopubkey FIO private key.
*
* @returns FIO account derived from pub key.
*/
public static accountHash(fiopubkey: string) {
const accountnm = accountHash(fiopubkey)
return { accountnm }
}

/**
* Is the Chain Code Valid?
*
Expand Down Expand Up @@ -608,6 +625,36 @@ export class FIOSDK {
return removePublicAddresses.execute(this.privateKey, this.publicKey, this.returnPreparedTrx)
}

/**
* This call allows a user to transfer locked tokens to the specified fio public key
*
* @param payeePublicKey this is the fio public key for the user to receive locked tokens.
* @param canVote true if these locked tokens can be voted, false if these locked tokens are not to be voted.
* @param periods this is an array of lockperiods defining the duration and percent of each period, must be in time order.
* @param amount this is the amount in SUFs to be transfered.
* @param maxFee Maximum amount of SUFs the user is willing to pay for fee. Should be preceded by /get_fee for correct value.
* @param technologyProviderId FIO Address of the wallet which generates this transaction.
*/
public transferLockedTokens(
payeePublicKey: string,
canVote: boolean,
periods: LockPeriod[],
amount: number,
maxFee: number,
technologyProviderId: string | null = null,
): Promise<TransferLockedTokensResponse> {
const transferLockedTokens = new SignedTransactions.TransferLockedTokens(
payeePublicKey,
canVote,
periods,
amount,
maxFee,
this.getTechnologyProviderId(technologyProviderId),
)
return transferLockedTokens.execute(this.privateKey, this.publicKey)
}



/**
* This call allows a user to remove all addresses from the specified FIO Address, all addresses except the FIO address will be removed.
Expand Down Expand Up @@ -828,6 +875,26 @@ export class FIOSDK {
return requestNewFunds.execute(this.privateKey, this.publicKey, this.returnPreparedTrx)
}

/**
* Retrieves info on locks for this pub key
*
* @param fioPublicKey FIO public key.
*/
public getLocks(fioPublicKey: string): Promise<LocksResponse> {
const getLocks = new queries.GetLocks(fioPublicKey)
return getLocks.execute(this.publicKey)
}

/*
* Retrieves info on account for this actor
*
* @param account FIO account.
*/
public getAccount(actor: string): Promise<AccountResponse> {
const getAccount = new queries.GetAccount(actor)
return getAccount.execute(this.publicKey)
}

/**
* Checks if a FIO Address or FIO Domain is available for registration.
*
Expand Down Expand Up @@ -969,6 +1036,16 @@ export class FIOSDK {
return transferTokens.execute(this.privateKey, this.publicKey, this.returnPreparedTrx)
}

/**
* Compute and return fee amount for specific call and specific user
*
* @param fioAddress FIO Address incurring the fee and owned by signer.
*/
public getFeeForTransferLockedTokens(fioAddress: string): Promise<FioFeeResponse> {
return this.getFee(EndPoint.transferLockedTokens, fioAddress)
}


/**
* Compute and return fee amount for specific call and specific user
*
Expand Down Expand Up @@ -1122,6 +1199,8 @@ export class FIOSDK {
switch (action) {
case 'getFioPublicKey':
return this.getFioPublicKey()
case 'getAccount':
return this.getAccount(params.account)
case 'registerFioAddress':
if (params.ownerPublicKey) {
return this.registerOwnerFioAddress(
Expand All @@ -1144,6 +1223,15 @@ export class FIOSDK {
params.maxFee,
params.technologyProviderId,
)
case 'transferLockedTokens':
return this.transferLockedTokens(
params.payeePublicKey,
params.canVote,
params.periods,
params.amount,
params.maxFee,
params.technologyProviderId,
)
case 'registerFioDomain':
return this.registerFioDomain(
params.fioDomain,
Expand Down Expand Up @@ -1212,6 +1300,8 @@ export class FIOSDK {
params.maxFee,
params.technologyProviderId,
)
case 'getLocks':
return this.getLocks(params.fioPublicKey)
case 'cancelFundsRequest':
return this.cancelFundsRequest(
params.fioRequestId,
Expand Down Expand Up @@ -1250,6 +1340,8 @@ export class FIOSDK {
params.hash,
params.offLineUrl,
)
case 'getFeeForTransferLockedTokens':
return this.getFeeForTransferLockedTokens(params.fioAddress)
case 'getObtData':
return this.getObtData(params.limit, params.offset, params.tokenCode)
case 'rejectFundsRequest':
Expand Down
5 changes: 5 additions & 0 deletions src/entities/AccountResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@


export interface AccountResponse {
ram_quota: number
}
5 changes: 3 additions & 2 deletions src/entities/BalanceResponse.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export interface BalanceResponse {
balance: number
}
balance: number,
available: number
}
1 change: 1 addition & 0 deletions src/entities/EndPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export enum EndPoint {
recordObtData = 'record_obt_data',
transferTokens = 'transfer_tokens_pub_key',
cancelFundsRequest = 'cancel_funds_request',
transferLockedTokens = 'transfer_locked_tokens',
}
Loading