okex订单薄校验checksum算法函数

Author: 亮锅, Date: 2021-05-13 21:23:16
Tags: ToolOKEX

• bids 数组，例如：[[“49194”, “5”, “0”, “1”], [“49190.4”, “1”, “0”, “1”]]
• asks 数组，例如：[[“49194.1”, “447”, “0”, “5”], [“49197.1”, “132”, “0”, “1”]]

• 返回计算结果，number, 例如：-1089185544

```

/*

- bids 数组，例如：[["49194", "5", "0", "1"], ["49190.4", "1", "0", "1"]]
- asks 数组，例如：[["49194.1", "447", "0", "5"], ["49197.1", "132", "0", "1"]]

- 返回计算结果，number, 例如：-1089185544
*/

// ********************* crc32 算法 开始 ***********************
function signed_crc_table() {
var c = 0, table = new Array(256);

for(var n =0; n != 256; ++n){
c = n;
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
table[n] = c;
}

return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table;
}

var T = signed_crc_table();

function crc32_str(str, seed) {
var C = seed ^ -1;
for(var i = 0, L=str.length, c, d; i < L;) {
c = str.charCodeAt(i++);
if(c < 0x80) {
C = (C>>>8) ^ T[(C ^ c)&0xFF];
} else if(c < 0x800) {
C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
} else if(c >= 0xD800 && c < 0xE000) {
c = (c&1023)+64; d = str.charCodeAt(i++)&1023;
C = (C>>>8) ^ T[(C ^ (240|((c>>8)&7)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|((c>>2)&63)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|(d&63)))&0xFF];
} else {
C = (C>>>8) ^ T[(C ^ (224|((c>>12)&15)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|((c>>6)&63)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
}
}
return C ^ -1;
}
// ********************* crc32 算法 结束 ***********************

function preCheckSumStr(bids, asks) {
var result = []
for (var i=0;i<25;i++) {
if (bids[i]) result.push('' + bids[i][0] + ':' + bids[i][1])
if (asks[i]) result.push('' + asks[i][0] + ':' + asks[i][1])
}
var rr = result.join(':')
Log('===> pre check str: ', rr)
return rr
}

\$.okex_checksum = function(bids, asks)  {
if (!Array.isArray(bids) || !Array.isArray(asks)) {
Log('计算okex_checksum出错，您传入的bids/asks不是数组')
}
var res = crc32_str(preCheckSumStr(bids, asks))
Log('===> checksum计算结果：', res)
return res
}

function main() {
var bids = [["49194", "5", "0", "1"], ["49190.4", "1", "0", "1"], ["49190.3", "50", "0", "1"], ["49189.9", "50", "0", "1"], ["49189.4", "340", "0", "1"], ["49188.5", "22", "0", "1"], ["49188.4", "164", "0", "1"], ["49187.6", "3", "0", "1"], ["49187.1", "123", "0", "1"], ["49186.1", "24", "0", "1"], ["49184.2", "66", "0", "2"], ["49184", "10", "0", "1"], ["49182.8", "14", "0", "1"], ["49182.4", "78", "0", "1"], ["49182.3", "463", "0", "1"], ["49181", "10", "0", "1"], ["49180.3", "40", "0", "1"], ["49178.3", "44", "0", "1"], ["49178", "10", "0", "1"], ["49176.9", "367", "0", "2"], ["49176.8", "379", "0", "2"], ["49176", "40", "0", "2"], ["49174", "10", "0", "1"], ["49173.9", "80", "0", "1"], ["49173.6", "40", "0", "1"], ["49173.2", "25", "0", "1"], ["49172.2", "93", "0", "1"], ["49170.3", "449", "0", "1"], ["49169.5", "100", "0", "1"], ["49169", "10", "0", "1"], ["49167.9", "2027", "0", "1"], ["49166", "100", "0", "1"], ["49165.8", "310", "0", "2"], ["49161.3", "150", "0", "1"], ["49159.4", "8", "0", "1"], ["49158.3", "200", "0", "1"], ["49158.2", "14", "0", "1"], ["49157.9", "77", "0", "1"], ["49156.2", "88", "0", "1"], ["49155.5", "283", "0", "1"], ["49155.2", "600", "0", "1"], ["49155", "100", "0", "1"], ["49154.8", "348", "0", "1"], ["49154.3", "100", "0", "1"], ["49154.1", "693", "0", "1"], ["49154", "2575", "0", "1"], ["49153.8", "555", "0", "2"], ["49152.7", "12", "0", "1"], ["49152.4", "113", "0", "1"], ["49151.7", "252", "0", "1"], ["49151.3", "393", "0", "1"], ["49149.9", "393", "0", "1"], ["49149.1", "30", "0", "1"], ["49147.5", "8", "0", "1"], ["49146.3", "16", "0", "1"], ["49145.7", "85", "0", "1"], ["49144.9", "20", "0", "1"], ["49142.5", "668", "0", "1"], ["49141.3", "1552", "0", "1"], ["49141.2", "1973", "0", "1"], ["49136", "6531", "0", "1"], ["49135.9", "3419", "0", "1"], ["49135.8", "601", "0", "1"], ["49135.1", "3373", "0", "1"], ["49134.4", "2", "0", "1"], ["49134", "44", "0", "1"], ["49132.9", "5", "0", "1"], ["49132.1", "1", "0", "1"], ["49129.9", "10", "0", "2"], ["49127.6", "200", "0", "1"], ["49126.7", "227", "0", "1"], ["49126.1", "303", "0", "1"], ["49123", "135", "0", "1"], ["49121.2", "600", "0", "1"], ["49114.4", "3", "0", "1"], ["49114", "2", "0", "1"], ["49113.4", "362", "0", "1"], ["49108.5", "1019", "0", "1"], ["49108.4", "3746", "0", "1"], ["49107.5", "1", "0", "1"], ["49106.6", "2", "0", "1"], ["49105.1", "22", "0", "1"], ["49100", "50", "0", "1"], ["49095.3", "6", "0", "1"], ["49091.4", "200", "0", "1"], ["49090.9", "10", "0", "2"], ["49088.2", "370", "0", "1"]]

var asks = [["49194.1", "447", "0", "5"], ["49197.1", "132", "0", "1"], ["49197.8", "4", "0", "1"], ["49203.9", "50", "0", "3"], ["49204.4", "216", "0", "1"], ["49208", "10", "0", "1"], ["49209.4", "112", "0", "1"], ["49209.8", "32", "0", "1"], ["49209.9", "113", "0", "2"], ["49210", "327", "0", "3"], ["49211.6", "39", "0", "1"], ["49214.6", "57", "0", "1"], ["49215", "10", "0", "1"], ["49215.9", "26", "0", "1"], ["49218.8", "100", "0", "1"], ["49219.6", "8", "0", "2"], ["49219.7", "181", "0", "3"], ["49219.9", "106", "0", "1"], ["49220", "10", "0", "1"], ["49222", "44", "0", "1"], ["49222.9", "150", "0", "1"], ["49225.1", "12", "0", "1"], ["49225.2", "100", "0", "1"], ["49225.9", "76", "0", "1"], ["49228.1", "95", "0", "2"], ["49228.2", "323", "0", "2"], ["49231.1", "463", "0", "1"], ["49233.5", "100", "0", "1"], ["49234.4", "2415", "0", "1"], ["49239.9", "100", "0", "1"], ["49240.7", "102", "0", "1"], ["49242.4", "200", "0", "1"], ["49242.7", "100", "0", "1"], ["49242.9", "300", "0", "1"], ["49243.8", "406", "0", "1"], ["49248.7", "1782", "0", "2"], ["49253.3", "20", "0", "1"], ["49253.5", "600", "0", "1"], ["49256.2", "40", "0", "1"], ["49257.2", "567", "0", "1"], ["49257.3", "2027", "0", "1"], ["49260.4", "12", "0", "1"], ["49260.9", "329", "0", "1"], ["49261.7", "65", "0", "1"], ["49264", "319", "0", "1"], ["49264.5", "379", "0", "1"], ["49265.5", "615", "0", "1"], ["49266", "130", "0", "1"], ["49266.2", "25", "0", "1"], ["49270.2", "49", "0", "1"], ["49270.8", "817", "0", "2"], ["49270.9", "2554", "0", "1"], ["49271.2", "54", "0", "1"], ["49275.4", "843", "0", "1"], ["49276.9", "19", "0", "2"], ["49277", "200", "0", "1"], ["49279.7", "1", "0", "1"], ["49279.9", "3513", "0", "1"], ["49282.5", "150", "0", "1"], ["49283.7", "112", "0", "1"], ["49287.7", "902", "0", "1"], ["49287.8", "3375", "0", "1"], ["49289", "3101", "0", "1"], ["49291.1", "32", "0", "1"], ["49294.7", "600", "0", "1"], ["49295.3", "1", "0", "1"], ["49297.2", "197", "0", "1"], ["49298.4", "300", "0", "1"], ["49298.6", "5", "0", "1"], ["49299", "300", "0", "1"], ["49300", "159", "0", "2"], ["49301.8", "23", "0", "1"], ["49303", "64", "0", "1"], ["49304.3", "1", "0", "1"], ["49305.9", "200", "0", "1"], ["49308.4", "335", "0", "1"], ["49308.5", "252", "0", "1"], ["49313.2", "311", "0", "1"], ["49321.2", "9", "0", "1"]]
var t = \$.okex_checksum(bids, asks)
Log('---> test expect -1089185544, and result is: ', t, 'type: ', typeof t)

// 测试不够25档的情况下
var bids2 = [["49194", "5", "0", "1"]]
var asks2 = [["49194.1", "447", "0", "5"], ["49197.1", "132", "0", "1"], ["49197.8", "4", "0", "1"]]
var pStr = preCheckSumStr(bids2, asks2)
Log('----> test expect pre check sum string: ', "49194:5:49194.1:447:49197.1:132:49197.8:4")
Log('----> result: ', pStr)

//other test
var asks3 =  [["51219","448","0","2"],["51221.2","2","0","1"],["51225.2","10","0","1"],["51225.4","10","0","1"],["51226.7","25","0","1"],["51231.7","40","0","1"],["51232","10","0","1"],["51232.8","12","0","1"],["51233.9","100","0","1"],["51235.2","109","0","1"],["51235.6","77","0","1"],["51236.1","5","0","1"],["51236.6","80","0","1"],["51236.8","100","0","1"],["51238.6","60","0","1"],["51239.1","5","0","1"],["51239.8","80","0","1"],["51240.1","8","0","1"],["51240.5","57","0","1"],["51240.6","75","0","1"],["51240.7","1","0","1"]]
var bids3 =  [["51218.9","10","0","2"],["51214.3","18","0","3"],["51214.2","18","0","3"],["51214.1","18","0","3"],["51214","18","0","3"],["51213.9","4008","0","18"],["51213.4","5","0","1"],["51212.7","10","0","1"],["51211","2421","0","6"],["51210.6","5","0","1"],["51208.5","79","0","1"],["51207.4","50","0","1"],["51206.8","27","0","1"],["51206.3","31","0","1"],["51206","1230","0","5"],["51205.1","2","0","1"],["51204.8","41","0","2"],["51204","4","0","1"],["51203.7","200","0","1"],["51203","21","0","1"],["51201.1","22","0","1"],["51201","229","0","2"],["51200.7","2","0","1"],["51199.7","4","0","1"],["51199","97","0","1"],["51198.5","55","0","1"],["51197.8","100","0","1"],["51197.4","86","0","1"],["51196.7","83","0","1"],["51196","24","0","2"],["51195.8","10","0","1"],["51194.2","1236","0","1"],["51193.8","59","0","1"],["51192.1","200","0","1"],["51190","20","0","2"],["51186","10","0","1"],["51185.8","111","0","1"],["51185.4","77","0","1"],["51184.1","261","0","2"],["51184","130","0","1"],["51183.4","4","0","1"],["51182.5","150","0","1"],["51181.3","100","0","1"],["51181","10","0","1"],["51180.1","80","0","1"],["51179.3","17","0","1"],["51178.8","2310","0","1"],["51178.2","80","0","1"],["51177.8","68","0","2"],["51177.4","60","0","1"]]
var pStr2 = preCheckSumStr(bids3, asks3)
Log('----> result v: ', pStr2)
Log('----> checksum v: ', \$.okex_checksum(bids3, asks3))
Log('----> test diy str: ', crc32_str('51218.9:10:51219:448:51214.3:18:51221.2:2:51214.2:18:51225.2:10:51214.1:18:51225.4:10:51214:18:51226.7:25:51213.9:4008:51231.7:40:51213.4:5:51232:10:51212.7:10:51232.8:12:51211:2421:51233.9:100:51210.6:5:51235.2:109:51208.5:79:51235.6:77:51207.4:50:51236.1:5:51206.8:27:51236.6:80:51206.3:31:51236.8:100:51206:1230:51238.6:60:51205.1:2:51239.1:5:51204.8:41:51239.8:80:51204:4:51240.1:8:51203.7:200:51240.5:57:51203:21:51240.6:75:51201.1:22:51240.7:1'))
}

```

Related

More