-
Notifications
You must be signed in to change notification settings - Fork 59
/
ct-version.ts
83 lines (76 loc) · 2.52 KB
/
ct-version.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
import { ConsensusProtocolVersion, VmVersion, AbiVersion } from '../constants';
import Node from '../../../Node';
/*
* First abi/vm by default
* @see {@link https://github.com/aeternity/protocol/blob/71cf111/contracts/contract_vms.md#virtual-machines-on-the-æternity-blockchain}
*/
export const ProtocolToVmAbi = {
[ConsensusProtocolVersion.Iris]: {
'contract-create': {
vmVersion: [VmVersion.Fate2], abiVersion: [AbiVersion.Fate],
},
'contract-call': {
vmVersion: [], abiVersion: [AbiVersion.Fate, AbiVersion.Sophia],
},
'oracle-call': {
vmVersion: [], abiVersion: [AbiVersion.NoAbi, AbiVersion.Fate],
},
},
[ConsensusProtocolVersion.Ceres]: {
'contract-create': {
vmVersion: [VmVersion.Fate3], abiVersion: [AbiVersion.Fate],
},
'contract-call': {
vmVersion: [], abiVersion: [AbiVersion.Fate],
},
'oracle-call': {
vmVersion: [], abiVersion: [AbiVersion.NoAbi, AbiVersion.Fate],
},
},
} as const;
export interface CtVersion {
vmVersion: VmVersion;
abiVersion: AbiVersion;
}
export function getProtocolDetails(
protocolVersion: ConsensusProtocolVersion,
type: 'contract-create' | 'contract-call' | 'oracle-call',
): CtVersion {
const protocol = ProtocolToVmAbi[protocolVersion][type];
return {
vmVersion: protocol.vmVersion[0] ?? VmVersion.Fate2,
abiVersion: protocol.abiVersion[0],
};
}
export default {
serialize(
value: CtVersion | undefined,
params: {},
{ consensusProtocolVersion = ConsensusProtocolVersion.Iris }:
{ consensusProtocolVersion?: ConsensusProtocolVersion },
): Buffer {
value ??= getProtocolDetails(consensusProtocolVersion, 'contract-create');
return Buffer.from([value.vmVersion, 0, value.abiVersion]);
},
async prepare(
value: CtVersion | undefined,
params: {},
// TODO: { consensusProtocolVersion: ConsensusProtocolVersion } | { onNode: Node } | {}
options: { consensusProtocolVersion?: ConsensusProtocolVersion; onNode?: Node },
): Promise<CtVersion | undefined> {
if (value != null) return value;
if (options.consensusProtocolVersion != null) return undefined;
if (Object.keys(ConsensusProtocolVersion).length === 2) return undefined;
if (options.onNode != null) {
return getProtocolDetails(
(await options.onNode.getNodeInfo()).consensusProtocolVersion,
'contract-create',
);
}
return undefined;
},
deserialize(buffer: Buffer): CtVersion {
const [vm, , abi] = buffer;
return { vmVersion: +vm, abiVersion: +abi };
},
};