Skip to content

Commit 2d5dfe6

Browse files
typecasts
1 parent c56ee0e commit 2d5dfe6

5 files changed

Lines changed: 60 additions & 7 deletions

File tree

client/src/CashscriptHoverProvider.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CashscriptHoverProvider implements vscode.HoverProvider{
2222

2323
// check special words
2424

25-
const varTypes = this.getVariableTypes(document, word);
25+
const varTypes = this.getVariableTypes(document, word); // fix this
2626
return new vscode.Hover(varTypes, range);
2727
}
2828

@@ -51,6 +51,9 @@ class CashscriptHoverProvider implements vscode.HoverProvider{
5151
];
5252
}
5353

54+
/*
55+
* Very bad way to get type annotations, better option: custom linter
56+
*/
5457
getVariableTypes(document:vscode.TextDocument, targetWord:string):vscode.MarkdownString[]{
5558
const reg = /([a-zA-Z0-9]+)\s+(pk)[^a-zA-Z0-9]/;
5659
const text = document.getText();

client/src/CashscriptSignatureCompleter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode';
2-
import { LANGUAGE } from './LanguageDesc';
2+
import { LANGUAGE, TYPECASTS } from './LanguageDesc';
33

44
class CashscriptSignatureCompleter implements vscode.SignatureHelpProvider{
55

@@ -13,11 +13,11 @@ class CashscriptSignatureCompleter implements vscode.SignatureHelpProvider{
1313

1414
this.channel.appendLine("signature " + word);
1515
const sh = new vscode.SignatureHelp();
16-
const data = LANGUAGE[word];
16+
const data = LANGUAGE[word] || TYPECASTS[word];
1717
sh.signatures = [new vscode.SignatureInformation(data.codeDesc, new vscode.MarkdownString().appendCodeblock(data.code))];
1818
//sh.signatures = this.SIG_DATA[word]; data
1919
//sh.signatures = [new vscode.SignatureInformation("param here", "documentation here")]
20-
return sh;
20+
return sh;
2121
}
2222

2323
}

client/src/LanguageDesc.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,13 @@ let STATEMENTS:Data = {
8282
}
8383
}
8484

85+
let TYPECASTS:Data = {
86+
int:{
87+
code:"int int( v )",
88+
codeDesc:"Converts to int"
89+
}
90+
}
8591

8692
let LANGUAGE:Data = {...GLOBAL_FUNCTIONS, ...OUTPUT_INSTANTIATION, ...STATEMENTS};
8793

88-
export { GLOBAL_FUNCTIONS, OUTPUT_INSTANTIATION, LANGUAGE };
94+
export { GLOBAL_FUNCTIONS, OUTPUT_INSTANTIATION, TYPECASTS, LANGUAGE };

client/testFixture/t.cash

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
pragma cashscript ^0.6.0;
22

33
contract ContractName() {
4-
function test() {
5-
require(true);
4+
function test(pubkey pk) {
5+
6+
require(hash160(pk) = true);
67
string x = "asdasd";
78
}
89
}

client/testFixture/time.cash

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
pragma cashscript ^0.6.0;
2+
3+
// This is an experimental contract for a more "streaming" Mecenas experience
4+
// Completely untested, just a concept
5+
contract Mecenas(bytes4 initialBlock, int pledgePerBlock, bytes20 recipient, bytes20 funder) {
6+
function receive(pubkey pk, sig s, int pledge) {
7+
require(checkSig(s, pk));
8+
9+
int initial = int(initialBlock);
10+
require(tx.time >= initial);
11+
12+
// Pledge amount calculation is done in client, verified in contract
13+
// When OP_MUL is enabled this can be done in contract
14+
// Double require to account for integer division
15+
int passedBlocks = int(tx.locktime) - initial;
16+
require(pledge / passedBlocks == pledgePerBlock);
17+
require((pledge - 1) / passedBlocks != pledgePerBlock);
18+
19+
// Cut out old initialBlock (PUSH1 0x04 <inheritor>)
20+
// Insert new initialBlock (PUSH1 0x04 <newInheritor>)
21+
bytes newContract = 0x4c04 + tx.locktime + tx.bytecode.split(6)[1];
22+
23+
int minerFee = 1000; // hardcoded fee
24+
int intValue = int(bytes(tx.value));
25+
26+
if (intValue <= pledge + minerFee) {
27+
bytes8 amount1 = bytes8(intValue - minerFee);
28+
bytes34 out1 = new OutputP2PKH(amount1, recipient);
29+
require(hash256(out1) == tx.hashOutputs);
30+
} else {
31+
bytes8 amount1 = bytes8(pledge);
32+
bytes8 amount2 = bytes8(intValue - pledge - minerFee);
33+
bytes34 out1 = new OutputP2PKH(amount1, recipient);
34+
bytes32 out2 = new OutputP2SH(amount2, hash160(newContract));
35+
require(hash256(out1 + out2) == tx.hashOutputs);
36+
}
37+
}
38+
39+
function reclaim(pubkey pk, sig s) {
40+
require(hash160(pk) == funder);
41+
require(checkSig(s, pk));
42+
}
43+
}

0 commit comments

Comments
 (0)