-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmarttx.ts
196 lines (164 loc) · 7.47 KB
/
smarttx.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import {
Account, Address, AggregateTransaction, Convert, CosignatureTransaction,
Deadline,
InnerTransaction,
Mosaic,
MosaicId,
PlainMessage,
PublicAccount, SignedTransaction, Transaction,
TransferTransaction,
UInt64
} from "symbol-sdk";
import {SymbolService} from "metal-on-symbol";
import {firstValueFrom} from "rxjs";
import assert from "assert";
export interface CallData {
method_name: string;
arguments: any[];
}
export interface CallTransactionPayload {
type: string;
metal_id: string;
hash: string;
deadline: number;
max_fee: string;
signer_public_key: string;
signature: string;
call_data: CallData,
}
export interface SymbolLibrary {
getAccountBalance(account: string, mosaic_id: string): Promise<number>;
transferMosaic(from: string, to: string, mosaic_id: string, amount: number, message: string): Promise<void>;
}
export class SmartTransactionService implements SymbolLibrary {
public static isCallTransactionPayload = (value: any): value is CallTransactionPayload =>
typeof(value.type) !== "undefined" &&
value.type === "smart" &&
typeof(value.metal_id) === "string" &&
typeof(value.hash) === "string" &&
typeof(value.deadline) === "number" &&
typeof(value.max_fee) === "string" &&
typeof(value.signer_public_key) === "string" &&
typeof(value.signature) === "string" &&
typeof(value.call_data) === "object";
private readonly transactions = new Array<InnerTransaction>();
constructor(
public readonly symbolService: SymbolService,
private readonly signerAccount: Account,
private readonly smartTxMetalId: string,
private readonly smartTxCallAddress: Address,
private readonly deadline: Deadline,
) { }
public addTx(tx: InnerTransaction) {
this.transactions.push(tx);
}
private async signTx(tx: Transaction) {
const { networkGenerationHash } = await this.symbolService.getNetwork();
const generationHashBytes = Array.from(Convert.hexToUint8(networkGenerationHash));
const serializedBytes = Array.from(Convert.hexToUint8(tx.serialize()));
const signature = Transaction.signRawTransaction(
this.signerAccount.privateKey,
Uint8Array.from(
tx.getSigningBytes(
serializedBytes,
generationHashBytes
)
)
);
const payload = Transaction.preparePayload(Uint8Array.from(serializedBytes), signature, this.signerAccount.publicKey);
const hash = Transaction.createTransactionHash(payload, generationHashBytes);
return { signature, hash, payload };
}
private async createSignedTx(tx: Transaction) {
const { networkGenerationHash, networkType } = await this.symbolService.getNetwork();
const generationHashBytes = Array.from(Convert.hexToUint8(networkGenerationHash));
const payload = tx.serialize();
const hash = Transaction.createTransactionHash(payload, generationHashBytes);
assert(tx.signer);
return new SignedTransaction(payload, hash, tx.signer.publicKey, tx.type, networkType);
}
public async call(callData: CallData) {
const { networkGenerationHash, epochAdjustment, networkCurrencyMosaicId, networkType } = await this.symbolService.getNetwork();
const feeMultiplier = await this.symbolService.getFeeMultiplier(0.35);
const smartyTx = AggregateTransaction.createComplete(
this.deadline,
this.transactions,
networkType,
[]
).setMaxFeeForAggregate(feeMultiplier, 1);
const { hash, signature } = await this.signTx(smartyTx);
// Build call transaction
const callTxPayload = JSON.stringify({
type: "smart",
metal_id: this.smartTxMetalId,
hash,
deadline: this.deadline.adjustedValue,
max_fee: smartyTx.maxFee.toString(),
signer_public_key: this.signerAccount.publicKey,
signature: Convert.uint8ToHex(signature),
call_data: callData,
});
const callTxs = new Array<InnerTransaction>();
callTxs.push(TransferTransaction.create(
Deadline.create(epochAdjustment),
this.smartTxCallAddress,
[ new Mosaic(networkCurrencyMosaicId, UInt64.fromUint(0)) ],
PlainMessage.create(callTxPayload),
networkType,
).toAggregate(this.signerAccount.publicAccount));
const signedTx = this.signerAccount.sign(
await this.symbolService.composeAggregateCompleteTx(feeMultiplier, 1, callTxs),
networkGenerationHash
);
await this.symbolService.announceTxWithCosignatures(signedTx, []);
const results = await this.symbolService.waitTxsFor(this.signerAccount, signedTx.hash, "confirmed");
if (results.filter((result) => result.error).length) {
throw new Error("Failed to announce call transaction.");
}
}
public async fulfill(callTxPayload: CallTransactionPayload) {
const { networkType } = await this.symbolService.getNetwork();
const callerPubAccount = PublicAccount.createFromPublicKey(callTxPayload.signer_public_key, networkType);
const smartyTx = AggregateTransaction.createComplete(
this.deadline,
this.transactions,
networkType,
[],
UInt64.fromNumericString(callTxPayload.max_fee),
callTxPayload.signature,
callerPubAccount
);
const signedTx = await this.createSignedTx(smartyTx);
const cosignature = CosignatureTransaction.signTransactionHash(this.signerAccount, signedTx.hash);
await this.symbolService.announceTxWithCosignatures(signedTx, [ cosignature ]);
const results = await this.symbolService.waitTxsFor(callerPubAccount, signedTx.hash, "confirmed");
if (results.filter((result) => result.error).length) {
throw new Error("Failed to announce call transaction.");
}
};
// WASM Import functions (Symbol libs)
public async getAccountBalance(account: string, mosaic_id: string): Promise<number> {
const { networkType, repositoryFactory } = await this.symbolService.getNetwork();
const accountHttp = repositoryFactory.createAccountRepository();
const mosaicIdObj = new MosaicId(mosaic_id);
return firstValueFrom(accountHttp.getAccountInfo(PublicAccount.createFromPublicKey(account, networkType).address))
.then((accountInfo) =>
accountInfo.mosaics
.filter((mosaic) => mosaic.id.equals(mosaicIdObj))
.reduce((acc, curr) => acc + Number(curr.amount.toString()), 0)
);
}
public async transferMosaic(from: string, to: string, mosaic_id: string, amount: number, message: string) {
const { networkType } = await this.symbolService.getNetwork();
const recipientPubAccount = PublicAccount.createFromPublicKey(to, networkType);
const senderPubAccount = PublicAccount.createFromPublicKey(from, networkType);
const transferTx = TransferTransaction.create(
this.deadline,
recipientPubAccount.address,
[ new Mosaic(new MosaicId(mosaic_id), UInt64.fromUint(amount)) ],
PlainMessage.create(message),
networkType
).toAggregate(senderPubAccount);
this.addTx(transferTx);
}
}