-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use canonical asset format. #277
Merged
abuiles
merged 1 commit into
master
from
use-canonical-representation-for-asset-to-string
Oct 15, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,171 +1,225 @@ | ||
describe('Asset', function() { | ||
describe('constructor', function() { | ||
it("throws an error when there's no issuer for non XLM type asset", function() { | ||
expect(() => new StellarBase.Asset('USD')).to.throw( | ||
/Issuer cannot be null/ | ||
); | ||
}); | ||
|
||
describe("constructor", function () { | ||
|
||
it("throws an error when there's no issuer for non XLM type asset", function () { | ||
expect(() => new StellarBase.Asset("USD")).to.throw(/Issuer cannot be null/) | ||
}); | ||
|
||
it("throws an error when code is invalid", function () { | ||
expect(() => new StellarBase.Asset("", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ")).to.throw(/Asset code is invalid/) | ||
expect(() => new StellarBase.Asset("1234567890123", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ")).to.throw(/Asset code is invalid/) | ||
expect(() => new StellarBase.Asset("ab_", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ")).to.throw(/Asset code is invalid/) | ||
}); | ||
|
||
it("throws an error when issuer is invalid", function () { | ||
expect(() => new StellarBase.Asset("USD", "GCEZWKCA5")).to.throw(/Issuer is invalid/) | ||
}); | ||
it('throws an error when code is invalid', function() { | ||
expect( | ||
() => | ||
new StellarBase.Asset( | ||
'', | ||
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' | ||
) | ||
).to.throw(/Asset code is invalid/); | ||
expect( | ||
() => | ||
new StellarBase.Asset( | ||
'1234567890123', | ||
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' | ||
) | ||
).to.throw(/Asset code is invalid/); | ||
expect( | ||
() => | ||
new StellarBase.Asset( | ||
'ab_', | ||
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' | ||
) | ||
).to.throw(/Asset code is invalid/); | ||
}); | ||
|
||
describe("getCode()", function () { | ||
it("returns a code for a native asset object", function () { | ||
var asset = new StellarBase.Asset.native(); | ||
expect(asset.getCode()).to.be.equal('XLM'); | ||
}); | ||
it('throws an error when issuer is invalid', function() { | ||
expect(() => new StellarBase.Asset('USD', 'GCEZWKCA5')).to.throw( | ||
/Issuer is invalid/ | ||
); | ||
}); | ||
}); | ||
|
||
it("returns a code for a non-native asset", function () { | ||
var asset = new StellarBase.Asset("USD", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ"); | ||
expect(asset.getCode()).to.be.equal('USD'); | ||
}); | ||
describe('getCode()', function() { | ||
it('returns a code for a native asset object', function() { | ||
var asset = new StellarBase.Asset.native(); | ||
expect(asset.getCode()).to.be.equal('XLM'); | ||
}); | ||
|
||
describe("getIssuer()", function () { | ||
it("returns a code for a native asset object", function () { | ||
var asset = new StellarBase.Asset.native(); | ||
expect(asset.getIssuer()).to.be.undefined; | ||
}); | ||
it('returns a code for a non-native asset', function() { | ||
var asset = new StellarBase.Asset( | ||
'USD', | ||
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' | ||
); | ||
expect(asset.getCode()).to.be.equal('USD'); | ||
}); | ||
}); | ||
|
||
it("returns a code for a non-native asset", function () { | ||
var asset = new StellarBase.Asset("USD", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ"); | ||
expect(asset.getIssuer()).to.be.equal('GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'); | ||
}); | ||
describe('getIssuer()', function() { | ||
it('returns a code for a native asset object', function() { | ||
var asset = new StellarBase.Asset.native(); | ||
expect(asset.getIssuer()).to.be.undefined; | ||
}); | ||
|
||
describe("getAssetType()", function () { | ||
it("returns native for native assets", function () { | ||
var asset = StellarBase.Asset.native(); | ||
expect(asset.getAssetType()).to.eq("native"); | ||
}); | ||
it('returns a code for a non-native asset', function() { | ||
var asset = new StellarBase.Asset( | ||
'USD', | ||
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' | ||
); | ||
expect(asset.getIssuer()).to.be.equal( | ||
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' | ||
); | ||
}); | ||
}); | ||
|
||
it("returns credit_alphanum4 if the asset code length is between 1 and 4", function () { | ||
var asset = new StellarBase.Asset("ABCD", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ"); | ||
expect(asset.getAssetType()).to.eq("credit_alphanum4"); | ||
}); | ||
describe('getAssetType()', function() { | ||
it('returns native for native assets', function() { | ||
var asset = StellarBase.Asset.native(); | ||
expect(asset.getAssetType()).to.eq('native'); | ||
}); | ||
|
||
it("returns credit_alphanum12 if the asset code length is between 5 and 12", function () { | ||
var asset = new StellarBase.Asset("ABCDEF", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ"); | ||
expect(asset.getAssetType()).to.eq("credit_alphanum12"); | ||
}); | ||
it('returns credit_alphanum4 if the asset code length is between 1 and 4', function() { | ||
var asset = new StellarBase.Asset( | ||
'ABCD', | ||
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' | ||
); | ||
expect(asset.getAssetType()).to.eq('credit_alphanum4'); | ||
}); | ||
|
||
describe("toXDRObject()", function () { | ||
it("parses a native asset object", function () { | ||
var asset = new StellarBase.Asset.native(); | ||
var xdr = asset.toXDRObject(); | ||
expect(xdr.toXDR().toString()).to.be.equal(Buffer.from([0,0,0,0]).toString()); | ||
}); | ||
it('returns credit_alphanum12 if the asset code length is between 5 and 12', function() { | ||
var asset = new StellarBase.Asset( | ||
'ABCDEF', | ||
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' | ||
); | ||
expect(asset.getAssetType()).to.eq('credit_alphanum12'); | ||
}); | ||
}); | ||
|
||
describe('toXDRObject()', function() { | ||
it('parses a native asset object', function() { | ||
var asset = new StellarBase.Asset.native(); | ||
var xdr = asset.toXDRObject(); | ||
expect(xdr.toXDR().toString()).to.be.equal( | ||
Buffer.from([0, 0, 0, 0]).toString() | ||
); | ||
}); | ||
|
||
it("parses a 3-alphanum asset object", function () { | ||
var asset = new StellarBase.Asset("USD", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ"); | ||
var xdr = asset.toXDRObject(); | ||
it('parses a 3-alphanum asset object', function() { | ||
var asset = new StellarBase.Asset( | ||
'USD', | ||
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' | ||
); | ||
var xdr = asset.toXDRObject(); | ||
|
||
expect(xdr).to.be.instanceof(StellarBase.xdr.Asset); | ||
expect(() => xdr.toXDR('hex')).to.not.throw(); | ||
expect(xdr).to.be.instanceof(StellarBase.xdr.Asset); | ||
expect(() => xdr.toXDR('hex')).to.not.throw(); | ||
|
||
expect(xdr.arm()).to.equal('alphaNum4'); | ||
expect(xdr.value().assetCode()).to.equal('USD\0'); | ||
}); | ||
expect(xdr.arm()).to.equal('alphaNum4'); | ||
expect(xdr.value().assetCode()).to.equal('USD\0'); | ||
}); | ||
|
||
it("parses a 4-alphanum asset object", function () { | ||
var asset = new StellarBase.Asset("BART", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ"); | ||
var xdr = asset.toXDRObject(); | ||
it('parses a 4-alphanum asset object', function() { | ||
var asset = new StellarBase.Asset( | ||
'BART', | ||
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' | ||
); | ||
var xdr = asset.toXDRObject(); | ||
|
||
expect(xdr).to.be.instanceof(StellarBase.xdr.Asset); | ||
expect(() => xdr.toXDR('hex')).to.not.throw(); | ||
expect(xdr).to.be.instanceof(StellarBase.xdr.Asset); | ||
expect(() => xdr.toXDR('hex')).to.not.throw(); | ||
|
||
expect(xdr.arm()).to.equal('alphaNum4'); | ||
expect(xdr.value().assetCode()).to.equal('BART'); | ||
}); | ||
expect(xdr.arm()).to.equal('alphaNum4'); | ||
expect(xdr.value().assetCode()).to.equal('BART'); | ||
}); | ||
|
||
it("parses a 5-alphanum asset object", function () { | ||
var asset = new StellarBase.Asset("12345", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ"); | ||
var xdr = asset.toXDRObject(); | ||
it('parses a 5-alphanum asset object', function() { | ||
var asset = new StellarBase.Asset( | ||
'12345', | ||
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' | ||
); | ||
var xdr = asset.toXDRObject(); | ||
|
||
expect(xdr).to.be.instanceof(StellarBase.xdr.Asset); | ||
expect(() => xdr.toXDR('hex')).to.not.throw(); | ||
expect(xdr).to.be.instanceof(StellarBase.xdr.Asset); | ||
expect(() => xdr.toXDR('hex')).to.not.throw(); | ||
|
||
expect(xdr.arm()).to.equal('alphaNum12'); | ||
expect(xdr.value().assetCode()).to.equal('12345\0\0\0\0\0\0\0'); | ||
}); | ||
expect(xdr.arm()).to.equal('alphaNum12'); | ||
expect(xdr.value().assetCode()).to.equal('12345\0\0\0\0\0\0\0'); | ||
}); | ||
|
||
it("parses a 12-alphanum asset object", function () { | ||
var asset = new StellarBase.Asset("123456789012", "GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ"); | ||
var xdr = asset.toXDRObject(); | ||
it('parses a 12-alphanum asset object', function() { | ||
var asset = new StellarBase.Asset( | ||
'123456789012', | ||
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' | ||
); | ||
var xdr = asset.toXDRObject(); | ||
|
||
expect(xdr).to.be.instanceof(StellarBase.xdr.Asset); | ||
expect(() => xdr.toXDR('hex')).to.not.throw(); | ||
expect(xdr).to.be.instanceof(StellarBase.xdr.Asset); | ||
expect(() => xdr.toXDR('hex')).to.not.throw(); | ||
|
||
expect(xdr.arm()).to.equal('alphaNum12'); | ||
expect(xdr.value().assetCode()).to.equal('123456789012'); | ||
}); | ||
expect(xdr.arm()).to.equal('alphaNum12'); | ||
expect(xdr.value().assetCode()).to.equal('123456789012'); | ||
}); | ||
}); | ||
|
||
describe("fromOperation()", function () { | ||
it("parses a native asset XDR", function () { | ||
var xdr = new StellarBase.xdr.Asset.assetTypeNative(); | ||
var asset = StellarBase.Asset.fromOperation(xdr); | ||
describe('fromOperation()', function() { | ||
it('parses a native asset XDR', function() { | ||
var xdr = new StellarBase.xdr.Asset.assetTypeNative(); | ||
var asset = StellarBase.Asset.fromOperation(xdr); | ||
|
||
expect(asset).to.be.instanceof(StellarBase.Asset); | ||
expect(asset.isNative()).to.equal(true); | ||
expect(asset).to.be.instanceof(StellarBase.Asset); | ||
expect(asset.isNative()).to.equal(true); | ||
}); | ||
|
||
it('parses a 4-alphanum asset XDR', function() { | ||
var issuer = 'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'; | ||
var assetCode = 'KHL'; | ||
var assetType = new StellarBase.xdr.AssetAlphaNum4({ | ||
assetCode: assetCode + '\0', | ||
issuer: StellarBase.Keypair.fromPublicKey(issuer).xdrAccountId() | ||
}); | ||
var xdr = new StellarBase.xdr.Asset( | ||
'assetTypeCreditAlphanum4', | ||
assetType | ||
); | ||
|
||
it("parses a 4-alphanum asset XDR", function () { | ||
var issuer = 'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' | ||
var assetCode = 'KHL'; | ||
var assetType = new StellarBase.xdr.AssetAlphaNum4({ | ||
assetCode: assetCode + '\0', | ||
issuer: StellarBase.Keypair.fromPublicKey(issuer).xdrAccountId() | ||
}); | ||
var xdr = new StellarBase.xdr.Asset('assetTypeCreditAlphanum4', assetType); | ||
var asset = StellarBase.Asset.fromOperation(xdr); | ||
|
||
var asset = StellarBase.Asset.fromOperation(xdr); | ||
expect(asset).to.be.instanceof(StellarBase.Asset); | ||
expect(asset.getCode()).to.equal(assetCode); | ||
expect(asset.getIssuer()).to.equal(issuer); | ||
}); | ||
|
||
expect(asset).to.be.instanceof(StellarBase.Asset); | ||
expect(asset.getCode()).to.equal(assetCode); | ||
expect(asset.getIssuer()).to.equal(issuer); | ||
it('parses a 12-alphanum asset XDR', function() { | ||
var issuer = 'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ'; | ||
var assetCode = 'KHLTOKEN'; | ||
var assetType = new StellarBase.xdr.AssetAlphaNum4({ | ||
assetCode: assetCode + '\0\0\0\0', | ||
issuer: StellarBase.Keypair.fromPublicKey(issuer).xdrAccountId() | ||
}); | ||
var xdr = new StellarBase.xdr.Asset( | ||
'assetTypeCreditAlphanum12', | ||
assetType | ||
); | ||
|
||
it("parses a 12-alphanum asset XDR", function () { | ||
var issuer = 'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' | ||
var assetCode = 'KHLTOKEN'; | ||
var assetType = new StellarBase.xdr.AssetAlphaNum4({ | ||
assetCode: assetCode + '\0\0\0\0', | ||
issuer: StellarBase.Keypair.fromPublicKey(issuer).xdrAccountId() | ||
}); | ||
var xdr = new StellarBase.xdr.Asset('assetTypeCreditAlphanum12', assetType); | ||
var asset = StellarBase.Asset.fromOperation(xdr); | ||
|
||
var asset = StellarBase.Asset.fromOperation(xdr); | ||
|
||
expect(asset).to.be.instanceof(StellarBase.Asset); | ||
expect(asset.getCode()).to.equal(assetCode); | ||
expect(asset.getIssuer()).to.equal(issuer); | ||
}); | ||
expect(asset).to.be.instanceof(StellarBase.Asset); | ||
expect(asset.getCode()).to.equal(assetCode); | ||
expect(asset.getIssuer()).to.equal(issuer); | ||
}); | ||
}); | ||
|
||
describe('toString()', function() { | ||
it("returns 'native' for native asset", function() { | ||
var asset = StellarBase.Asset.native(); | ||
expect(asset.toString()).to.be.equal('native'); | ||
}); | ||
describe('toString()', function() { | ||
it("returns 'native' for native asset", function() { | ||
var asset = StellarBase.Asset.native(); | ||
expect(asset.toString()).to.be.equal('native'); | ||
}); | ||
|
||
it("returns 'code-issuer' for non-native asset", function() { | ||
var asset = new StellarBase.Asset( | ||
'USD', | ||
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' | ||
); | ||
expect(asset.toString()).to.be.equal( | ||
'USD-GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' | ||
); | ||
}); | ||
it("returns 'code-issuer' for non-native asset", function() { | ||
var asset = new StellarBase.Asset( | ||
'USD', | ||
'GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' | ||
); | ||
expect(asset.toString()).to.be.equal( | ||
'USD:GCEZWKCA5VLDNRLN3RPRJMRZOX3Z6G5CHCGSNFHEYVXM3XOJMDS674JZ' | ||
); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo - need
code:issuer