solana-labs / solana-program-library

A collection of Solana programs maintained by Solana Labs
https://solanalabs.com
Apache License 2.0
3.5k stars 2.04k forks source link

token-js: `u64` should implement its own constructor #2563

Closed arcticmatt closed 2 years ago

arcticmatt commented 2 years ago

Try this code:

const bn = new BN(5);
const u = new u64(10);
const u2 = new u64(bn);

console.log("bn.toBuffer", bn.toBuffer);
console.log("u.toBuffer", u.toBuffer);
console.log("u2.toBuffer", u2.toBuffer);

Logging u.toBuffer shows a function. But logging u2.toBuffer shows undefined, even though toBuffer is defined right here! https://github.com/solana-labs/solana-program-library/blob/master/token/js/client/token.js#L54-L65

Why? It's because of BN's constructor: https://github.com/indutny/bn.js/blob/master/lib/bn.js#L21-L24

This behavior is very misleading in TypeScript, because u2 is typed as a u64, which you expect to have a toBuffer function.

In order to fix this, I think we should override BN's constructor in u64.

tian000 commented 2 years ago

@joncinque gave this one a shot, could you take a look at #2975

jordaaash commented 2 years ago

This issue no longer applies -- @solana/spl-token uses native bigint.