Create TransferTokensTransaction class

This commit is contained in:
Michael Vines
2018-09-14 08:27:40 -07:00
parent cc4019f56f
commit 7dad281f69
3 changed files with 37 additions and 23 deletions

View File

@@ -9,7 +9,7 @@ import type {Account, PublicKey} from './account';
/**
* @private
*/
export function bs58DecodePublicKey(key: PublicKey): Buffer {
function bs58DecodePublicKey(key: PublicKey): Buffer {
const keyBytes = Buffer.from(bs58.decode(key));
assert(keyBytes.length === 32);
return keyBytes;
@@ -132,3 +132,27 @@ export class Transaction {
return wireTransaction;
}
}
/**
* A Transaction that immediately transfers tokens
*/
export class TransferTokensTransaction extends Transaction {
constructor(from: PublicKey, to: PublicKey, amount: number) {
super();
// Forge a simple Budget Pay contract into `userdata`
// TODO: Clean this up
const userdata = Buffer.alloc(68); // 68 = serialized size of Budget enum
userdata.writeUInt32LE(60, 0);
userdata.writeUInt32LE(amount, 12); // u64
userdata.writeUInt32LE(amount, 28); // u64
const toData = bs58DecodePublicKey(to);
toData.copy(userdata, 36);
Object.assign(this, {
fee: 0,
keys: [from, to],
userdata
});
}
}