Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async createOrder(order: ActionsOrder, priority: Priority) {
if (!this.context.authentication) {
throw new Error('Client not connected. Please initialize your client first.');
}
const orderGateway = new Gateway(this.context.provider);
// Set order's payer.
if (!order.payerId && !order.wildcardSigner) {
throw new Error('Payer must be specified if `wildcardSigner` tag is set to false.');
}
// Checks if payer id is order's signer.
if (order.payerId) {
const isPayerSigner = order.signersIds.find((s) => s.toLowerCase() === order.payerId.toLowerCase());
if (!isPayerSigner) {
throw new Error('Payer must be listed as order\'s signer.');
}
}
const multiplier = new BigNumber(1000000000000000000);
const orderActions: FrameworkActionsOrderAction[] = [];
public async createDeployment(deployData: AssetLedgerDeploymentData, priority: Priority) {
if (!this.context.authentication) {
throw new Error('Client not connected. Please initialize your client first.');
}
const deployGateway = new Gateway(this.context.provider);
const date = Date.now();
const multiplier = new BigNumber(1000000000000000000);
const paymentAmount = this.context.payment.assetDeployCost;
const value = new BigNumber(paymentAmount).multipliedBy(multiplier);
const assetLedgerDeployOrder = {
kind: OrderKind.ASSET_LEDGER_DEPLOY_ORDER,
makerId: this.context.provider.accountId,
seed: date,
expiration: Date.now() + 172800000, // 2 days
tokenTransferData: {
ledgerId: this.context.payment.tokenAddress,
receiverId: this.context.payment.receiverAddress,
value: value.toFixed(0),
},
assetLedgerData: deployData,
};
break;
}
case (ActionKind.SET_ABILITIES): {
orderActions.push({
kind: ActionsOrderActionKind.SET_ABILITIES,
receiverId: action.receiverId,
senderId: action.senderId,
ledgerId: action.assetLedgerId,
abilities: action.abilities,
} as FrameworkActionsOrderAction);
paymentAmount += this.context.payment.setAbilitiesCost;
break;
}
case (ActionKind.DESTROY_ASSET): {
orderActions.push({
kind: ActionsOrderActionKind.DESTROY_ASSET,
senderId: action.senderId,
ledgerId: action.assetLedgerId,
assetId: action.id,
} as FrameworkActionsOrderAction);
paymentAmount += this.context.payment.assetDestroyCost;
break;
}
default: {
break;
}
}
}
// Add order payment action.
const value = new BigNumber(paymentAmount).multipliedBy(multiplier);
const expiration = Date.now() + 172800000; // 2 days
} as FrameworkActionsOrderAction);
paymentAmount += this.context.payment.assetDestroyCost;
break;
}
default: {
break;
}
}
}
// Add order payment action.
const value = new BigNumber(paymentAmount).multipliedBy(multiplier);
const expiration = Date.now() + 172800000; // 2 days
orderActions.push({
kind: ActionsOrderActionKind.TRANSFER_VALUE,
senderId: order.payerId,
receiverId: this.context.payment.receiverAddress,
value: value.toFixed(0),
ledgerId: this.context.payment.tokenAddress,
} as FrameworkActionsOrderAction);
// Parse signers into valid API structure.
const signers: Signer[] = order.signersIds.map((s) => { return { accountId: s, claim: '' }; });
// Check if account is specified as signer and generate its claim.
const accountSignerIndex = signers.findIndex((s) => s.accountId.toLowerCase() === this.context.provider.accountId.toLowerCase());
if (accountSignerIndex !== -1) {
const claimOrder = {
kind: order.wildcardSigner ? OrderKind.SIGNED_DYNAMIC_ACTIONS_ORDER : OrderKind.SIGNED_FIXED_ACTIONS_ORDER,
seed: date,
signers: order.signersIds,
public async createDeployment(deployData: AssetLedgerDeploymentData, priority: Priority) {
if (!this.context.authentication) {
throw new Error('Client not connected. Please initialize your client first.');
}
const deployGateway = new Gateway(this.context.provider);
const date = Date.now();
const multiplier = new BigNumber(1000000000000000000);
const paymentAmount = this.context.payment.assetDeployCost;
const value = new BigNumber(paymentAmount).multipliedBy(multiplier);
const assetLedgerDeployOrder = {
kind: OrderKind.ASSET_LEDGER_DEPLOY_ORDER,
makerId: this.context.provider.accountId,
seed: date,
expiration: Date.now() + 172800000, // 2 days
tokenTransferData: {
ledgerId: this.context.payment.tokenAddress,
receiverId: this.context.payment.receiverAddress,
value: value.toFixed(0),
},
assetLedgerData: deployData,
};
const claim = await deployGateway.sign(assetLedgerDeployOrder as AssetLedgerDeployOrder);
delete assetLedgerDeployOrder.kind; // kind will be automatically assigned in the API
return clientFetch(`${this.context.apiUrl}/deployments`, {
method: 'POST',
body: JSON.stringify({
priority,
orderActions.push({
kind: ActionsOrderActionKind.TRANSFER_VALUE,
senderId: order.payerId,
receiverId: this.context.payment.receiverAddress,
value: value.toFixed(0),
ledgerId: this.context.payment.tokenAddress,
} as FrameworkActionsOrderAction);
// Parse signers into valid API structure.
const signers: Signer[] = order.signersIds.map((s) => { return { accountId: s, claim: '' }; });
// Check if account is specified as signer and generate its claim.
const accountSignerIndex = signers.findIndex((s) => s.accountId.toLowerCase() === this.context.provider.accountId.toLowerCase());
if (accountSignerIndex !== -1) {
const claimOrder = {
kind: order.wildcardSigner ? OrderKind.SIGNED_DYNAMIC_ACTIONS_ORDER : OrderKind.SIGNED_FIXED_ACTIONS_ORDER,
seed: date,
signers: order.signersIds,
expiration,
actions: orderActions,
} as FrameworkActionsOrder;
signers[accountSignerIndex].claim = await orderGateway.sign(claimOrder);
}
return clientFetch(`${this.context.apiUrl}/orders`, {
method: 'POST',
body: JSON.stringify({
priority,
order: {
seed: date,
signers,
throw new Error('There was a problem while fetching order data.');
}
if (!order) {
throw new Error('Order doesn\'t. exists');
}
const claimOrder = {
kind: order.wildcardSigner ? OrderKind.SIGNED_DYNAMIC_ACTIONS_ORDER : OrderKind.SIGNED_FIXED_ACTIONS_ORDER,
seed: order.order.seed,
signers: order.order.signers.map((s: Signer) => s.accountId),
expiration: order.order.expiration,
actions: order.order.actions,
} as FrameworkActionsOrder;
const orderGateway = new Gateway(this.context.provider);
const claim = await orderGateway.sign(claimOrder);
return clientFetch(`${this.context.apiUrl}/orders/${orderRef}/sign`, {
method: 'PUT',
body: JSON.stringify({
claim,
}),
headers: {
'Content-Type': 'application/json',
'Authorization': this.context.authentication,
},
});
}
const isPayerSigner = order.signersIds.find((s) => s.toLowerCase() === order.payerId.toLowerCase());
if (!isPayerSigner) {
throw new Error('Payer must be listed as order\'s signer.');
}
}
const multiplier = new BigNumber(1000000000000000000);
const orderActions: FrameworkActionsOrderAction[] = [];
const date = Date.now();
let paymentAmount = 0;
for (const action of order.actions) {
switch (action.kind) {
case (ActionKind.CREATE_ASSET): {
orderActions.push({
kind: ActionsOrderActionKind.CREATE_ASSET,
senderId: action.senderId,
receiverId: action.receiverId,
assetId: action.id,
assetImprint: action.imprint,
ledgerId: action.assetLedgerId,
} as FrameworkActionsOrderAction);
paymentAmount += this.context.payment.assetCreateCost;
break;
}
case (ActionKind.TRANSFER_ASSET): {
orderActions.push({
kind: ActionsOrderActionKind.TRANSFER_ASSET,
receiverId: action.receiverId,
assetId: action.id,
ledgerId: action.assetLedgerId,
senderId: action.senderId,
break;
}
case (ActionKind.TRANSFER_VALUE): {
orderActions.push({
kind: ActionsOrderActionKind.TRANSFER_VALUE,
senderId: action.senderId,
receiverId: action.receiverId,
value: new BigNumber(action.value).multipliedBy(multiplier).toFixed(0),
ledgerId: action.valueLedgerId,
} as FrameworkActionsOrderAction);
paymentAmount += this.context.payment.valueTransferCost;
break;
}
case (ActionKind.SET_ABILITIES): {
orderActions.push({
kind: ActionsOrderActionKind.SET_ABILITIES,
receiverId: action.receiverId,
senderId: action.senderId,
ledgerId: action.assetLedgerId,
abilities: action.abilities,
} as FrameworkActionsOrderAction);
paymentAmount += this.context.payment.setAbilitiesCost;
break;
}
case (ActionKind.DESTROY_ASSET): {
orderActions.push({
kind: ActionsOrderActionKind.DESTROY_ASSET,
senderId: action.senderId,
ledgerId: action.assetLedgerId,
assetId: action.id,
} as FrameworkActionsOrderAction);
paymentAmount += this.context.payment.assetDestroyCost;
switch (action.kind) {
case (ActionKind.CREATE_ASSET): {
orderActions.push({
kind: ActionsOrderActionKind.CREATE_ASSET,
senderId: action.senderId,
receiverId: action.receiverId,
assetId: action.id,
assetImprint: action.imprint,
ledgerId: action.assetLedgerId,
} as FrameworkActionsOrderAction);
paymentAmount += this.context.payment.assetCreateCost;
break;
}
case (ActionKind.TRANSFER_ASSET): {
orderActions.push({
kind: ActionsOrderActionKind.TRANSFER_ASSET,
receiverId: action.receiverId,
assetId: action.id,
ledgerId: action.assetLedgerId,
senderId: action.senderId,
} as FrameworkActionsOrderAction);
paymentAmount += this.context.payment.assetTransferCost;
break;
}
case (ActionKind.TRANSFER_VALUE): {
orderActions.push({
kind: ActionsOrderActionKind.TRANSFER_VALUE,
senderId: action.senderId,
receiverId: action.receiverId,
value: new BigNumber(action.value).multipliedBy(multiplier).toFixed(0),
ledgerId: action.valueLedgerId,
} as FrameworkActionsOrderAction);